1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
import argparse
import datetime
import re
from tests.internal import utils
utils.append_home_to_path(__file__)
from tomahawk.command import CommandMain
from tomahawk.constants import TimeoutError
from tomahawk.expect import CommandWithExpect
def test_00_run(monkeypatch):
EXPECTED = {
'command': 'uptime',
'command_output': "0:40 up 1 day, 8:19, 4 users, load averages: 0.00 0.50 1.00",
'exit_status': 0,
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(command = [ EXPECTED['command'] ])
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return EXPECTED['exit_status'], EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
o = stdout.stop().value()
assert status == 0
s = \
"""tomahawk@localhost %% %(command)s
%(command_output)s
""" % EXPECTED
assert o == s
def test_01_run_error(monkeypatch):
EXPECTED = {
'command': 'command_not_found',
'exit_status': 127,
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(command = [ EXPECTED['command'] ])
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return EXPECTED['exit_status'], "/bin/sh: command_not_found: command not found"
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 1
assert re.search(r'failed on host', stderr.stop().value())
def test_02_run_timeout(monkeypatch):
EXPECTED = {
'command': 'sleep 3',
'command_output': "/bin/sh: command_not_found: command not found",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ], timeout = 1
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
raise TimeoutError()
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 1
assert re.search(r'timed out on host', stderr.stop().value())
def test_03_run_escape_shell_chars(monkeypatch):
EXPECTED = {
'command': 'echo \\\\',
'command_output': "\\",
'exit_status': 0,
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
debug_enabled = True,
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return EXPECTED['exit_status'], EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
main.run()
o = stdout.stop().value().strip()
assert o == "tomahawk@localhost % echo \\\\\n\\"
def test_04_run_without_user(monkeypatch):
EXPECTED = {
'command': 'uptime',
'command_output': "0:40 up 1 day, 8:19, 4 users, load averages: 0.00 0.50 1.00",
'exit_status': 0,
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command=[EXPECTED['command']],
ssh_user=None,
ssh_options='-o User=tomahawk')
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return EXPECTED['exit_status'], EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
o = stdout.stop().value()
assert status == 0
s = \
"""[user]@localhost %% %(command)s
%(command_output)s
""" % EXPECTED
assert o == s
def test_10_run_option_host_files(monkeypatch):
EXPECTED = {
'command': 'echo "hello world"',
'command_output': "hello world",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
hosts = 'localhost,localhost',
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 0
assert re.search(r'hello world', stdout.stop().value())
def test_20_run_option_continue_on_error(monkeypatch):
EXPECTED = {
'command': 'failure_command',
'command_output': "hello world",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
continue_on_error = True,
hosts = 'localhost,127.0.0.1',
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 127, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
err = stderr.stop().value().strip()
assert status == 1
assert len(err.split('\n')) == 3
def test_21_run_option_parallel_continue_on_error(monkeypatch):
EXPECTED = {
'command': 'failure_command',
'command_output': "hello world",
}
stdout, stderr = utils.capture_stdout_stderr()
target_hosts = [
'localhost', 'localhost', 'localhost', 'localhost',
'127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1',
]
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
continue_on_error = True,
parallel = 2,
hosts = ','.join(target_hosts),
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 127, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 1
# parse output to collect failure hosts.
hosts = []
hosts_start = False
for line in stderr.stop().value().split('\n'):
if re.search(r'failed on following hosts', line, re.I):
hosts_start = True
continue
if hosts_start:
h = line.strip()
if h != '':
hosts.append(h)
assert hosts == target_hosts
def test_30_execute_option_ssh_options(monkeypatch):
EXPECTED = {
'command': 'echo "hello world"',
'command_output': "hello world",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
ssh_options = '-c arcfour',
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 0
assert re.search(EXPECTED['command_output'], stdout.stop().value())
def test_40_output_format(monkeypatch):
EXPECTED = {
'command': 'uptime',
'command_output': r'localhost @ uptime',
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
output_format = r'${host} @ ${command}',
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
out = stdout.stop().value().strip()
assert status == 0
assert out == EXPECTED['command_output']
def test_41_output_format_newline(monkeypatch):
"""\n new line test"""
EXPECTED = {
'command': 'uptime',
'command_output': "localhost\nuptime",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
output_format = r"${host}\n${command}",
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 0
assert stdout.stop().value().strip() == EXPECTED['command_output']
def test_42_output_format_no_newline(monkeypatch):
"""\\n no new line test"""
EXPECTED = {
'command': 'uptime',
'command_output': r"localhost \\n uptime",
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
output_format = r'${host} \\n ${command}',
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status == 0
assert stdout.stop().value().strip() == EXPECTED['command_output']
def test_50_parallel_adjustment(monkeypatch):
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ 'uptime' ], parallel = 10
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, "mock execute"
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
main.run()
assert main.context.options['parallel'] == 1
def test_60_verify_output_ok(monkeypatch):
EXPECTED = {
'command': 'echo "hello world"',
'command_output': r'hello world',
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
hosts = 'localhost,127.0.0.1',
verify_output = True,
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, EXPECTED['command_output']
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
out = stdout.stop().value().strip()
assert status == 0
assert out == """
tomahawk@localhost % echo "hello world"
hello world
tomahawk@127.0.0.1 % echo "hello world"
hello world
Verified output of all hosts.
""".strip()
def test_61_verify_output_ng(monkeypatch):
EXPECTED = {
'command': 'date',
'command_output': r'hello world',
}
stdout, stderr = utils.capture_stdout_stderr()
def mock_parse_args(self, args):
return utils.create_command_namespace(
command = [ EXPECTED['command'] ],
hosts = 'localhost,127.0.0.1',
verify_output = True,
)
monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)
def mock_execute(self):
return 0, str(datetime.datetime.now())
monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)
main = CommandMain('tomahawk')
status = main.run()
assert status != 0
|