1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
|
from __future__ import annotations
from pathlib import Path
from typing import Optional
import pytest
import typer
from inline_snapshot import snapshot
from zabbix_cli.app.app import StatefulApp
from zabbix_cli.bulk import BulkCommand
from zabbix_cli.bulk import BulkRunner
from zabbix_cli.bulk import BulkRunnerMode
from zabbix_cli.bulk import CommandExecution
from zabbix_cli.bulk import CommandResult
from zabbix_cli.bulk import CommentLine
from zabbix_cli.bulk import EmptyLine
from zabbix_cli.exceptions import CommandFileError
from zabbix_cli.output.console import exit_err
from zabbix_cli.output.console import exit_ok
from zabbix_cli.output.console import info
@pytest.mark.parametrize(
"line, expect",
[
pytest.param(
"show_zabbixcli_config",
BulkCommand(args=["show_zabbixcli_config"], line="show_zabbixcli_config"),
id="simple",
),
pytest.param(
"create_user username name surname passwd role autologin autologout groups",
BulkCommand(
args=[
"create_user",
"username",
"name",
"surname",
"passwd",
"role",
"autologin",
"autologout",
"groups",
],
line="create_user username name surname passwd role autologin autologout groups",
),
id="Legacy positional args",
),
pytest.param(
"create_user username --firstname name --lastname surname --passwd mypass --role 1 --autologin --autologout 86400 --groups '1,2'",
BulkCommand(
args=[
"create_user",
"username",
"--firstname",
"name",
"--lastname",
"surname",
"--passwd",
"mypass",
"--role",
"1",
"--autologin",
"--autologout",
"86400",
"--groups",
"1,2",
],
line="create_user username --firstname name --lastname surname --passwd mypass --role 1 --autologin --autologout 86400 --groups '1,2'",
),
id="args and kwargs",
),
pytest.param(
"create_user username myname --passwd mypass surname",
BulkCommand(
args=[
"create_user",
"username",
"myname",
"--passwd",
"mypass",
"surname",
],
line="create_user username myname --passwd mypass surname",
),
id="kwarg between args",
),
pytest.param(
"create_user myuser --firstname myname --passwd mypasswd --role 1 # comment here --option value",
BulkCommand(
args=[
"create_user",
"myuser",
"--firstname",
"myname",
"--passwd",
"mypasswd",
"--role",
"1",
],
line="create_user myuser --firstname myname --passwd mypasswd --role 1 # comment here --option value",
),
id="Trailing comment",
),
pytest.param(
"",
BulkCommand(),
id="fails (empty)",
marks=pytest.mark.xfail(raises=EmptyLine, strict=True),
),
pytest.param(
"#",
BulkCommand(),
id="fails (comment symbol)",
marks=pytest.mark.xfail(raises=CommentLine, strict=True),
),
pytest.param(
"# create_user myuser myname mypasswd --role 1",
BulkCommand(),
id="fails (commented out line)",
marks=pytest.mark.xfail(raises=CommentLine, strict=True),
),
],
)
def test_bulk_command_from_line(line: str, expect: BulkCommand) -> None:
assert BulkCommand.from_line(line) == expect
def test_load_command_file(tmp_path: Path, ctx: typer.Context) -> None:
"""Test loading a command file."""
file = tmp_path / "commands.txt"
file.write_text(
"""# comment
show_zabbixcli_config # next line will be blank
create_user username --firstname name --lastname surname mypass 1 1 86400 1,2
create_user username --firstname name --lastname surname --passwd mypass --role 1 --autologin --autologout 86400 --groups '1,2'
# comment explaining the next command
create_user username --firstname name --lastname surname --passwd mypass # trailing comment
# Command with flag
acknowledge_event 123,456,789 --message "foo message" --close
# Command with negative flag
show_templategroup mygroup --no-templates
# Command with optional boolean flags
show_host *.example.com --no-maintenance --monitored
# Command with enum option (human-readable string)
show_host *.example.com --active available
# Command with enum option (API values)
show_host *.example.com --active 0
show_host *.example.com --active 1
show_host *.example.com --active 2
# we will end with a blank line
"""
)
b = BulkRunner(ctx, file)
commands = b.load_command_file()
assert len(commands) == snapshot(11)
assert commands == snapshot(
[
BulkCommand(
args=["show_zabbixcli_config"],
line="show_zabbixcli_config # next line will be blank",
line_number=2,
),
BulkCommand(
args=[
"create_user",
"username",
"--firstname",
"name",
"--lastname",
"surname",
"mypass",
"1",
"1",
"86400",
"1,2",
],
line="create_user username --firstname name --lastname surname mypass 1 1 86400 1,2",
line_number=4,
),
BulkCommand(
args=[
"create_user",
"username",
"--firstname",
"name",
"--lastname",
"surname",
"--passwd",
"mypass",
"--role",
"1",
"--autologin",
"--autologout",
"86400",
"--groups",
"1,2",
],
line="create_user username --firstname name --lastname surname --passwd mypass --role 1 --autologin --autologout 86400 --groups '1,2'",
line_number=5,
),
BulkCommand(
args=[
"create_user",
"username",
"--firstname",
"name",
"--lastname",
"surname",
"--passwd",
"mypass",
],
line="create_user username --firstname name --lastname surname --passwd mypass # trailing comment",
line_number=7,
),
BulkCommand(
args=[
"acknowledge_event",
"123,456,789",
"--message",
"foo message",
"--close",
],
line='acknowledge_event 123,456,789 --message "foo message" --close',
line_number=9,
),
BulkCommand(
args=["show_templategroup", "mygroup", "--no-templates"],
line="show_templategroup mygroup --no-templates",
line_number=11,
),
BulkCommand(
args=["show_host", "*.example.com", "--no-maintenance", "--monitored"],
line="show_host *.example.com --no-maintenance --monitored",
line_number=13,
),
BulkCommand(
args=["show_host", "*.example.com", "--active", "available"],
line="show_host *.example.com --active available",
line_number=15,
),
BulkCommand(
args=["show_host", "*.example.com", "--active", "0"],
line="show_host *.example.com --active 0",
line_number=17,
),
BulkCommand(
args=["show_host", "*.example.com", "--active", "1"],
line="show_host *.example.com --active 1",
line_number=18,
),
BulkCommand(
args=["show_host", "*.example.com", "--active", "2"],
line="show_host *.example.com --active 2",
line_number=19,
),
]
)
@pytest.mark.parametrize(
"mode",
[BulkRunnerMode.STRICT, BulkRunnerMode.CONTINUE],
)
def test_bulk_runner_mode_invalid_line_strict(
tmp_path: Path, ctx: typer.Context, mode: BulkRunnerMode
) -> None:
"""Test loading a command file with invalid lines in strict/continue mode."""
file = tmp_path / "commands.txt"
file.write_text(
"""\
# comment
show_host "*.example.com # Missing closing quote
"""
)
b = BulkRunner(ctx, file, mode=mode)
with pytest.raises(CommandFileError):
b.load_command_file()
def test_bulk_runner_mode_invalid_line_skip(tmp_path: Path, ctx: typer.Context) -> None:
"""Test loading a command file with invalid lines in skip mode."""
file = tmp_path / "commands.txt"
file.write_text(
"""\
# comment
show_host *.example.com --active 0
show_host "*.example.com # Missing closing quote
create_host foo.example.com --hostgroup "Linux servers"
"""
)
b = BulkRunner(ctx, file, mode=BulkRunnerMode.SKIP)
commands = b.load_command_file()
assert len(commands) == 2 # show_host, create_host
assert len(b.executions) == 0
assert len(b.skipped) == 2 # comment, invalid command
# First line is comment
# Second line is invalid command
result = b.skipped[1]
assert result.command == snapshot(
BulkCommand(
line='show_host "*.example.com # Missing closing quote', line_number=3
)
)
assert result.result == CommandResult.SKIPPED
assert repr(result.error) == snapshot("ValueError('No closing quotation')")
assert result.line_number == snapshot(3)
def test_load_command_file_not_found(tmp_path: Path, ctx: typer.Context) -> None:
"""Test loading a command file that does not exist."""
file = tmp_path / "commands.txt"
assert not file.exists()
b = BulkRunner(ctx, file)
with pytest.raises(CommandFileError):
b.load_command_file()
@pytest.mark.parametrize("mode", [BulkRunnerMode.STRICT, BulkRunnerMode.CONTINUE])
def test_bulk_runner_exit_code_handling(
tmp_path: Path, app: StatefulApp, ctx: typer.Context, mode: BulkRunnerMode
) -> None:
"""Test handling of exit codes."""
file = tmp_path / "commands.txt"
file.write_text(
"""\
# comment
no_exit
exits_ok
exits_error
"""
)
@app.command(name="exits_ok")
def exits_ok() -> None:
exit_ok("This command exits with code 0")
@app.command(name="exits_error")
def exits_error() -> None:
exit_err("This command exits with code 1")
@app.command(name="no_exit")
def on_exit() -> None:
info("We just print a message here")
cmd = typer.main.get_command(app)
ctx.command = cmd
b = BulkRunner(ctx, file, mode)
with pytest.raises(CommandFileError) as excinfo:
b.run_bulk()
assert len(b.executions) == 3
assert b.executions[0].result == CommandResult.SUCCESS
assert b.executions[1].result == CommandResult.SUCCESS
assert b.executions[2].result == CommandResult.FAILURE
# Differing error messages between strict and continue
exc = excinfo.exconly()
if mode == BulkRunnerMode.STRICT:
assert (
exc
== "zabbix_cli.exceptions.CommandFileError: Command failed: [command]exits_error[/]: 1"
)
else:
assert (
exc
== "zabbix_cli.exceptions.CommandFileError: 1 commands failed:\nLine 4: [command]exits_error[/] [i](1)[/]"
)
@pytest.mark.parametrize(
"mode", [BulkRunnerMode.STRICT, BulkRunnerMode.CONTINUE, BulkRunnerMode.SKIP]
)
def test_bulk_commands_complex(
tmp_path: Path, app: StatefulApp, ctx: typer.Context, mode: BulkRunnerMode
) -> None:
"""Test bulk execution of commands with multiple options and arguments."""
file = tmp_path / "commands.txt"
file.write_text(
"""\
# comment
# Every type of option and argument
mixed_command "some arg" "optional arg" --opt value --reqopt 42 --flag --boolopt
mixed_command "some arg" "optional arg" --opt value --reqopt 42 --flag --no-boolopt
# Again with short options (no optional arg)
mixed_command "some arg" -O value -R 123 -F --boolopt
mixed_command "some arg" -O value -R 123 -F --no-boolopt
# Omit optional options
mixed_command "some arg" -O value -R 123
# Only required arguments/options
mixed_command "some arg" --reqopt 42
# Only arguments (with quotes)
only_args "arg1" "arg2" "arg3"
# Only arguments (no quotes)
only_args arg1 arg2 arg3
# Only options
only_options --opt1 value --opt2 value --opt3 42 --opt4 42
only_options -O "str" -S "Optional[str]" -I 42 -N 42
"""
)
@app.command(name="mixed_command")
def mixed_command(
ctx: typer.Context,
reqarg: str = typer.Argument(),
optarg: Optional[str] = typer.Argument(None),
opt: Optional[str] = typer.Option(None, "--opt", "-O"),
reqopt: int = typer.Option(
..., # type: ignore
"--reqopt",
"-R",
),
flag: bool = typer.Option(False, "--flag", "-F"),
boolopt: Optional[bool] = typer.Option(
False,
# Not specifying anything here should generate the options
# --boolopt / --no-boolopt
),
) -> None:
"""Command with every type of option and argument"""
exit_ok("Running mixed_command")
@app.command(name="only_args")
def only_args(
ctx: typer.Context,
arg1: str = typer.Argument(),
arg2: str = typer.Argument("default value"),
arg3: Optional[str] = typer.Argument(None),
) -> None:
exit_ok("Running only_args")
@app.command(name="only_options")
def only_options(
ctx: typer.Context,
opt1: str = typer.Option(..., "--opt1", "-O"),
opt2: Optional[str] = typer.Option(None, "--opt2", "-S"),
opt3: int = typer.Option(..., "--opt3", "-I"),
opt4: Optional[int] = typer.Option(None, "--opt4", "-N"),
) -> None:
exit_ok("Running only_options")
cmd = typer.main.get_command(app)
ctx.command = cmd
b = BulkRunner(ctx, file, mode)
b.run_bulk()
assert len(b.executions) == snapshot(10)
assert b.executions == snapshot(
[
CommandExecution(
command=BulkCommand(
args=[
"mixed_command",
"some arg",
"optional arg",
"--opt",
"value",
"--reqopt",
"42",
"--flag",
"--boolopt",
],
line='mixed_command "some arg" "optional arg" --opt value --reqopt 42 --flag --boolopt',
line_number=3,
),
result=CommandResult.SUCCESS,
line_number=3,
),
CommandExecution(
command=BulkCommand(
args=[
"mixed_command",
"some arg",
"optional arg",
"--opt",
"value",
"--reqopt",
"42",
"--flag",
"--no-boolopt",
],
line='mixed_command "some arg" "optional arg" --opt value --reqopt 42 --flag --no-boolopt',
line_number=4,
),
result=CommandResult.SUCCESS,
line_number=4,
),
CommandExecution(
command=BulkCommand(
args=[
"mixed_command",
"some arg",
"-O",
"value",
"-R",
"123",
"-F",
"--boolopt",
],
line='mixed_command "some arg" -O value -R 123 -F --boolopt',
line_number=6,
),
result=CommandResult.SUCCESS,
line_number=6,
),
CommandExecution(
command=BulkCommand(
args=[
"mixed_command",
"some arg",
"-O",
"value",
"-R",
"123",
"-F",
"--no-boolopt",
],
line='mixed_command "some arg" -O value -R 123 -F --no-boolopt',
line_number=7,
),
result=CommandResult.SUCCESS,
line_number=7,
),
CommandExecution(
command=BulkCommand(
args=["mixed_command", "some arg", "-O", "value", "-R", "123"],
line='mixed_command "some arg" -O value -R 123',
line_number=9,
),
result=CommandResult.SUCCESS,
line_number=9,
),
CommandExecution(
command=BulkCommand(
args=["mixed_command", "some arg", "--reqopt", "42"],
line='mixed_command "some arg" --reqopt 42',
line_number=11,
),
result=CommandResult.SUCCESS,
line_number=11,
),
CommandExecution(
command=BulkCommand(
args=["only_args", "arg1", "arg2", "arg3"],
line='only_args "arg1" "arg2" "arg3"',
line_number=13,
),
result=CommandResult.SUCCESS,
line_number=13,
),
CommandExecution(
command=BulkCommand(
args=["only_args", "arg1", "arg2", "arg3"],
line="only_args arg1 arg2 arg3",
line_number=15,
),
result=CommandResult.SUCCESS,
line_number=15,
),
CommandExecution(
command=BulkCommand(
args=[
"only_options",
"--opt1",
"value",
"--opt2",
"value",
"--opt3",
"42",
"--opt4",
"42",
],
line="only_options --opt1 value --opt2 value --opt3 42 --opt4 42",
line_number=17,
),
result=CommandResult.SUCCESS,
line_number=17,
),
CommandExecution(
command=BulkCommand(
args=[
"only_options",
"-O",
"str",
"-S",
"Optional[str]",
"-I",
"42",
"-N",
"42",
],
line='only_options -O "str" -S "Optional[str]" -I 42 -N 42',
line_number=18,
),
result=CommandResult.SUCCESS,
line_number=18,
),
]
)
assert len(b.skipped) == snapshot(8)
assert b.skipped == snapshot(
[
CommandExecution(
command=BulkCommand(line="# comment", line_number=1),
result=CommandResult.SKIPPED,
line_number=1,
),
CommandExecution(
command=BulkCommand(
line="# Every type of option and argument", line_number=2
),
result=CommandResult.SKIPPED,
line_number=2,
),
CommandExecution(
command=BulkCommand(
line="# Again with short options (no optional arg)", line_number=5
),
result=CommandResult.SKIPPED,
line_number=5,
),
CommandExecution(
command=BulkCommand(line="# Omit optional options", line_number=8),
result=CommandResult.SKIPPED,
line_number=8,
),
CommandExecution(
command=BulkCommand(
line="# Only required arguments/options", line_number=10
),
result=CommandResult.SKIPPED,
line_number=10,
),
CommandExecution(
command=BulkCommand(
line="# Only arguments (with quotes)", line_number=12
),
result=CommandResult.SKIPPED,
line_number=12,
),
CommandExecution(
command=BulkCommand(
line="# Only arguments (no quotes)", line_number=14
),
result=CommandResult.SKIPPED,
line_number=14,
),
CommandExecution(
command=BulkCommand(line="# Only options", line_number=16),
result=CommandResult.SKIPPED,
line_number=16,
),
]
)
|