forked from canonical/ubuntu-pro-client
/
test_cli.py
429 lines (342 loc) · 13.2 KB
/
test_cli.py
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import io
import logging
import mock
import os
import socket
import stat
import sys
import textwrap
import pytest
from uaclient.cli import (
assert_attached,
assert_not_attached,
assert_root,
get_parser,
main,
require_valid_entitlement_name,
setup_logging,
)
from uaclient.exceptions import (
AlreadyAttachedError,
NonRootUserError,
UserFacingError,
UnattachedError,
)
from uaclient.testing.fakes import FakeConfig
from uaclient import status
from uaclient import util
BIG_DESC = "123456789 " * 7 + "next line"
BIG_URL = "http://" + "adsf" * 10
SERVICES_WRAPPED_HELP = textwrap.dedent(
"""
Client to manage Ubuntu Advantage services on a machine.
- cc-eal: Common Criteria EAL2 Provisioning Packages
(https://ubuntu.com/cc-eal)
- cis-audit: Center for Internet Security Audit Tools
(https://ubuntu.com/cis-audit)
- esm-apps: UA Apps: Extended Security Maintenance (https://ubuntu.com/esm)
- esm-infra: UA Infra: Extended Security Maintenance (https://ubuntu.com/esm)
- fips: NIST-certified FIPS modules (https://ubuntu.com/fips)
- fips-updates: Uncertified security updates to FIPS modules
- livepatch: Canonical Livepatch service (https://ubuntu.com/livepatch)
"""
)
@pytest.fixture(params=["direct", "--help", "ua help"])
def get_help(request, capsys):
if request.param == "direct":
def _get_help_output():
parser = get_parser()
help_file = io.StringIO()
parser.print_help(file=help_file)
return help_file.getvalue()
elif request.param == "--help":
def _get_help_output():
parser = get_parser()
with mock.patch("sys.argv", ["ua", "--help"]):
with pytest.raises(SystemExit):
parser.parse_args()
out, _err = capsys.readouterr()
return out
elif request.param == "ua help":
def _get_help_output():
with mock.patch("sys.argv", ["ua", "help"]):
main()
out, _err = capsys.readouterr()
return out
else:
raise NotImplementedError("Unknown help source: {}", request.param)
return _get_help_output
class TestCLIParser:
maxDiff = None
@mock.patch("uaclient.cli.entitlements")
def test_help_descr_and_url_is_wrapped_at_eighty_chars(
self, m_entitlements, get_help
):
"""Help lines are wrapped at 80 chars"""
def cls_mock_factory(desc, url):
return mock.Mock(description=desc, help_doc_url=url)
m_entitlements.ENTITLEMENT_CLASS_BY_NAME = {
"test": cls_mock_factory(BIG_DESC, BIG_URL)
}
lines = [
" - test: " + " ".join(["123456789"] * 7),
" next line ({url})".format(url=BIG_URL),
]
assert "\n".join(lines) in get_help()
def test_help_sourced_dynamically_from_each_entitlement(self, get_help):
"""Help output is sourced from entitlement name and description."""
assert SERVICES_WRAPPED_HELP in get_help()
class TestAssertRoot:
def test_assert_root_when_root(self):
arg, kwarg = mock.sentinel.arg, mock.sentinel.kwarg
@assert_root
def test_function(arg, *, kwarg):
assert arg == mock.sentinel.arg
assert kwarg == mock.sentinel.kwarg
return mock.sentinel.success
with mock.patch("uaclient.cli.os.getuid", return_value=0):
ret = test_function(arg, kwarg=kwarg)
assert mock.sentinel.success == ret
def test_assert_root_when_not_root(self):
@assert_root
def test_function():
pass
with mock.patch("uaclient.cli.os.getuid", return_value=1000):
with pytest.raises(NonRootUserError):
test_function()
# Test multiple uids, to be sure that the root checking is absent
@pytest.mark.parametrize("uid", [0, 1000])
class TestAssertAttached:
def test_assert_attached_when_attached(self, capsys, uid):
@assert_attached()
def test_function(args, cfg):
return mock.sentinel.success
cfg = FakeConfig.for_attached_machine()
with mock.patch("uaclient.cli.os.getuid", return_value=uid):
ret = test_function(mock.Mock(), cfg)
assert mock.sentinel.success == ret
out, _err = capsys.readouterr()
assert "" == out.strip()
def test_assert_attached_when_unattached(self, uid):
@assert_attached()
def test_function(args, cfg):
pass
cfg = FakeConfig()
with mock.patch("uaclient.cli.os.getuid", return_value=uid):
with pytest.raises(UnattachedError):
test_function(mock.Mock(), cfg)
@pytest.mark.parametrize("uid", [0, 1000])
class TestAssertNotAttached:
def test_when_attached(self, uid):
@assert_not_attached
def test_function(args, cfg):
pass
cfg = FakeConfig.for_attached_machine()
with mock.patch("uaclient.cli.os.getuid", return_value=uid):
with pytest.raises(AlreadyAttachedError):
test_function(mock.Mock(), cfg)
def test_when_not_attached(self, capsys, uid):
@assert_not_attached
def test_function(args, cfg):
return mock.sentinel.success
cfg = FakeConfig()
with mock.patch("uaclient.cli.os.getuid", return_value=uid):
ret = test_function(mock.Mock(), cfg)
assert mock.sentinel.success == ret
out, _err = capsys.readouterr()
assert "" == out.strip()
class TestRequireValidEntitlementName:
# rven used in test names as short hand for require_valid_entitlement_name
def test_rven_without_name(self):
@require_valid_entitlement_name("operation")
def test_function(args, cfg):
return mock.sentinel.success
assert mock.sentinel.success == test_function(object(), object())
def test_rven_with_valid_name(self):
@require_valid_entitlement_name("operation")
def test_function(args, cfg):
return mock.sentinel.success
m_args = mock.Mock()
m_args.name = "esm-infra"
assert mock.sentinel.success == test_function(m_args, object())
@pytest.mark.parametrize("operation_name", ["operation1", "operation2"])
def test_rven_with_invalid_name(self, operation_name):
@require_valid_entitlement_name(operation_name)
def test_function(args, cfg):
pass
m_args = mock.Mock()
m_args.name = "invalid_entitlement"
with pytest.raises(UserFacingError) as excinfo:
test_function(m_args, object())
assert (
"Cannot {} 'invalid_entitlement'".format(operation_name)
in excinfo.value.msg
)
class TestMain:
@mock.patch("uaclient.cli.setup_logging")
@mock.patch("uaclient.cli.get_parser")
def test_keyboard_interrupt_handled_gracefully(
self,
m_get_parser,
_m_setup_logging,
capsys,
logging_sandbox,
caplog_text,
):
m_args = m_get_parser.return_value.parse_args.return_value
m_args.action.side_effect = KeyboardInterrupt
with pytest.raises(SystemExit) as excinfo:
main(["some", "args"])
exc = excinfo.value
assert 1 == exc.code
out, err = capsys.readouterr()
assert "" == out
assert "Interrupt received; exiting.\n" == err
error_log = caplog_text()
assert "Traceback (most recent call last):" in error_log
@pytest.mark.parametrize(
"exception,expected_exit_code",
[
(UserFacingError("You need to know about this."), 1),
(AlreadyAttachedError(mock.MagicMock()), 0),
],
)
@mock.patch("uaclient.cli.setup_logging")
@mock.patch("uaclient.cli.get_parser")
def test_user_facing_error_handled_gracefully(
self,
m_get_parser,
_m_setup_logging,
capsys,
logging_sandbox,
caplog_text,
exception,
expected_exit_code,
):
m_args = m_get_parser.return_value.parse_args.return_value
m_args.action.side_effect = exception
expected_msg = exception.msg
with pytest.raises(SystemExit) as excinfo:
main(["some", "args"])
exc = excinfo.value
assert expected_exit_code == exc.code
out, err = capsys.readouterr()
assert "" == out
assert "{}\n".format(expected_msg) == err
error_log = caplog_text()
# pytest 4.6.x started indenting trailing lines in log messages, which
# meant that our matching here stopped working once we introduced
# newlines into this log output in #973. (If focal moves onto pytest
# 5.x before release, then we can remove this workaround.) The
# upstream issue is https://github.com/pytest-dev/pytest/issues/5515
error_log = "\n".join(
[line.strip() for line in error_log.splitlines()]
)
assert expected_msg in error_log
assert "Traceback (most recent call last):" in error_log
@pytest.mark.parametrize(
"error_url,expected_log",
(
(
None,
"Check your Internet connection and try again."
" [Errno -2] Name or service not known",
),
(
"http://nowhere.com",
"Check your Internet connection and try again."
" Failed to access URL: http://nowhere.com."
" [Errno -2] Name or service not known",
),
),
)
@mock.patch("uaclient.cli.setup_logging")
@mock.patch("uaclient.cli.get_parser")
def test_url_error_handled_gracefully(
self,
m_get_parser,
_m_setup_logging,
error_url,
expected_log,
capsys,
logging_sandbox,
caplog_text,
):
m_args = m_get_parser.return_value.parse_args.return_value
m_args.action.side_effect = util.UrlError(
socket.gaierror(-2, "Name or service not known"), url=error_url
)
with pytest.raises(SystemExit) as excinfo:
main(["some", "args"])
exc = excinfo.value
assert 1 == exc.code
out, err = capsys.readouterr()
assert "" == out
assert "{}\n".format(status.MESSAGE_CONNECTIVITY_ERROR) == err
error_log = caplog_text()
assert expected_log in error_log
assert "Traceback (most recent call last):" in error_log
class TestSetupLogging:
@pytest.mark.parametrize("level", (logging.INFO, logging.ERROR))
def test_console_log_configured_if_not_present(
self, level, capsys, logging_sandbox
):
setup_logging(level, logging.INFO)
logging.log(level, "after setup")
logging.log(level - 1, "not present")
_, err = capsys.readouterr()
assert "after setup" in err
assert "not present" not in err
def test_console_log_configured_if_already_present(
self, capsys, logging_sandbox
):
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logging.error("before setup")
setup_logging(logging.INFO, logging.INFO)
logging.error("after setup")
# 'before setup' will be in stderr, so check that setup_logging
# configures the format
_, err = capsys.readouterr()
assert "ERROR: before setup" not in err
assert "ERROR: after setup" in err
@mock.patch("uaclient.cli.os.getuid", return_value=100)
def test_file_log_not_configured_if_not_root(
self, m_getuid, tmpdir, logging_sandbox
):
log_file = tmpdir.join("log_file")
setup_logging(logging.INFO, logging.INFO, log_file=log_file.strpath)
logging.info("after setup")
assert not log_file.exists()
@pytest.mark.parametrize("log_filename", (None, "file.log"))
@mock.patch("uaclient.cli.os.getuid", return_value=0)
@mock.patch("uaclient.cli.config")
def test_file_log_configured_if_root(
self, m_config, _m_getuid, log_filename, logging_sandbox, tmpdir
):
if log_filename is None:
log_filename = "default.log"
log_file = tmpdir.join(log_filename)
m_config.CONFIG_DEFAULTS = {"log_file": log_file.strpath}
else:
log_file = tmpdir.join(log_filename)
setup_logging(logging.INFO, logging.INFO, log_file=log_file.strpath)
logging.info("after setup")
assert "after setup" in log_file.read()
@pytest.mark.parametrize("pre_existing", (True, False))
@mock.patch("uaclient.cli.os.getuid", return_value=0)
@mock.patch("uaclient.cli.config")
def test_file_log_only_readable_by_root(
self, m_config, _m_getuid, logging_sandbox, tmpdir, pre_existing
):
log_file = tmpdir.join("root-only.log")
log_path = log_file.strpath
if pre_existing:
log_file.write("existing content\n")
assert 0o600 != stat.S_IMODE(os.lstat(log_path).st_mode)
setup_logging(logging.INFO, logging.INFO, log_file=log_path)
logging.info("after setup")
assert 0o600 == stat.S_IMODE(os.lstat(log_path).st_mode)
log_content = log_file.read()
assert "after setup" in log_content
if pre_existing:
assert "existing content" in log_content