Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tfoote committed Feb 29, 2024
1 parent 47ffdbb commit 64238ed
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Then you can run pytest.
Notes:

- Make sure to use the python3 instance of pytest from inside the environment.
- The tests include an nvidia test which assumes you're using a machine with an nvidia gpu.
- The tests include an nvidia test which assumes you're using a machine with an nvidia gpu. To skip them use `-m "not nvidia"`


# Example usage
Expand Down
7 changes: 6 additions & 1 deletion src/rocker/rmw_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def __init__(self):
self.name = RMW.get_name()

def get_docker_args(self, cli_args):
implementation = cli_args.get('rmw')[0]
rmw_config = cli_args.get('rmw')
if not rmw_config:
return '' # not active
implementation = rmw_config[0]
args = f' -e RMW_IMPLEMENTATION=rmw_{implementation}_cpp'
return args #% self.get_environment_subs()

Expand All @@ -61,6 +64,8 @@ def get_snippet(self, cliargs):
rmw = cliargs.get('rmw', None)
if rmw:
rmw = rmw[0]
else:
return '' # rmw not active
data['rmw'] = rmw
data['packages'] = RMW.get_package_names(rmw)
# data['rosdistro'] = 'rolling'
Expand Down
2 changes: 1 addition & 1 deletion src/rocker/templates/rmw_snippet.Dockerfile.em
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN \
@(' '.join(packages)) \
&& apt-get clean ;\
else \
echo "Found rmw implementation no need to install" ; \
echo "Found rmw packages @(' '.join(packages)) no need to install" ; \
fi
@[ end if ]@

95 changes: 95 additions & 0 deletions test/test_rmw_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import em
import os
import unittest
import pytest


from rocker.core import DockerImageGenerator
from rocker.core import list_plugins

from test_extension import plugin_load_parser_correctly



class rmwExtensionTest(unittest.TestCase):

def setUp(self):
# Work around interference between empy Interpreter
# stdout proxy and test runner. empy installs a proxy on stdout
# to be able to capture the information.
# And the test runner creates a new stdout object for each test.
# This breaks empy as it assumes that the proxy has persistent
# between instances of the Interpreter class
# empy will error with the exception
# "em.Error: interpreter stdout proxy lost"
em.Interpreter._wasProxyInstalled = False

def test_rmw_extension(self):
plugins = list_plugins()
rmw_plugin = plugins['rmw']
self.assertEqual(rmw_plugin.get_name(), 'rmw')

p = rmw_plugin()
self.assertTrue(plugin_load_parser_correctly(rmw_plugin))


mock_cliargs = {'rmw': ['cyclonedds']}
# TODO self.assertEqual(p.get_snippet(mock_cliargs), '')
self.assertEqual(p.get_preamble(mock_cliargs), '')
args = p.get_docker_args(mock_cliargs)
self.assertIn('-e RMW_IMPLEMENTATION=rmw_cyclonedds_cpp', args)
snippet = p.get_snippet(mock_cliargs)
self.assertIn('rmw-cyclonedds-cpp', snippet)


#without it set
mock_cliargs = {'rmw': None}
args = p.get_docker_args(mock_cliargs)
snippet = p.get_snippet(mock_cliargs)
self.assertNotIn('RMW_IMPLEMENTATION', args)
self.assertNotIn('rmw-cyclonedds-cpp', snippet)


@pytest.mark.docker
class rmwRuntimeExtensionTest(unittest.TestCase):

def setUp(self):
# Work around interference between empy Interpreter
# stdout proxy and test runner. empy installs a proxy on stdout
# to be able to capture the information.
# And the test runner creates a new stdout object for each test.
# This breaks empy as it assumes that the proxy has persistent
# between instances of the Interpreter class
# empy will error with the exception
# "em.Error: interpreter stdout proxy lost"
em.Interpreter._wasProxyInstalled = False

def test_rmw_extension(self):
plugins = list_plugins()
rmw_plugin = plugins['rmw']

p = rmw_plugin()
self.assertTrue(plugin_load_parser_correctly(rmw_plugin))

mock_cliargs = {'rmw': ['cyclonedds']}
dig = DockerImageGenerator([rmw_plugin()], mock_cliargs, 'ros:rolling')
self.assertEqual(dig.build(), 0)
self.assertEqual(dig.run(command='dpkg -l ros-rolling-rmw-cyclonedds-cpp'), 0)
self.assertIn('-e RMW_IMPLEMENTATION=rmw_cyclonedds_cpp', dig.generate_docker_cmd('', mode='dry-run'))

0 comments on commit 64238ed

Please sign in to comment.