Merge ~chad.smith/curtin:fix-autocollect-error-logs-makedirs into curtin:master

Proposed by Chad Smith
Status: Merged
Approved by: Chad Smith
Approved revision: a9aad7a19dfbd55da4c18f268c77643ed50742d7
Merge reported by: Chad Smith
Merged at revision: 1cb417c73d2aa1b142c74851a21806b039d6a894
Proposed branch: ~chad.smith/curtin:fix-autocollect-error-logs-makedirs
Merge into: curtin:master
Diff against target: 73 lines (+29/-5)
2 files modified
curtin/commands/collect_logs.py (+3/-0)
tests/unittests/test_commands_collect_logs.py (+26/-5)
Reviewer Review Type Date Requested Status
Ryan Harper (community) Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+336876@code.launchpad.net

Commit message

error logs: Create error_tarfile path if path does not exist

When curtin's configured error_tarfile path does not exist in the
ephermeral environment, create it.

LP: #1746363

Description of the change

see commit message

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) :
review: Needs Fixing
a9aad7a... by Chad Smith

use util.ensure_dir instead of a call to os.makedirs directly

Revision history for this message
Chad Smith (chad.smith) :
Revision history for this message
Ryan Harper (raharper) wrote :

Looks good

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/curtin/commands/collect_logs.py b/curtin/commands/collect_logs.py
2index 59b66c4..a61d91e 100644
3--- a/curtin/commands/collect_logs.py
4+++ b/curtin/commands/collect_logs.py
5@@ -61,6 +61,9 @@ def create_log_tarfile(tarfile, config):
6 """
7 if not (isinstance(tarfile, util.string_types) and tarfile):
8 raise ValueError("Invalid value '%s' for tarfile" % tarfile)
9+ target_dir = os.path.dirname(tarfile)
10+ if target_dir and not os.path.exists(target_dir):
11+ util.ensure_dir(target_dir)
12
13 instcfg = config.get('install', {})
14 logfile = instcfg.get('log_file')
15diff --git a/tests/unittests/test_commands_collect_logs.py b/tests/unittests/test_commands_collect_logs.py
16index 100fbc1..d20cc54 100644
17--- a/tests/unittests/test_commands_collect_logs.py
18+++ b/tests/unittests/test_commands_collect_logs.py
19@@ -226,11 +226,11 @@ class TestCollectLogs(CiTestCase):
20 self.assertEqual(f.read(), expected)
21
22
23-class TestWBCreateTar(CiTestCase):
24- """Whitebox texting of create_log_tarfile."""
25+class TestCreateTar(CiTestCase):
26+ """Whitebox testing of create_log_tarfile."""
27
28 def setUp(self):
29- super(TestWBCreateTar, self).setUp()
30+ super(TestCreateTar, self).setUp()
31 self.new_root = self.tmp_dir()
32 date = datetime.utcnow().strftime('%Y-%m-%d-%H-%M')
33 self.tardir = 'curtin-logs-%s' % date
34@@ -256,8 +256,29 @@ class TestWBCreateTar(CiTestCase):
35 self.mock_subp.call_args_list)
36 self.m_sys_info.assert_called_with(self.tardir, {})
37
38+ def test_create_log_tarfile_creates_target_tar_directory_if_absent(self):
39+ """create_log_tarfile makes the tarfile's directory if needed."""
40+ tarfile = self.tmp_path('my.tar',
41+ _dir=os.path.join(self.new_root, 'dont/exist'))
42+ destination_dir = os.path.dirname(tarfile)
43+ self.assertFalse(os.path.exists(destination_dir),
44+ 'Expected absent directory: %s' % destination_dir)
45+ self.add_patch('curtin.util.subp', 'mock_subp')
46+ self.mock_subp.return_value = ('', '')
47+ with mock.patch('sys.stderr'):
48+ with self.assertRaises(SystemExit) as context_manager:
49+ collect_logs.create_log_tarfile(tarfile, config={})
50+ self.assertEqual('0', str(context_manager.exception))
51+ self.assertIn(
52+ mock.call(['tar', '-cvf', tarfile, self.tardir],
53+ capture=True),
54+ self.mock_subp.call_args_list)
55+ self.m_sys_info.assert_called_with(self.tardir, {})
56+ self.assertTrue(os.path.exists(destination_dir),
57+ 'Expected directory created: %s' % destination_dir)
58+
59 def test_create_log_tarfile_copies_configured_logs(self):
60- """create_log_tarfile copies configured log_file and post_files if present.
61+ """create_log_tarfile copies configured log_file and post_files.
62
63 Configured log_file or post_files which don't exist are ignored.
64 """
65@@ -306,7 +327,7 @@ class TestWBCreateTar(CiTestCase):
66
67
68 class TestWBCollectLogs(CiTestCase):
69- """Whitebox texting of _redact_sensitive_information."""
70+ """Whitebox testing of _redact_sensitive_information."""
71
72 def test_wb_redact_sensitive_information(self):
73 """_redact_sensitive_information replaces redact_values in any file."""

Subscribers

People subscribed via source and target branches