| 1 |
# test/test_dput_main.py |
| 2 |
# Part of ‘dput’, a Debian package upload toolkit. |
| 3 |
# |
| 4 |
# This is free software, and you are welcome to redistribute it under |
| 5 |
# certain conditions; see the end of this file for copyright |
| 6 |
# information, grant of license, and disclaimer of warranty. |
| 7 |
|
| 8 |
""" Unit tests for ‘dput’ module `main` function. """ |
| 9 |
|
| 10 |
import contextlib |
| 11 |
import doctest |
| 12 |
import itertools |
| 13 |
import os |
| 14 |
import sys |
| 15 |
import tempfile |
| 16 |
import textwrap |
| 17 |
import types |
| 18 |
import unittest.mock |
| 19 |
|
| 20 |
import testscenarios |
| 21 |
import testtools |
| 22 |
import testtools.matchers |
| 23 |
|
| 24 |
from dput.control_file import upload_changes |
| 25 |
import dput.dput |
| 26 |
from dput.helper import dputhelper |
| 27 |
|
| 28 |
from .control_file.test_upload_changes import ( |
| 29 |
make_changes_file_path, |
| 30 |
set_changes_file_scenario, |
| 31 |
setup_changes_file_fixtures, |
| 32 |
) |
| 33 |
from .helper import ( |
| 34 |
EXIT_STATUS_FAILURE, |
| 35 |
EXIT_STATUS_SUCCESS, |
| 36 |
FakeSystemExit, |
| 37 |
make_fake_distribution, |
| 38 |
make_options_namespace, |
| 39 |
make_unique_slug, |
| 40 |
patch_importlib_metadata_distribution, |
| 41 |
patch_os_environ, |
| 42 |
patch_os_getuid, |
| 43 |
patch_os_isatty, |
| 44 |
patch_pwd_getpwuid, |
| 45 |
patch_sys_argv, |
| 46 |
patch_system_interfaces, |
| 47 |
) |
| 48 |
from .test_configfile import ( |
| 49 |
patch_active_config_files, |
| 50 |
patch_get_fqdn_for_host, |
| 51 |
patch_get_incoming_for_host, |
| 52 |
patch_get_login_for_host, |
| 53 |
patch_get_port_for_host, |
| 54 |
patch_get_progress_indicator_for_host, |
| 55 |
patch_get_upload_method_name_for_host, |
| 56 |
patch_print_config, |
| 57 |
patch_print_default_upload_method, |
| 58 |
patch_print_host_list, |
| 59 |
patch_read_configs, |
| 60 |
patch_runtime_config_options, |
| 61 |
patch_verify_config_upload_methods, |
| 62 |
set_config, |
| 63 |
) |
| 64 |
from .test_dputhelper import ( |
| 65 |
patch_get_username_from_system, |
| 66 |
patch_getopt, |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def set_upload_methods(testcase): |
| 71 |
""" Set the `upload_methods` value for the test case. """ |
| 72 |
if not hasattr(testcase, 'upload_methods'): |
| 73 |
upload_methods = dput.dput.import_upload_functions() |
| 74 |
testcase.upload_methods = { |
| 75 |
name: unittest.mock.MagicMock(method) |
| 76 |
for (name, method) in upload_methods.items()} |
| 77 |
|
| 78 |
|
| 79 |
def patch_import_upload_functions(testcase): |
| 80 |
""" Patch the `import_upload_functions` function for the test case. """ |
| 81 |
func_patcher = unittest.mock.patch.object( |
| 82 |
dput.dput, "import_upload_functions", autospec=True, |
| 83 |
return_value=testcase.upload_methods) |
| 84 |
func_patcher.start() |
| 85 |
testcase.addCleanup(func_patcher.stop) |
| 86 |
|
| 87 |
|
| 88 |
def patch_distribution(testcase): |
| 89 |
""" Patch the “dput” installed distribution for the `testcase`. """ |
| 90 |
testcase.fake_distribution = make_fake_distribution( |
| 91 |
name="dput", |
| 92 |
version=getattr(testcase, 'dput_version', None), |
| 93 |
) |
| 94 |
patch_importlib_metadata_distribution( |
| 95 |
testcase, fake_distribution=testcase.fake_distribution) |
| 96 |
|
| 97 |
|
| 98 |
class make_usage_message_TestCase(testtools.TestCase): |
| 99 |
""" Test cases for `make_usage_message` function. """ |
| 100 |
|
| 101 |
def test_returns_text_with_program_name(self): |
| 102 |
""" Should return text with expected program name. """ |
| 103 |
result = dput.dput.make_usage_message() |
| 104 |
expected_result = textwrap.dedent("""\ |
| 105 |
Usage: dput ... |
| 106 |
... |
| 107 |
""") |
| 108 |
self.expectThat( |
| 109 |
result, |
| 110 |
testtools.matchers.DocTestMatches( |
| 111 |
expected_result, flags=doctest.ELLIPSIS)) |
| 112 |
|
| 113 |
|
| 114 |
class make_delayed_queue_path_TestCase( |
| 115 |
testscenarios.WithScenarios, |
| 116 |
testtools.TestCase): |
| 117 |
""" Test cases for `make_delayed_queue_path` function. """ |
| 118 |
|
| 119 |
scenarios = [ |
| 120 |
('days-unspecified', { |
| 121 |
'test_days': None, |
| 122 |
'expected_result': "", |
| 123 |
}), |
| 124 |
('days-zero', { |
| 125 |
'test_days': 0, |
| 126 |
'expected_result': "DELAYED/0-day", |
| 127 |
}), |
| 128 |
('days-five', { |
| 129 |
'test_days': 5, |
| 130 |
'expected_result': "DELAYED/5-day", |
| 131 |
}), |
| 132 |
] |
| 133 |
|
| 134 |
def setUp(self): |
| 135 |
""" Set up test fixtures. """ |
| 136 |
super().setUp() |
| 137 |
|
| 138 |
self.set_test_args() |
| 139 |
|
| 140 |
def set_test_args(self): |
| 141 |
""" Set the arguments for the test call to the function. """ |
| 142 |
self.test_args = dict( |
| 143 |
days=self.test_days, |
| 144 |
) |
| 145 |
|
| 146 |
def test_returns_expected_result(self): |
| 147 |
""" Should return expected path value. """ |
| 148 |
result = dput.dput.make_delayed_queue_path(**self.test_args) |
| 149 |
self.assertEqual(self.expected_result, result) |
| 150 |
|
| 151 |
|
| 152 |
def patch_make_delayed_queue_path(testcase): |
| 153 |
""" Patch the `make_delayed_queue_path` function for `testcase`. """ |
| 154 |
if not hasattr(testcase, 'delayed_queue_path'): |
| 155 |
testcase.delayed_queue_path = os.path.basename(tempfile.mktemp()) |
| 156 |
func_patcher = unittest.mock.patch.object( |
| 157 |
dput.dput, 'make_delayed_queue_path', autospec=True, |
| 158 |
return_value=testcase.delayed_queue_path) |
| 159 |
func_patcher.start() |
| 160 |
testcase.addCleanup(func_patcher.stop) |
| 161 |
|
| 162 |
|
| 163 |
class upload_files_via_BaseTestCase(testtools.TestCase): |
| 164 |
""" Base for test cases for `upload_files_via_*` functions. """ |
| 165 |
|
| 166 |
placeholder_args = { |
| 167 |
'fqdn': object(), |
| 168 |
'login': object(), |
| 169 |
'incoming': object(), |
| 170 |
'files_to_upload': object(), |
| 171 |
'debug': object(), |
| 172 |
} |
| 173 |
|
| 174 |
def setUp(self): |
| 175 |
""" Set up test fixtures. """ |
| 176 |
super().setUp() |
| 177 |
patch_system_interfaces(self) |
| 178 |
|
| 179 |
set_config(self, 'exist-simple') |
| 180 |
patch_runtime_config_options(self) |
| 181 |
|
| 182 |
set_upload_methods(self) |
| 183 |
self.test_method = self.runtime_config_parser.get( |
| 184 |
self.test_host, 'method') |
| 185 |
self.test_method_func = self.upload_methods[self.test_method] |
| 186 |
|
| 187 |
if not hasattr(self, 'omit_kwargs'): |
| 188 |
self.omit_kwargs = [] |
| 189 |
self.set_test_args() |
| 190 |
self.set_expected_method_and_args() |
| 191 |
|
| 192 |
patch_get_port_for_host(self) |
| 193 |
|
| 194 |
def set_test_args(self): |
| 195 |
""" Set the arguments for the test call to the function. """ |
| 196 |
raise NotImplementedError |
| 197 |
|
| 198 |
def set_expected_method_and_args(self): |
| 199 |
""" Set the expected upload method and its arguments. """ |
| 200 |
self.expected_method_func = self.test_method_func |
| 201 |
self.expected_args_list = [ |
| 202 |
self.placeholder_args['fqdn'], |
| 203 |
self.placeholder_args['login'], |
| 204 |
self.placeholder_args['incoming'], |
| 205 |
self.placeholder_args['files_to_upload'], |
| 206 |
self.placeholder_args['debug'], |
| 207 |
] |
| 208 |
self.expected_args = tuple(self.expected_args_list) |
| 209 |
|
| 210 |
|
| 211 |
def patch_upload_files_via(testcase): |
| 212 |
""" Patch the `upload_files_via_*` functions for `testcase`. """ |
| 213 |
for func_name in [ |
| 214 |
'upload_files_via_{}'.format(suffix) |
| 215 |
for suffix in [ |
| 216 |
'simulate', |
| 217 |
'method', 'method_local', 'method_ftp', 'method_scp'] |
| 218 |
]: |
| 219 |
func_patcher = unittest.mock.patch.object( |
| 220 |
dput.dput, func_name, autospec=True) |
| 221 |
func_patcher.start() |
| 222 |
testcase.addCleanup(func_patcher.stop) |
| 223 |
|
| 224 |
|
| 225 |
class upload_files_via_simulate_TestCase( |
| 226 |
testscenarios.WithScenarios, |
| 227 |
upload_files_via_BaseTestCase): |
| 228 |
""" Test cases for `upload_files_via_simulate` function. """ |
| 229 |
|
| 230 |
function_to_test = staticmethod(dput.dput.upload_files_via_simulate) |
| 231 |
|
| 232 |
scenarios = [ |
| 233 |
('files-zero', { |
| 234 |
'files_to_upload': [], |
| 235 |
}), |
| 236 |
('files-one', { |
| 237 |
'files_to_upload': [tempfile.mktemp()], |
| 238 |
}), |
| 239 |
('files-three', { |
| 240 |
'files_to_upload': [tempfile.mktemp() for __ in range(3)], |
| 241 |
}), |
| 242 |
] |
| 243 |
|
| 244 |
def set_test_args(self): |
| 245 |
""" Set the arguments for the test call to the function. """ |
| 246 |
self.test_args = self.placeholder_args.copy() |
| 247 |
self.test_args.update({ |
| 248 |
'method': make_unique_slug(self), |
| 249 |
'files_to_upload': self.files_to_upload, |
| 250 |
'config': self.runtime_config_parser, |
| 251 |
'host': self.test_host, |
| 252 |
}) |
| 253 |
|
| 254 |
def set_expected_method_and_args(self): |
| 255 |
""" Set the expected upload method and its arguments. """ |
| 256 |
# Upload method is called multiple times with different arguments. |
| 257 |
pass |
| 258 |
|
| 259 |
def test_emits_expected_information_message(self): |
| 260 |
""" Should emit expected information message. """ |
| 261 |
self.function_to_test(**self.test_args) |
| 262 |
expected_stderr_output = ( |
| 263 |
"Not performing upload: ‘simulate’ option specified.") |
| 264 |
self.assertIn(expected_stderr_output, sys.stderr.getvalue()) |
| 265 |
|
| 266 |
def test_emits_diagnostic_message_for_each_file_to_upload(self): |
| 267 |
""" Should emit diagnostic message for each file to upload. """ |
| 268 |
self.function_to_test(**self.test_args) |
| 269 |
for path in self.files_to_upload: |
| 270 |
expected_output = textwrap.dedent("""\ |
| 271 |
Uploading with {method}: {path} to {fqdn}:{incoming} |
| 272 |
""").format( |
| 273 |
method=self.test_args['method'], |
| 274 |
path=path, |
| 275 |
fqdn=self.test_args['fqdn'], |
| 276 |
incoming=self.test_args['incoming']) |
| 277 |
self.expectThat( |
| 278 |
sys.stderr.getvalue(), |
| 279 |
testtools.matchers.Contains(expected_output)) |
| 280 |
|
| 281 |
|
| 282 |
def patch_upload_files_via_simulate(testcase): |
| 283 |
""" Patch the `upload_files_via_simulate` function for the `testcase`. """ |
| 284 |
func_patcher = unittest.mock.patch.object( |
| 285 |
dput.dput, "upload_files_via_simulate", autospec=True) |
| 286 |
func_patcher.start() |
| 287 |
testcase.addCleanup(func_patcher.stop) |
| 288 |
|
| 289 |
|
| 290 |
class upload_files_via_method_TestCase( |
| 291 |
testscenarios.WithScenarios, |
| 292 |
upload_files_via_BaseTestCase): |
| 293 |
""" Test cases for `upload_files_via_method` function. """ |
| 294 |
|
| 295 |
placeholder_args = upload_files_via_BaseTestCase.placeholder_args.copy() |
| 296 |
placeholder_args.update({ |
| 297 |
'files_to_upload': object(), |
| 298 |
'debug': object(), |
| 299 |
'dummy': object(), |
| 300 |
'progress': object(), |
| 301 |
}) |
| 302 |
|
| 303 |
scenarios = [ |
| 304 |
('method-http', { |
| 305 |
'config_method': "http", |
| 306 |
'expected_kwargs': { |
| 307 |
'dummy': placeholder_args['dummy'], |
| 308 |
'progress': placeholder_args['progress'], |
| 309 |
}, |
| 310 |
}), |
| 311 |
('method-http default-dummy', { |
| 312 |
'config_method': "http", |
| 313 |
'omit_kwargs': ['dummy'], |
| 314 |
'expected_kwargs': { |
| 315 |
'dummy': 0, |
| 316 |
'progress': placeholder_args['progress'], |
| 317 |
}, |
| 318 |
}), |
| 319 |
('method-http default-progress', { |
| 320 |
'config_method': "http", |
| 321 |
'omit_kwargs': ['progress'], |
| 322 |
'expected_kwargs': { |
| 323 |
'dummy': placeholder_args['dummy'], |
| 324 |
'progress': 0, |
| 325 |
}, |
| 326 |
}), |
| 327 |
('method-rsync', { |
| 328 |
'config_method': "rsync", |
| 329 |
'expected_kwargs': { |
| 330 |
'dummy': placeholder_args['dummy'], |
| 331 |
'progress': placeholder_args['progress'], |
| 332 |
}, |
| 333 |
}), |
| 334 |
] |
| 335 |
|
| 336 |
def set_test_args(self): |
| 337 |
""" Set the arguments for the test call to the function. """ |
| 338 |
self.test_args = { |
| 339 |
name: self.placeholder_args[name] |
| 340 |
for name in [ |
| 341 |
'fqdn', 'login', 'incoming', 'files_to_upload', 'debug', |
| 342 |
'dummy', 'progress'] |
| 343 |
if name not in self.omit_kwargs} |
| 344 |
self.test_args.update({ |
| 345 |
'method_name': self.test_method, |
| 346 |
'method': self.test_method_func, |
| 347 |
'config': self.runtime_config_parser, |
| 348 |
'host': self.test_host, |
| 349 |
}) |
| 350 |
if hasattr(self, 'test_args_extra'): |
| 351 |
self.test_args.update(self.test_args_extra) |
| 352 |
|
| 353 |
def test_calls_upload_method_with_expected_args(self): |
| 354 |
""" Should call upload method function with expected args. """ |
| 355 |
dput.dput.upload_files_via_method(**self.test_args) |
| 356 |
self.expected_method_func.assert_called_with( |
| 357 |
*self.expected_args, **self.expected_kwargs) |
| 358 |
|
| 359 |
|
| 360 |
class upload_files_via_method_local_TestCase( |
| 361 |
testscenarios.WithScenarios, |
| 362 |
upload_files_via_BaseTestCase): |
| 363 |
""" Test cases for `upload_files_via_method_local` function. """ |
| 364 |
|
| 365 |
placeholder_args = upload_files_via_BaseTestCase.placeholder_args.copy() |
| 366 |
placeholder_args.update({ |
| 367 |
'files_to_upload': object(), |
| 368 |
'debug': object(), |
| 369 |
'compress': object(), |
| 370 |
'progress': object(), |
| 371 |
}) |
| 372 |
|
| 373 |
scenarios = [ |
| 374 |
('method-local', { |
| 375 |
'config_method': "local", |
| 376 |
'expected_kwargs': { |
| 377 |
'compress': placeholder_args['compress'], |
| 378 |
'progress': placeholder_args['progress'], |
| 379 |
}, |
| 380 |
}), |
| 381 |
('method-local default-compress', { |
| 382 |
'config_method': "local", |
| 383 |
'omit_kwargs': ['compress'], |
| 384 |
'expected_kwargs': { |
| 385 |
'compress': False, |
| 386 |
'progress': placeholder_args['progress'], |
| 387 |
}, |
| 388 |
}), |
| 389 |
('method-local default-progress', { |
| 390 |
'config_method': "local", |
| 391 |
'omit_kwargs': ['progress'], |
| 392 |
'expected_kwargs': { |
| 393 |
'compress': placeholder_args['compress'], |
| 394 |
'progress': 0, |
| 395 |
}, |
| 396 |
}), |
| 397 |
] |
| 398 |
|
| 399 |
def set_test_args(self): |
| 400 |
""" Set the arguments for the test call to the function. """ |
| 401 |
self.test_args = { |
| 402 |
name: self.placeholder_args[name] |
| 403 |
for name in [ |
| 404 |
'fqdn', 'login', 'incoming', 'files_to_upload', 'debug', |
| 405 |
'compress', 'progress'] |
| 406 |
if name not in self.omit_kwargs} |
| 407 |
self.test_args.update({ |
| 408 |
'method_name': self.test_method, |
| 409 |
'method': self.test_method_func, |
| 410 |
'config': self.runtime_config_parser, |
| 411 |
'host': self.test_host, |
| 412 |
}) |
| 413 |
if hasattr(self, 'test_args_extra'): |
| 414 |
self.test_args.update(self.test_args_extra) |
| 415 |
|
| 416 |
def test_calls_upload_method_with_expected_args(self): |
| 417 |
""" Should call upload method function with expected args. """ |
| 418 |
dput.dput.upload_files_via_method_local(**self.test_args) |
| 419 |
self.expected_method_func.assert_called_with( |
| 420 |
*self.expected_args, **self.expected_kwargs) |
| 421 |
|
| 422 |
|
| 423 |
class upload_files_via_method_ftp_TestCase( |
| 424 |
testscenarios.WithScenarios, |
| 425 |
upload_files_via_BaseTestCase): |
| 426 |
""" Test cases for `upload_files_via_method_ftp` function. """ |
| 427 |
|
| 428 |
placeholder_args = upload_files_via_BaseTestCase.placeholder_args.copy() |
| 429 |
placeholder_args.update({ |
| 430 |
'files_to_upload': object(), |
| 431 |
'debug': False, |
| 432 |
'commandline_passive': False, |
| 433 |
'progress': object(), |
| 434 |
}) |
| 435 |
|
| 436 |
scenarios = [ |
| 437 |
('method-ftp', { |
| 438 |
'config_method': "ftp", |
| 439 |
'get_port_for_host_return_value': None, |
| 440 |
'expected_kwargs': { |
| 441 |
'ftp_mode': False, |
| 442 |
'progress': placeholder_args['progress'], |
| 443 |
'port': 21, |
| 444 |
}, |
| 445 |
'expected_stdout_output': "\n".join([ |
| 446 |
"D: FTP port: 21", |
| 447 |
"D: Using active ftp", |
| 448 |
]), |
| 449 |
}), |
| 450 |
('method-ftp default-passive', { |
| 451 |
'config_method': "ftp", |
| 452 |
'omit_kwargs': ['commandline_passive'], |
| 453 |
'get_port_for_host_return_value': None, |
| 454 |
'expected_kwargs': { |
| 455 |
'ftp_mode': False, |
| 456 |
'progress': placeholder_args['progress'], |
| 457 |
'port': 21, |
| 458 |
}, |
| 459 |
'expected_stdout_output': "\n".join([ |
| 460 |
"D: FTP port: 21", |
| 461 |
"D: Using active ftp", |
| 462 |
]), |
| 463 |
}), |
| 464 |
('method-ftp default-progress', { |
| 465 |
'config_method': "ftp", |
| 466 |
'omit_kwargs': ['progress'], |
| 467 |
'get_port_for_host_return_value': None, |
| 468 |
'expected_kwargs': { |
| 469 |
'ftp_mode': False, |
| 470 |
'progress': 0, |
| 471 |
'port': 21, |
| 472 |
}, |
| 473 |
'expected_stdout_output': "\n".join([ |
| 474 |
"D: FTP port: 21", |
| 475 |
"D: Using active ftp", |
| 476 |
]), |
| 477 |
}), |
| 478 |
('method-ftp config-port-custom', { |
| 479 |
'config_method': "ftp", |
| 480 |
'get_port_for_host_return_value': 555, |
| 481 |
'expected_kwargs': { |
| 482 |
'ftp_mode': False, |
| 483 |
'progress': placeholder_args['progress'], |
| 484 |
'port': 555, |
| 485 |
}, |
| 486 |
'expected_stdout_output': "\n".join([ |
| 487 |
"D: FTP port: 555", |
| 488 |
"D: Using active ftp", |
| 489 |
]), |
| 490 |
}), |
| 491 |
('method-ftp commandline-passive', { |
| 492 |
'config_method': "ftp", |
| 493 |
'get_port_for_host_return_value': None, |
| 494 |
'test_args_extra': { |
| 495 |
'commandline_passive': True, |
| 496 |
}, |
| 497 |
'expected_kwargs': { |
| 498 |
'ftp_mode': True, |
| 499 |
'progress': placeholder_args['progress'], |
| 500 |
'port': 21, |
| 501 |
}, |
| 502 |
'expected_stdout_output': "\n".join([ |
| 503 |
"D: FTP port: 21", |
| 504 |
"D: Using passive ftp", |
| 505 |
]), |
| 506 |
}), |
| 507 |
('method-ftp config-passive', { |
| 508 |
'config_method': "ftp", |
| 509 |
'config_extras': { |
| 510 |
'host': { |
| 511 |
'passive_ftp': "True", |
| 512 |
}, |
| 513 |
}, |
| 514 |
'omit_kwargs': ['commandline_passive'], |
| 515 |
'get_port_for_host_return_value': None, |
| 516 |
'expected_kwargs': { |
| 517 |
'ftp_mode': True, |
| 518 |
'progress': placeholder_args['progress'], |
| 519 |
'port': 21, |
| 520 |
}, |
| 521 |
'expected_stdout_output': "\n".join([ |
| 522 |
"D: FTP port: 21", |
| 523 |
"D: Using passive ftp", |
| 524 |
]), |
| 525 |
}), |
| 526 |
('method-ftp config-passive-false commandline-passive', { |
| 527 |
'config_method': "ftp", |
| 528 |
'config_extras': { |
| 529 |
'host': { |
| 530 |
'passive_ftp': "False", |
| 531 |
}, |
| 532 |
}, |
| 533 |
'test_args_extra': { |
| 534 |
'commandline_passive': True, |
| 535 |
}, |
| 536 |
'get_port_for_host_return_value': None, |
| 537 |
'expected_kwargs': { |
| 538 |
'ftp_mode': True, |
| 539 |
'progress': placeholder_args['progress'], |
| 540 |
'port': 21, |
| 541 |
}, |
| 542 |
'expected_stdout_output': "\n".join([ |
| 543 |
"D: FTP port: 21", |
| 544 |
"D: Using passive ftp", |
| 545 |
]), |
| 546 |
}), |
| 547 |
('method-ftp config-passive commandline-passive-false', { |
| 548 |
'config_method': "ftp", |
| 549 |
'config_extras': { |
| 550 |
'host': { |
| 551 |
'passive_ftp': "True", |
| 552 |
}, |
| 553 |
}, |
| 554 |
'test_args_extra': { |
| 555 |
'commandline_passive': False, |
| 556 |
}, |
| 557 |
'get_port_for_host_return_value': None, |
| 558 |
'expected_kwargs': { |
| 559 |
'ftp_mode': False, |
| 560 |
'progress': placeholder_args['progress'], |
| 561 |
'port': 21, |
| 562 |
}, |
| 563 |
'expected_stdout_output': "\n".join([ |
| 564 |
"D: FTP port: 21", |
| 565 |
"D: Using active ftp", |
| 566 |
]), |
| 567 |
}), |
| 568 |
] |
| 569 |
|
| 570 |
def set_test_args(self): |
| 571 |
""" Set the arguments for the test call to the function. """ |
| 572 |
self.test_args = { |
| 573 |
name: self.placeholder_args[name] |
| 574 |
for name in [ |
| 575 |
'fqdn', 'login', 'incoming', 'files_to_upload', 'debug', |
| 576 |
'commandline_passive', 'progress'] |
| 577 |
if name not in self.omit_kwargs} |
| 578 |
self.test_args.update({ |
| 579 |
'method_name': self.test_method, |
| 580 |
'method': self.test_method_func, |
| 581 |
'config': self.runtime_config_parser, |
| 582 |
'host': self.test_host, |
| 583 |
}) |
| 584 |
if hasattr(self, 'test_args_extra'): |
| 585 |
self.test_args.update(self.test_args_extra) |
| 586 |
|
| 587 |
def test_emits_expected_debug_message(self): |
| 588 |
""" Should emit expected debug message. """ |
| 589 |
self.test_args['debug'] = True |
| 590 |
dput.dput.upload_files_via_method_ftp(**self.test_args) |
| 591 |
if hasattr(self, 'expected_stdout_output'): |
| 592 |
self.assertIn(self.expected_stdout_output, sys.stdout.getvalue()) |
| 593 |
|
| 594 |
def test_calls_upload_method_with_expected_args(self): |
| 595 |
""" Should call upload method function with expected args. """ |
| 596 |
dput.dput.upload_files_via_method_ftp(**self.test_args) |
| 597 |
self.expected_method_func.assert_called_with( |
| 598 |
*self.expected_args, **self.expected_kwargs) |
| 599 |
|
| 600 |
|
| 601 |
class upload_files_via_method_scp_TestCase( |
| 602 |
testscenarios.WithScenarios, |
| 603 |
upload_files_via_BaseTestCase): |
| 604 |
""" Test cases for `upload_files_via_method_scp` function. """ |
| 605 |
|
| 606 |
placeholder_args = upload_files_via_BaseTestCase.placeholder_args.copy() |
| 607 |
placeholder_args.update({ |
| 608 |
'files_to_upload': object(), |
| 609 |
'debug': False, |
| 610 |
'compress': False, |
| 611 |
'ssh_config_options': None, |
| 612 |
}) |
| 613 |
|
| 614 |
scenarios = [ |
| 615 |
('method-scp', { |
| 616 |
'config_method': "scp", |
| 617 |
'expected_kwargs': { |
| 618 |
'compress': placeholder_args['compress'], |
| 619 |
'ssh_config_options': [], |
| 620 |
}, |
| 621 |
'expected_stdout_output': "\n".join([ |
| 622 |
"D: ssh config options:", |
| 623 |
" ", |
| 624 |
]), |
| 625 |
}), |
| 626 |
('method-scp default-compress', { |
| 627 |
'config_method': "scp", |
| 628 |
'omit_kwargs': ['compress'], |
| 629 |
'expected_kwargs': { |
| 630 |
'compress': False, |
| 631 |
'ssh_config_options': [], |
| 632 |
}, |
| 633 |
'expected_stdout_output': "\n".join([ |
| 634 |
"D: ssh config options:", |
| 635 |
" ", |
| 636 |
]), |
| 637 |
}), |
| 638 |
('method-scp default-ssh-options', { |
| 639 |
'config_method': "scp", |
| 640 |
'omit_kwargs': ['ssh_config_options'], |
| 641 |
'expected_kwargs': { |
| 642 |
'compress': placeholder_args['compress'], |
| 643 |
'ssh_config_options': [], |
| 644 |
}, |
| 645 |
'expected_stdout_output': "\n".join([ |
| 646 |
"D: ssh config options:", |
| 647 |
" ", |
| 648 |
]), |
| 649 |
}), |
| 650 |
('method-scp scp-compress', { |
| 651 |
'config_method': "scp", |
| 652 |
'test_args_extra': { |
| 653 |
'compress': True, |
| 654 |
}, |
| 655 |
'expected_kwargs': { |
| 656 |
'compress': True, |
| 657 |
'ssh_config_options': [], |
| 658 |
}, |
| 659 |
'expected_stdout_output': "\n".join([ |
| 660 |
"D: Setting compression for scp", |
| 661 |
"D: ssh config options:", |
| 662 |
" ", |
| 663 |
]), |
| 664 |
}), |
| 665 |
('method-scp ssh-options-one', { |
| 666 |
'config_method': "scp", |
| 667 |
'test_args_extra': { |
| 668 |
'compress': placeholder_args['compress'], |
| 669 |
'ssh_config_options': ["lorem ipsum"], |
| 670 |
}, |
| 671 |
'expected_kwargs': { |
| 672 |
'compress': placeholder_args['compress'], |
| 673 |
'ssh_config_options': ["lorem ipsum"], |
| 674 |
}, |
| 675 |
'expected_stdout_output': "\n".join([ |
| 676 |
"D: ssh config options:", |
| 677 |
" ", |
| 678 |
" lorem ipsum", |
| 679 |
" ", |
| 680 |
]), |
| 681 |
}), |
| 682 |
('method-scp ssh-options-three', { |
| 683 |
'config_method': "scp", |
| 684 |
'test_args_extra': { |
| 685 |
'compress': placeholder_args['compress'], |
| 686 |
'ssh_config_options': ["spam", "eggs", "beans"], |
| 687 |
}, |
| 688 |
'expected_kwargs': { |
| 689 |
'compress': placeholder_args['compress'], |
| 690 |
'ssh_config_options': ["spam", "eggs", "beans"], |
| 691 |
}, |
| 692 |
'expected_stdout_output': "\n".join([ |
| 693 |
"D: ssh config options:", |
| 694 |
" ", |
| 695 |
" spam", |
| 696 |
" eggs", |
| 697 |
" beans", |
| 698 |
" ", |
| 699 |
]), |
| 700 |
}), |
| 701 |
] |
| 702 |
|
| 703 |
def set_test_args(self): |
| 704 |
""" Set the arguments for the test call to the function. """ |
| 705 |
self.test_args = { |
| 706 |
name: self.placeholder_args[name] |
| 707 |
for name in [ |
| 708 |
'fqdn', 'login', 'incoming', 'files_to_upload', 'debug', |
| 709 |
'compress', 'ssh_config_options'] |
| 710 |
if name not in self.omit_kwargs} |
| 711 |
self.test_args.update({ |
| 712 |
'method_name': self.test_method, |
| 713 |
'method': self.test_method_func, |
| 714 |
'config': self.runtime_config_parser, |
| 715 |
'host': self.test_host, |
| 716 |
}) |
| 717 |
if hasattr(self, 'test_args_extra'): |
| 718 |
self.test_args.update(self.test_args_extra) |
| 719 |
|
| 720 |
def test_emits_expected_debug_message(self): |
| 721 |
""" Should emit expected debug message. """ |
| 722 |
self.test_args['debug'] = True |
| 723 |
dput.dput.upload_files_via_method_scp(**self.test_args) |
| 724 |
if hasattr(self, 'expected_stdout_output'): |
| 725 |
self.assertIn(self.expected_stdout_output, sys.stdout.getvalue()) |
| 726 |
|
| 727 |
def test_calls_upload_method_with_expected_args(self): |
| 728 |
""" Should call upload method function with expected args. """ |
| 729 |
dput.dput.upload_files_via_method_scp(**self.test_args) |
| 730 |
self.expected_method_func.assert_called_with( |
| 731 |
*self.expected_args, **self.expected_kwargs) |
| 732 |
|
| 733 |
|
| 734 |
class upload_files_TestCase( |
| 735 |
testscenarios.WithScenarios, |
| 736 |
testtools.TestCase): |
| 737 |
""" Test cases for `upload_files` function. """ |
| 738 |
|
| 739 |
files_scenarios = [ |
| 740 |
('files-none', { |
| 741 |
'files_to_upload': [], |
| 742 |
}), |
| 743 |
('files-one', { |
| 744 |
'files_to_upload': [tempfile.mktemp()], |
| 745 |
}), |
| 746 |
('files-three', { |
| 747 |
'files_to_upload': [tempfile.mktemp() for __ in range(3)], |
| 748 |
}), |
| 749 |
] |
| 750 |
|
| 751 |
method_scenarios = [ |
| 752 |
('method-http', { |
| 753 |
'method_name': "http", |
| 754 |
'config_method': "http", |
| 755 |
'get_progress_indicator_for_host_return_value': 23, |
| 756 |
'expected_func_name': 'upload_files_via_method', |
| 757 |
'expected_kwargs_extra': { |
| 758 |
'dummy': 0, |
| 759 |
'progress': 23, |
| 760 |
}, |
| 761 |
}), |
| 762 |
('method-http', { |
| 763 |
'method_name': "https", |
| 764 |
'config_method': "https", |
| 765 |
'get_progress_indicator_for_host_return_value': 23, |
| 766 |
'expected_func_name': 'upload_files_via_method', |
| 767 |
'expected_kwargs_extra': { |
| 768 |
'dummy': 0, |
| 769 |
'progress': 23, |
| 770 |
}, |
| 771 |
}), |
| 772 |
('method-http', { |
| 773 |
'method_name': "rsync", |
| 774 |
'config_method': "rsync", |
| 775 |
'get_progress_indicator_for_host_return_value': 23, |
| 776 |
'expected_func_name': 'upload_files_via_method', |
| 777 |
'expected_kwargs_extra': { |
| 778 |
'dummy': 0, |
| 779 |
'progress': 23, |
| 780 |
}, |
| 781 |
}), |
| 782 |
('method-local', { |
| 783 |
'method_name': "local", |
| 784 |
'config_method': "local", |
| 785 |
'get_progress_indicator_for_host_return_value': 23, |
| 786 |
'expected_func_name': 'upload_files_via_method_local', |
| 787 |
'expected_kwargs_extra': { |
| 788 |
'compress': 0, |
| 789 |
'progress': 23, |
| 790 |
}, |
| 791 |
}), |
| 792 |
('method-ftp', { |
| 793 |
'method_name': "ftp", |
| 794 |
'config_method': "ftp", |
| 795 |
'get_progress_indicator_for_host_return_value': 23, |
| 796 |
'test_kwargs_extra': { |
| 797 |
'commandline_passive': True, |
| 798 |
}, |
| 799 |
'expected_func_name': 'upload_files_via_method_ftp', |
| 800 |
'expected_kwargs_extra': { |
| 801 |
'commandline_passive': True, |
| 802 |
'progress': 23, |
| 803 |
}, |
| 804 |
}), |
| 805 |
('method-scp ssh-config-options-none', { |
| 806 |
'method_name': "scp", |
| 807 |
'config_method': "scp", |
| 808 |
'config_extras': { |
| 809 |
'host': { |
| 810 |
'scp_compress': "True", |
| 811 |
'ssh_config_options': "", |
| 812 |
}, |
| 813 |
}, |
| 814 |
'get_progress_indicator_for_host_return_value': 23, |
| 815 |
'expected_func_name': 'upload_files_via_method_scp', |
| 816 |
'expected_kwargs_extra': { |
| 817 |
'compress': True, |
| 818 |
'ssh_config_options': [], |
| 819 |
}, |
| 820 |
}), |
| 821 |
('method-scp ssh-config-options-three', { |
| 822 |
'method_name': "scp", |
| 823 |
'config_method': "scp", |
| 824 |
'config_extras': { |
| 825 |
'host': { |
| 826 |
'scp_compress': "True", |
| 827 |
'ssh_config_options': "spam\n\teggs\n beans\n", |
| 828 |
}, |
| 829 |
}, |
| 830 |
'get_progress_indicator_for_host_return_value': 23, |
| 831 |
'expected_func_name': 'upload_files_via_method_scp', |
| 832 |
'expected_kwargs_extra': { |
| 833 |
'compress': True, |
| 834 |
'ssh_config_options': ["spam", "eggs", "beans"], |
| 835 |
}, |
| 836 |
}), |
| 837 |
] |
| 838 |
|
| 839 |
delayed_scenarios = [ |
| 840 |
('delayed-none', { |
| 841 |
'delayed_queue_path': "", |
| 842 |
'expected_queue_annotation': None, |
| 843 |
}), |
| 844 |
('delayed-arbitrary', { |
| 845 |
'delayed_queue_path': "delayed-path", |
| 846 |
'expected_queue_annotation': "[delayed-path]", |
| 847 |
}), |
| 848 |
] |
| 849 |
|
| 850 |
scenarios = testscenarios.multiply_scenarios( |
| 851 |
files_scenarios, method_scenarios, delayed_scenarios) |
| 852 |
|
| 853 |
for (scenario_name, scenario) in scenarios: |
| 854 |
incoming_root = tempfile.mktemp() |
| 855 |
scenario['get_incoming_for_host_return_value'] = incoming_root |
| 856 |
expected_incoming = incoming_root |
| 857 |
if scenario['delayed_queue_path'] is not None: |
| 858 |
expected_incoming = os.path.join( |
| 859 |
incoming_root, |
| 860 |
scenario['delayed_queue_path']) |
| 861 |
scenario['expected_incoming'] = expected_incoming |
| 862 |
del incoming_root, expected_incoming |
| 863 |
del scenario_name, scenario |
| 864 |
|
| 865 |
def setUp(self): |
| 866 |
""" Set up test fixtures. """ |
| 867 |
super().setUp() |
| 868 |
patch_system_interfaces(self) |
| 869 |
|
| 870 |
set_config(self, 'exist-simple') |
| 871 |
patch_runtime_config_options(self) |
| 872 |
|
| 873 |
self.set_files_to_upload() |
| 874 |
set_upload_methods(self) |
| 875 |
|
| 876 |
self.set_test_args() |
| 877 |
|
| 878 |
patch_determine_delay_days(self) |
| 879 |
patch_make_delayed_queue_path(self) |
| 880 |
patch_get_login_for_host(self) |
| 881 |
self.expected_login = dput.dput.get_login_for_host.return_value |
| 882 |
patch_get_fqdn_for_host(self) |
| 883 |
self.expected_fqdn = dput.dput.get_fqdn_for_host.return_value |
| 884 |
|
| 885 |
patch_get_incoming_for_host(self) |
| 886 |
self.expected_incoming = dput.dput.get_incoming_for_host.return_value |
| 887 |
if dput.dput.make_delayed_queue_path.return_value: |
| 888 |
self.expected_incoming = os.path.join( |
| 889 |
dput.dput.get_incoming_for_host.return_value, |
| 890 |
dput.dput.make_delayed_queue_path.return_value) |
| 891 |
|
| 892 |
patch_get_progress_indicator_for_host(self) |
| 893 |
patch_upload_files_via(self) |
| 894 |
|
| 895 |
def set_files_to_upload(self): |
| 896 |
""" Set the `files_to_upload` collection for this instance. """ |
| 897 |
if not hasattr(self, 'files_to_upload'): |
| 898 |
self.files_to_upload = [] |
| 899 |
|
| 900 |
def set_test_args(self): |
| 901 |
""" Set the arguments for the test call to the function. """ |
| 902 |
self.test_args = dict( |
| 903 |
upload_methods=self.upload_methods, |
| 904 |
config=self.runtime_config_parser, |
| 905 |
host=self.test_host, |
| 906 |
files_to_upload=self.files_to_upload, |
| 907 |
debug=False, |
| 908 |
) |
| 909 |
if hasattr(self, 'test_kwargs_extra'): |
| 910 |
self.test_args.update(self.test_kwargs_extra) |
| 911 |
if hasattr(self, 'test_simulate'): |
| 912 |
self.test_args['simulate'] = self.test_simulate |
| 913 |
|
| 914 |
def test_emits_expected_queue_message(self): |
| 915 |
""" Should emit expected message for the upload queue. """ |
| 916 |
dput.dput.upload_files(**self.test_args) |
| 917 |
if self.expected_queue_annotation is not None: |
| 918 |
expected_destination_template = "{host} {annotation}" |
| 919 |
else: |
| 920 |
expected_destination_template = "{host}" |
| 921 |
expected_destination = expected_destination_template.format( |
| 922 |
host=self.test_host, |
| 923 |
annotation=self.expected_queue_annotation) |
| 924 |
expected_method_name = self.method_name |
| 925 |
expected_output = ( |
| 926 |
"Uploading to {destination}" |
| 927 |
" (via {method} to {fqdn})" |
| 928 |
).format( |
| 929 |
destination=expected_destination, |
| 930 |
method=expected_method_name, fqdn=self.expected_fqdn) |
| 931 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 932 |
|
| 933 |
def test_emits_expected_diagnostic_message(self): |
| 934 |
""" Should emit expected diagnostic debug message. """ |
| 935 |
self.test_args['debug'] = True |
| 936 |
dput.dput.upload_files(**self.test_args) |
| 937 |
self.expected_fqdn = dput.dput.get_fqdn_for_host.return_value |
| 938 |
self.expected_login = dput.dput.get_login_for_host.return_value |
| 939 |
expected_output = textwrap.dedent("""\ |
| 940 |
D: FQDN: {fqdn} |
| 941 |
D: Login: {login} |
| 942 |
D: Incoming: {incoming} |
| 943 |
""").format( |
| 944 |
fqdn=self.expected_fqdn, |
| 945 |
login=self.expected_login, |
| 946 |
incoming=self.expected_incoming) |
| 947 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 948 |
|
| 949 |
def test_calls_expected_upload_function_with_expected_args(self): |
| 950 |
""" Should call expected upload function with expected args. """ |
| 951 |
dput.dput.upload_files(**self.test_args) |
| 952 |
expected_func = getattr(dput.dput, self.expected_func_name) |
| 953 |
expected_method_func = self.upload_methods[self.method_name] |
| 954 |
expected_args_list = [ |
| 955 |
self.method_name, |
| 956 |
expected_method_func, |
| 957 |
self.runtime_config_parser, |
| 958 |
self.test_host, |
| 959 |
self.expected_fqdn, |
| 960 |
self.expected_login, |
| 961 |
self.expected_incoming, |
| 962 |
self.files_to_upload, |
| 963 |
self.test_args['debug'], |
| 964 |
] |
| 965 |
self.expected_args = tuple(expected_args_list) |
| 966 |
self.expected_kwargs = {} |
| 967 |
if hasattr(self, 'expected_kwargs_extra'): |
| 968 |
self.expected_kwargs.update(self.expected_kwargs_extra) |
| 969 |
expected_func.assert_called_with( |
| 970 |
*self.expected_args, **self.expected_kwargs) |
| 971 |
|
| 972 |
def test_omits_upload_method_function_when_simulate(self): |
| 973 |
""" Should omit call to upload method function when ‘simulate’. """ |
| 974 |
self.test_args['simulate'] = True |
| 975 |
dput.dput.upload_files(**self.test_args) |
| 976 |
expected_func = getattr(dput.dput, self.expected_func_name) |
| 977 |
self.assertFalse( |
| 978 |
expected_func.called, |
| 979 |
"should not call {func}".format(func=expected_func)) |
| 980 |
|
| 981 |
|
| 982 |
def patch_upload_files(testcase): |
| 983 |
""" Patch the `upload_files` function for `testcase`. """ |
| 984 |
func_patcher = unittest.mock.patch.object( |
| 985 |
dput.dput, "upload_files", autospec=True) |
| 986 |
func_patcher.start() |
| 987 |
testcase.addCleanup(func_patcher.stop) |
| 988 |
|
| 989 |
|
| 990 |
class parse_commandline_BaseTestCase( |
| 991 |
testtools.TestCase): |
| 992 |
""" Base class for test cases for function `parse_commandline`. """ |
| 993 |
|
| 994 |
function_to_test = staticmethod(dput.dput.parse_commandline) |
| 995 |
|
| 996 |
progname = "lorem" |
| 997 |
dput_version = "ipsum" |
| 998 |
dput_usage_message = "Lorem ipsum, dolor sit amet." |
| 999 |
|
| 1000 |
def setUp(self): |
| 1001 |
""" Set up test fixtures. """ |
| 1002 |
super().setUp() |
| 1003 |
patch_system_interfaces(self) |
| 1004 |
|
| 1005 |
setup_changes_file_fixtures(self) |
| 1006 |
set_changes_file_scenario(self, 'exist-minimal') |
| 1007 |
self.set_files_to_upload() |
| 1008 |
|
| 1009 |
self.patch_make_usage_message() |
| 1010 |
patch_distribution(self) |
| 1011 |
patch_get_login_for_host(self) |
| 1012 |
patch_verify_config_upload_methods(self) |
| 1013 |
|
| 1014 |
self.options_defaults = dict(self.function_to_test.options_defaults) |
| 1015 |
self.patch_getopt() |
| 1016 |
|
| 1017 |
if not hasattr(self, 'test_argv'): |
| 1018 |
self.test_argv = self.make_test_argv() |
| 1019 |
self.test_args = dict( |
| 1020 |
argv=self.test_argv, |
| 1021 |
) |
| 1022 |
if hasattr(self, 'expected_options'): |
| 1023 |
self.set_expected_options_mapping() |
| 1024 |
self.set_expected_args() |
| 1025 |
|
| 1026 |
def set_files_to_upload(self): |
| 1027 |
""" Set the `files_to_upload` collection for this test case. """ |
| 1028 |
if not hasattr(self, 'files_to_upload'): |
| 1029 |
self.files_to_upload = [self.changes_file_double] |
| 1030 |
|
| 1031 |
def make_test_argv(self): |
| 1032 |
""" Make the `argv` value for this test case. """ |
| 1033 |
test_argv = [self.progname] + [ |
| 1034 |
fake_file.path for fake_file in self.files_to_upload] |
| 1035 |
return test_argv |
| 1036 |
|
| 1037 |
def patch_make_usage_message(self): |
| 1038 |
""" Patch the `make_usage_message` function. """ |
| 1039 |
if not hasattr(self, 'dput_usage_message'): |
| 1040 |
self.dput_usage_message = self.getUniqueString() |
| 1041 |
func_patcher = unittest.mock.patch.object( |
| 1042 |
dput.dput, "make_usage_message", autospec=True, |
| 1043 |
return_value=self.dput_usage_message) |
| 1044 |
func_patcher.start() |
| 1045 |
self.addCleanup(func_patcher.stop) |
| 1046 |
|
| 1047 |
def set_expected_args(self): |
| 1048 |
""" Set the `expected_args` collection for this test case. """ |
| 1049 |
if not hasattr(self, 'expected_args'): |
| 1050 |
self.expected_args = self.getopt_args |
| 1051 |
|
| 1052 |
def set_expected_options_mapping(self): |
| 1053 |
""" Set the `expected_options_mapping` for this test case. """ |
| 1054 |
if not hasattr(self, 'expected_options_mapping'): |
| 1055 |
self.expected_options_mapping = self.options_defaults.copy() |
| 1056 |
self.expected_options_mapping.update(self.expected_options) |
| 1057 |
|
| 1058 |
def patch_getopt(self): |
| 1059 |
""" Patch the `dputhelper.getopt` function. """ |
| 1060 |
if not hasattr(self, 'getopt_opts'): |
| 1061 |
self.getopt_opts = [] |
| 1062 |
if not hasattr(self, 'getopt_args'): |
| 1063 |
self.getopt_args = [ |
| 1064 |
fake_file.path for fake_file in self.files_to_upload] |
| 1065 |
|
| 1066 |
patch_getopt(self) |
| 1067 |
|
| 1068 |
|
| 1069 |
class parse_commandline_TestCase( |
| 1070 |
testscenarios.WithScenarios, |
| 1071 |
parse_commandline_BaseTestCase): |
| 1072 |
""" Test cases for function `parse_commandline`. """ |
| 1073 |
|
| 1074 |
scenarios = [ |
| 1075 |
('host-omitted files-one options-none', { |
| 1076 |
'test_argv': ["commandname"] + [ |
| 1077 |
make_changes_file_path(tempfile.mktemp())], |
| 1078 |
'expected_options': {}, |
| 1079 |
}), |
| 1080 |
('host-omitted files-three options-none', { |
| 1081 |
'test_argv': ["commandname"] + [ |
| 1082 |
make_changes_file_path(tempfile.mktemp()) |
| 1083 |
for __ in range(3)], |
| 1084 |
'expected_options': {}, |
| 1085 |
}), |
| 1086 |
('host-named files-one options-none', { |
| 1087 |
'test_argv': ["commandname"] + ["lorem"] + [ |
| 1088 |
make_changes_file_path(tempfile.mktemp())], |
| 1089 |
'expected_options': {}, |
| 1090 |
}), |
| 1091 |
('host-named files-three options-none', { |
| 1092 |
'test_argv': ["commandname"] + ["lorem"] + [ |
| 1093 |
make_changes_file_path(tempfile.mktemp()) |
| 1094 |
for __ in range(3)], |
| 1095 |
'expected_options': {}, |
| 1096 |
}), |
| 1097 |
] |
| 1098 |
|
| 1099 |
def test_calls_getopt_with_expected_args(self): |
| 1100 |
""" Should call `getopt` with expected arguments. """ |
| 1101 |
self.function_to_test(**self.test_args) |
| 1102 |
dputhelper.getopt.assert_called_with( |
| 1103 |
self.test_argv[1:], |
| 1104 |
unittest.mock.ANY, unittest.mock.ANY) |
| 1105 |
|
| 1106 |
def test_returns_expected_values(self): |
| 1107 |
""" Should return expected 2-tuple of (`options`, `args`). """ |
| 1108 |
(options, args) = self.function_to_test(**self.test_args) |
| 1109 |
options_mapping = { |
| 1110 |
name: getattr(options, name) |
| 1111 |
for name in dir(options) |
| 1112 |
if not name.startswith("_")} |
| 1113 |
self.assertEqual(self.expected_options_mapping, options_mapping) |
| 1114 |
self.assertEqual(self.expected_args, args) |
| 1115 |
|
| 1116 |
@unittest.mock.patch.object(dput.dput, 'debug', new=object()) |
| 1117 |
def test_sets_debug_flag_when_debug_option(self): |
| 1118 |
""" Should set `debug` when ‘--debug’ option. """ |
| 1119 |
self.getopt_opts = [("--debug", None)] |
| 1120 |
self.function_to_test(**self.test_args) |
| 1121 |
self.assertEqual(dput.dput.debug, True) |
| 1122 |
|
| 1123 |
|
| 1124 |
class parse_commandline_ValidOptionsTestCase( |
| 1125 |
testscenarios.WithScenarios, |
| 1126 |
parse_commandline_BaseTestCase): |
| 1127 |
""" Test cases for function `parse_commandline`, valid options. """ |
| 1128 |
|
| 1129 |
args_scenarios = [ |
| 1130 |
('host-omitted files-one', { |
| 1131 |
'test_argv_command': "commandname", |
| 1132 |
'test_argv_args': [ |
| 1133 |
make_changes_file_path(tempfile.mktemp())], |
| 1134 |
}), |
| 1135 |
('host-omitted files-three', { |
| 1136 |
'test_argv_command': "commandname", |
| 1137 |
'test_argv_args': [ |
| 1138 |
make_changes_file_path(tempfile.mktemp()) |
| 1139 |
for __ in range(3)], |
| 1140 |
}), |
| 1141 |
('host-named files-one', { |
| 1142 |
'test_argv_command': "commandname", |
| 1143 |
'test_argv_args': ["lorem"] + [ |
| 1144 |
make_changes_file_path(tempfile.mktemp())], |
| 1145 |
}), |
| 1146 |
('host-named files-three', { |
| 1147 |
'test_argv_command': "commandname", |
| 1148 |
'test_argv_args': ["lorem"] + [ |
| 1149 |
make_changes_file_path(tempfile.mktemp()) |
| 1150 |
for __ in range(3)], |
| 1151 |
}), |
| 1152 |
] |
| 1153 |
|
| 1154 |
options_scenarios = [ |
| 1155 |
('options-debug-short', { |
| 1156 |
'test_argv_options': ["-d"], |
| 1157 |
'expected_options': { |
| 1158 |
'debug': True, |
| 1159 |
}, |
| 1160 |
}), |
| 1161 |
('options-debug-long', { |
| 1162 |
'test_argv_options': ["--debug"], |
| 1163 |
'expected_options': { |
| 1164 |
'debug': True, |
| 1165 |
}, |
| 1166 |
}), |
| 1167 |
('options-dinstall-short', { |
| 1168 |
'test_argv_options': ["-D"], |
| 1169 |
'expected_options': { |
| 1170 |
'run_dinstall': True, |
| 1171 |
}, |
| 1172 |
}), |
| 1173 |
('options-dinstall-long', { |
| 1174 |
'test_argv_options': ["--dinstall"], |
| 1175 |
'expected_options': { |
| 1176 |
'run_dinstall': True, |
| 1177 |
}, |
| 1178 |
}), |
| 1179 |
('options-config-short', { |
| 1180 |
'test_argv_options': ["-c", "/tmp/nonexistent"], |
| 1181 |
'expected_options': { |
| 1182 |
'config_file_path': "/tmp/nonexistent", |
| 1183 |
}, |
| 1184 |
}), |
| 1185 |
('options-config-long', { |
| 1186 |
'test_argv_options': ["--config", "/tmp/nonexistent"], |
| 1187 |
'expected_options': { |
| 1188 |
'config_file_path': "/tmp/nonexistent", |
| 1189 |
}, |
| 1190 |
}), |
| 1191 |
('options-force-short', { |
| 1192 |
'test_argv_options': ["-f"], |
| 1193 |
'expected_options': { |
| 1194 |
'force_upload': True, |
| 1195 |
}, |
| 1196 |
}), |
| 1197 |
('options-force-long', { |
| 1198 |
'test_argv_options': ["--force"], |
| 1199 |
'expected_options': { |
| 1200 |
'force_upload': True, |
| 1201 |
}, |
| 1202 |
}), |
| 1203 |
('options-host-list-short', { |
| 1204 |
'test_argv_options': ["-H"], |
| 1205 |
'expected_options': { |
| 1206 |
'config_host_list': True, |
| 1207 |
}, |
| 1208 |
}), |
| 1209 |
('options-host-list-long', { |
| 1210 |
'test_argv_options': ["--host-list"], |
| 1211 |
'expected_options': { |
| 1212 |
'config_host_list': True, |
| 1213 |
}, |
| 1214 |
}), |
| 1215 |
('options-lintian-short', { |
| 1216 |
'test_argv_options': ["-l"], |
| 1217 |
'expected_options': { |
| 1218 |
'run_lintian': True, |
| 1219 |
}, |
| 1220 |
}), |
| 1221 |
('options-lintian-long', { |
| 1222 |
'test_argv_options': ["--lintian"], |
| 1223 |
'expected_options': { |
| 1224 |
'run_lintian': True, |
| 1225 |
}, |
| 1226 |
}), |
| 1227 |
('options-no-upload-log-short', { |
| 1228 |
'test_argv_options': ["-U"], |
| 1229 |
'expected_options': { |
| 1230 |
'upload_log': False, |
| 1231 |
}, |
| 1232 |
}), |
| 1233 |
('options-no-upload-log-long', { |
| 1234 |
'test_argv_options': ["--no-upload-log"], |
| 1235 |
'expected_options': { |
| 1236 |
'upload_log': False, |
| 1237 |
}, |
| 1238 |
}), |
| 1239 |
('options-check-only-short', { |
| 1240 |
'test_argv_options': ["-o"], |
| 1241 |
'expected_options': { |
| 1242 |
'check_only': True, |
| 1243 |
}, |
| 1244 |
}), |
| 1245 |
('options-check-only-long', { |
| 1246 |
'test_argv_options': ["--check-only"], |
| 1247 |
'expected_options': { |
| 1248 |
'check_only': True, |
| 1249 |
}, |
| 1250 |
}), |
| 1251 |
('options-print-short', { |
| 1252 |
'test_argv_options': ["-p"], |
| 1253 |
'expected_options': { |
| 1254 |
'config_print': True, |
| 1255 |
}, |
| 1256 |
}), |
| 1257 |
('options-print-long', { |
| 1258 |
'test_argv_options': ["--print"], |
| 1259 |
'expected_options': { |
| 1260 |
'config_print': True, |
| 1261 |
}, |
| 1262 |
}), |
| 1263 |
('options-passive-short', { |
| 1264 |
'test_argv_options': ["-P"], |
| 1265 |
'expected_options': { |
| 1266 |
'commandline_passive': True, |
| 1267 |
}, |
| 1268 |
}), |
| 1269 |
('options-passive-long', { |
| 1270 |
'test_argv_options': ["--passive"], |
| 1271 |
'expected_options': { |
| 1272 |
'commandline_passive': True, |
| 1273 |
}, |
| 1274 |
}), |
| 1275 |
('options-simulate-short', { |
| 1276 |
'test_argv_options': ["-s"], |
| 1277 |
'expected_options': { |
| 1278 |
'simulate': True, |
| 1279 |
}, |
| 1280 |
}), |
| 1281 |
('options-simulate-long', { |
| 1282 |
'test_argv_options': ["--simulate"], |
| 1283 |
'expected_options': { |
| 1284 |
'simulate': True, |
| 1285 |
}, |
| 1286 |
}), |
| 1287 |
('options-unchecked-short', { |
| 1288 |
'test_argv_options': ["-u"], |
| 1289 |
'expected_options': { |
| 1290 |
'allow_unsigned_uploads': True, |
| 1291 |
}, |
| 1292 |
}), |
| 1293 |
('options-unchecked-long', { |
| 1294 |
'test_argv_options': ["--unchecked"], |
| 1295 |
'expected_options': { |
| 1296 |
'allow_unsigned_uploads': True, |
| 1297 |
}, |
| 1298 |
}), |
| 1299 |
('options-delayed-short', { |
| 1300 |
'test_argv_options': ["-e", "7"], |
| 1301 |
'expected_options': { |
| 1302 |
'delay_days_text': "7", |
| 1303 |
}, |
| 1304 |
}), |
| 1305 |
('options-delayed-long', { |
| 1306 |
'test_argv_options': ["--delayed", "7"], |
| 1307 |
'expected_options': { |
| 1308 |
'delay_days_text': "7", |
| 1309 |
}, |
| 1310 |
}), |
| 1311 |
('options-check-version-short', { |
| 1312 |
'test_argv_options': ["-V"], |
| 1313 |
'expected_options': { |
| 1314 |
'check_version': True, |
| 1315 |
}, |
| 1316 |
}), |
| 1317 |
('options-check-version-long', { |
| 1318 |
'test_argv_options': ["--check-version"], |
| 1319 |
'expected_options': { |
| 1320 |
'check_version': True, |
| 1321 |
}, |
| 1322 |
}), |
| 1323 |
('options-check-version-no-upload-log-lintian-long', { |
| 1324 |
'test_argv_options': [ |
| 1325 |
"--check-version", |
| 1326 |
"--no-upload-log", |
| 1327 |
"--lintian", |
| 1328 |
], |
| 1329 |
'getopt_opts': [ |
| 1330 |
("--check-version", None), |
| 1331 |
("--no-upload-log", None), |
| 1332 |
("--lintian", None), |
| 1333 |
], |
| 1334 |
'expected_options': { |
| 1335 |
'check_version': True, |
| 1336 |
'run_lintian': True, |
| 1337 |
'upload_log': False, |
| 1338 |
}, |
| 1339 |
}), |
| 1340 |
# Legacy mis-spelled option name. |
| 1341 |
('options-check_version-long-legacy', { |
| 1342 |
'test_argv_options': ["--check_version"], |
| 1343 |
'getopt_opts': [ |
| 1344 |
# The parameters to `getopt` are such that the parsed |
| 1345 |
# options will never contain the option spelled this way. |
| 1346 |
# This scenario is designed only to exercise the legacy |
| 1347 |
# code. |
| 1348 |
("--check_version", None), |
| 1349 |
], |
| 1350 |
'expected_options': { |
| 1351 |
'check_version': True, |
| 1352 |
}, |
| 1353 |
}), |
| 1354 |
('options-check_version-no-upload-log-lintian-long', { |
| 1355 |
'test_argv_options': [ |
| 1356 |
"--check_version", |
| 1357 |
"--no-upload-log", |
| 1358 |
"--lintian", |
| 1359 |
], |
| 1360 |
'getopt_opts': [ |
| 1361 |
# The parameters to `getopt` are such that the parsed |
| 1362 |
# options will never contain the option spelled this way. |
| 1363 |
# This scenario is designed only to exercise the legacy |
| 1364 |
# code. |
| 1365 |
("--check_version", None), |
| 1366 |
("--no-upload-log", None), |
| 1367 |
("--lintian", None), |
| 1368 |
], |
| 1369 |
'expected_options': { |
| 1370 |
'check_version': True, |
| 1371 |
'run_lintian': True, |
| 1372 |
'upload_log': False, |
| 1373 |
}, |
| 1374 |
}), |
| 1375 |
] |
| 1376 |
|
| 1377 |
scenarios = testscenarios.multiply_scenarios( |
| 1378 |
args_scenarios, options_scenarios) |
| 1379 |
for (scenario_name, scenario) in scenarios: |
| 1380 |
scenario['test_argv'] = [scenario['test_argv_command']] |
| 1381 |
scenario['test_argv'].extend(scenario['test_argv_options']) |
| 1382 |
scenario['test_argv'].extend(scenario['test_argv_args']) |
| 1383 |
scenario['expected_arguments'] = scenario['test_argv_args'] |
| 1384 |
scenario['getopt_args'] = scenario['test_argv_args'] |
| 1385 |
if 'getopt_opts' not in scenario: |
| 1386 |
fake_parsed_option = scenario['test_argv_options'][0] |
| 1387 |
fake_parsed_option_value = ( |
| 1388 |
scenario['test_argv_options'][1] if len( |
| 1389 |
scenario['test_argv_options']) > 1 |
| 1390 |
else None) |
| 1391 |
scenario['getopt_opts'] = [ |
| 1392 |
(fake_parsed_option, fake_parsed_option_value), |
| 1393 |
] |
| 1394 |
del scenario_name, scenario |
| 1395 |
|
| 1396 |
def test_calls_getopt_with_expected_args(self): |
| 1397 |
""" Should call `getopt` with expected arguments. """ |
| 1398 |
self.function_to_test(**self.test_args) |
| 1399 |
dputhelper.getopt.assert_called_with( |
| 1400 |
self.test_argv[1:], |
| 1401 |
unittest.mock.ANY, unittest.mock.ANY) |
| 1402 |
|
| 1403 |
def test_returns_expected_values(self): |
| 1404 |
""" Should return expected 2-tuple of (`options`, `args`). """ |
| 1405 |
(options, args) = self.function_to_test(**self.test_args) |
| 1406 |
options_mapping = { |
| 1407 |
name: getattr(options, name) |
| 1408 |
for name in dir(options) |
| 1409 |
if not name.startswith("_")} |
| 1410 |
self.assertEqual(self.expected_options_mapping, options_mapping) |
| 1411 |
self.assertEqual(self.expected_args, args) |
| 1412 |
|
| 1413 |
|
| 1414 |
class parse_commandline_ErrorTestCase(parse_commandline_BaseTestCase): |
| 1415 |
""" Test cases for `parse_commandline`, error conditions. """ |
| 1416 |
|
| 1417 |
def test_calls_sys_exit_when_no_nonoption_args(self): |
| 1418 |
""" Should call `sys.exit` when no non-option args. """ |
| 1419 |
self.getopt_args = [] |
| 1420 |
for option in [ |
| 1421 |
"--debug", "--force", "--simulate", "--lintian"]: |
| 1422 |
self.getopt_opts = [(option, None)] |
| 1423 |
with self.subTest(test_opts=self.getopt_opts): |
| 1424 |
with testtools.ExpectedException(FakeSystemExit): |
| 1425 |
self.function_to_test(**self.test_args) |
| 1426 |
sys.exit.assert_called_with(os.EX_USAGE) |
| 1427 |
|
| 1428 |
def test_calls_sys_exit_when_unknown_option(self): |
| 1429 |
""" Should call `sys.exit` when an unknown option. """ |
| 1430 |
self.getopt_args.insert(1, "--b0gUs") |
| 1431 |
dputhelper.getopt.side_effect = dputhelper.DputException |
| 1432 |
with testtools.ExpectedException(FakeSystemExit): |
| 1433 |
self.function_to_test(**self.test_args) |
| 1434 |
sys.exit.assert_called_with(os.EX_USAGE) |
| 1435 |
|
| 1436 |
|
| 1437 |
class parse_commandline_PrintAndEndTestCase(parse_commandline_BaseTestCase): |
| 1438 |
""" Test cases for `parse_commandline` that print and end. """ |
| 1439 |
|
| 1440 |
def test_emits_usage_message_when_option_help(self): |
| 1441 |
""" Should emit usage message when ‘--help’ option. """ |
| 1442 |
self.getopt_opts = [("--help", None)] |
| 1443 |
with contextlib.suppress(FakeSystemExit): |
| 1444 |
self.function_to_test(**self.test_args) |
| 1445 |
expected_output = self.dput_usage_message |
| 1446 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 1447 |
|
| 1448 |
def test_calls_sys_exit_when_option_help(self): |
| 1449 |
""" Should call `sys.exit` when ‘--help’ option. """ |
| 1450 |
self.getopt_opts = [("--help", None)] |
| 1451 |
with testtools.ExpectedException(FakeSystemExit): |
| 1452 |
self.function_to_test(**self.test_args) |
| 1453 |
sys.exit.assert_called_with(EXIT_STATUS_SUCCESS) |
| 1454 |
|
| 1455 |
def test_emits_version_message_when_option_version(self): |
| 1456 |
""" Should emit version message when ‘--version’ option. """ |
| 1457 |
self.getopt_opts = [("--version", None)] |
| 1458 |
with contextlib.suppress(FakeSystemExit): |
| 1459 |
self.function_to_test(**self.test_args) |
| 1460 |
expected_progname = self.progname |
| 1461 |
expected_version = self.fake_distribution.version |
| 1462 |
expected_output = "{progname} {version}".format( |
| 1463 |
progname=expected_progname, version=expected_version) |
| 1464 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 1465 |
|
| 1466 |
def test_calls_sys_exit_when_option_version(self): |
| 1467 |
""" Should call `sys.exit` when ‘--version’ option. """ |
| 1468 |
self.getopt_opts = [("--version", None)] |
| 1469 |
with testtools.ExpectedException(FakeSystemExit): |
| 1470 |
self.function_to_test(**self.test_args) |
| 1471 |
sys.exit.assert_called_with(EXIT_STATUS_SUCCESS) |
| 1472 |
|
| 1473 |
def test_returns_options_normally_when_option_print_config(self): |
| 1474 |
""" Should set ‘config_print’ when option ‘--print’ is present. """ |
| 1475 |
self.getopt_opts = [("--print", None)] |
| 1476 |
self.getopt_args = [] |
| 1477 |
(options, __) = self.function_to_test(**self.test_args) |
| 1478 |
self.assertEqual(True, options.config_print) |
| 1479 |
|
| 1480 |
def test_returns_options_normally_when_option_host_list(self): |
| 1481 |
""" Should set ‘config_host_list’ when option ‘--host-list’. """ |
| 1482 |
self.getopt_opts = [("--host-list", None)] |
| 1483 |
self.getopt_args = [] |
| 1484 |
(options, __) = self.function_to_test(**self.test_args) |
| 1485 |
self.assertEqual(True, options.config_host_list) |
| 1486 |
|
| 1487 |
|
| 1488 |
class determine_delay_days_TestCase( |
| 1489 |
testscenarios.WithScenarios, |
| 1490 |
testtools.TestCase): |
| 1491 |
""" Test cases for `determine_delay_days` function. """ |
| 1492 |
|
| 1493 |
function_to_test = staticmethod(dput.dput.determine_delay_days) |
| 1494 |
|
| 1495 |
commandline_scenarios = [ |
| 1496 |
('commandline-unspecified', {}), |
| 1497 |
('commandline-specified-empty-string', { |
| 1498 |
'test_commandline_text_value': "", |
| 1499 |
'expected_exception': TypeError, |
| 1500 |
}), |
| 1501 |
('commandline-specified-non-numeric-string', { |
| 1502 |
'test_commandline_text_value': "DEADB33F", |
| 1503 |
'expected_exception': TypeError, |
| 1504 |
}), |
| 1505 |
('commandline-specified-numerals', { |
| 1506 |
'test_commandline_text_value': "3", |
| 1507 |
'expected_result': 3, |
| 1508 |
}), |
| 1509 |
('commandline-specified-numerals-out-of-range', { |
| 1510 |
'test_commandline_text_value': "16", |
| 1511 |
'expected_exception': ValueError, |
| 1512 |
}), |
| 1513 |
('commandline-specified-negative-number', { |
| 1514 |
'test_commandline_text_value': "-3", |
| 1515 |
'expected_exception': ValueError, |
| 1516 |
}), |
| 1517 |
] |
| 1518 |
|
| 1519 |
config_scenarios = [ |
| 1520 |
('config-unspecified', { |
| 1521 |
'config_delayed': None, |
| 1522 |
}), |
| 1523 |
('config-specified-empty-string', { |
| 1524 |
'config_delayed': "", |
| 1525 |
'expected_value_if_host_config_used': None, |
| 1526 |
}), |
| 1527 |
('config-specified-numerals', { |
| 1528 |
'config_delayed': "9", |
| 1529 |
'expected_value_if_host_config_used': 9, |
| 1530 |
}), |
| 1531 |
] |
| 1532 |
|
| 1533 |
config_default_scenarios = [ |
| 1534 |
('config-default-unspecified', { |
| 1535 |
'config_default_delayed': None, |
| 1536 |
'expected_value_if_config_default_used': None, |
| 1537 |
}), |
| 1538 |
('config-default-specified-empty-string', { |
| 1539 |
'config_default_delayed': "", |
| 1540 |
'expected_value_if_config_default_used': None, |
| 1541 |
}), |
| 1542 |
('config-default-specified-numerals', { |
| 1543 |
'config_default_delayed': "9", |
| 1544 |
'expected_value_if_config_default_used': 9, |
| 1545 |
}), |
| 1546 |
] |
| 1547 |
|
| 1548 |
scenarios = testscenarios.multiply_scenarios( |
| 1549 |
commandline_scenarios, config_scenarios, config_default_scenarios) |
| 1550 |
|
| 1551 |
def setUp(self): |
| 1552 |
""" Set up test fixtures. """ |
| 1553 |
super().setUp() |
| 1554 |
|
| 1555 |
set_config(self, 'exist-simple') |
| 1556 |
patch_runtime_config_options(self) |
| 1557 |
|
| 1558 |
self.set_test_args() |
| 1559 |
self.set_expected_result() |
| 1560 |
|
| 1561 |
def set_test_args(self): |
| 1562 |
""" Set the ‘test_args’ value for this test case. """ |
| 1563 |
test_options = types.SimpleNamespace() |
| 1564 |
if hasattr(self, 'test_commandline_text_value'): |
| 1565 |
test_options.delay_days_text = ( |
| 1566 |
self.test_commandline_text_value) |
| 1567 |
test_config_params = self.config_scenario['configs_by_name']['runtime'] |
| 1568 |
test_config_parser = test_config_params['config'] |
| 1569 |
self.test_args = { |
| 1570 |
'options': test_options, |
| 1571 |
'host_config': test_config_parser[self.test_host], |
| 1572 |
} |
| 1573 |
|
| 1574 |
def set_expected_result(self): |
| 1575 |
""" Set the ‘expected_result’ value for this test case. """ |
| 1576 |
if not hasattr(self, 'expected_result'): |
| 1577 |
if not hasattr(self, 'expected_value_if_host_config_used'): |
| 1578 |
if not hasattr(self, 'expected_value_if_config_default_used'): |
| 1579 |
self.expected_result = None |
| 1580 |
else: |
| 1581 |
self.expected_result = ( |
| 1582 |
self.expected_value_if_config_default_used) |
| 1583 |
else: |
| 1584 |
self.expected_result = self.expected_value_if_host_config_used |
| 1585 |
|
| 1586 |
def test_returns_expected_result(self): |
| 1587 |
""" Should give expected result for inputs. """ |
| 1588 |
if hasattr(self, 'expected_exception'): |
| 1589 |
with testtools.ExpectedException(self.expected_exception): |
| 1590 |
self.function_to_test(**self.test_args) |
| 1591 |
else: |
| 1592 |
result = self.function_to_test(**self.test_args) |
| 1593 |
self.assertEqual(self.expected_result, result) |
| 1594 |
|
| 1595 |
|
| 1596 |
def patch_determine_delay_days(testcase): |
| 1597 |
""" Patch the `determine_delay_days` function for the `testcase`. """ |
| 1598 |
if not hasattr(testcase, 'determine_delay_days_return_value'): |
| 1599 |
testcase.determine_delay_days_return_value = None |
| 1600 |
func_patcher = unittest.mock.patch.object( |
| 1601 |
dput.dput, "determine_delay_days", autospec=True, |
| 1602 |
return_value=testcase.determine_delay_days_return_value) |
| 1603 |
func_patcher.start() |
| 1604 |
testcase.addCleanup(func_patcher.stop) |
| 1605 |
|
| 1606 |
|
| 1607 |
class main_TestCase( |
| 1608 |
testtools.TestCase): |
| 1609 |
""" Base for test cases for `main` function. """ |
| 1610 |
|
| 1611 |
function_to_test = staticmethod(dput.dput.main) |
| 1612 |
|
| 1613 |
def setUp(self): |
| 1614 |
""" Set up test fixtures. """ |
| 1615 |
super().setUp() |
| 1616 |
patch_system_interfaces(self) |
| 1617 |
|
| 1618 |
set_config( |
| 1619 |
self, |
| 1620 |
getattr(self, 'config_scenario_name', 'exist-simple')) |
| 1621 |
patch_runtime_config_options(self) |
| 1622 |
|
| 1623 |
setup_changes_file_fixtures(self) |
| 1624 |
set_changes_file_scenario(self, 'exist-minimal') |
| 1625 |
|
| 1626 |
patch_os_isatty(self) |
| 1627 |
patch_os_environ(self) |
| 1628 |
patch_os_getuid(self) |
| 1629 |
patch_pwd_getpwuid(self) |
| 1630 |
patch_sys_argv(self) |
| 1631 |
|
| 1632 |
self.set_files_to_upload() |
| 1633 |
set_upload_methods(self) |
| 1634 |
|
| 1635 |
self.set_test_args() |
| 1636 |
|
| 1637 |
self.patch_make_usage_message() |
| 1638 |
patch_distribution(self) |
| 1639 |
|
| 1640 |
self.options_defaults = dict( |
| 1641 |
dput.dput.parse_commandline.options_defaults) |
| 1642 |
self.patch_parse_commandline() |
| 1643 |
patch_active_config_files(self) |
| 1644 |
patch_read_configs(self) |
| 1645 |
patch_print_config(self) |
| 1646 |
patch_get_username_from_system(self) |
| 1647 |
patch_get_login_for_host(self) |
| 1648 |
patch_import_upload_functions(self) |
| 1649 |
patch_get_upload_method_name_for_host(self) |
| 1650 |
patch_make_delayed_queue_path(self) |
| 1651 |
patch_upload_files(self) |
| 1652 |
patch_get_progress_indicator_for_host(self) |
| 1653 |
patch_get_fqdn_for_host(self) |
| 1654 |
patch_get_incoming_for_host(self) |
| 1655 |
patch_determine_delay_days(self) |
| 1656 |
patch_print_default_upload_method(self) |
| 1657 |
patch_print_host_list(self) |
| 1658 |
self.patch_guess_upload_host() |
| 1659 |
self.patch_check_upload_logfile() |
| 1660 |
self.patch_verify_files() |
| 1661 |
self.patch_run_lintian_test() |
| 1662 |
self.patch_execute_command() |
| 1663 |
self.patch_create_upload_file() |
| 1664 |
self.patch_dinstall_caller() |
| 1665 |
|
| 1666 |
def set_files_to_upload(self): |
| 1667 |
""" Set the `files_to_upload` collection for this instance. """ |
| 1668 |
if not hasattr(self, 'files_to_upload'): |
| 1669 |
self.files_to_upload = [] |
| 1670 |
|
| 1671 |
def set_test_args(self): |
| 1672 |
""" Set the arguments for the test call to the function. """ |
| 1673 |
self.test_args = dict() |
| 1674 |
|
| 1675 |
def patch_make_usage_message(self): |
| 1676 |
""" Patch the `make_usage_message` function. """ |
| 1677 |
if not hasattr(self, 'dput_usage_message'): |
| 1678 |
self.dput_usage_message = self.getUniqueString() |
| 1679 |
func_patcher = unittest.mock.patch.object( |
| 1680 |
dput.dput, "make_usage_message", autospec=True, |
| 1681 |
return_value=self.dput_usage_message) |
| 1682 |
func_patcher.start() |
| 1683 |
self.addCleanup(func_patcher.stop) |
| 1684 |
|
| 1685 |
def set_fake_options(self, options_mapping=None): |
| 1686 |
""" Set the `fake_options` object, updated with `options_mapping`. """ |
| 1687 |
if options_mapping is None: |
| 1688 |
options_mapping = self.fake_options_mapping |
| 1689 |
full_options_mapping = dict(itertools.chain( |
| 1690 |
self.options_defaults.items(), |
| 1691 |
options_mapping.items(), |
| 1692 |
)) |
| 1693 |
self.fake_options = make_options_namespace(full_options_mapping) |
| 1694 |
|
| 1695 |
def patch_parse_commandline(self): |
| 1696 |
""" Patch the `parse_commandline` function. """ |
| 1697 |
if not hasattr(self, 'fake_options_mapping'): |
| 1698 |
self.fake_options_mapping = {} |
| 1699 |
self.set_fake_options(self.fake_options_mapping) |
| 1700 |
if not hasattr(self, 'fake_args'): |
| 1701 |
self.fake_args = [self.changes_file_double.path] |
| 1702 |
|
| 1703 |
def fake_parse_commandline(*args, **kwargs): |
| 1704 |
return (self.fake_options, self.fake_args) |
| 1705 |
|
| 1706 |
func_patcher = unittest.mock.patch.object( |
| 1707 |
dput.dput, "parse_commandline", |
| 1708 |
side_effect=fake_parse_commandline) |
| 1709 |
func_patcher.start() |
| 1710 |
self.addCleanup(func_patcher.stop) |
| 1711 |
|
| 1712 |
def patch_guess_upload_host(self): |
| 1713 |
""" Patch the `guess_upload_host` function. """ |
| 1714 |
if not hasattr(self, 'guess_upload_host_return_value'): |
| 1715 |
self.guess_upload_host_return_value = self.test_host |
| 1716 |
func_patcher = unittest.mock.patch.object( |
| 1717 |
upload_changes, "guess_upload_host", autospec=True, |
| 1718 |
return_value=self.guess_upload_host_return_value) |
| 1719 |
func_patcher.start() |
| 1720 |
self.addCleanup(func_patcher.stop) |
| 1721 |
|
| 1722 |
def patch_check_upload_logfile(self): |
| 1723 |
""" Patch the `check_upload_logfile` function. """ |
| 1724 |
func_patcher = unittest.mock.patch.object( |
| 1725 |
dput.dput, "check_upload_logfile", autospec=True) |
| 1726 |
func_patcher.start() |
| 1727 |
self.addCleanup(func_patcher.stop) |
| 1728 |
|
| 1729 |
def patch_verify_files(self): |
| 1730 |
""" Patch the `verify_files` function. """ |
| 1731 |
func_patcher = unittest.mock.patch.object( |
| 1732 |
upload_changes, "verify_files", autospec=True, |
| 1733 |
return_value=self.files_to_upload) |
| 1734 |
func_patcher.start() |
| 1735 |
self.addCleanup(func_patcher.stop) |
| 1736 |
|
| 1737 |
def patch_run_lintian_test(self): |
| 1738 |
""" Patch the `run_lintian_test` function. """ |
| 1739 |
func_patcher = unittest.mock.patch.object( |
| 1740 |
dput.dput, "run_lintian_test", autospec=True) |
| 1741 |
func_patcher.start() |
| 1742 |
self.addCleanup(func_patcher.stop) |
| 1743 |
|
| 1744 |
def patch_execute_command(self): |
| 1745 |
""" Patch the `execute_command` function. """ |
| 1746 |
func_patcher = unittest.mock.patch.object( |
| 1747 |
dput.dput, "execute_command", autospec=True) |
| 1748 |
func_patcher.start() |
| 1749 |
self.addCleanup(func_patcher.stop) |
| 1750 |
|
| 1751 |
def patch_create_upload_file(self): |
| 1752 |
""" Patch the `create_upload_file` function. """ |
| 1753 |
func_patcher = unittest.mock.patch.object( |
| 1754 |
dput.dput, "create_upload_file", autospec=True) |
| 1755 |
func_patcher.start() |
| 1756 |
self.addCleanup(func_patcher.stop) |
| 1757 |
|
| 1758 |
def patch_dinstall_caller(self): |
| 1759 |
""" Patch the `dinstall_caller` function. """ |
| 1760 |
func_patcher = unittest.mock.patch.object( |
| 1761 |
dput.dput, "dinstall_caller", autospec=True) |
| 1762 |
func_patcher.start() |
| 1763 |
self.addCleanup(func_patcher.stop) |
| 1764 |
|
| 1765 |
|
| 1766 |
class main_CommandLineProcessingTestCase(main_TestCase): |
| 1767 |
""" Test cases for `main` command-line processing. """ |
| 1768 |
|
| 1769 |
sys_argv = ["lorem"] + [ |
| 1770 |
make_changes_file_path(tempfile.mktemp()) |
| 1771 |
for __ in range(3)] |
| 1772 |
|
| 1773 |
def test_calls_parse_commandline_with_expected_args(self): |
| 1774 |
""" Should call `parse_commandline` with expected arguments. """ |
| 1775 |
self.function_to_test(**self.test_args) |
| 1776 |
dput.dput.parse_commandline.assert_called_with(self.sys_argv) |
| 1777 |
|
| 1778 |
def test_returns_exit_status_when_systemexit(self): |
| 1779 |
""" Should return exit status from `SystemExit` exception. """ |
| 1780 |
exit_status = self.getUniqueInteger() |
| 1781 |
dput.dput.parse_commandline.side_effect = SystemExit(exit_status) |
| 1782 |
result = self.function_to_test(**self.test_args) |
| 1783 |
self.assertEqual(result, exit_status) |
| 1784 |
|
| 1785 |
|
| 1786 |
class main_CommandLineProcessing_ErrorTestCase(main_TestCase): |
| 1787 |
""" Test cases for `main` command-line processing, error conditions. """ |
| 1788 |
|
| 1789 |
fake_args = [ |
| 1790 |
make_changes_file_path(tempfile.mktemp()) |
| 1791 |
for __ in range(3)] |
| 1792 |
|
| 1793 |
def test_emits_error_message_when_filename_not_match_glob(self): |
| 1794 |
""" Should emit an error message when filename does not match glob. """ |
| 1795 |
self.fake_args[2] = tempfile.mktemp() |
| 1796 |
self.patch_parse_commandline() |
| 1797 |
with contextlib.suppress(FakeSystemExit): |
| 1798 |
self.function_to_test(**self.test_args) |
| 1799 |
self.expected_stderr_output = ( |
| 1800 |
"Filename does not match ‘*.changes’: {}".format( |
| 1801 |
self.fake_args[2])) |
| 1802 |
self.assertIn(self.expected_stderr_output, sys.stderr.getvalue()) |
| 1803 |
|
| 1804 |
def test_calls_sys_exit_when_filename_not_match_glob(self): |
| 1805 |
""" Should call `sys.exit` when filename does not match glob. """ |
| 1806 |
self.fake_args[2] = tempfile.mktemp() |
| 1807 |
self.patch_parse_commandline() |
| 1808 |
with testtools.ExpectedException(FakeSystemExit): |
| 1809 |
self.function_to_test(**self.test_args) |
| 1810 |
|
| 1811 |
def test_emits_error_when_delayed_invalid(self): |
| 1812 |
""" Should emit error message when ‘--delayed’ invalid. """ |
| 1813 |
days_error = TypeError("wrong") |
| 1814 |
dput.dput.determine_delay_days.side_effect = days_error |
| 1815 |
with contextlib.suppress(FakeSystemExit): |
| 1816 |
self.function_to_test(**self.test_args) |
| 1817 |
expected_output = textwrap.dedent("""\ |
| 1818 |
Incorrect delayed argument: {exc_type}: ... |
| 1819 |
""".format( |
| 1820 |
exc_type=type(days_error).__name__)) |
| 1821 |
self.assertThat( |
| 1822 |
sys.stdout.getvalue(), |
| 1823 |
testtools.matchers.DocTestMatches( |
| 1824 |
expected_output, doctest.ELLIPSIS)) |
| 1825 |
|
| 1826 |
def test_emits_error_when_delayed_value_out_of_range(self): |
| 1827 |
""" Should emit error message when ‘--delayed’ value out of range. """ |
| 1828 |
days_error = ValueError("bad") |
| 1829 |
dput.dput.determine_delay_days.side_effect = days_error |
| 1830 |
with contextlib.suppress(FakeSystemExit): |
| 1831 |
self.function_to_test(**self.test_args) |
| 1832 |
expected_output = textwrap.dedent("""\ |
| 1833 |
Incorrect delayed argument: {exc_type}: ... |
| 1834 |
""".format( |
| 1835 |
exc_type=type(days_error).__name__)) |
| 1836 |
self.assertThat( |
| 1837 |
sys.stdout.getvalue(), |
| 1838 |
testtools.matchers.DocTestMatches( |
| 1839 |
expected_output, doctest.ELLIPSIS)) |
| 1840 |
|
| 1841 |
def test_calls_sys_exit_when_invalid_delayed_value(self): |
| 1842 |
""" Should call `sys.exit` when ‘--delayed’ invalid. """ |
| 1843 |
days_error = ValueError("bad") |
| 1844 |
dput.dput.determine_delay_days.side_effect = days_error |
| 1845 |
with testtools.ExpectedException(FakeSystemExit): |
| 1846 |
self.function_to_test(**self.test_args) |
| 1847 |
sys.exit.assert_called_with(os.EX_USAGE) |
| 1848 |
|
| 1849 |
|
| 1850 |
class main_DebugMessageTestCase(main_TestCase): |
| 1851 |
""" Test cases for `main` debug messages. """ |
| 1852 |
|
| 1853 |
progname = "lorem" |
| 1854 |
dput_version = "ipsum" |
| 1855 |
|
| 1856 |
@unittest.mock.patch.object(dput.dput, 'debug', new=True) |
| 1857 |
def test_emits_debug_message_with_dput_version(self): |
| 1858 |
""" Should emit debug message showing DPut version string. """ |
| 1859 |
self.fake_options.debug = True |
| 1860 |
self.function_to_test(**self.test_args) |
| 1861 |
expected_progname = self.progname |
| 1862 |
expected_version = self.fake_distribution.version |
| 1863 |
expected_output = textwrap.dedent("""\ |
| 1864 |
D: {progname} {version} |
| 1865 |
""").format( |
| 1866 |
progname=expected_progname, |
| 1867 |
version=expected_version) |
| 1868 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 1869 |
|
| 1870 |
|
| 1871 |
class main_DefaultFunctionCallTestCase(main_TestCase): |
| 1872 |
""" Test cases for `main` defaults calling other functions. """ |
| 1873 |
|
| 1874 |
def test_calls_import_upload_functions_with_expected_args(self): |
| 1875 |
""" Should call `import_upload_functions` with expected arguments. """ |
| 1876 |
self.function_to_test(**self.test_args) |
| 1877 |
dput.dput.import_upload_functions.assert_called_with() |
| 1878 |
|
| 1879 |
|
| 1880 |
class main_ConfigFileTestCase( |
| 1881 |
testscenarios.WithScenarios, |
| 1882 |
main_TestCase): |
| 1883 |
""" Test cases for `main` specification of configuration file. """ |
| 1884 |
|
| 1885 |
scenarios = [ |
| 1886 |
('default', { |
| 1887 |
'expected_config_path': "", |
| 1888 |
}), |
| 1889 |
('config-from-command-line', { |
| 1890 |
'fake_options_mapping': { |
| 1891 |
'config_file_path': "lorem.conf", |
| 1892 |
}, |
| 1893 |
'expected_config_path': "lorem.conf", |
| 1894 |
}), |
| 1895 |
] |
| 1896 |
|
| 1897 |
def test_calls_active_config_files_with_expected_args(self): |
| 1898 |
""" Should call `active_config_files` with expected arguments. """ |
| 1899 |
expected_args = (self.expected_config_path, unittest.mock.ANY) |
| 1900 |
self.function_to_test(**self.test_args) |
| 1901 |
dput.configfile.active_config_files.assert_called_with(*expected_args) |
| 1902 |
|
| 1903 |
def test_calls_read_configs_with_expected_args(self): |
| 1904 |
""" Should call `read_configs` with expected arguments. """ |
| 1905 |
expected_config_files = self.fake_config_files |
| 1906 |
expected_args = tuple([expected_config_files]) |
| 1907 |
expected_kwargs = dict(debug=unittest.mock.ANY) |
| 1908 |
self.function_to_test(**self.test_args) |
| 1909 |
dput.configfile.read_configs.assert_called_with( |
| 1910 |
*expected_args, **expected_kwargs) |
| 1911 |
|
| 1912 |
def test_calls_sys_exit_if_configuration_error_when_opening_files(self): |
| 1913 |
""" |
| 1914 |
Should call `sys.exit` if `ConfigurationError` from opening files. |
| 1915 |
""" |
| 1916 |
set_config(self, 'all-not-exist') |
| 1917 |
self.set_test_args() |
| 1918 |
fake_error_text = "Could not open any configfile, tried ‘foo’, ‘bar’" |
| 1919 |
fake_error = dput.configfile.ConfigurationError(fake_error_text) |
| 1920 |
dput.configfile.active_config_files.side_effect = fake_error |
| 1921 |
with testtools.ExpectedException(FakeSystemExit): |
| 1922 |
self.function_to_test(**self.test_args) |
| 1923 |
expected_output = textwrap.dedent("""\ |
| 1924 |
...{error_text}... |
| 1925 |
""".format(error_text=fake_error_text)) |
| 1926 |
self.assertThat( |
| 1927 |
sys.stderr.getvalue(), |
| 1928 |
testtools.matchers.DocTestMatches( |
| 1929 |
expected_output, flags=doctest.ELLIPSIS)) |
| 1930 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 1931 |
|
| 1932 |
def test_calls_sys_exit_if_configuration_error_when_reading_files(self): |
| 1933 |
""" |
| 1934 |
Should call `sys.exit` if `ConfigurationError` from reading files. |
| 1935 |
""" |
| 1936 |
fake_error_text = "Hidden Illuminati messages in config file" |
| 1937 |
fake_error = dput.configfile.ConfigurationError(fake_error_text) |
| 1938 |
dput.configfile.read_configs.side_effect = fake_error |
| 1939 |
with testtools.ExpectedException(FakeSystemExit): |
| 1940 |
self.function_to_test(**self.test_args) |
| 1941 |
expected_output = textwrap.dedent("""\ |
| 1942 |
...{error_text}... |
| 1943 |
""".format(error_text=fake_error_text)) |
| 1944 |
self.assertThat( |
| 1945 |
sys.stderr.getvalue(), |
| 1946 |
testtools.matchers.DocTestMatches( |
| 1947 |
expected_output, flags=doctest.ELLIPSIS)) |
| 1948 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 1949 |
|
| 1950 |
|
| 1951 |
class main_PrintAndEndTestCase(main_TestCase): |
| 1952 |
""" Test cases for `main` that print and end. """ |
| 1953 |
|
| 1954 |
progname = "lorem" |
| 1955 |
dput_version = "ipsum" |
| 1956 |
dput_usage_message = "Lorem ipsum, dolor sit amet." |
| 1957 |
|
| 1958 |
def test_print_config_when_option_print(self): |
| 1959 |
""" Should call `print_config` when ‘--print’. """ |
| 1960 |
self.fake_options_mapping.update({'config_print': True}) |
| 1961 |
self.set_fake_options() |
| 1962 |
with contextlib.suppress(FakeSystemExit): |
| 1963 |
self.function_to_test(**self.test_args) |
| 1964 |
dput.configfile.print_config.assert_called_with( |
| 1965 |
self.runtime_config_parser, dput.dput.debug) |
| 1966 |
|
| 1967 |
def test_print_config_then_sys_exit_when_option_print(self): |
| 1968 |
""" Should call `print_config`, then `sys.exit`, when ‘--print’. """ |
| 1969 |
self.fake_options_mapping.update({'config_print': True}) |
| 1970 |
self.set_fake_options() |
| 1971 |
with testtools.ExpectedException(FakeSystemExit): |
| 1972 |
self.function_to_test(**self.test_args) |
| 1973 |
sys.exit.assert_called_with(EXIT_STATUS_SUCCESS) |
| 1974 |
|
| 1975 |
def test_calls_print_default_upload_method_when_option_host_list(self): |
| 1976 |
""" Should call `print_default_upload_method` when ‘--host-list’. """ |
| 1977 |
self.fake_options_mapping.update({'config_host_list': True}) |
| 1978 |
self.set_fake_options() |
| 1979 |
with contextlib.suppress(FakeSystemExit): |
| 1980 |
self.function_to_test(**self.test_args) |
| 1981 |
dput.configfile.print_default_upload_method.assert_called_with( |
| 1982 |
self.runtime_config_parser) |
| 1983 |
|
| 1984 |
def test_calls_print_host_list_when_option_host_list(self): |
| 1985 |
""" Should call `print_host_list` when option ‘--host-list’. """ |
| 1986 |
self.fake_options_mapping.update({'config_host_list': True}) |
| 1987 |
self.set_fake_options() |
| 1988 |
with contextlib.suppress(FakeSystemExit): |
| 1989 |
self.function_to_test(**self.test_args) |
| 1990 |
dput.configfile.print_host_list.assert_called_with( |
| 1991 |
self.runtime_config_parser) |
| 1992 |
|
| 1993 |
def test_calls_sys_exit_when_option_host_list(self): |
| 1994 |
""" Should call `sys.exit` when option ‘--host-list’. """ |
| 1995 |
self.fake_options_mapping.update({'config_host_list': True}) |
| 1996 |
self.set_fake_options() |
| 1997 |
with testtools.ExpectedException(FakeSystemExit): |
| 1998 |
self.function_to_test(**self.test_args) |
| 1999 |
sys.exit.assert_called_with(EXIT_STATUS_SUCCESS) |
| 2000 |
|
| 2001 |
|
| 2002 |
class main_NamedHost_TestCase( |
| 2003 |
testscenarios.WithScenarios, |
| 2004 |
main_TestCase): |
| 2005 |
""" Test cases for `main` function, named host processing. """ |
| 2006 |
|
| 2007 |
scenarios = [ |
| 2008 |
('host-from-command-line', { |
| 2009 |
'config_scenario_name': "exist-simple-host-three", |
| 2010 |
'fake_args': ["bar", "lorem.changes", "ipsum.changes"], |
| 2011 |
'expected_host': "bar", |
| 2012 |
'expected_debug_output': "D: Host bar found in config", |
| 2013 |
}), |
| 2014 |
('host-from-command-line check-only', { |
| 2015 |
'config_scenario_name': "exist-simple-host-three", |
| 2016 |
'fake_options_mapping': {'check_only': True}, |
| 2017 |
'fake_args': ["bar", "lorem.changes", "ipsum.changes"], |
| 2018 |
'expected_host': "bar", |
| 2019 |
'expected_debug_output': "D: Host bar found in config", |
| 2020 |
}), |
| 2021 |
('host-from-command-line force-upload', { |
| 2022 |
'config_scenario_name': "exist-simple-host-three", |
| 2023 |
'fake_options_mapping': {'force_upload': True}, |
| 2024 |
'fake_args': ["bar", "lorem.changes", "ipsum.changes"], |
| 2025 |
'expected_host': "bar", |
| 2026 |
'expected_debug_output': "D: Host bar found in config", |
| 2027 |
}), |
| 2028 |
('only-changes-filenames', { |
| 2029 |
'config_scenario_name': "exist-simple-host-three", |
| 2030 |
'fake_args': ["lorem.changes", "ipsum.changes"], |
| 2031 |
'expected_host': "foo", |
| 2032 |
'expected_debug_output': "D: No host named on command line.", |
| 2033 |
}), |
| 2034 |
('only-changes-filenames check-only', { |
| 2035 |
'config_scenario_name': "exist-simple-host-three", |
| 2036 |
'fake_options_mapping': { |
| 2037 |
'debug': True, |
| 2038 |
'check_only': True, |
| 2039 |
}, |
| 2040 |
'fake_args': ["lorem.changes", "ipsum.changes"], |
| 2041 |
'expected_host': "foo", |
| 2042 |
'expected_debug_output': "D: No host named on command line.", |
| 2043 |
'follow_the_goddamned_branches': True, |
| 2044 |
}), |
| 2045 |
] |
| 2046 |
|
| 2047 |
def test_emits_expected_debug_message(self): |
| 2048 |
""" Should emit expected debug message. """ |
| 2049 |
self.fake_options.debug = True |
| 2050 |
with contextlib.suppress(FakeSystemExit): |
| 2051 |
self.function_to_test(**self.test_args) |
| 2052 |
expected_output = self.expected_debug_output.format( |
| 2053 |
host=self.expected_host) |
| 2054 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 2055 |
|
| 2056 |
def test_check_upload_logfile_called_with_expected_host(self): |
| 2057 |
""" Should call `check_upload_logfile` with expected host. """ |
| 2058 |
with contextlib.suppress(FakeSystemExit): |
| 2059 |
self.function_to_test(**self.test_args) |
| 2060 |
expected_fqdn = dput.dput.get_fqdn_for_host.return_value |
| 2061 |
expected_args = ( |
| 2062 |
unittest.mock.ANY, self.expected_host, expected_fqdn, |
| 2063 |
unittest.mock.ANY, unittest.mock.ANY, |
| 2064 |
unittest.mock.ANY) |
| 2065 |
dput.dput.check_upload_logfile.assert_called_with(*expected_args) |
| 2066 |
|
| 2067 |
def test_verify_files_called_with_expected_host(self): |
| 2068 |
""" Should call `verify_files` with expected host. """ |
| 2069 |
with contextlib.suppress(FakeSystemExit): |
| 2070 |
self.function_to_test(**self.test_args) |
| 2071 |
expected_args = ( |
| 2072 |
unittest.mock.ANY, unittest.mock.ANY, self.expected_host, |
| 2073 |
unittest.mock.ANY, unittest.mock.ANY, |
| 2074 |
unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY) |
| 2075 |
upload_changes.verify_files.assert_called_with(*expected_args) |
| 2076 |
|
| 2077 |
|
| 2078 |
class main_NamedHostNotInConfigTestCase( |
| 2079 |
testscenarios.WithScenarios, |
| 2080 |
main_TestCase): |
| 2081 |
""" Test cases for `main` function, named host not in config. """ |
| 2082 |
|
| 2083 |
scenarios = [ |
| 2084 |
('host-from-command-line', { |
| 2085 |
'config_scenario_name': "exist-simple-host-three", |
| 2086 |
'fake_args': ["b0gUs", "lorem.changes", "ipsum.changes"], |
| 2087 |
'expected_stderr_output': "No host b0gUs found in config.", |
| 2088 |
'expected_exception': FakeSystemExit, |
| 2089 |
'expected_exit_status': EXIT_STATUS_FAILURE, |
| 2090 |
}), |
| 2091 |
('host-from-command-line check-only', { |
| 2092 |
'config_scenario_name': "exist-simple-host-three", |
| 2093 |
'fake_options_mapping': {'check_only': True}, |
| 2094 |
'fake_args': ["b0gUs", "lorem.changes", "ipsum.changes"], |
| 2095 |
'expected_stderr_output': "No host b0gUs found in config.", |
| 2096 |
}), |
| 2097 |
] |
| 2098 |
|
| 2099 |
def test_emits_expected_debug_message(self): |
| 2100 |
""" Should emit expected debug message. """ |
| 2101 |
self.fake_options.debug = True |
| 2102 |
with contextlib.suppress(FakeSystemExit): |
| 2103 |
self.function_to_test(**self.test_args) |
| 2104 |
if hasattr(self, 'expected_stdout_output'): |
| 2105 |
self.assertIn(self.expected_stdout_output, sys.stdout.getvalue()) |
| 2106 |
if hasattr(self, 'expected_stderr_output'): |
| 2107 |
self.assertIn(self.expected_stderr_output, sys.stderr.getvalue()) |
| 2108 |
|
| 2109 |
def test_calls_sys_exit_when_host_not_in_config(self): |
| 2110 |
""" Should call `sys.exit` when named host not in config. """ |
| 2111 |
if not hasattr(self, 'expected_exception'): |
| 2112 |
self.skipTest("No expected exception for this scenario") |
| 2113 |
with testtools.ExpectedException(self.expected_exception): |
| 2114 |
self.function_to_test(**self.test_args) |
| 2115 |
sys.exit.assert_called_with(self.expected_exit_status) |
| 2116 |
|
| 2117 |
|
| 2118 |
class main_check_upload_logfile_CallTestCase( |
| 2119 |
testscenarios.WithScenarios, |
| 2120 |
main_TestCase): |
| 2121 |
""" Test cases for `main` function calling `check_upload_logfile`. """ |
| 2122 |
|
| 2123 |
scenarios = [ |
| 2124 |
('default', {}), |
| 2125 |
('command-line-option-check-only', { |
| 2126 |
'fake_options_mapping': {'check_only': True}, |
| 2127 |
'expected_check_only': True, |
| 2128 |
}), |
| 2129 |
('command-line-option-force', { |
| 2130 |
'fake_options_mapping': {'force_upload': True}, |
| 2131 |
'expected_force_upload': True, |
| 2132 |
}), |
| 2133 |
('command-line-options-two', { |
| 2134 |
'fake_options_mapping': { |
| 2135 |
'force_upload': True, |
| 2136 |
'check_only': True, |
| 2137 |
}, |
| 2138 |
'expected_check_only': True, |
| 2139 |
'expected_force_upload': True, |
| 2140 |
}), |
| 2141 |
] |
| 2142 |
|
| 2143 |
def test_calls_check_upload_logfile_with_expected_args(self): |
| 2144 |
""" Should invoke `check_upload_logfile` with expected args. """ |
| 2145 |
self.function_to_test(**self.test_args) |
| 2146 |
expected_fqdn = dput.dput.get_fqdn_for_host.return_value |
| 2147 |
expected_args = ( |
| 2148 |
self.changes_file_double.path, |
| 2149 |
self.test_host, expected_fqdn, |
| 2150 |
getattr(self, 'expected_check_only', False), |
| 2151 |
getattr(self, 'expected_force_upload', False), |
| 2152 |
unittest.mock.ANY) |
| 2153 |
dput.dput.check_upload_logfile.assert_called_with(*expected_args) |
| 2154 |
|
| 2155 |
|
| 2156 |
class main_verify_files_CallTestCase( |
| 2157 |
testscenarios.WithScenarios, |
| 2158 |
main_TestCase): |
| 2159 |
""" Test cases for `main` function calling `verify_files`. """ |
| 2160 |
|
| 2161 |
scenarios = [ |
| 2162 |
('default', {}), |
| 2163 |
('command-line-option-check-only', { |
| 2164 |
'fake_options_mapping': {'check_only': True}, |
| 2165 |
'expected_check_only': True, |
| 2166 |
}), |
| 2167 |
('command-line-option-check-version', { |
| 2168 |
'fake_options_mapping': {'check_version': True}, |
| 2169 |
'expected_check_version': True, |
| 2170 |
}), |
| 2171 |
('command-line-option-unchecked', { |
| 2172 |
'fake_options_mapping': {'allow_unsigned_uploads': True}, |
| 2173 |
'expected_allow_unsigned_uploads': True, |
| 2174 |
}), |
| 2175 |
('command-line-options-three', { |
| 2176 |
'fake_options_mapping': { |
| 2177 |
'check_only': True, |
| 2178 |
'check_version': True, |
| 2179 |
'allow_unsigned_uploads': True, |
| 2180 |
}, |
| 2181 |
'expected_check_only': True, |
| 2182 |
'expected_check_version': True, |
| 2183 |
'expected_allow_unsigned_uploads': True, |
| 2184 |
}), |
| 2185 |
] |
| 2186 |
|
| 2187 |
def test_calls_verify_files_with_expected_args(self): |
| 2188 |
""" Should invoke `verify_files` with expected args. """ |
| 2189 |
self.function_to_test(**self.test_args) |
| 2190 |
expected_args = ( |
| 2191 |
os.path.dirname(self.changes_file_double.path), |
| 2192 |
os.path.basename(self.changes_file_double.path), |
| 2193 |
self.test_host, |
| 2194 |
self.runtime_config_parser, |
| 2195 |
getattr(self, 'expected_check_only', False), |
| 2196 |
getattr(self, 'expected_check_version', False), |
| 2197 |
getattr(self, 'expected_allow_unsigned_uploads', False), |
| 2198 |
unittest.mock.ANY) |
| 2199 |
upload_changes.verify_files.assert_called_with(*expected_args) |
| 2200 |
|
| 2201 |
|
| 2202 |
class main_run_lintian_test_CallTestCase( |
| 2203 |
testscenarios.WithScenarios, |
| 2204 |
main_TestCase): |
| 2205 |
""" Test cases for `main` function calling `run_lintian_test`. """ |
| 2206 |
|
| 2207 |
scenarios = [ |
| 2208 |
('option-from-command-line', { |
| 2209 |
'fake_options_mapping': {'run_lintian': True}, |
| 2210 |
}), |
| 2211 |
('option-from-config', { |
| 2212 |
'config_run_lintian': True, |
| 2213 |
}), |
| 2214 |
] |
| 2215 |
|
| 2216 |
def test_calls_run_lintian_test_with_expected_args(self): |
| 2217 |
""" Should invoke `run_lintian_test` with expected args. """ |
| 2218 |
self.function_to_test(**self.test_args) |
| 2219 |
expected_args = (self.changes_file_double.path,) |
| 2220 |
dput.dput.run_lintian_test.assert_called_with(*expected_args) |
| 2221 |
|
| 2222 |
|
| 2223 |
class main_run_lintian_test_NoCallTestCase( |
| 2224 |
testscenarios.WithScenarios, |
| 2225 |
main_TestCase): |
| 2226 |
""" Test cases for `main` function omitting `run_lintian_test`. """ |
| 2227 |
|
| 2228 |
def test_omits_run_lintian_test(self): |
| 2229 |
""" Should omit `run_lintian_test` when not requested. """ |
| 2230 |
self.function_to_test(**self.test_args) |
| 2231 |
self.assertFalse(dput.dput.run_lintian_test.called) |
| 2232 |
|
| 2233 |
|
| 2234 |
class main_UploadHookCommandsTestCase(main_TestCase): |
| 2235 |
""" Test cases for `main` function upload hook commands. """ |
| 2236 |
|
| 2237 |
def test_calls_execute_command_with_pre_upload_command(self): |
| 2238 |
""" Should invoke `execute_command` when `pre_upload_command`. """ |
| 2239 |
test_command = self.getUniqueString() |
| 2240 |
self.runtime_config_parser.set( |
| 2241 |
self.test_host, 'pre_upload_command', test_command) |
| 2242 |
self.function_to_test(**self.test_args) |
| 2243 |
expected_command = test_command |
| 2244 |
dput.dput.execute_command.assert_called_with( |
| 2245 |
expected_command, "pre", unittest.mock.ANY) |
| 2246 |
|
| 2247 |
def test_calls_execute_command_with_post_upload_command(self): |
| 2248 |
""" Should invoke `execute_command` when `post_upload_command`. """ |
| 2249 |
test_command = self.getUniqueString() |
| 2250 |
self.runtime_config_parser.set( |
| 2251 |
self.test_host, 'post_upload_command', test_command) |
| 2252 |
self.function_to_test(**self.test_args) |
| 2253 |
expected_command = test_command |
| 2254 |
dput.dput.execute_command.assert_called_with( |
| 2255 |
expected_command, "post", unittest.mock.ANY) |
| 2256 |
|
| 2257 |
|
| 2258 |
class main_UploadFilesTestCase( |
| 2259 |
testscenarios.WithScenarios, |
| 2260 |
main_TestCase): |
| 2261 |
""" Test cases for `main` function, call `upload_files`. """ |
| 2262 |
|
| 2263 |
debug_scenarios = [ |
| 2264 |
('debug-default', { |
| 2265 |
'expected_kwarg_debug': False, |
| 2266 |
}), |
| 2267 |
('debug-true', { |
| 2268 |
'fake_option_debug': True, |
| 2269 |
'expected_kwarg_debug': True, |
| 2270 |
}), |
| 2271 |
] |
| 2272 |
|
| 2273 |
simulate_scenarios = [ |
| 2274 |
('simulate-default', { |
| 2275 |
'expected_kwarg_simulate': False, |
| 2276 |
}), |
| 2277 |
('simulate-true', { |
| 2278 |
'fake_option_simulate': True, |
| 2279 |
'expected_kwarg_simulate': True, |
| 2280 |
}), |
| 2281 |
] |
| 2282 |
|
| 2283 |
passive_scenarios = [ |
| 2284 |
('passive-default', { |
| 2285 |
'expected_kwarg_commandline_passive': None, |
| 2286 |
}), |
| 2287 |
('passive-true', { |
| 2288 |
'fake_option_commandline_passive': True, |
| 2289 |
'expected_kwarg_commandline_passive': True, |
| 2290 |
}), |
| 2291 |
] |
| 2292 |
|
| 2293 |
delay_scenarios = [ |
| 2294 |
('delay-unspecified', { |
| 2295 |
'determine_delay_days_return_value': None, |
| 2296 |
'expected_kwarg_delay_days': None, |
| 2297 |
}), |
| 2298 |
('delay-specified-none', { |
| 2299 |
'determine_delay_days_return_value': None, |
| 2300 |
'expected_kwarg_delay_days': None, |
| 2301 |
}), |
| 2302 |
('delay-specified-integer', { |
| 2303 |
'determine_delay_days_return_value': 555, |
| 2304 |
'expected_kwarg_delay_days': 555, |
| 2305 |
}), |
| 2306 |
] |
| 2307 |
|
| 2308 |
scenarios = testscenarios.multiply_scenarios( |
| 2309 |
debug_scenarios, simulate_scenarios, |
| 2310 |
passive_scenarios, delay_scenarios) |
| 2311 |
|
| 2312 |
def setUp(self): |
| 2313 |
""" Set up fixtures for this test case. """ |
| 2314 |
super().setUp() |
| 2315 |
|
| 2316 |
self.set_expected_kwargs() |
| 2317 |
|
| 2318 |
def set_fake_options(self, options_mapping=None): |
| 2319 |
""" Set the `fake_option_mapping` based on scenario attributes. """ |
| 2320 |
for option_name in [ |
| 2321 |
'debug', |
| 2322 |
'simulate', |
| 2323 |
'commandline_passive', |
| 2324 |
]: |
| 2325 |
scenario_attribute_name = "fake_option_{}".format(option_name) |
| 2326 |
if hasattr(self, scenario_attribute_name): |
| 2327 |
fake_option_value = getattr(self, scenario_attribute_name) |
| 2328 |
options_mapping[option_name] = fake_option_value |
| 2329 |
super().set_fake_options(options_mapping) |
| 2330 |
|
| 2331 |
def set_expected_kwargs(self): |
| 2332 |
""" Set the `expected_kwargs` based on scenario attributes. """ |
| 2333 |
self.expected_kwargs = {} |
| 2334 |
for arg_name in [ |
| 2335 |
'debug', |
| 2336 |
'simulate', |
| 2337 |
'commandline_passive', |
| 2338 |
'delay_days', |
| 2339 |
]: |
| 2340 |
scenario_attribute_name = "expected_kwarg_{}".format(arg_name) |
| 2341 |
if hasattr(self, scenario_attribute_name): |
| 2342 |
expected_kwarg_value = getattr(self, scenario_attribute_name) |
| 2343 |
self.expected_kwargs[arg_name] = expected_kwarg_value |
| 2344 |
|
| 2345 |
def test_calls_upload_files_with_expected_args(self): |
| 2346 |
""" Should call `upload_files` function with expected args. """ |
| 2347 |
self.expected_args = ( |
| 2348 |
self.upload_methods, |
| 2349 |
self.runtime_config_parser, |
| 2350 |
self.test_host, |
| 2351 |
self.files_to_upload, |
| 2352 |
) |
| 2353 |
self.function_to_test(**self.test_args) |
| 2354 |
dput.dput.upload_files.assert_called_with( |
| 2355 |
*self.expected_args, **self.expected_kwargs) |
| 2356 |
|
| 2357 |
|
| 2358 |
class main_UploadLogTestCase( |
| 2359 |
testscenarios.WithScenarios, |
| 2360 |
main_TestCase): |
| 2361 |
""" Test cases for `main` function, creating upload log file. """ |
| 2362 |
|
| 2363 |
scenarios = [ |
| 2364 |
('default', { |
| 2365 |
'expect_call': True, |
| 2366 |
}), |
| 2367 |
('command-line-disable-option', { |
| 2368 |
'fake_options_mapping': {'upload_log': False}, |
| 2369 |
'expect_call': False, |
| 2370 |
}), |
| 2371 |
('command-line-simulate-option', { |
| 2372 |
'fake_options_mapping': {'simulate': True}, |
| 2373 |
'expect_call': False, |
| 2374 |
}), |
| 2375 |
] |
| 2376 |
|
| 2377 |
def test_calls_create_upload_file_if_specified(self): |
| 2378 |
""" Should call `create_upload_file` if specified. """ |
| 2379 |
self.function_to_test(**self.test_args) |
| 2380 |
if self.expect_call: |
| 2381 |
expected_args = ( |
| 2382 |
os.path.basename(self.changes_file_double.path), |
| 2383 |
self.test_host, dput.dput.get_fqdn_for_host.return_value, |
| 2384 |
os.path.dirname(self.changes_file_double.path), |
| 2385 |
self.files_to_upload, |
| 2386 |
unittest.mock.ANY) |
| 2387 |
dput.dput.create_upload_file.assert_called_with(*expected_args) |
| 2388 |
else: |
| 2389 |
self.assertFalse(dput.dput.create_upload_file.called) |
| 2390 |
|
| 2391 |
|
| 2392 |
class main_DinstallTestCase( |
| 2393 |
testscenarios.WithScenarios, |
| 2394 |
main_TestCase): |
| 2395 |
""" Test cases for `main` function, invoking ‘dinstall’ command. """ |
| 2396 |
|
| 2397 |
scenarios = [ |
| 2398 |
('option-from-command-line', { |
| 2399 |
'fake_options_mapping': {'run_dinstall': True}, |
| 2400 |
'expected_output': "D: run_dinstall: True", |
| 2401 |
}), |
| 2402 |
('option-from-config', { |
| 2403 |
'config_run_dinstall': True, |
| 2404 |
'expected_output': "D: Host Config: True", |
| 2405 |
}), |
| 2406 |
] |
| 2407 |
|
| 2408 |
def test_emits_debug_message_for_options(self): |
| 2409 |
""" Should emit debug message for options. """ |
| 2410 |
self.fake_options.debug = True |
| 2411 |
self.function_to_test(**self.test_args) |
| 2412 |
self.assertIn(self.expected_output, sys.stdout.getvalue()) |
| 2413 |
|
| 2414 |
def test_calls_dinstall_caller_with_expected_args(self): |
| 2415 |
""" Should call `dinstall_caller` with expected args. """ |
| 2416 |
expected_args = ( |
| 2417 |
os.path.basename(self.changes_file_double.path), |
| 2418 |
self.test_host, dput.dput.get_fqdn_for_host.return_value, |
| 2419 |
unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY) |
| 2420 |
self.function_to_test(**self.test_args) |
| 2421 |
dput.dput.dinstall_caller.assert_called_with(*expected_args) |
| 2422 |
|
| 2423 |
def test_emits_expected_information_message(self): |
| 2424 |
""" Should emit expected information message. """ |
| 2425 |
self.fake_options.simulate = True |
| 2426 |
self.function_to_test(**self.test_args) |
| 2427 |
expected_stderr_output = ( |
| 2428 |
"Not running ‘dinstall’: ‘simulate’ option specified.") |
| 2429 |
self.assertIn(expected_stderr_output, sys.stderr.getvalue()) |
| 2430 |
|
| 2431 |
|
| 2432 |
# Copyright © 2015–2026 Ben Finney <[email protected]> |
| 2433 |
# |
| 2434 |
# This is free software: you may copy, modify, and/or distribute this work |
| 2435 |
# under the terms of the GNU General Public License as published by the |
| 2436 |
# Free Software Foundation; version 3 of that license or any later version. |
| 2437 |
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. |
| 2438 |
|
| 2439 |
|
| 2440 |
# Local variables: |
| 2441 |
# coding: utf-8 |
| 2442 |
# mode: python |
| 2443 |
# End: |
| 2444 |
# vim: fileencoding=utf-8 filetype=python : |