/
test_pulp_smash_cli.py
381 lines (336 loc) · 15.6 KB
/
test_pulp_smash_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
# coding=utf-8
"""Unit tests for :mod:`pulp_smash.pulp_smash_cli`."""
import json
import os
import unittest
from unittest import mock
from click.testing import CliRunner
from pulp_smash import exceptions, pulp_smash_cli, utils
from .test_config import PULP_SMASH_CONFIG
class BasePulpSmashCliTestCase(unittest.TestCase):
"""Base class for all pulp_smash_cli tests."""
def setUp(self):
"""Configure a CliRunner."""
super().setUp()
self.cli_runner = CliRunner()
class MissingSettingsFileMixin:
# pylint:disable=too-few-public-methods
"""Test missing settings file.
Classes that inherit from this mixin should provide the
``settings_subcommand`` attribute set to the settings subcommand to run.
"""
def test_missing_settings_file(self):
"""Ensure show outputs proper settings file."""
with self.cli_runner.isolated_filesystem():
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.side_effect = exceptions.ConfigFileNotFoundError( # noqa: E501
"No config file found."
)
result = self.cli_runner.invoke(pulp_smash_cli.settings, [self.settings_subcommand])
self.assertNotEqual(result.exit_code, 0, result.output)
class SettingsCreateTestCase(BasePulpSmashCliTestCase):
"""Test ``pulp_smash.pulp_smash_cli.settings_create`` command."""
maxDiff = None
def setUp(self):
"""Generate a default expected config dict."""
super().setUp()
self.expected_config_dict = {
"pulp": {
"auth": ["admin", "admin"],
"selinux enabled": True,
"version": "2.13",
},
"general": {"timeout": 1800},
"hosts": [
{
"hostname": "pulp.example.com",
"roles": {
"amqp broker": {"service": "qpidd"},
"api": {
"scheme": "https",
"service": "httpd",
"verify": True,
},
"mongod": {},
"pulp celerybeat": {},
"pulp cli": {},
"pulp resource manager": {},
"pulp workers": {},
"shell": {"transport": "ssh"},
"squid": {},
},
}
],
}
self.expected_config_dict_3 = {
"pulp": {
"auth": ["admin", "admin"],
"selinux enabled": True,
"version": "3.0",
},
"general": {"timeout": 1800},
"hosts": [
{
"hostname": "pulp3.example.com",
"roles": {
"api": {
"scheme": "https",
"service": "nginx",
"verify": True,
"port": 24817,
},
"content": {
"scheme": "https",
"service": "pulp_content_app",
"verify": True,
"port": 24816,
},
"pulp resource manager": {},
"pulp workers": {},
"shell": {"transport": "ssh"},
"redis": {},
},
}
],
}
def _test_common_logic(self, create_input, glp_return_value=None):
"""Test common settings create logic.
:param create_input: the input stream for the prompts.
:param glp_return_value: the return_value of
``PulpSmashConfig.get_load_path`` or, if None, it will
have the side_effect of raising a
:obj:`pulp_smash.exceptions.ConfigFileNotFoundError`.
:return: the generated settings.json as string
"""
with self.cli_runner.isolated_filesystem():
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
if glp_return_value is None:
psc.get_load_path.side_effect = exceptions.ConfigFileNotFoundError(
"Config not found."
) # noqa: E501
else:
psc.get_load_path.return_value = glp_return_value
psc.get_save_path.return_value = "settings.json"
result = self.cli_runner.invoke(
pulp_smash_cli.settings, ["create"], input=create_input
)
self.assertEqual(result.exit_code, 0, result.output)
self.assertTrue(os.path.isfile("settings.json"))
with open("settings.json") as handler:
return handler.read()
def test_create_with_defaults(self):
"""Create settings file with default values values."""
create_input = (
"2.13\n" # pulp version
"\n" # pulp admin username
"\n" # pulp admin password
"\n" # pulp selinux enabled
"\n" # task time out
"pulp.example.com\n" # host hostname
"\n" # host amqp broker
"\n" # host api scheme
"\n" # host api ssl verification
"\n" # host api ssl cert path
"\n" # host api port
"\n" # host api web server
"\n" # running on Pulp host
)
generated_settings = json.loads(self._test_common_logic(create_input))
self.assertEqual(generated_settings, self.expected_config_dict)
def test_settings_already_exists(self):
"""Create settings file by overriding existing one."""
create_input = (
"y\n" # overwrite existing settings file?
"2.13\n" # pulp version
"\n" # pulp admin username
"\n" # pulp admin password
"\n" # pulp selinux enabled
"\n" # task time out
"pulp.example.com\n" # host hostname
"\n" # host amqp broker
"\n" # host api scheme
"\n" # host api ssl verification
"\n" # host api ssl cert path
"\n" # host api port
"\n" # host api web server
"\n" # running on Pulp host
)
generated_settings = json.loads(self._test_common_logic(create_input, "settings.json"))
self.assertEqual(generated_settings, self.expected_config_dict)
def test_create_defaults_and_verify(self):
"""Create settings file with defaults and custom SSL certificate."""
create_input = (
"2.13\n" # pulp version
"\n" # pulp admin username
"\n" # pulp admin password
"\n" # pulp selinux enabled
"\n" # task time out
"pulp.example.com\n" # host hostname
"\n" # host amqp broker
"\n" # host api scheme
"\n" # host api ssl verification
"/path/to/cert\n" # host api ssl cert path
"\n" # host api port
"\n" # host api web server
"\n" # running on Pulp host
)
generated_settings = json.loads(self._test_common_logic(create_input))
self.expected_config_dict["hosts"][0]["roles"]["api"]["verify"] = "/path/to/cert"
self.assertEqual(generated_settings, self.expected_config_dict)
def test_create_other_values(self):
"""Create settings file with custom values."""
create_input = (
"2.13\n" # pulp version
"username\n" # pulp admin username
"password\n" # pulp admin password
"n\n" # pulp selinux enabled
"900\n" # task time out
"pulp.example.com\n" # host hostname
"rabbitmq\n" # host amqp broker
"http\n" # host api scheme
"n\n" # host api ssl verification
"1234\n" # host api port
"nginx\n" # host api web server
"y\n" # running on Pulp host
)
generated_settings = json.loads(self._test_common_logic(create_input))
self.expected_config_dict["pulp"]["auth"] = ["username", "password"]
self.expected_config_dict["pulp"]["selinux enabled"] = False
self.expected_config_dict["general"]["timeout"] = 900
host_roles = self.expected_config_dict["hosts"][0]["roles"]
host_roles["amqp broker"]["service"] = "rabbitmq"
host_roles["api"]["port"] = 1234
host_roles["api"]["scheme"] = "http"
host_roles["api"]["service"] = "nginx"
host_roles["api"]["verify"] = False
host_roles["shell"]["transport"] = "local"
self.assertEqual(generated_settings, self.expected_config_dict)
def test_create_defaults_and_verify_pulp3(self):
"""Create settings file with defaults and custom SSL certificate."""
create_input = (
"3.0\n" # version
"\n" # user
"\n" # password
"\n" # selinux enabled?
"\n" # task time out
"pulp3.example.com\n" # hostname
"\n" # api scheme
"\n" # api verify?
"/path/to/cert/\n"
"\n" # api port
"\n" # api service
"\n" # api on same host
"\n" # shell ssh user
"n\n" # content on same host
"\n" # content scheme
"\n" # content verify
"/path/to/cert/\n" # content cert
"\n" # content port
"\n" # service name
)
generated_settings = json.loads(self._test_common_logic(create_input))
roles = self.expected_config_dict_3["hosts"][0]["roles"]
roles["api"]["verify"] = roles["content"]["verify"] = "/path/to/cert/"
self.assertEqual(generated_settings, self.expected_config_dict_3)
def test_create_other_value_pulp3(self):
"""Create settings file with custom values."""
create_input = (
"3.0\n" # version
"username\n" # user
"password\n" # password
"n\n" # selinux enabled
"900\n" # task time out
"pulp3.example.com\n" # api hostname
"http\n" # api scheme
"n\n" # verify api SSL?
"1234\n" # api port
"httpd\n" # api service
"y\n" # api on same host
"y\n" # content on same host
)
generated_settings = json.loads(self._test_common_logic(create_input))
self.expected_config_dict_3["pulp"]["auth"] = ["username", "password"]
self.expected_config_dict_3["pulp"]["selinux enabled"] = False
self.expected_config_dict_3["general"]["timeout"] = 900
roles = self.expected_config_dict_3["hosts"][0]["roles"]
roles["api"]["port"] = 1234
roles["api"]["scheme"] = "http"
roles["api"]["service"] = "httpd"
roles["api"]["verify"] = False
roles["shell"]["transport"] = "local"
del roles["content"]
self.assertEqual(generated_settings, self.expected_config_dict_3)
class SettingsPathTestCase(BasePulpSmashCliTestCase, MissingSettingsFileMixin):
"""Test ``pulp_smash.pulp_smash_cli.settings_path`` command."""
settings_subcommand = "path"
def test_settings_path(self):
"""Ensure ``path`` outputs proper settings file path."""
with self.cli_runner.isolated_filesystem():
with open("settings.json", "w") as handler:
handler.write(PULP_SMASH_CONFIG)
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.return_value = utils.uuid4()
result = self.cli_runner.invoke(pulp_smash_cli.settings, [self.settings_subcommand])
self.assertEqual(result.exit_code, 0, result.output)
self.assertEqual(psc.get_load_path.return_value, result.output.strip())
class SettingsLoadPathTestCase(BasePulpSmashCliTestCase, MissingSettingsFileMixin):
"""Test ``pulp_smash.pulp_smash_cli.settings_load_path`` command."""
settings_subcommand = "load-path"
def test_settings_load_path(self):
"""Ensure ``load-path`` outputs proper settings file path."""
with self.cli_runner.isolated_filesystem():
with open("settings.json", "w") as handler:
handler.write(PULP_SMASH_CONFIG)
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.return_value = utils.uuid4()
result = self.cli_runner.invoke(pulp_smash_cli.settings, [self.settings_subcommand])
self.assertEqual(result.exit_code, 0, result.output)
self.assertEqual(psc.get_load_path.return_value, result.output.strip())
class SettingsSavePathTestCase(BasePulpSmashCliTestCase):
"""Test ``pulp_smash.pulp_smash_cli.settings_save_path`` command."""
def test_settings_save_path(self):
"""Ensure ``save-path`` outputs proper settings file path."""
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_save_path.return_value = utils.uuid4()
result = self.cli_runner.invoke(pulp_smash_cli.settings, ["save-path"])
self.assertEqual(result.exit_code, 0, result.output)
self.assertEqual(psc.get_save_path.return_value, result.output.strip())
class SettingsShowTestCase(BasePulpSmashCliTestCase, MissingSettingsFileMixin):
"""Test ``pulp_smash.pulp_smash_cli.settings_show`` command."""
settings_subcommand = "show"
def test_settings_show(self):
"""Ensure show outputs proper settings file."""
with self.cli_runner.isolated_filesystem():
with open("settings.json", "w") as handler:
handler.write(PULP_SMASH_CONFIG)
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.return_value = "settings.json"
result = self.cli_runner.invoke(pulp_smash_cli.settings, ["show"])
self.assertEqual(result.exit_code, 0)
self.assertIn(
json.dumps(json.loads(PULP_SMASH_CONFIG), indent=2, sort_keys=True),
result.output,
)
class SettingsValidateTestCase(BasePulpSmashCliTestCase, MissingSettingsFileMixin):
"""Test ``pulp_smash.pulp_smash_cli.settings_validate`` command."""
settings_subcommand = "validate"
def test_valid_config(self):
"""Ensure validate does not complain about valid settings."""
with self.cli_runner.isolated_filesystem():
with open("settings.json", "w") as handler:
handler.write(PULP_SMASH_CONFIG)
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.return_value = "settings.json"
result = self.cli_runner.invoke(pulp_smash_cli.settings, ["validate"])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, "")
def test_invalid_config(self):
"""Ensure validate fails on invalid config file schema."""
cfg_file = "".join([line for line in PULP_SMASH_CONFIG.splitlines() if "auth" not in line])
with self.cli_runner.isolated_filesystem():
with open("settings.json", "w") as handler:
handler.write(cfg_file)
with mock.patch.object(pulp_smash_cli, "PulpSmashConfig") as psc:
psc.get_load_path.return_value = "settings.json"
result = self.cli_runner.invoke(pulp_smash_cli.settings, ["validate"])
self.assertNotEqual(result.exit_code, 0)