Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

featured: use run() to run cli commands in place of check_call() #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
judyjoseph marked this conversation as resolved.
Show resolved Hide resolved
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
123 changes: 62 additions & 61 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, any_order=True)

# Change the state to disabled
MockSelect.reset_event_queue()
Expand All @@ -393,10 +393,10 @@ def test_feature_events(self, mock_syslog, get_runtime):
daemon.start(time.time())
except TimeoutError:
pass
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)
expected = [call(['sudo', 'systemctl', 'stop', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'disable', 'dhcp_relay.service'], capture_output=True, check=True, text=True),
call(['sudo', 'systemctl', 'mask', 'dhcp_relay.service'], capture_output=True, check=True, text=True)]
mocked_subprocess.run.assert_has_calls(expected, any_order=True)

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, any_order=True)

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=True)

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, any_order=True)
Loading
Loading