From 8c4601351c893f66b23b2d5c15491a78ec5a104b Mon Sep 17 00:00:00 2001 From: Dheyay Date: Wed, 31 Jan 2024 10:23:12 -0800 Subject: [PATCH] collect-logs: Updated pro status call and its unit tests Fixes: #2926 --- apport/source_ubuntu-advantage-tools.py | 4 ++-- features/collect_logs.feature | 4 ++-- uaclient/actions.py | 9 ++++++--- uaclient/cli/tests/test_cli_collect_logs.py | 4 +++- uaclient/tests/test_actions.py | 6 +++++- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apport/source_ubuntu-advantage-tools.py b/apport/source_ubuntu-advantage-tools.py index d66b610e18..39b8b5fc43 100644 --- a/apport/source_ubuntu-advantage-tools.py +++ b/apport/source_ubuntu-advantage-tools.py @@ -17,8 +17,8 @@ def add_info(report, ui=None): auto_include_log_files = { "cloud-id.txt", "cloud-id.txt-error", - "ua-status.json", - "ua-status.json-error", + "pro-status.json", + "pro-status.json-error", "livepatch-status.txt", "livepatch-status.txt-error", "pro-journal.txt", diff --git a/features/collect_logs.feature b/features/collect_logs.feature index fd0832a56b..651d49f8ac 100644 --- a/features/collect_logs.feature +++ b/features/collect_logs.feature @@ -23,12 +23,12 @@ Feature: Command behaviour when attached to an Ubuntu Pro subscription jobs-status.json livepatch-status.txt-error pro-journal.txt + pro-status.json systemd-timers.txt ua-auto-attach.path.txt(-error)? ua-auto-attach.service.txt(-error)? uaclient.conf ua-reboot-cmds.service.txt - ua-status.json ua-timer.service.txt ua-timer.timer.txt ubuntu-advantage.log @@ -69,12 +69,12 @@ Feature: Command behaviour when attached to an Ubuntu Pro subscription jobs-status.json livepatch-status.txt-error pro-journal.txt + pro-status.json systemd-timers.txt ua-auto-attach.path.txt(-error)? ua-auto-attach.service.txt(-error)? uaclient.conf ua-reboot-cmds.service.txt - ua-status.json ua-timer.service.txt ua-timer.timer.txt ubuntu-advantage.log diff --git a/uaclient/actions.py b/uaclient/actions.py index 756b7bed8b..a13d18eff9 100644 --- a/uaclient/actions.py +++ b/uaclient/actions.py @@ -1,5 +1,6 @@ import datetime import glob +import json import logging import os import re @@ -243,9 +244,6 @@ def collect_logs(cfg: config.UAConfig, output_dir: str): _write_command_output_to_file( "cloud-id", "{}/cloud-id.txt".format(output_dir) ) - _write_command_output_to_file( - "pro status --format json", "{}/ua-status.json".format(output_dir) - ) _write_command_output_to_file( "{} status".format(livepatch.LIVEPATCH_CMD), "{}/livepatch-status.txt".format(output_dir), @@ -277,6 +275,11 @@ def collect_logs(cfg: config.UAConfig, output_dir: str): "{}/{}.txt".format(output_dir, service), return_codes=[0, 3], ) + pro_status, _ = status(cfg=cfg, show_all=False) + system.write_file( + "{}/pro-status.json".format(output_dir), + json.dumps(pro_status), + ) state_files = _get_state_files(cfg) user_log_files = ( diff --git a/uaclient/cli/tests/test_cli_collect_logs.py b/uaclient/cli/tests/test_cli_collect_logs.py index 32aaa2fe80..1e8b62c524 100644 --- a/uaclient/cli/tests/test_cli_collect_logs.py +++ b/uaclient/cli/tests/test_cli_collect_logs.py @@ -67,6 +67,7 @@ def test_collect_logs_help( @mock.patch("os.chown") @mock.patch("os.path.isfile", return_value=True) @mock.patch("shutil.copy") + @mock.patch("uaclient.actions.status") @mock.patch("uaclient.system.write_file") @mock.patch("uaclient.system.load_file") @mock.patch("uaclient.system.subp", return_value=(None, None)) @@ -81,6 +82,7 @@ def test_collect_logs( m_subp, _load_file, _write_file, + m_status, m_shutilcopy, m_isfile, _chown, @@ -96,6 +98,7 @@ def test_collect_logs( FakeConfig, tmpdir, ): + m_status.return_value = {"user-id": ""}, 0 m_get_release_info.return_value.series = series util_we_are_currently_root.return_value = is_root m_get_user.return_value = tmpdir.join("user-log").strpath @@ -116,7 +119,6 @@ def test_collect_logs( assert m_subp.call_args_list == [ mock.call(["cloud-id"], rcs=None), - mock.call(["pro", "status", "--format", "json"], rcs=None), mock.call(["/snap/bin/canonical-livepatch", "status"], rcs=None), mock.call(["systemctl", "list-timers", "--all"], rcs=None), mock.call( diff --git a/uaclient/tests/test_actions.py b/uaclient/tests/test_actions.py index f689ad0f45..7c1c480fb6 100644 --- a/uaclient/tests/test_actions.py +++ b/uaclient/tests/test_actions.py @@ -364,6 +364,7 @@ def test_raise_unexpected_errors( @mock.patch("uaclient.actions._write_command_output_to_file") class TestCollectLogs: + @mock.patch("uaclient.actions.status") @mock.patch("uaclient.actions.LOG.warning") @mock.patch("uaclient.util.we_are_currently_root", return_value=False) @mock.patch("uaclient.system.write_file") @@ -382,9 +383,11 @@ def test_collect_logs_invalid_file( m_write_file, m_we_are_currently_root, m_log_warning, + m_status, m_write_cmd, tmpdir, ): + m_status.return_value = ({"test": "test"}, 0) log_file = tmpdir.join("user-log").strpath m_get_user.return_value = log_file m_get_state_files.return_value = ["a", "b"] @@ -400,7 +403,7 @@ def test_collect_logs_invalid_file( mock.call("a"), mock.call("b"), ] == m_load_file.call_args_list - assert 3 == m_write_file.call_count + assert 4 == m_write_file.call_count # apparmor checks assert 1 == m_system_subp.call_count @@ -410,6 +413,7 @@ def test_collect_logs_invalid_file( print(m_write_file.call_args_list) assert [ + mock.call("test/pro-status.json", '{"test": "test"}'), mock.call("test/user0.log", "test"), mock.call("test/b", "test"), mock.call("test/apparmor_logs.txt", APPARMOR_DENIED),