Skip to content

Commit

Permalink
collect-logs: Updated pro status call and its unit tests
Browse files Browse the repository at this point in the history
Fixes: #2926
  • Loading branch information
dheyay authored and orndorffgrant committed Feb 13, 2024
1 parent 97e63a3 commit 8c46013
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apport/source_ubuntu-advantage-tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions features/collect_logs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions uaclient/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import glob
import json
import logging
import os
import re
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 = (
Expand Down
4 changes: 3 additions & 1 deletion uaclient/cli/tests/test_cli_collect_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -81,6 +82,7 @@ def test_collect_logs(
m_subp,
_load_file,
_write_file,
m_status,
m_shutilcopy,
m_isfile,
_chown,
Expand All @@ -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
Expand All @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion uaclient/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"]
Expand All @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit 8c46013

Please sign in to comment.