Skip to content

Commit

Permalink
featured: use run() to run cli commands in place of check_call()
Browse files Browse the repository at this point in the history
Signed-off-by: anamehra [email protected]
  • Loading branch information
anamehra committed Oct 31, 2024
1 parent 47fd128 commit feffc38
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 184 deletions.
6 changes: 5 additions & 1 deletion scripts/featured
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ PORT_INIT_TIMEOUT_SEC = 180

def run_cmd(cmd, log_err=True, raise_exception=False):
try:
subprocess.check_call(cmd)
result = subprocess.run(cmd,
capture_output=True,
check=True, text=True)
syslog.syslog(syslog.LOG_INFO, "Output: {} , Stderr: {}"
.format(result.stdout, result.stderr))
except Exception as err:
if log_err:
syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}"
Expand Down
117 changes: 59 additions & 58 deletions tests/featured/featured_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def test_sync_state_field(self, test_scenario_name, config_data, fs):
feature_table_state_db_calls = self.get_state_db_set_calls(feature_table)

self.checks_systemd_config_file(device_type, config_data['config_db']['FEATURE'], feature_systemd_name_map)
mocked_subprocess.check_call.assert_has_calls(config_data['enable_feature_subprocess_calls'],
mocked_subprocess.run.assert_has_calls(config_data['enable_feature_subprocess_calls'],
any_order=True)
mocked_subprocess.check_call.assert_has_calls(config_data['daemon_reload_subprocess_call'],
mocked_subprocess.run.assert_has_calls(config_data['daemon_reload_subprocess_call'],
any_order=True)
feature_state_table_mock.set.assert_has_calls(feature_table_state_db_calls)
self.checks_systemd_config_file(device_type, config_data['config_db']['FEATURE'], feature_systemd_name_map)
Expand Down Expand Up @@ -227,9 +227,9 @@ def test_handler(self, test_scenario_name, config_data, fs):
feature_systemd_name_map[feature_name] = feature_names

self.checks_systemd_config_file(device_type, config_data['config_db']['FEATURE'], feature_systemd_name_map)
mocked_subprocess.check_call.assert_has_calls(config_data['enable_feature_subprocess_calls'],
mocked_subprocess.run.assert_has_calls(config_data['enable_feature_subprocess_calls'],
any_order=True)
mocked_subprocess.check_call.assert_has_calls(config_data['daemon_reload_subprocess_call'],
mocked_subprocess.run.assert_has_calls(config_data['daemon_reload_subprocess_call'],
any_order=True)

def test_feature_config_parsing(self):
Expand Down Expand Up @@ -375,15 +375,15 @@ def test_feature_events(self, mock_syslog, get_runtime):
daemon.start(time.time())
except TimeoutError as e:
pass
expected = [call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'mux.service']),
call(['sudo', 'systemctl', 'enable', 'mux.service']),
call(['sudo', 'systemctl', 'start', 'mux.service'])]
mocked_subprocess.check_call.assert_has_calls(expected)
expected = [call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'mux.service'], capture_output=True, check=True, text=True)]
mocked_subprocess.run.assert_has_calls(expected)

# Change the state to disabled
MockSelect.reset_event_queue()
Expand All @@ -396,7 +396,7 @@ def test_feature_events(self, mock_syslog, get_runtime):
expected = [call(['sudo', 'systemctl', 'stop', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'disable', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'mask', 'dhcp_relay.service'])]
mocked_subprocess.check_call.assert_has_calls(expected)
mocked_subprocess.run.assert_has_calls(expected)

def test_delayed_service(self, mock_syslog, get_runtime):
MockSelect.set_event_queue([('FEATURE', 'dhcp_relay'),
Expand All @@ -417,20 +417,20 @@ def test_delayed_service(self, mock_syslog, get_runtime):
daemon.start(time.time())
except TimeoutError:
pass
expected = [call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'mux.service']),
call(['sudo', 'systemctl', 'enable', 'mux.service']),
call(['sudo', 'systemctl', 'start', 'mux.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service']),
call(['sudo', 'systemctl', 'enable', 'telemetry.service']),
call(['sudo', 'systemctl', 'start', 'telemetry.service'])]

mocked_subprocess.check_call.assert_has_calls(expected)
expected = [call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'telemetry.service'], capture_output=True, check=True, text=True)]

mocked_subprocess.run.assert_has_calls(expected)

def test_advanced_reboot(self, mock_syslog, get_runtime):
MockRestartWaiter.advancedReboot = True
Expand All @@ -442,25 +442,26 @@ def test_advanced_reboot(self, mock_syslog, get_runtime):
daemon = featured.FeatureDaemon()
daemon.render_all_feature_states()
daemon.register_callbacks()
try:
try:
daemon.start(time.time())
except TimeoutError:
pass
expected = [call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'mux.service']),
call(['sudo', 'systemctl', 'enable', 'mux.service']),
call(['sudo', 'systemctl', 'start', 'mux.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service']),
call(['sudo', 'systemctl', 'enable', 'telemetry.service']),
call(['sudo', 'systemctl', 'start', 'telemetry.service'])]

mocked_subprocess.check_call.assert_has_calls(expected)

pass
expected = [
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'telemetry.service'], capture_output=True, check=True, text=True)]

mocked_subprocess.run.assert_has_calls(expected, any_order=False)

def test_portinit_timeout(self, mock_syslog, get_runtime):
print(MockConfigDb.CONFIG_DB)
MockSelect.NUM_TIMEOUT_TRIES = 1
Expand All @@ -479,16 +480,16 @@ def test_portinit_timeout(self, mock_syslog, get_runtime):
daemon.start(0.0)
except TimeoutError:
pass
expected = [call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'mux.service']),
call(['sudo', 'systemctl', 'enable', 'mux.service']),
call(['sudo', 'systemctl', 'start', 'mux.service']),
call(['sudo', 'systemctl', 'daemon-reload']),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service']),
call(['sudo', 'systemctl', 'enable', 'telemetry.service']),
call(['sudo', 'systemctl', 'start', 'telemetry.service'])]
mocked_subprocess.check_call.assert_has_calls(expected)
expected = [call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True)),
call(['sudo', 'systemctl', 'unmask', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'mux.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'daemon-reload'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'unmask', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'enable', 'telemetry.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'start', 'telemetry.service'], capture_output=True, check=True, text=True)]
mocked_subprocess.run.assert_has_calls(expected)
Loading

0 comments on commit feffc38

Please sign in to comment.