-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
otk/command: implement 'validate' command
- Loading branch information
1 parent
8328510
commit cd92399
Showing
7 changed files
with
220 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,68 @@ | ||
import pytest | ||
|
||
from otk.command import parser_create | ||
|
||
|
||
def test_parse_no_command(): | ||
def test_parse_no_command(capsys): | ||
p = parser_create() | ||
with pytest.raises(SystemExit) as sys_exit: | ||
p.parse_args([]) | ||
assert sys_exit.type == SystemExit | ||
assert sys_exit.value.code == 2 | ||
|
||
r = p.parse_args([]) | ||
|
||
assert r.command is None | ||
assert r.verbose == 0 | ||
assert not r.json | ||
assert r.identifier is None | ||
|
||
r = p.parse_args(["-j"]) | ||
assert r.command is None | ||
assert r.json | ||
assert r.verbose == 0 | ||
assert r.identifier is None | ||
|
||
r = p.parse_args(["-j", "-v"]) | ||
assert r.command is None | ||
assert r.json | ||
assert r.verbose == 1 | ||
assert r.identifier is None | ||
|
||
r = p.parse_args(["-j", "-vvvv"]) | ||
assert r.command is None | ||
assert r.json | ||
assert r.verbose == 4 | ||
assert r.identifier is None | ||
captured_stderr = capsys.readouterr().err | ||
assert "the following arguments are required: command" in captured_stderr | ||
|
||
r = p.parse_args(["--json", "--verbose", "--verbose"]) | ||
assert r.command is None | ||
assert r.json | ||
assert r.verbose == 2 | ||
assert r.identifier is None | ||
|
||
r = p.parse_args(["-i", "foo"]) | ||
assert r.command is None | ||
assert not r.json | ||
assert r.verbose == 0 | ||
assert r.identifier == "foo" | ||
|
||
|
||
def test_parse_compile(): | ||
@pytest.mark.parametrize( | ||
"command,results", | ||
[ | ||
(["compile"], {"command": "compile", "output": None, "input": None}), | ||
(["compile", "foo"], {"command": "compile", "output": None, "input": "foo"}), | ||
( | ||
["compile", "-o", "output_manifest.yaml"], | ||
{"command": "compile", "output": "output_manifest.yaml", "input": None}, | ||
), | ||
( | ||
["compile", "-o", "output_manifest.yaml", "input_omifest.yaml"], | ||
{ | ||
"command": "compile", | ||
"output": "output_manifest.yaml", | ||
"input": "input_omifest.yaml", | ||
}, | ||
), | ||
(["validate", "foo.yaml"], {"command": "validate", "input": "foo.yaml"}), | ||
], | ||
) | ||
def test_parse_commands_success(capsys, command, results): | ||
p = parser_create() | ||
r = p.parse_args(command) | ||
for k in results.keys(): | ||
assert getattr(r, k) == results[k] | ||
|
||
r = p.parse_args(["compile"]) | ||
assert r.command == "compile" | ||
assert r.output is None | ||
|
||
r = p.parse_args(["compile", "foo"]) | ||
assert r.command == "compile" | ||
assert r.input == "foo" | ||
assert r.output is None | ||
|
||
r = p.parse_args(["compile", "-o", "file.yaml"]) | ||
assert r.command == "compile" | ||
assert r.input is None | ||
assert r.output == "file.yaml" | ||
@pytest.mark.parametrize( | ||
"command,sys_exit_code,sys_exit_message", | ||
[ | ||
( | ||
["compile", "--no_such_option"], | ||
2, | ||
"unrecognized arguments: --no_such_option", | ||
), | ||
( | ||
["validate", "-o", "output_manifest.yaml", "input_omifest.yaml"], | ||
2, | ||
# deliberately we expect '-o input_omifest.yaml' as argparse acts like this if -o is not defined | ||
"error: unrecognized arguments: -o input_omifest.yaml", | ||
), | ||
], | ||
) | ||
def test_parse_commands_failure(capsys, command, sys_exit_code, sys_exit_message): | ||
p = parser_create() | ||
with pytest.raises(SystemExit) as sys_exit: | ||
p.parse_args(command) | ||
assert sys_exit.type == SystemExit | ||
assert sys_exit.value.code == sys_exit_code | ||
|
||
r = p.parse_args(["compile", "-o", "file.yaml", "foo.yaml"]) | ||
assert r.command == "compile" | ||
assert r.input == "foo.yaml" | ||
assert r.output == "file.yaml" | ||
captured_stderr = capsys.readouterr().err | ||
assert sys_exit_message in captured_stderr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import argparse | ||
import os.path | ||
import pytest | ||
|
||
from otk.command import compile, parser_create | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"arguments, input_data, output_data, sys_exit_code, log_message", | ||
[ | ||
# "GENERATE" is a placeholder to append "tmp_path" from pytest | ||
( | ||
argparse.Namespace(input="GENERATE", output="GENERATE", target=None), | ||
"""otk.version: 1 | ||
otk.target.osbuild.qcow2: { test: 1 } | ||
""", | ||
"""{ | ||
"test": 1, | ||
"version": "2", | ||
"sources": {} | ||
}""", | ||
0, | ||
None, | ||
), | ||
], | ||
) | ||
def test_compile( | ||
tmp_path, | ||
caplog, | ||
capsys, | ||
monkeypatch, | ||
arguments, | ||
input_data, | ||
output_data, | ||
sys_exit_code, | ||
log_message, | ||
): | ||
if arguments.input == "GENERATE": | ||
input_filename = "input.yaml" | ||
arguments.input = os.path.join(tmp_path, input_filename) | ||
with open(arguments.input, "w") as f: | ||
f.write(input_data) | ||
|
||
if arguments.output == "GENERATE": | ||
# fix path so we only write to tmp_path | ||
arguments.output = os.path.join(tmp_path, "output.yaml") | ||
parser = parser_create() | ||
|
||
ret = compile(parser, arguments) | ||
assert ret == sys_exit_code | ||
|
||
if log_message: | ||
assert log_message in caplog.text | ||
|
||
assert os.path.exists(arguments.output) | ||
with open(arguments.output) as f: | ||
assert f.readlines() == output_data.splitlines(True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import argparse | ||
import os.path | ||
import pytest | ||
|
||
from otk.command import validate, parser_create | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"arguments, input_data, sys_exit_code, log_message", | ||
[ | ||
# "GENERATE" is a placeholder to append "tmp_path" from pytest | ||
( | ||
argparse.Namespace(input="GENERATE", output="GENERATE", target=None), | ||
"""otk.version: 1 | ||
otk.target.osbuild.qcow2: { test: 1 } | ||
""", | ||
0, | ||
None, | ||
), | ||
( | ||
argparse.Namespace(input="GENERATE", output=None, target=None), | ||
"""otk.version: 1 | ||
otk.target.osbuild.qcow2: { test: 1 } | ||
""", | ||
0, | ||
None, | ||
), | ||
( | ||
argparse.Namespace(input="GENERATE", output="GENERATE", target=None), | ||
"""otk.version: 1 | ||
otk.target.osbuild.qcow2: { test: 1 } | ||
otk.target.osbuild.ami: { test: 2 } | ||
""", | ||
1, | ||
"INPUT contains multiple targets, `-t` is required", | ||
), | ||
], | ||
) | ||
def test_validate( | ||
tmp_path, | ||
caplog, | ||
capsys, | ||
monkeypatch, | ||
arguments, | ||
input_data, | ||
sys_exit_code, | ||
log_message, | ||
): | ||
if arguments.input == "GENERATE": | ||
input_filename = "input.yaml" | ||
arguments.input = os.path.join(tmp_path, input_filename) | ||
with open(arguments.input, "w") as f: | ||
f.write(input_data) | ||
|
||
if arguments.output == "GENERATE": | ||
# fix path so we only write to tmp_path | ||
arguments.output = os.path.join(tmp_path, "output.yaml") | ||
parser = parser_create() | ||
|
||
ret = validate(parser, arguments) | ||
assert ret == sys_exit_code | ||
|
||
if log_message: | ||
assert log_message in caplog.text | ||
|
||
if arguments.output: | ||
assert not os.path.exists(arguments.output) |