|
| 1 | +# Copyright 2021 Southwest Research Institute, All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# DISTRIBUTION A. Approved for public release; distribution unlimited. |
| 16 | +# OPSEC #4584. |
| 17 | +# |
| 18 | +# Delivered to the U.S. Government with Unlimited Rights, as defined in DFARS |
| 19 | +# Part 252.227-7013 or 7014 (Feb 2014). |
| 20 | +# |
| 21 | +# This notice must appear in all copies of this file and its derivatives. |
| 22 | + |
| 23 | +"""Tests for the ExecuteLocal Action.""" |
| 24 | + |
| 25 | +import os |
| 26 | +import sys |
| 27 | + |
| 28 | +from launch import LaunchDescription |
| 29 | +from launch import LaunchService |
| 30 | +from launch.actions import ExecuteLocal |
| 31 | +from launch.actions import OpaqueFunction |
| 32 | +from launch.actions import Shutdown |
| 33 | +from launch.actions import TimerAction |
| 34 | +from launch.descriptions import Executable |
| 35 | + |
| 36 | +import pytest |
| 37 | + |
| 38 | +@pytest.mark.parametrize('test_input,expected', [ |
| 39 | + (None, [True, False]), |
| 40 | + ({'TEST_NEW_ENV': '2'}, [False, True]) |
| 41 | +]) |
| 42 | +def test_execute_process_with_env(test_input, expected): |
| 43 | + """Test launching a process with an environment variable.""" |
| 44 | + os.environ['TEST_CHANGE_CURRENT_ENV'] = '1' |
| 45 | + additional_env = {'TEST_PROCESS_WITH_ENV': 'Hello World'} |
| 46 | + executable = ExecuteLocal( |
| 47 | + process_description=Executable( |
| 48 | + cmd=[sys.executable, 'TEST_PROCESS_WITH_ENV'], |
| 49 | + env=test_input, |
| 50 | + additional_env=additional_env |
| 51 | + ), |
| 52 | + output='screen' |
| 53 | + ) |
| 54 | + ld = LaunchDescription([executable]) |
| 55 | + ls = LaunchService() |
| 56 | + ls.include_launch_description(ld) |
| 57 | + assert 0 == ls.run() |
| 58 | + env = executable.process_details['env'] |
| 59 | + assert env['TEST_PROCESS_WITH_ENV'] == 'Hello World' |
| 60 | + assert ('TEST_CHANGE_CURRENT_ENV' in env) is expected[0] |
| 61 | + if expected[0]: |
| 62 | + assert env['TEST_CHANGE_CURRENT_ENV'] == '1' |
| 63 | + assert ('TEST_NEW_ENV' in env) is expected[1] |
| 64 | + if expected[1]: |
| 65 | + assert env['TEST_NEW_ENV'] == '2' |
| 66 | + |
| 67 | + |
| 68 | +def test_execute_process_with_on_exit_behavior(): |
| 69 | + """Test a process' on_exit callback and actions are processed.""" |
| 70 | + def on_exit_callback(event, context): |
| 71 | + on_exit_callback.called = True |
| 72 | + on_exit_callback.called = False |
| 73 | + |
| 74 | + executable_with_on_exit_callback = ExecuteLocal( |
| 75 | + process_description=Executable(cmd=[sys.executable, '-c', "print('callback')"]), |
| 76 | + output='screen', on_exit=on_exit_callback |
| 77 | + ) |
| 78 | + assert len(executable_with_on_exit_callback.get_sub_entities()) == 0 |
| 79 | + |
| 80 | + def on_exit_function(context): |
| 81 | + on_exit_function.called = True |
| 82 | + on_exit_function.called = False |
| 83 | + on_exit_action = OpaqueFunction(function=on_exit_function) |
| 84 | + executable_with_on_exit_action = ExecuteLocal( |
| 85 | + process_description=Executable(cmd=[sys.executable, '-c', "print('callback')"]), |
| 86 | + output='screen', on_exit=[on_exit_action] |
| 87 | + ) |
| 88 | + assert executable_with_on_exit_action.get_sub_entities() == [on_exit_action] |
| 89 | + |
| 90 | + ld = LaunchDescription([ |
| 91 | + executable_with_on_exit_callback, |
| 92 | + executable_with_on_exit_action |
| 93 | + ]) |
| 94 | + ls = LaunchService() |
| 95 | + ls.include_launch_description(ld) |
| 96 | + assert 0 == ls.run() |
| 97 | + assert on_exit_callback.called |
| 98 | + assert on_exit_function.called |
| 99 | + |
| 100 | + |
| 101 | +def test_execute_process_with_respawn(): |
| 102 | + """Test launching a process with a respawn and respawn_delay attribute.""" |
| 103 | + def on_exit_callback(event, context): |
| 104 | + on_exit_callback.called_count = on_exit_callback.called_count + 1 |
| 105 | + on_exit_callback.called_count = 0 |
| 106 | + |
| 107 | + respawn_delay = 2.0 |
| 108 | + shutdown_time = 3.0 # to shutdown the launch service, so that the process only respawn once |
| 109 | + expected_called_count = 2 # normal exit and respawn exit |
| 110 | + |
| 111 | + def generate_launch_description(): |
| 112 | + return LaunchDescription([ |
| 113 | + |
| 114 | + ExecuteLocal( |
| 115 | + process_description=Executable(cmd=[sys.executable, '-c', "print('action')"]), |
| 116 | + respawn=True, respawn_delay=respawn_delay, on_exit=on_exit_callback |
| 117 | + ), |
| 118 | + |
| 119 | + TimerAction( |
| 120 | + period=shutdown_time, |
| 121 | + actions=[ |
| 122 | + Shutdown(reason='Timer expired') |
| 123 | + ] |
| 124 | + ) |
| 125 | + ]) |
| 126 | + |
| 127 | + ls = LaunchService() |
| 128 | + ls.include_launch_description(generate_launch_description()) |
| 129 | + assert 0 == ls.run() |
| 130 | + assert expected_called_count == on_exit_callback.called_count |
0 commit comments