| 1 |
# test/test_dput.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. """ |
| 9 |
|
| 10 |
import builtins |
| 11 |
import contextlib |
| 12 |
import doctest |
| 13 |
import io |
| 14 |
import os |
| 15 |
import os.path |
| 16 |
import signal |
| 17 |
import subprocess |
| 18 |
import sys |
| 19 |
import tempfile |
| 20 |
import textwrap |
| 21 |
import unittest.mock |
| 22 |
|
| 23 |
import testscenarios |
| 24 |
import testtools |
| 25 |
import testtools.matchers |
| 26 |
|
| 27 |
import dput.dput |
| 28 |
from dput.helper import dputhelper |
| 29 |
|
| 30 |
from .control_file.test_upload_changes import ( |
| 31 |
make_changes_file_scenarios, |
| 32 |
set_changes_file_scenario, |
| 33 |
set_fake_upload_file_paths, |
| 34 |
setup_changes_file_fixtures, |
| 35 |
) |
| 36 |
from .helper import ( |
| 37 |
ARG_ANY, |
| 38 |
EXIT_STATUS_FAILURE, |
| 39 |
FakeSystemExit, |
| 40 |
FileDouble, |
| 41 |
SubprocessDouble, |
| 42 |
make_unique_slug, |
| 43 |
patch_os_access, |
| 44 |
patch_os_path_exists, |
| 45 |
patch_signal_signal, |
| 46 |
patch_subprocess_call, |
| 47 |
patch_subprocess_check_call, |
| 48 |
patch_system_interfaces, |
| 49 |
setup_fake_file_fixtures, |
| 50 |
setup_file_double_behaviour, |
| 51 |
) |
| 52 |
from .test_configfile import ( |
| 53 |
patch_runtime_config_options, |
| 54 |
set_config, |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
class create_upload_file_TestCase( |
| 59 |
testscenarios.WithScenarios, |
| 60 |
testtools.TestCase): |
| 61 |
""" Test cases for `create_upload_file` function. """ |
| 62 |
|
| 63 |
scenarios = [ |
| 64 |
('simple', {}), |
| 65 |
('local', { |
| 66 |
'test_host': "foo", |
| 67 |
'test_fqdn': "localhost", |
| 68 |
}), |
| 69 |
('log file exists', { |
| 70 |
'fake_file_scenario_name': 'exist-empty', |
| 71 |
'file_access_scenario_name': 'okay', |
| 72 |
'expected_open_mode': 'a', |
| 73 |
}), |
| 74 |
] |
| 75 |
|
| 76 |
def setUp(self): |
| 77 |
""" Set up test fixtures. """ |
| 78 |
super().setUp() |
| 79 |
patch_system_interfaces(self) |
| 80 |
|
| 81 |
if not hasattr(self, 'test_host'): |
| 82 |
self.test_host = make_unique_slug(self) |
| 83 |
self.test_fqdn = make_unique_slug(self) |
| 84 |
|
| 85 |
setup_changes_file_fixtures(self) |
| 86 |
set_changes_file_scenario( |
| 87 |
self, |
| 88 |
getattr(self, 'changes_file_scenario_name', 'exist-minimal')) |
| 89 |
|
| 90 |
set_fake_upload_file_paths(self) |
| 91 |
self.set_test_args() |
| 92 |
|
| 93 |
setup_fake_file_fixtures(self) |
| 94 |
self.set_fake_file_scenario( |
| 95 |
getattr(self, 'fake_file_scenario_name', 'not-found')) |
| 96 |
if not hasattr(self, 'expected_open_mode'): |
| 97 |
self.expected_open_mode = 'w' |
| 98 |
|
| 99 |
self.set_upload_log_file_double() |
| 100 |
self.set_upload_log_file_params() |
| 101 |
|
| 102 |
patch_os_access(self) |
| 103 |
|
| 104 |
def set_test_args(self): |
| 105 |
""" Set the arguments for the test call to the function. """ |
| 106 |
changes_file_name = os.path.basename(self.changes_file_double.path) |
| 107 |
self.package_file_name = "{name_stem}.source".format( |
| 108 |
name_stem=os.path.splitext(changes_file_name)[0]) |
| 109 |
self.test_args = dict( |
| 110 |
changes_file_name=self.package_file_name, |
| 111 |
host=self.test_host, |
| 112 |
fqdn=self.test_fqdn, |
| 113 |
changes_file_directory=os.path.dirname( |
| 114 |
self.changes_file_double.path), |
| 115 |
files_to_upload=self.fake_upload_file_paths, |
| 116 |
debug=False, |
| 117 |
) |
| 118 |
|
| 119 |
def set_upload_log_file_double(self): |
| 120 |
""" Set the fake upload log file. """ |
| 121 |
changes_file_name = os.path.basename(self.changes_file_double.path) |
| 122 |
changes_file_name_base = os.path.splitext(changes_file_name)[0] |
| 123 |
upload_log_file_name = "{base}.{host}.upload".format( |
| 124 |
base=changes_file_name_base, host=self.test_host) |
| 125 |
self.file_double.path = os.path.join( |
| 126 |
os.path.dirname(self.changes_file_double.path), |
| 127 |
upload_log_file_name) |
| 128 |
self.upload_log_file_double = self.file_double |
| 129 |
self.upload_log_file_double.set_os_access_scenario( |
| 130 |
getattr( |
| 131 |
self, 'file_access_scenario_name', 'not_exist')) |
| 132 |
self.upload_log_file_double.register_for_testcase(self) |
| 133 |
|
| 134 |
def set_upload_log_file_params(self): |
| 135 |
""" Set the parameters for the upload log file. """ |
| 136 |
self.upload_log_file_double.set_open_scenario('nonexist') |
| 137 |
|
| 138 |
def set_fake_file_scenario(self, name): |
| 139 |
""" Set the output file scenario for this test case. """ |
| 140 |
self.fake_file_scenario = self.fake_file_scenarios[name] |
| 141 |
self.file_double = self.fake_file_scenario['file_double'] |
| 142 |
|
| 143 |
def test_emits_upload_log_debug_message(self): |
| 144 |
""" Should emit debug message for creation of upload log file. """ |
| 145 |
self.test_args['debug'] = True |
| 146 |
dput.dput.create_upload_file(**self.test_args) |
| 147 |
expected_output = textwrap.dedent("""\ |
| 148 |
D: Writing logfile: {path} |
| 149 |
""").format(path=self.upload_log_file_double.path) |
| 150 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 151 |
|
| 152 |
def test_opens_log_file_with_expected_mode_and_encoding(self): |
| 153 |
""" Should open log file with expected mode and text encoding. """ |
| 154 |
dput.dput.create_upload_file(**self.test_args) |
| 155 |
builtins.open.assert_called_with( |
| 156 |
self.upload_log_file_double.path, |
| 157 |
mode=self.expected_open_mode, |
| 158 |
encoding='UTF-8') |
| 159 |
|
| 160 |
def test_writes_expected_message_for_each_file(self): |
| 161 |
""" Should write log message for each upload file path. """ |
| 162 |
with open( |
| 163 |
self.upload_log_file_double.path, mode='w', encoding='UTF-8', |
| 164 |
) as fake_file: |
| 165 |
with unittest.mock.patch.object(fake_file, "close", autospec=True): |
| 166 |
dput.dput.create_upload_file(**self.test_args) |
| 167 |
fake_file.flush() |
| 168 |
output_text = str(fake_file.buffer.getvalue(), encoding='UTF-8') |
| 169 |
expected_host = self.test_host |
| 170 |
expected_fqdn = self.test_fqdn |
| 171 |
for file_path in self.fake_upload_file_paths: |
| 172 |
expected_message = ( |
| 173 |
"Successfully uploaded {filename}" |
| 174 |
" to {fqdn} for {host}.\n").format( |
| 175 |
filename=os.path.basename(file_path), |
| 176 |
fqdn=expected_fqdn, host=expected_host) |
| 177 |
self.expectThat( |
| 178 |
output_text, |
| 179 |
testtools.matchers.Contains(expected_message)) |
| 180 |
|
| 181 |
def test_calls_sys_exit_if_write_denied(self): |
| 182 |
""" Should call `sys.exit` if write permission denied. """ |
| 183 |
self.upload_log_file_double.set_os_access_scenario('read_only') |
| 184 |
self.upload_log_file_double.set_open_scenario('write_denied') |
| 185 |
with testtools.ExpectedException(FakeSystemExit): |
| 186 |
dput.dput.create_upload_file(**self.test_args) |
| 187 |
expected_output = textwrap.dedent("""\ |
| 188 |
Could not write {path} |
| 189 |
""").format(path=self.upload_log_file_double.path) |
| 190 |
self.assertIn(expected_output, sys.stderr.getvalue()) |
| 191 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 192 |
|
| 193 |
|
| 194 |
class run_lintian_test_TestCase(testtools.TestCase): |
| 195 |
""" Test cases for `run_lintian_test` function. """ |
| 196 |
|
| 197 |
scenarios = [ |
| 198 |
('simple', {}), |
| 199 |
] |
| 200 |
|
| 201 |
def setUp(self): |
| 202 |
""" Set up test fixtures. """ |
| 203 |
super().setUp() |
| 204 |
patch_system_interfaces(self) |
| 205 |
|
| 206 |
patch_signal_signal(self) |
| 207 |
patch_os_access(self) |
| 208 |
|
| 209 |
setup_changes_file_fixtures(self) |
| 210 |
set_changes_file_scenario( |
| 211 |
self, |
| 212 |
getattr(self, 'changes_file_scenario_name', 'exist-minimal')) |
| 213 |
|
| 214 |
self.set_test_args() |
| 215 |
|
| 216 |
self.patch_lintian_program_file() |
| 217 |
self.lintian_program_file_double.set_os_access_scenario('okay') |
| 218 |
|
| 219 |
patch_subprocess_check_call(self) |
| 220 |
self.set_lintian_subprocess_double() |
| 221 |
|
| 222 |
def set_test_args(self): |
| 223 |
""" Set the arguments for the test call to the function. """ |
| 224 |
self.test_args = dict( |
| 225 |
changes_file=self.changes_file_double.path, |
| 226 |
) |
| 227 |
|
| 228 |
def patch_lintian_program_file(self): |
| 229 |
""" Patch the Lintian program file for this test case. """ |
| 230 |
file_path = "/usr/bin/lintian" |
| 231 |
file_double = FileDouble(file_path) |
| 232 |
file_double.register_for_testcase(self) |
| 233 |
self.lintian_program_file_double = file_double |
| 234 |
|
| 235 |
def set_lintian_subprocess_double(self): |
| 236 |
""" Set the test double for the Lintian subprocess. """ |
| 237 |
argv = [ |
| 238 |
os.path.basename(self.lintian_program_file_double.path), |
| 239 |
"-i", ARG_ANY] |
| 240 |
double = SubprocessDouble( |
| 241 |
self.lintian_program_file_double.path, argv=argv) |
| 242 |
double.register_for_testcase(self) |
| 243 |
double.set_subprocess_check_call_scenario('not_found') |
| 244 |
self.lintian_subprocess_double = double |
| 245 |
|
| 246 |
def test_calls_sys_exit_if_read_denied(self): |
| 247 |
""" Should call `sys.exit` if read permission denied. """ |
| 248 |
self.changes_file_double.set_os_access_scenario('denied') |
| 249 |
self.changes_file_double.set_open_scenario('read_denied') |
| 250 |
with testtools.ExpectedException(FakeSystemExit): |
| 251 |
dput.dput.run_lintian_test(**self.test_args) |
| 252 |
expected_output = textwrap.dedent("""\ |
| 253 |
Can't read {path} |
| 254 |
""").format(path=self.changes_file_double.path) |
| 255 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 256 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 257 |
|
| 258 |
def test_emits_message_when_lintian_access_denied(self): |
| 259 |
""" Should emit an explanatory message when Lintian access denied. """ |
| 260 |
self.lintian_program_file_double.set_os_access_scenario('denied') |
| 261 |
dput.dput.run_lintian_test(**self.test_args) |
| 262 |
expected_output = textwrap.dedent("""\ |
| 263 |
lintian is not installed, skipping package test. |
| 264 |
""") |
| 265 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 266 |
|
| 267 |
def test_omits_check_call_when_lintian_access_denied(self): |
| 268 |
""" Should omit `subprocess.check_call` when Lintian access denied. """ |
| 269 |
self.lintian_program_file_double.set_os_access_scenario('denied') |
| 270 |
dput.dput.run_lintian_test(**self.test_args) |
| 271 |
self.assertFalse(subprocess.check_call.called) |
| 272 |
|
| 273 |
def test_sets_default_signal_handler_for_pipe_signal(self): |
| 274 |
""" Should set default signal handler for PIPE signal. """ |
| 275 |
with testtools.ExpectedException(FakeSystemExit): |
| 276 |
dput.dput.run_lintian_test(**self.test_args) |
| 277 |
signal.signal.assert_called_with(signal.SIGPIPE, signal.SIG_DFL) |
| 278 |
|
| 279 |
def test_calls_check_call_with_expected_args(self): |
| 280 |
""" Should call `subprocess.check_call` with expected arguments. """ |
| 281 |
lintian_command_argv = [ |
| 282 |
os.path.basename(self.lintian_program_file_double.path), |
| 283 |
"-i", self.changes_file_double.path] |
| 284 |
with testtools.ExpectedException(FakeSystemExit): |
| 285 |
dput.dput.run_lintian_test(**self.test_args) |
| 286 |
subprocess.check_call.assert_called_with(lintian_command_argv) |
| 287 |
|
| 288 |
def test_resets_signal_handler_for_pipe_signal_when_lintian_success(self): |
| 289 |
""" Should reset signal handler for PIPE when Lintian succeeds. """ |
| 290 |
fake_orig_pipe_signal = self.getUniqueInteger() |
| 291 |
signal.signal.return_value = fake_orig_pipe_signal |
| 292 |
subprocess_double = self.lintian_subprocess_double |
| 293 |
subprocess_double.set_subprocess_check_call_scenario('success') |
| 294 |
dput.dput.run_lintian_test(**self.test_args) |
| 295 |
expected_calls = [ |
| 296 |
unittest.mock.call(signal.SIGPIPE, signal.SIG_DFL), |
| 297 |
unittest.mock.call(signal.SIGPIPE, fake_orig_pipe_signal), |
| 298 |
] |
| 299 |
signal.signal.assert_has_calls(expected_calls) |
| 300 |
|
| 301 |
def test_calls_sys_exit_when_lintian_failure(self): |
| 302 |
""" Should call `sys.exit` when Lintian reports failure. """ |
| 303 |
with testtools.ExpectedException(FakeSystemExit): |
| 304 |
dput.dput.run_lintian_test(**self.test_args) |
| 305 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 306 |
|
| 307 |
def test_emits_message_when_lintian_failure(self): |
| 308 |
""" Should emit explanatory message when Lintian reports failure. """ |
| 309 |
with testtools.ExpectedException(FakeSystemExit): |
| 310 |
dput.dput.run_lintian_test(**self.test_args) |
| 311 |
expected_output = textwrap.dedent("""\ |
| 312 |
... |
| 313 |
Lintian says this package is not compliant with ... |
| 314 |
... |
| 315 |
""") |
| 316 |
self.assertThat( |
| 317 |
sys.stdout.getvalue(), |
| 318 |
testtools.matchers.DocTestMatches( |
| 319 |
expected_output, doctest.ELLIPSIS)) |
| 320 |
|
| 321 |
|
| 322 |
class dinstall_caller_TestCase( |
| 323 |
testtools.TestCase): |
| 324 |
""" Test cases for `dinstall_caller` function. """ |
| 325 |
|
| 326 |
def setUp(self): |
| 327 |
""" Set up test fixtures. """ |
| 328 |
super().setUp() |
| 329 |
patch_system_interfaces(self) |
| 330 |
|
| 331 |
patch_subprocess_check_call(self) |
| 332 |
|
| 333 |
self.set_test_args() |
| 334 |
|
| 335 |
self.patch_ssh_program_file() |
| 336 |
self.set_subprocess_double() |
| 337 |
self.set_expected_command_argv() |
| 338 |
|
| 339 |
def patch_ssh_program_file(self): |
| 340 |
""" Patch the SSH program file for this test case. """ |
| 341 |
file_path = "/usr/bin/ssh" |
| 342 |
file_double = FileDouble(file_path) |
| 343 |
file_double.register_for_testcase(self) |
| 344 |
self.ssh_program_file_double = file_double |
| 345 |
|
| 346 |
def set_subprocess_double(self): |
| 347 |
""" Set the test double for the subprocess. """ |
| 348 |
argv = [ |
| 349 |
os.path.basename(self.ssh_program_file_double.path), |
| 350 |
ARG_ANY, |
| 351 |
"cd", ARG_ANY, |
| 352 |
";", |
| 353 |
"dinstall", "-n", ARG_ANY] |
| 354 |
double = SubprocessDouble(self.ssh_program_file_double.path, argv=argv) |
| 355 |
double.register_for_testcase(self) |
| 356 |
double.set_subprocess_check_call_scenario('success') |
| 357 |
self.ssh_subprocess_double = double |
| 358 |
|
| 359 |
def set_test_args(self): |
| 360 |
""" Set the arguments for the test call to the function. """ |
| 361 |
self.test_args = dict( |
| 362 |
filename=tempfile.mktemp(), |
| 363 |
host=make_unique_slug(self), |
| 364 |
fqdn=make_unique_slug(self), |
| 365 |
login=make_unique_slug(self), |
| 366 |
incoming=tempfile.mktemp(), |
| 367 |
debug=False, |
| 368 |
) |
| 369 |
|
| 370 |
def set_expected_command_argv(self): |
| 371 |
""" Set the expected argv for the command. """ |
| 372 |
expected_fqdn = self.test_args['fqdn'] |
| 373 |
self.expected_host_spec = "{user}@{host}".format( |
| 374 |
user=self.test_args['login'], host=expected_fqdn) |
| 375 |
self.expected_command_argv = [ |
| 376 |
os.path.basename(self.ssh_program_file_double.path), |
| 377 |
self.expected_host_spec, |
| 378 |
"cd", self.test_args['incoming'], ";", |
| 379 |
"dinstall", "-n", self.test_args['filename'], |
| 380 |
] |
| 381 |
|
| 382 |
def test_calls_check_call_with_expected_args(self): |
| 383 |
""" Should call `subprocess.check_call` with expected arguments. """ |
| 384 |
dput.dput.dinstall_caller(**self.test_args) |
| 385 |
subprocess.check_call.assert_called_with(self.expected_command_argv) |
| 386 |
|
| 387 |
def test_emits_remote_command_debug_message(self): |
| 388 |
""" Should emit a debug message for the remote command. """ |
| 389 |
self.test_args['debug'] = True |
| 390 |
dput.dput.dinstall_caller(**self.test_args) |
| 391 |
expected_output = textwrap.dedent("""\ |
| 392 |
D: Logging into {user}@{host}:{incoming_path} |
| 393 |
D: dinstall -n {file_path} |
| 394 |
""").format( |
| 395 |
user=self.test_args['login'], host=self.test_args['host'], |
| 396 |
incoming_path=self.test_args['incoming'], |
| 397 |
file_path=self.test_args['filename']) |
| 398 |
self.assertThat( |
| 399 |
sys.stdout.getvalue(), |
| 400 |
testtools.matchers.DocTestMatches( |
| 401 |
expected_output, doctest.ELLIPSIS)) |
| 402 |
|
| 403 |
def test_calls_sys_exit_when_dinstall_failure(self): |
| 404 |
""" Should call `sys.exit` when Dinstall reports failure. """ |
| 405 |
self.ssh_subprocess_double.set_subprocess_check_call_scenario( |
| 406 |
'failure') |
| 407 |
with testtools.ExpectedException(FakeSystemExit): |
| 408 |
dput.dput.dinstall_caller(**self.test_args) |
| 409 |
sys.exit.assert_called_with(EXIT_STATUS_FAILURE) |
| 410 |
|
| 411 |
def test_emits_message_when_dinstall_failure(self): |
| 412 |
""" Should emit explanatory message when Dinstall reports failure. """ |
| 413 |
self.ssh_subprocess_double.set_subprocess_check_call_scenario( |
| 414 |
'failure') |
| 415 |
with testtools.ExpectedException(FakeSystemExit): |
| 416 |
dput.dput.dinstall_caller(**self.test_args) |
| 417 |
expected_output = textwrap.dedent("""\ |
| 418 |
Error occured while trying to connect, or while attempting ... |
| 419 |
""") |
| 420 |
self.assertThat( |
| 421 |
sys.stdout.getvalue(), |
| 422 |
testtools.matchers.DocTestMatches( |
| 423 |
expected_output, doctest.ELLIPSIS)) |
| 424 |
|
| 425 |
|
| 426 |
class execute_command_TestCase( |
| 427 |
testtools.TestCase): |
| 428 |
""" Test cases for `execute_command` function. """ |
| 429 |
|
| 430 |
def setUp(self): |
| 431 |
""" Set up test fixtures. """ |
| 432 |
super().setUp() |
| 433 |
patch_system_interfaces(self) |
| 434 |
|
| 435 |
self.test_position = make_unique_slug(self) |
| 436 |
self.set_program_file_double() |
| 437 |
self.test_command = self.getUniqueString() |
| 438 |
|
| 439 |
self.set_test_args() |
| 440 |
|
| 441 |
patch_subprocess_call(self) |
| 442 |
self.set_upload_command_scenario('simple') |
| 443 |
|
| 444 |
def set_program_file_double(self): |
| 445 |
""" Set the file double for the command program. """ |
| 446 |
self.upload_command_program_file_double = FileDouble() |
| 447 |
|
| 448 |
def set_test_args(self): |
| 449 |
""" Set the arguments for the test call to the function. """ |
| 450 |
self.test_args = dict( |
| 451 |
command=self.test_command, |
| 452 |
position=self.test_position, |
| 453 |
debug=False, |
| 454 |
) |
| 455 |
|
| 456 |
def set_upload_command_scenario(self, name): |
| 457 |
""" Set the scenario for the upload command behaviour. """ |
| 458 |
double = SubprocessDouble( |
| 459 |
self.upload_command_program_file_double.path, |
| 460 |
argv=self.test_command.split()) |
| 461 |
double.register_for_testcase(self) |
| 462 |
double.set_subprocess_call_scenario('success') |
| 463 |
self.upload_command_subprocess_double = double |
| 464 |
|
| 465 |
def test_emits_debug_message_for_command(self): |
| 466 |
""" Should emit a debug message for the specified command. """ |
| 467 |
self.test_args['debug'] = True |
| 468 |
dput.dput.execute_command(**self.test_args) |
| 469 |
expected_output = textwrap.dedent("""\ |
| 470 |
D: Command: {command} |
| 471 |
""").format(command=self.test_command) |
| 472 |
self.assertIn(expected_output, sys.stdout.getvalue()) |
| 473 |
|
| 474 |
def test_calls_subprocess_call_with_expected_args(self): |
| 475 |
""" Should call `subprocess.call` with expected arguments. """ |
| 476 |
dput.dput.execute_command(**self.test_args) |
| 477 |
subprocess.call.assert_called_with(self.test_command, shell=True) |
| 478 |
|
| 479 |
def test_raises_error_when_command_failure(self): |
| 480 |
""" Should raise error when command exits with failure. """ |
| 481 |
double = self.upload_command_subprocess_double |
| 482 |
double.set_subprocess_call_scenario('failure') |
| 483 |
with testtools.ExpectedException(dputhelper.DputUploadFatalError): |
| 484 |
dput.dput.execute_command(**self.test_args) |
| 485 |
|
| 486 |
def test_raises_error_when_command_not_found(self): |
| 487 |
""" Should raise error when command not found. """ |
| 488 |
double = self.upload_command_subprocess_double |
| 489 |
double.set_subprocess_call_scenario('not_found') |
| 490 |
with testtools.ExpectedException(dputhelper.DputUploadFatalError): |
| 491 |
dput.dput.execute_command(**self.test_args) |
| 492 |
|
| 493 |
|
| 494 |
class check_upload_logfile_BaseTestCase( |
| 495 |
testscenarios.WithScenarios, |
| 496 |
testtools.TestCase): |
| 497 |
""" Base for test cases for `check_upload_logfile` function. """ |
| 498 |
|
| 499 |
scenarios = NotImplemented |
| 500 |
|
| 501 |
def setUp(self): |
| 502 |
""" Set up test fixtures. """ |
| 503 |
super().setUp() |
| 504 |
patch_system_interfaces(self) |
| 505 |
|
| 506 |
set_config(self, 'exist-simple') |
| 507 |
patch_runtime_config_options(self) |
| 508 |
|
| 509 |
setup_changes_file_fixtures(self) |
| 510 |
self.set_changes_file_scenario('no-format') |
| 511 |
|
| 512 |
self.set_test_args() |
| 513 |
|
| 514 |
patch_os_path_exists(self) |
| 515 |
|
| 516 |
self.set_upload_log_file_double() |
| 517 |
|
| 518 |
setup_file_double_behaviour( |
| 519 |
self, |
| 520 |
[self.changes_file_double, self.upload_log_file_double]) |
| 521 |
|
| 522 |
def set_changes_file_scenario(self, name): |
| 523 |
""" Set the package changes file based on scenario name. """ |
| 524 |
scenarios = make_changes_file_scenarios() |
| 525 |
scenario = dict(scenarios)[name] |
| 526 |
self.changes_file_double = scenario['file_double'] |
| 527 |
|
| 528 |
def set_test_args(self): |
| 529 |
""" Set the arguments for the test call to the function. """ |
| 530 |
self.test_args = dict( |
| 531 |
changes_file_path=self.changes_file_double.path, |
| 532 |
host=self.test_host, |
| 533 |
fqdn=self.runtime_config_parser.get(self.test_host, 'fqdn'), |
| 534 |
check_only=False, |
| 535 |
force_upload=False, |
| 536 |
debug=False, |
| 537 |
) |
| 538 |
custom_args = getattr(self, 'custom_args', {}) |
| 539 |
self.test_args.update(custom_args) |
| 540 |
|
| 541 |
def set_upload_log_file_double(self): |
| 542 |
""" Set the upload log file double. """ |
| 543 |
(file_dir_path, changes_file_name) = os.path.split( |
| 544 |
self.changes_file_double.path) |
| 545 |
(file_basename, __) = os.path.splitext(changes_file_name) |
| 546 |
file_name = "{basename}.{host}.upload".format( |
| 547 |
basename=file_basename, host=self.test_host) |
| 548 |
file_path = os.path.join(file_dir_path, file_name) |
| 549 |
|
| 550 |
double = FileDouble(file_path) |
| 551 |
double.set_os_path_exists_scenario( |
| 552 |
getattr(self, 'path_exists_scenario_name', 'exist')) |
| 553 |
double.set_open_scenario( |
| 554 |
getattr(self, 'open_scenario_name', 'okay')) |
| 555 |
|
| 556 |
file_content = getattr(self, 'log_content', "") |
| 557 |
double.fake_file = io.StringIO(file_content) |
| 558 |
|
| 559 |
self.upload_log_file_double = double |
| 560 |
|
| 561 |
|
| 562 |
class check_upload_logfile_SuccessTestCase(check_upload_logfile_BaseTestCase): |
| 563 |
""" Success test cases for `check_upload_logfile` function. """ |
| 564 |
|
| 565 |
scenarios = [ |
| 566 |
('simple', {}), |
| 567 |
('not_exist', { |
| 568 |
'path_exists_scenario_name': 'not_exist', |
| 569 |
'open_scenario_name': 'nonexist', |
| 570 |
}), |
| 571 |
('check_only', { |
| 572 |
'custom_args': { |
| 573 |
'check_only': True, |
| 574 |
}, |
| 575 |
}), |
| 576 |
('force_upload', { |
| 577 |
'custom_args': { |
| 578 |
'force_upload': True, |
| 579 |
}, |
| 580 |
}), |
| 581 |
('check_only and force_upload', { |
| 582 |
'custom_args': { |
| 583 |
'check_only': True, |
| 584 |
'force_upload': True, |
| 585 |
}, |
| 586 |
}), |
| 587 |
('method-ftp not-uploaded', { |
| 588 |
'config_method': "ftp", |
| 589 |
'config_fqdn': "quux.example.com", |
| 590 |
'log_content': "foo lorem-ipsum bar\n", |
| 591 |
}), |
| 592 |
('method-local not-uploaded', { |
| 593 |
'config_method': "local", |
| 594 |
'log_content': "foo lorem-ipsum bar\n", |
| 595 |
}), |
| 596 |
] |
| 597 |
|
| 598 |
for (scenario_name, scenario) in scenarios: |
| 599 |
scenario['expected_result'] = None |
| 600 |
del scenario_name, scenario |
| 601 |
|
| 602 |
def test_returns_expected_result(self): |
| 603 |
""" Should return expected result for the scenario. """ |
| 604 |
with contextlib.suppress(FakeSystemExit): |
| 605 |
result = dput.dput.check_upload_logfile(**self.test_args) |
| 606 |
self.assertEqual(self.expected_result, result) |
| 607 |
|
| 608 |
|
| 609 |
class check_upload_logfile_ExitTestCase(check_upload_logfile_BaseTestCase): |
| 610 |
""" Exit test cases for `check_upload_logfile` function. """ |
| 611 |
|
| 612 |
scenarios = [ |
| 613 |
('uploaded', { |
| 614 |
'config_method': "ftp", |
| 615 |
'config_fqdn': "quux.example.com", |
| 616 |
'log_content': "foo quux.example.com bar\n", |
| 617 |
'expected_exit_status': os.EX_OK, |
| 618 |
}), |
| 619 |
] |
| 620 |
|
| 621 |
def test_emits_expected_result_message(self): |
| 622 |
""" Should emit a message explaining the result of the check. """ |
| 623 |
with contextlib.suppress(FakeSystemExit): |
| 624 |
dput.dput.check_upload_logfile(**self.test_args) |
| 625 |
expected_stderr_output = textwrap.dedent("""\ |
| 626 |
Package has already been uploaded to {host} on {fqdn}, |
| 627 |
recorded in upload log file ‘{logfile_path}’. |
| 628 |
Not attempting upload. |
| 629 |
""").format( |
| 630 |
host=self.test_host, |
| 631 |
fqdn=self.config_fqdn, |
| 632 |
logfile_path=self.upload_log_file_double.path, |
| 633 |
) |
| 634 |
self.assertIn(expected_stderr_output, sys.stderr.getvalue()) |
| 635 |
|
| 636 |
def test_emits_expected_suggestion_message(self): |
| 637 |
""" Should emit a message suggesting how to override the check. """ |
| 638 |
with contextlib.suppress(FakeSystemExit): |
| 639 |
dput.dput.check_upload_logfile(**self.test_args) |
| 640 |
expected_stderr_output = textwrap.dedent("""\ |
| 641 |
To ignore this check and attempt upload anyway, |
| 642 |
use the ‘--force’ option. |
| 643 |
""").format( |
| 644 |
host=self.test_host, |
| 645 |
fqdn=self.config_fqdn, |
| 646 |
logfile_path=self.upload_log_file_double.path, |
| 647 |
) |
| 648 |
self.assertIn(expected_stderr_output, sys.stderr.getvalue()) |
| 649 |
|
| 650 |
def test_calls_sys_exit_with_expected_exit_status(self): |
| 651 |
""" Should call `sys.exit` with expected exit status. """ |
| 652 |
with testtools.ExpectedException(FakeSystemExit): |
| 653 |
dput.dput.check_upload_logfile(**self.test_args) |
| 654 |
sys.exit.assert_called_with(self.expected_exit_status) |
| 655 |
|
| 656 |
|
| 657 |
class check_upload_logfile_ErrorTestCase(check_upload_logfile_BaseTestCase): |
| 658 |
""" Error test cases for `check_upload_logfile` function. """ |
| 659 |
|
| 660 |
log_file_scenarios = [ |
| 661 |
('denied', { |
| 662 |
'path_exists_scenario_name': 'exist', |
| 663 |
'open_scenario_name': 'read_denied', |
| 664 |
'expected_exit_status': 1, |
| 665 |
}), |
| 666 |
] |
| 667 |
|
| 668 |
scenarios = log_file_scenarios |
| 669 |
|
| 670 |
def test_calls_sys_exit_with_expected_exit_status(self): |
| 671 |
""" Should call `sys.exit` with expected exit status. """ |
| 672 |
with testtools.ExpectedException(FakeSystemExit): |
| 673 |
dput.dput.check_upload_logfile(**self.test_args) |
| 674 |
sys.exit.assert_called_with(self.expected_exit_status) |
| 675 |
|
| 676 |
|
| 677 |
# Copyright © 2015–2026 Ben Finney <[email protected]> |
| 678 |
# |
| 679 |
# This is free software: you may copy, modify, and/or distribute this work |
| 680 |
# under the terms of the GNU General Public License as published by the |
| 681 |
# Free Software Foundation; version 3 of that license or any later version. |
| 682 |
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. |
| 683 |
|
| 684 |
|
| 685 |
# Local variables: |
| 686 |
# coding: utf-8 |
| 687 |
# mode: python |
| 688 |
# End: |
| 689 |
# vim: fileencoding=utf-8 filetype=python : |