Esempio n. 1
0
def test_get_package_prefix():
    set_ament_prefix_path(['prefix1', 'prefix2'])

    def get_package_prefix_basename(package_name):
        return os.path.basename(get_package_prefix(package_name))

    assert get_package_prefix_basename(
        'foo') == 'prefix1', "Expected 'foo' in 'prefix1'"
    # found in both prefix1 and prefix2, but prefix1 is ahead on the APP
    assert get_package_prefix_basename(
        'bar') == 'prefix1', "Expected 'bar' in 'prefix2'"
    assert get_package_prefix_basename(
        'baz') == 'prefix2', "Expected 'baz' in 'prefix2'"

    try:
        get_package_prefix('does_not_exist')
    except PackageNotFoundError:
        pass
    except Exception as exc:
        assert False, 'Expected PackageNotFoundError, got: {}'.format(
            type(exc))

    try:
        get_package_prefix('does_not_exist')
    except KeyError:
        pass
    except Exception as exc:
        assert False, 'Expected KeyError or subclass, got: {}'.format(
            type(exc))
Esempio n. 2
0
    def generate_test_description():
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing',
            'terminating_proc.py'
        )

        EXIT_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing',
            'exit_code_proc.py'
        )

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, TEST_PROC_PATH, '--exception']
            ),

            # This process makes sure we can handle processes that exit with a code but don't
            # generate any output.  This is a regression test for an issue fixed in PR31
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, EXIT_PROC_PATH, '--silent']
            ),

            launch_testing.actions.ReadyToTest(),
        ])
def test_get_package_prefix():
    set_ament_prefix_path(['prefix1', 'prefix2'])

    def get_package_prefix_basename(package_name):
        return PurePath(get_package_prefix(package_name)).name

    assert get_package_prefix_basename('foo') == 'prefix1', "Expected 'foo' in 'prefix1'"
    # found in both prefix1 and prefix2, but prefix1 is ahead on the APP
    assert get_package_prefix_basename('bar') == 'prefix1', "Expected 'bar' in 'prefix2'"
    assert get_package_prefix_basename('baz') == 'prefix2', "Expected 'baz' in 'prefix2'"

    with pytest.raises(PackageNotFoundError):
        get_package_prefix('does_not_exist')
    assert issubclass(PackageNotFoundError, KeyError)
def generate_test_description():
    dut_process = launch.actions.ExecuteProcess(
        cmd=[
            sys.executable,
            os.path.join(
                ament_index_python.get_package_prefix('launch_testing'),
                'lib/launch_testing',
                'terminating_proc',
            ),

            # Arguments
            launch.substitutions.LaunchConfiguration('dut_arg')
        ], )

    return launch.LaunchDescription([

        # This argument can be passed into the test, and can be discovered by running
        # launch_test --show-args
        launch.actions.DeclareLaunchArgument(
            'dut_arg',
            default_value=['default'],
            description='Passed to the terminating process',
        ),
        dut_process,

        # In tests where all of the procs under tests terminate themselves, it's necessary
        # to add a dummy process not under test to keep the launch alive. launch_test
        # provides a simple launch action that does this:
        launch_testing.util.KeepAliveProc(),
        launch_testing.actions.ReadyToTest()
    ]), {
        'dut_process': dut_process
    }
Esempio n. 5
0
def test_nominally_good_dut(source_test_loader):
    def test_ok(self):
        pass

    TEST_PROC_PATH = os.path.join(
        ament_index_python.get_package_prefix('launch_testing'),
        'lib/launch_testing', 'good_proc')

    def generate_test_description():
        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, TEST_PROC_PATH]),
            launch_testing.actions.ReadyToTest(),
        ])

    runner = LaunchTestRunner(
        source_test_loader(
            generate_test_description,
            pre_shutdown_tests=[test_ok],
            post_shutdown_tests=[test_ok],
        ))

    results = runner.run()

    for result in results.values():
        assert result.wasSuccessful()
    def generate_test_description(ready_fn):
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('apex_launchtest'),
            'lib/apex_launchtest', 'terminating_proc')

        EXIT_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('apex_launchtest'),
            'lib/apex_launchtest', 'exit_code_proc')

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(cmd=[TEST_PROC_PATH, '--exception']),

            # This process makes sure we can handle processes that exit with a code but don't
            # generate any output.  This is a regression test for an issue fixed in PR31
            launch.actions.ExecuteProcess(cmd=[EXIT_PROC_PATH, '--silent']),
            launch.actions.OpaqueFunction(function=lambda context: ready_fn()),
        ])
    def generate_test_description(ready_fn):
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('apex_launchtest'),
            'lib/apex_launchtest', 'terminating_proc')

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(cmd=[TEST_PROC_PATH]),
            launch.actions.OpaqueFunction(function=lambda context: ready_fn()),
        ])
Esempio n. 8
0
    def generate_test_description():
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing', 'terminating_proc')

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, TEST_PROC_PATH]),
            launch_testing.actions.ReadyToTest(),
        ])
Esempio n. 9
0
def get_test_process_action():
    TEST_PROC_PATH = os.path.join(
        ament_index_python.get_package_prefix('launch_testing'),
        'lib/launch_testing', 'good_proc')
    return launch.actions.ExecuteProcess(
        cmd=[sys.executable, TEST_PROC_PATH],
        name='good_proc',
        # This is necessary to get unbuffered output from the process under test
        additional_env={'PYTHONUNBUFFERED': '1'},
    )
Esempio n. 10
0
def get_test_process_action(*, args=[]):
    test_proc_path = os.path.join(
        ament_index_python.get_package_prefix('launch_testing'),
        'lib/launch_testing', 'terminating_proc')
    return launch.actions.ExecuteProcess(
        cmd=[sys.executable, test_proc_path, *args],
        name='terminating_proc',
        # This is necessary to get unbuffered output from the process under test
        additional_env={'PYTHONUNBUFFERED': '1'},
        output='screen')
    def setUpClass(cls):
        # Run a launch so we can get some real looking proc_info and proc_output objects to test
        # against
        proc_command = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing',
            'good_proc',
        )
        proc_env = os.environ.copy()
        proc_env['PYTHONUNBUFFERED'] = '1'

        def generate_test_description():
            no_arg_proc = launch.actions.ExecuteProcess(cmd=[sys.executable],
                                                        env=proc_env)

            one_arg_proc = launch.actions.ExecuteProcess(
                cmd=[sys.executable, proc_command, '--one-arg'], env=proc_env)

            two_arg_proc = launch.actions.ExecuteProcess(
                cmd=[sys.executable, proc_command, '--two-arg', 'arg_two'],
                env=proc_env)

            ld = launch.LaunchDescription([
                no_arg_proc,
                one_arg_proc,
                two_arg_proc,
                launch_testing.actions.ReadyToTest(),
            ])

            return (ld, locals())

        arr = []

        class PreShutdownTests(unittest.TestCase):
            def test_wait_self(self, proc_output, proc_info, no_arg_proc,
                               one_arg_proc, two_arg_proc):
                proc_output.assertWaitFor('--one-arg', stream='stdout')
                proc_output.assertWaitFor('--two-arg', stream='stdout')
                proc_output.assertWaitFor('arg_two', stream='stdout')

                arr.append(proc_info)

        # Set up a fake module containing the test data:
        test_module = types.ModuleType('test_module')
        test_module.generate_test_description = generate_test_description
        test_module.FakePreShutdownTests = PreShutdownTests

        # Run the test:
        runner = LaunchTestRunner(LoadTestsFromPythonModule(test_module))

        runner.run()

        # Grab the very realistic proc_info object to use for tests below
        cls.proc_info = arr[0]
Esempio n. 12
0
 def __find_csharp_libs(self):
     for package_name in self.ament_dependencies:
         try:
             package_install_path = get_package_prefix(package_name)
             package_lib_path = package_install_path + '/lib/dotnet'
             if os.path.isdir(package_lib_path):
                 for csharp_lib_file in os.listdir(package_lib_path):
                     if csharp_lib_file.endswith('.dll'):
                         self.cs_lib_source_dict[csharp_lib_file] = (
                             package_lib_path + '/' + csharp_lib_file)
         except PackageNotFoundError:
             print('{} not found!'.format(package_name))
Esempio n. 13
0
    def generate_test_description():
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing', 'good_proc.py')

        good_process = launch.actions.ExecuteProcess(
            cmd=[sys.executable, TEST_PROC_PATH], )

        # Start 'good_proc.py' after a ten second timer elapses
        return launch.LaunchDescription([
            launch.actions.TimerAction(period=10.0, actions=[good_process]),
            launch_testing.actions.ReadyToTest(),
        ]), {
            'good_process': good_process
        }
Esempio n. 14
0
    def generate_test_description(arg_val):
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing', 'good_proc')

        # This is necessary to get unbuffered output from the process under test
        proc_env = os.environ.copy()
        proc_env['PYTHONUNBUFFERED'] = '1'

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, TEST_PROC_PATH],
                env=proc_env,
            ),
            launch_testing.actions.ReadyToTest(),
        ])
    def generate_test_description(arg_val, ready_fn):
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('apex_launchtest'),
            'lib/apex_launchtest', 'good_proc')

        # This is necessary to get unbuffered output from the process under test
        proc_env = os.environ.copy()
        proc_env['PYTHONUNBUFFERED'] = '1'

        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[TEST_PROC_PATH],
                env=proc_env,
            ),
            launch.actions.OpaqueFunction(function=lambda context: ready_fn())
        ])
Esempio n. 16
0
def test_nominally_good_dut():

    # The following is test setup nonsense to turn a string into a python module that can be
    # passed to the test runner.  You can skip over this.  It does not add to your understanding
    # of the test.
    test_code = """
import unittest
from launch_testing import post_shutdown_test

class PreTest(unittest.TestCase):
    def test_pre_ok(self):
        pass

@post_shutdown_test()
class PostTest(unittest.TestCase):
    def test_post_ok(self):
        pass
    """
    module = imp.new_module('test_module')
    exec(test_code, module.__dict__)

    # Here's the actual 'test' part of the test:
    TEST_PROC_PATH = os.path.join(
        ament_index_python.get_package_prefix('launch_testing'),
        'lib/launch_testing',
        'good_proc'
    )

    def generate_test_description(ready_fn):
        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(
                cmd=[sys.executable, TEST_PROC_PATH]
            ),

            launch.actions.OpaqueFunction(function=lambda context: ready_fn()),
        ])

    module.generate_test_description = generate_test_description

    runner = LaunchTestRunner(
        LoadTestsFromPythonModule(module)
    )

    results = runner.run()

    for result in results.values():
        assert result.wasSuccessful()
Esempio n. 17
0
def generate_test_description(ready_fn):
    package_name = 'ifm3d_ros2'
    node_exe = 'camera_standalone'
    namespace = NS_
    nodename = NN_

    exe = os.path.join(get_package_prefix(package_name), 'lib', package_name,
                       node_exe)

    return launch.LaunchDescription([
        ExecuteProcess(
            cmd=[exe, '__ns:=%s' % namespace,
                 '__node:=%s' % nodename],
            log_cmd=True,
            output='screen'),
        OpaqueFunction(function=lambda context: ready_fn()),
    ])
Esempio n. 18
0
    def setUp(self):
        # Set up a launch description for the tests to use
        proc_env = os.environ.copy()
        proc_env["PYTHONUNBUFFERED"] = "1"

        self.terminating_proc = launch.actions.ExecuteProcess(cmd=[
            os.path.join(
                ament_index_python.get_package_prefix('apex_launchtest'),
                'lib/apex_launchtest',
                'terminating_proc',
            )
        ],
                                                              env=proc_env)

        self.launch_description = launch.LaunchDescription([
            self.terminating_proc,
        ])
Esempio n. 19
0
    def setUp(self):
        # Set up a launch description for the tests to use
        proc_env = os.environ.copy()
        proc_env['PYTHONUNBUFFERED'] = '1'

        self.terminating_proc = launch.actions.ExecuteProcess(cmd=[
            sys.executable,
            os.path.join(
                ament_index_python.get_package_prefix('launch_testing'),
                'lib/launch_testing',
                'terminating_proc',
            )
        ],
                                                              env=proc_env)

        self.launch_description = launch.LaunchDescription([
            self.terminating_proc,
        ])
Esempio n. 20
0
def generate_test_description(arg_param, ready_fn):

    terminating_process = launch.actions.ExecuteProcess(cmd=[
        os.path.join(
            ament_index_python.get_package_prefix('apex_launchtest'),
            'lib/apex_launchtest',
            'terminating_proc',
        ),
        # Use the parameter passed to generate_test_description as an argument
        # to the terminating_proc
        '--{}'.format(arg_param),
    ])

    return (launch.LaunchDescription([
        terminating_process,
        apex_launchtest.util.KeepAliveProc(),
        launch.actions.OpaqueFunction(function=lambda context: ready_fn())
    ]), {
        'dut_process': terminating_process
    })
Esempio n. 21
0
def generate_test_description():
    TEST_PROC_PATH = os.path.join(
        ament_index_python.get_package_prefix('launch_testing'),
        'lib/launch_testing', 'good_proc.py')

    # This is necessary to get unbuffered output from the process under test
    proc_env = os.environ.copy()
    proc_env['PYTHONUNBUFFERED'] = '1'

    dut_process = launch.actions.ExecuteProcess(
        cmd=[sys.executable, TEST_PROC_PATH], env=proc_env, output='screen')

    return launch.LaunchDescription([
        dut_process,

        # Start tests right away - no need to wait for anything
        launch_testing.actions.ReadyToTest(),
    ]), {
        'dut_process': dut_process
    }
Esempio n. 22
0
def generate_test_description(arg_param):

    terminating_process = launch.actions.ExecuteProcess(cmd=[
        sys.executable,
        os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing',
            'terminating_proc',
        ),
        # Use the parameter passed to generate_test_description as an argument
        # to the terminating_proc
        '--{}'.format(arg_param),
    ])

    return (launch.LaunchDescription([
        terminating_process,
        launch_testing.util.KeepAliveProc(),
        launch_testing.actions.ReadyToTest(),
    ]), {
        'dut_process': terminating_process
    })
Esempio n. 23
0
def test_nominally_good_dut():

    # The following is test setup nonsense to turn a string into a python module that can be
    # passed to the apex runner.  You can skip over this.  It does not add to your understanding
    # of the test.
    test_code = """
import unittest
from apex_launchtest import post_shutdown_test

class PreTest(unittest.TestCase):
    def test_pre_ok(self):
        pass

@post_shutdown_test()
class PostTest(unittest.TestCase):
    def test_post_ok(self):
        pass
    """
    module = imp.new_module("test_module")
    exec(test_code, module.__dict__)

    # Here's the actual 'test' part of the test:
    TEST_PROC_PATH = os.path.join(
        ament_index_python.get_package_prefix('apex_launchtest'),
        'lib/apex_launchtest', 'good_proc')

    def generate_test_description(ready_fn):
        return launch.LaunchDescription([
            launch.actions.ExecuteProcess(cmd=[TEST_PROC_PATH]),
            launch.actions.OpaqueFunction(function=lambda context: ready_fn()),
        ])

    runner = ApexRunner(gen_launch_description_fn=generate_test_description,
                        test_module=module)

    pre_result, post_result = runner.run()

    assert pre_result.wasSuccessful()

    assert pre_result.wasSuccessful()
Esempio n. 24
0
    def __find_c_libs(self):
        for package_name in self.ament_dependencies:
            try:
                package_install_path = get_package_prefix(package_name)
                package_install_parent_directory = os.path.abspath(
                    os.path.join(package_install_path, os.pardir))

                if platform.system() == 'Windows':
                    filename_extensions = ('.dll')
                    lib_folder = '\\bin'
                    package_lib_path = package_install_path + lib_folder
                else:
                    filename_extensions = ('.so', '.so.1', 'so.2')
                    lib_folder = '/lib'
                    package_lib_path = package_install_path + lib_folder

                if os.path.isdir(package_lib_path):
                    for c_lib_file in os.listdir(package_lib_path):
                        if c_lib_file.endswith(filename_extensions):
                            self.c_lib_source_dict[c_lib_file] = (
                                package_lib_path + '/' + c_lib_file)

                # Find ament compiled projects not found by ament_index
                # by looking in parent folders of ros install directories
                for non_ros_package in self.dependencies:
                    if non_ros_package in os.listdir(
                            package_install_parent_directory):
                        lib_path = package_install_parent_directory + '/' + non_ros_package + lib_folder
                        if os.path.isdir(lib_path):
                            try:
                                for c_lib_file in os.listdir(lib_path):
                                    if c_lib_file.endswith(
                                            filename_extensions):
                                        self.c_lib_source_dict[c_lib_file] = (
                                            lib_path + '/' + c_lib_file)
                            except PackageNotFoundError:
                                pass

            except PackageNotFoundError:
                print('{} not found!'.format(package_name))
Esempio n. 25
0
    def generate_test_description():
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing', 'good_proc.py')

        good_process = launch.actions.ExecuteProcess(
            cmd=[sys.executable, TEST_PROC_PATH], )

        # Let 'good_process' run for 10 seconds, then terminate it
        return launch.LaunchDescription([
            good_process,
            launch_testing.util.KeepAliveProc(),
            launch.actions.TimerAction(
                period=10.0,
                actions=[
                    launch.actions.EmitEvent(
                        event=launch.events.process.SignalProcess(
                            signal_number=signal.SIGINT,
                            process_matcher=lambda proc: proc is good_process))
                ]),
            launch_testing.actions.ReadyToTest(),
        ]), {
            'good_process': good_process
        }
Esempio n. 26
0
 def get_package_prefix_basename(package_name):
     return os.path.basename(get_package_prefix(package_name))
Esempio n. 27
0
def get_prefix_path(package_name):
    try:
        prefix_path = get_package_prefix(package_name)
    except (PackageNotFoundError, ValueError):
        return None
    return prefix_path
 def get_package_prefix_basename(package_name):
     return PurePath(get_package_prefix(package_name)).name
Esempio n. 29
0
# 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 os
import unittest

import ament_index_python
import apex_launchtest
from apex_launchtest.asserts import assertSequentialStdout
import launch
import launch.actions

TEST_PROC_PATH = os.path.join(
    ament_index_python.get_package_prefix('apex_launchtest'),
    'lib/apex_launchtest', 'good_proc')

# This is necessary to get unbuffered output from the process under test
proc_env = os.environ.copy()
proc_env['PYTHONUNBUFFERED'] = '1'

dut_process = launch.actions.ExecuteProcess(
    cmd=[TEST_PROC_PATH],
    env=proc_env,
)


def generate_test_description(ready_fn):

    return launch.LaunchDescription([
Esempio n. 30
0
import sys
import unittest

import ament_index_python

import launch
from launch.actions import RegisterEventHandler
from launch.event_handlers import OnProcessIO
from launch.event_handlers import OnProcessStart

from launch_testing import ActiveIoHandler
from launch_testing.asserts import assertInStdout
# from launch_testing.asserts import NO_CMD_ARGS

TEST_PROC_PATH = os.path.join(
    ament_index_python.get_package_prefix('launch_testing'),
    'lib/launch_testing', 'terminating_proc')
TEST_CMD = [sys.executable, TEST_PROC_PATH]


class TestIoHandlerAndAssertions(unittest.TestCase):

    EXPECTED_TEXT = 'Ready'  # Expected to be in every test run
    NOT_FOUND_TEXT = 'Zazzlefraz'  # Not expected to be in the output anywhere

    @classmethod
    def setUpClass(cls):
        # It's easier to actually capture some IO from the launch system than it is to fake it
        # but it takes a few seconds.  We'll do it once and run tests on the same captured
        # IO