-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(uat): migrate tests to use gdk_cli abstraction (#76)
* test(uat): add abstraction for instrumented gdk * test(uat): migrate tests to use gdk_cli abstraction * ci: track coverage for each test suite * refactor(test): rename --gdkimpl arg to --instrumented * refactor(test): control output capturing with arg
- Loading branch information
Showing
8 changed files
with
157 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
import pytest | ||
|
||
from t_setup import GdkProcess, GdkInstrumentedProcess | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--instrumented", action="store_true", default=False, help="run tests against code instead of installed gdk cli" | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def change_test_dir(tmpdir, monkeypatch): | ||
monkeypatch.chdir(tmpdir) | ||
return tmpdir | ||
|
||
|
||
@pytest.fixture() | ||
def gdk_cli(request): | ||
if request.config.getoption("--instrumented"): | ||
return GdkInstrumentedProcess() | ||
else: | ||
return GdkProcess() |
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,70 @@ | ||
import subprocess as sp | ||
import io | ||
from contextlib import redirect_stdout | ||
from importlib import reload | ||
|
||
import gdk.CLIParser as CLIParser | ||
import gdk.common.parse_args_actions as parse_args_actions | ||
import gdk.common.utils as utils | ||
|
||
|
||
class ProcessOutput: | ||
def __init__(self, exit_code, output) -> None: | ||
self.returncode = exit_code | ||
self.output = output | ||
|
||
|
||
class GdkProcess: | ||
def __init__(self) -> None: | ||
pass | ||
|
||
@classmethod | ||
def run(self, arguments=None, capture_output=True) -> ProcessOutput: | ||
if arguments is None: | ||
arguments = [] | ||
try: | ||
if capture_output: | ||
output = sp.run(["gdk"] + arguments, check=True, stdout=sp.PIPE) | ||
return ProcessOutput(output.returncode, output.stdout.decode()) | ||
else: | ||
output = sp.run(["gdk"] + arguments) | ||
return ProcessOutput(output.returncode, "") | ||
except sp.CalledProcessError as e: | ||
return ProcessOutput(e.returncode, e.stdout.decode()) | ||
|
||
|
||
class GdkInstrumentedProcess(GdkProcess): | ||
def __init__(self) -> None: | ||
pass | ||
|
||
@classmethod | ||
def run(self, arguments=None, capture_output=True) -> ProcessOutput: | ||
if arguments is None: | ||
arguments = [] | ||
|
||
# | ||
# In each cli execution, python interpreter reloads all gdk modules, here this simulates that effect. | ||
# | ||
reload(CLIParser) | ||
reload(parse_args_actions) | ||
reload(utils) | ||
|
||
# parsed args | ||
args = CLIParser.cli_parser.parse_args(arguments) | ||
|
||
exit_code = 0 | ||
output = "" | ||
|
||
try: | ||
if capture_output: | ||
f = io.StringIO() | ||
with redirect_stdout(f): | ||
parse_args_actions.run_command(args) | ||
output = f.getvalue() | ||
else: | ||
parse_args_actions.run_command(args) | ||
except Exception as e: | ||
exit_code = 1 | ||
output = str(e) | ||
|
||
return ProcessOutput(exit_code, output) |
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,12 +1,9 @@ | ||
import subprocess as sp | ||
def test_list_template(gdk_cli): | ||
check_list_template = gdk_cli.run(["component", "list", "--template"]) | ||
assert "HelloWorld-python" in check_list_template.output | ||
assert "HelloWorld-java" in check_list_template.output | ||
|
||
|
||
def test_list_template(): | ||
check_list_template = sp.run(["gdk", "component", "list", "--template"], check=True, stdout=sp.PIPE) | ||
assert "HelloWorld-python" in check_list_template.stdout.decode() | ||
assert "HelloWorld-java" in check_list_template.stdout.decode() | ||
|
||
|
||
def test_list_repository(): | ||
check_list_template = sp.run(["gdk", "component", "list", "--repository"], check=True, stdout=sp.PIPE) | ||
assert "aws-greengrass-labs-database-influxdb" in check_list_template.stdout.decode() | ||
def test_list_repository(gdk_cli): | ||
check_list_template = gdk_cli.run(["component", "list", "--repository"]) | ||
assert "aws-greengrass-labs-database-influxdb" in check_list_template.output |
Oops, something went wrong.