1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# This file is part of cloud-init. See LICENSE file for license information.
import glob
import os
import re
from datetime import datetime
from io import StringIO
import pytest
from cloudinit.cmd.devel import logs
from cloudinit.cmd.devel.logs import ApportFile
from cloudinit.subp import subp
from cloudinit.util import ensure_dir, load_file, write_file
from tests.unittests.helpers import mock
M_PATH = "cloudinit.cmd.devel.logs."
INSTANCE_JSON_SENSITIVE_FILE = "instance-data-sensitive.json"
@mock.patch("cloudinit.cmd.devel.logs.os.getuid")
class TestCollectLogs:
def test_collect_logs_with_userdata_requires_root_user(
self, m_getuid, tmpdir
):
"""collect-logs errors when non-root user collects userdata ."""
m_getuid.return_value = 100 # non-root
output_tarfile = tmpdir.join("logs.tgz")
with mock.patch("sys.stderr", new_callable=StringIO) as m_stderr:
assert 1 == logs.collect_logs(
output_tarfile, include_userdata=True
)
assert (
"To include userdata, root user is required."
" Try sudo cloud-init collect-logs\n" == m_stderr.getvalue()
)
def test_collect_logs_creates_tarfile(self, m_getuid, mocker, tmpdir):
"""collect-logs creates a tarfile with all related cloud-init info."""
m_getuid.return_value = 100
log1 = tmpdir.join("cloud-init.log")
write_file(log1, "cloud-init-log")
log2 = tmpdir.join("cloud-init-output.log")
write_file(log2, "cloud-init-output-log")
run_dir = tmpdir.join("run")
write_file(run_dir.join("results.json"), "results")
write_file(
run_dir.join(
INSTANCE_JSON_SENSITIVE_FILE,
),
"sensitive",
)
output_tarfile = str(tmpdir.join("logs.tgz"))
date = datetime.utcnow().date().strftime("%Y-%m-%d")
date_logdir = "cloud-init-logs-{0}".format(date)
version_out = "/usr/bin/cloud-init 18.2fake\n"
expected_subp = {
(
"dpkg-query",
"--show",
"-f=${Version}\n",
"cloud-init",
): "0.7fake\n",
("cloud-init", "--version"): version_out,
("dmesg",): "dmesg-out\n",
("journalctl", "--boot=0", "-o", "short-precise"): "journal-out\n",
("tar", "czvf", output_tarfile, date_logdir): "",
}
def fake_subp(cmd):
cmd_tuple = tuple(cmd)
if cmd_tuple not in expected_subp:
raise AssertionError(
"Unexpected command provided to subp: {0}".format(cmd)
)
if cmd == ["tar", "czvf", output_tarfile, date_logdir]:
subp(cmd) # Pass through tar cmd so we can check output
return expected_subp[cmd_tuple], ""
fake_stderr = mock.MagicMock()
mocker.patch(M_PATH + "subp", side_effect=fake_subp)
mocker.patch(M_PATH + "sys.stderr", fake_stderr)
mocker.patch(M_PATH + "CLOUDINIT_LOGS", [log1, log2])
mocker.patch(M_PATH + "CLOUDINIT_RUN_DIR", run_dir)
mocker.patch(M_PATH + "INSTALLER_APPORT_FILES", [])
mocker.patch(M_PATH + "INSTALLER_APPORT_SENSITIVE_FILES", [])
logs.collect_logs(output_tarfile, include_userdata=False)
# unpack the tarfile and check file contents
subp(["tar", "zxvf", output_tarfile, "-C", str(tmpdir)])
out_logdir = tmpdir.join(date_logdir)
assert not os.path.exists(
os.path.join(
out_logdir,
"run",
"cloud-init",
INSTANCE_JSON_SENSITIVE_FILE,
)
), (
"Unexpected file found: %s" % INSTANCE_JSON_SENSITIVE_FILE
)
assert "0.7fake\n" == load_file(
os.path.join(out_logdir, "dpkg-version")
)
assert version_out == load_file(os.path.join(out_logdir, "version"))
assert "cloud-init-log" == load_file(
os.path.join(out_logdir, "cloud-init.log")
)
assert "cloud-init-output-log" == load_file(
os.path.join(out_logdir, "cloud-init-output.log")
)
assert "dmesg-out\n" == load_file(
os.path.join(out_logdir, "dmesg.txt")
)
assert "journal-out\n" == load_file(
os.path.join(out_logdir, "journal.txt")
)
assert "results" == load_file(
os.path.join(out_logdir, "run", "cloud-init", "results.json")
)
fake_stderr.write.assert_any_call("Wrote %s\n" % output_tarfile)
def test_collect_logs_includes_optional_userdata(
self, m_getuid, mocker, tmpdir
):
"""collect-logs include userdata when --include-userdata is set."""
m_getuid.return_value = 0
log1 = tmpdir.join("cloud-init.log")
write_file(log1, "cloud-init-log")
log2 = tmpdir.join("cloud-init-output.log")
write_file(log2, "cloud-init-output-log")
userdata = tmpdir.join("user-data.txt")
write_file(userdata, "user-data")
run_dir = tmpdir.join("run")
write_file(run_dir.join("results.json"), "results")
write_file(
run_dir.join(INSTANCE_JSON_SENSITIVE_FILE),
"sensitive",
)
output_tarfile = str(tmpdir.join("logs.tgz"))
date = datetime.utcnow().date().strftime("%Y-%m-%d")
date_logdir = "cloud-init-logs-{0}".format(date)
version_out = "/usr/bin/cloud-init 18.2fake\n"
expected_subp = {
(
"dpkg-query",
"--show",
"-f=${Version}\n",
"cloud-init",
): "0.7fake",
("cloud-init", "--version"): version_out,
("dmesg",): "dmesg-out\n",
("journalctl", "--boot=0", "-o", "short-precise"): "journal-out\n",
("tar", "czvf", output_tarfile, date_logdir): "",
}
def fake_subp(cmd):
cmd_tuple = tuple(cmd)
if cmd_tuple not in expected_subp:
raise AssertionError(
"Unexpected command provided to subp: {0}".format(cmd)
)
if cmd == ["tar", "czvf", output_tarfile, date_logdir]:
subp(cmd) # Pass through tar cmd so we can check output
return expected_subp[cmd_tuple], ""
fake_stderr = mock.MagicMock()
mocker.patch(M_PATH + "subp", side_effect=fake_subp)
mocker.patch(M_PATH + "sys.stderr", fake_stderr)
mocker.patch(M_PATH + "CLOUDINIT_LOGS", [log1, log2])
mocker.patch(M_PATH + "CLOUDINIT_RUN_DIR", run_dir)
mocker.patch(M_PATH + "INSTALLER_APPORT_FILES", [])
mocker.patch(M_PATH + "INSTALLER_APPORT_SENSITIVE_FILES", [])
mocker.patch(M_PATH + "_get_user_data_file", return_value=userdata)
logs.collect_logs(output_tarfile, include_userdata=True)
# unpack the tarfile and check file contents
subp(["tar", "zxvf", output_tarfile, "-C", str(tmpdir)])
out_logdir = tmpdir.join(date_logdir)
assert "user-data" == load_file(
os.path.join(out_logdir, "user-data.txt")
)
assert "sensitive" == load_file(
os.path.join(
out_logdir,
"run",
"cloud-init",
INSTANCE_JSON_SENSITIVE_FILE,
)
)
fake_stderr.write.assert_any_call("Wrote %s\n" % output_tarfile)
class TestCollectInstallerLogs:
@pytest.mark.parametrize(
"include_userdata, apport_files, apport_sensitive_files",
(
pytest.param(True, [], [], id="no_files_include_userdata"),
pytest.param(False, [], [], id="no_files_exclude_userdata"),
pytest.param(
True,
(ApportFile("log1", "Label1"), ApportFile("log2", "Label2")),
(
ApportFile("private1", "LabelPrivate1"),
ApportFile("private2", "PrivateLabel2"),
),
id="files_and_dirs_include_userdata",
),
pytest.param(
False,
(ApportFile("log1", "Label1"), ApportFile("log2", "Label2")),
(
ApportFile("private1", "LabelPrivate1"),
ApportFile("private2", "PrivateLabel2"),
),
id="files_and_dirs_exclude_userdata",
),
),
)
def test_include_installer_logs_when_present(
self,
include_userdata,
apport_files,
apport_sensitive_files,
tmpdir,
mocker,
):
src_dir = tmpdir.join("src")
ensure_dir(src_dir.strpath)
# collect-logs nests full directory path to file in the tarfile
destination_dir = tmpdir.join(src_dir)
# Create tmppath-based userdata_files, installer_logs, installer_dirs
expected_files = []
# Create last file in list to assert ignoring absent files
apport_files = [
logs.ApportFile(src_dir.join(apport.path).strpath, apport.label)
for apport in apport_files
]
if apport_files:
write_file(apport_files[-1].path, apport_files[-1].label)
expected_files += [
destination_dir.join(
os.path.basename(apport_files[-1].path)
).strpath
]
apport_sensitive_files = [
logs.ApportFile(src_dir.join(apport.path).strpath, apport.label)
for apport in apport_sensitive_files
]
if apport_sensitive_files:
write_file(
apport_sensitive_files[-1].path,
apport_sensitive_files[-1].label,
)
if include_userdata:
expected_files += [
destination_dir.join(
os.path.basename(apport_sensitive_files[-1].path)
).strpath
]
mocker.patch(M_PATH + "INSTALLER_APPORT_FILES", apport_files)
mocker.patch(
M_PATH + "INSTALLER_APPORT_SENSITIVE_FILES", apport_sensitive_files
)
logs.collect_installer_logs(
log_dir=tmpdir.strpath,
include_userdata=include_userdata,
verbosity=0,
)
expect_userdata = bool(include_userdata and apport_sensitive_files)
# when subiquity artifacts exist, and userdata set true, expect logs
expect_subiquity_logs = any([apport_files, expect_userdata])
if expect_subiquity_logs:
assert destination_dir.exists(), "Missing subiquity artifact dir"
assert sorted(expected_files) == sorted(
glob.glob(f"{destination_dir.strpath}/*")
)
else:
assert not destination_dir.exists(), "Unexpected subiquity dir"
class TestParser:
def test_parser_help_has_userdata_file(self, mocker, tmpdir):
userdata = str(tmpdir.join("user-data.txt"))
mocker.patch(M_PATH + "_get_user_data_file", return_value=userdata)
assert userdata in re.sub(r"\s+", "", logs.get_parser().format_help())
|