|
|
|
@ -8,6 +8,7 @@ from io import StringIO
|
|
|
|
|
from typing import Callable
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from _pytest.logging import LogCaptureFixture, caplog
|
|
|
|
|
|
|
|
|
|
from python_terraform import IsFlagged, IsNotFlagged, Terraform, TerraformCommandError
|
|
|
|
|
|
|
|
|
@ -42,7 +43,11 @@ CMD_CASES = [
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
lambda x: x.cmd(
|
|
|
|
|
"plan", "var_to_output", no_color=IsFlagged, var={"test_var": "test"}
|
|
|
|
|
"plan",
|
|
|
|
|
"var_to_output",
|
|
|
|
|
no_color=IsFlagged,
|
|
|
|
|
var={"test_var": "test"},
|
|
|
|
|
raise_on_error=False,
|
|
|
|
|
),
|
|
|
|
|
# Expected output varies by terraform version
|
|
|
|
|
"Plan: 0 to add, 0 to change, 0 to destroy.",
|
|
|
|
@ -52,35 +57,27 @@ CMD_CASES = [
|
|
|
|
|
"var_to_output",
|
|
|
|
|
],
|
|
|
|
|
# try import aws instance
|
|
|
|
|
[
|
|
|
|
|
lambda x: x.cmd(
|
|
|
|
|
"import", "aws_instance.foo", "i-abcd1234", no_color=IsFlagged
|
|
|
|
|
),
|
|
|
|
|
"",
|
|
|
|
|
1,
|
|
|
|
|
False,
|
|
|
|
|
"Command: terraform import -no-color aws_instance.foo i-abcd1234",
|
|
|
|
|
"",
|
|
|
|
|
],
|
|
|
|
|
# try import aws instance with raise_on_error
|
|
|
|
|
[
|
|
|
|
|
lambda x: x.cmd(
|
|
|
|
|
"import",
|
|
|
|
|
"aws_instance.foo",
|
|
|
|
|
"i-abcd1234",
|
|
|
|
|
no_color=IsFlagged,
|
|
|
|
|
raise_on_error=True,
|
|
|
|
|
raise_on_error=False,
|
|
|
|
|
),
|
|
|
|
|
"",
|
|
|
|
|
1,
|
|
|
|
|
True,
|
|
|
|
|
"Command: terraform import -no-color aws_instance.foo i-abcd1234",
|
|
|
|
|
False,
|
|
|
|
|
"Error: No Terraform configuration files",
|
|
|
|
|
"",
|
|
|
|
|
],
|
|
|
|
|
# test with space and special character in file path
|
|
|
|
|
[
|
|
|
|
|
lambda x: x.cmd(
|
|
|
|
|
"plan", "var_to_output", out=FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS
|
|
|
|
|
"plan",
|
|
|
|
|
"var_to_output",
|
|
|
|
|
out=FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS,
|
|
|
|
|
raise_on_error=False,
|
|
|
|
|
),
|
|
|
|
|
"",
|
|
|
|
|
0,
|
|
|
|
@ -90,7 +87,9 @@ CMD_CASES = [
|
|
|
|
|
],
|
|
|
|
|
# test workspace command (commands with subcommand)
|
|
|
|
|
[
|
|
|
|
|
lambda x: x.cmd("workspace", "show", no_color=IsFlagged),
|
|
|
|
|
lambda x: x.cmd(
|
|
|
|
|
"workspace", "show", no_color=IsFlagged, raise_on_error=False
|
|
|
|
|
),
|
|
|
|
|
"",
|
|
|
|
|
0,
|
|
|
|
|
False,
|
|
|
|
@ -114,24 +113,23 @@ def fmt_test_file(request):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def string_logger(request) -> Callable[..., str]:
|
|
|
|
|
log_stream = StringIO()
|
|
|
|
|
handler = logging.StreamHandler(log_stream)
|
|
|
|
|
root_logger.addHandler(handler)
|
|
|
|
|
# @pytest.fixture()
|
|
|
|
|
# def string_logger(request) -> Callable[..., str]:
|
|
|
|
|
# log_stream = StringIO()
|
|
|
|
|
# handler = logging.StreamHandler(log_stream)
|
|
|
|
|
# root_logger.addHandler(handler)
|
|
|
|
|
|
|
|
|
|
def td():
|
|
|
|
|
root_logger.removeHandler(handler)
|
|
|
|
|
log_stream.close()
|
|
|
|
|
# def td():
|
|
|
|
|
# root_logger.removeHandler(handler)
|
|
|
|
|
# log_stream.close()
|
|
|
|
|
|
|
|
|
|
request.addfinalizer(td)
|
|
|
|
|
return lambda: str(log_stream.getvalue())
|
|
|
|
|
# request.addfinalizer(td)
|
|
|
|
|
# return lambda: str(log_stream.getvalue())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def workspace_setup_teardown():
|
|
|
|
|
"""
|
|
|
|
|
Fixture used in workspace related tests
|
|
|
|
|
"""Fixture used in workspace related tests.
|
|
|
|
|
|
|
|
|
|
Create and tear down a workspace
|
|
|
|
|
*Use as a contextmanager*
|
|
|
|
@ -151,11 +149,9 @@ def workspace_setup_teardown():
|
|
|
|
|
yield wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTerraform(object):
|
|
|
|
|
class TestTerraform:
|
|
|
|
|
def teardown_method(self, _) -> None:
|
|
|
|
|
""" teardown any state that was previously setup with a setup_method
|
|
|
|
|
call.
|
|
|
|
|
"""
|
|
|
|
|
"""Teardown any state that was previously setup with a setup_method call."""
|
|
|
|
|
exclude = ["test_tfstate_file", "test_tfstate_file2", "test_tfstate_file3"]
|
|
|
|
|
|
|
|
|
|
def purge(dir: str, pattern: str) -> None:
|
|
|
|
@ -190,24 +186,23 @@ class TestTerraform(object):
|
|
|
|
|
expected_ret_code: int,
|
|
|
|
|
expected_exception: bool,
|
|
|
|
|
expected_logs: str,
|
|
|
|
|
string_logger: Callable[..., str],
|
|
|
|
|
folder,
|
|
|
|
|
caplog: LogCaptureFixture,
|
|
|
|
|
folder: str,
|
|
|
|
|
):
|
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
tf.init(folder)
|
|
|
|
|
try:
|
|
|
|
|
ret, out, _ = method(tf)
|
|
|
|
|
assert not expected_exception
|
|
|
|
|
except TerraformCommandError as e:
|
|
|
|
|
assert expected_exception
|
|
|
|
|
ret = e.returncode
|
|
|
|
|
out = e.out
|
|
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.replace("\n", "")
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
tf.init(folder)
|
|
|
|
|
try:
|
|
|
|
|
ret, out, _ = method(tf)
|
|
|
|
|
assert not expected_exception
|
|
|
|
|
except TerraformCommandError as e:
|
|
|
|
|
assert expected_exception
|
|
|
|
|
ret = e.returncode
|
|
|
|
|
out = e.out
|
|
|
|
|
|
|
|
|
|
assert expected_output in out
|
|
|
|
|
assert expected_ret_code == ret
|
|
|
|
|
assert expected_logs in logs
|
|
|
|
|
assert expected_logs in caplog.text
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
("folder", "variables", "var_files", "expected_output", "options"),
|
|
|
|
@ -254,15 +249,18 @@ class TestTerraform(object):
|
|
|
|
|
assert expected_output in out.replace("\n", "").replace(" ", "")
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
def test_apply_with_var_file(self, string_logger):
|
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
def test_apply_with_var_file(self, caplog: LogCaptureFixture):
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
|
|
|
|
|
tf.init()
|
|
|
|
|
tf.apply(var_file=os.path.join(current_path, "tfvar_file", "test.tfvars"))
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.split("\n")
|
|
|
|
|
for log in logs:
|
|
|
|
|
if log.startswith("command: terraform apply"):
|
|
|
|
|
folder = "var_to_output"
|
|
|
|
|
tf.init(folder)
|
|
|
|
|
tf.apply(
|
|
|
|
|
folder,
|
|
|
|
|
var_file=os.path.join(current_path, "tfvar_files", "test.tfvars"),
|
|
|
|
|
)
|
|
|
|
|
for log in caplog.messages:
|
|
|
|
|
if log.startswith("Command: terraform apply"):
|
|
|
|
|
assert log.count("-var-file=") == 1
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
@ -315,33 +313,23 @@ class TestTerraform(object):
|
|
|
|
|
out = tf.output("test_output")
|
|
|
|
|
assert "test2" in out
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("param"), [({}), ({"module": "test2"}),])
|
|
|
|
|
def test_output(self, param, string_logger):
|
|
|
|
|
tf = Terraform(working_dir=current_path, variables={"test_var": "test"})
|
|
|
|
|
tf.init("var_to_output")
|
|
|
|
|
tf.apply("var_to_output")
|
|
|
|
|
result = tf.output("test_output", **param)
|
|
|
|
|
regex = re.compile(
|
|
|
|
|
"terraform output (-module=test2 -json|-json -module=test2) test_output"
|
|
|
|
|
)
|
|
|
|
|
log_str = string_logger()
|
|
|
|
|
if param:
|
|
|
|
|
assert re.search(regex, log_str), log_str
|
|
|
|
|
else:
|
|
|
|
|
assert result == "test"
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("param"), [({}), ({"module": "test2"}),])
|
|
|
|
|
def test_output_all(self, param, string_logger):
|
|
|
|
|
tf = Terraform(working_dir=current_path, variables={"test_var": "test"})
|
|
|
|
|
tf.init("var_to_output")
|
|
|
|
|
tf.apply("var_to_output")
|
|
|
|
|
result = tf.output(**param)
|
|
|
|
|
regex = re.compile("terraform output (-module=test2 -json|-json -module=test2)")
|
|
|
|
|
log_str = string_logger()
|
|
|
|
|
if param:
|
|
|
|
|
assert re.search(regex, log_str), log_str
|
|
|
|
|
@pytest.mark.parametrize("output_all", [True, False])
|
|
|
|
|
def test_output(self, caplog: LogCaptureFixture, output_all: bool):
|
|
|
|
|
expected_value = "test"
|
|
|
|
|
required_output = "test_output"
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
|
tf = Terraform(
|
|
|
|
|
working_dir=current_path, variables={"test_var": expected_value}
|
|
|
|
|
)
|
|
|
|
|
tf.init("var_to_output")
|
|
|
|
|
tf.apply("var_to_output")
|
|
|
|
|
params = tuple() if output_all else (required_output,)
|
|
|
|
|
result = tf.output(*params)
|
|
|
|
|
if output_all:
|
|
|
|
|
assert result[required_output]["value"] == expected_value
|
|
|
|
|
else:
|
|
|
|
|
assert result["test_output"]["value"] == "test"
|
|
|
|
|
assert result == expected_value
|
|
|
|
|
assert expected_value in caplog.messages[-1]
|
|
|
|
|
|
|
|
|
|
def test_destroy(self):
|
|
|
|
|
tf = Terraform(working_dir=current_path, variables={"test_var": "test"})
|
|
|
|
@ -355,22 +343,19 @@ class TestTerraform(object):
|
|
|
|
|
)
|
|
|
|
|
def test_plan(self, plan, variables, expected_ret):
|
|
|
|
|
tf = Terraform(working_dir=current_path, variables=variables)
|
|
|
|
|
ret, out, err = tf.plan(plan)
|
|
|
|
|
assert ret == expected_ret
|
|
|
|
|
tf.init(plan)
|
|
|
|
|
with pytest.raises(TerraformCommandError) as e:
|
|
|
|
|
tf.plan(plan)
|
|
|
|
|
assert (
|
|
|
|
|
e.value.err
|
|
|
|
|
== """\nError: Missing required argument\n\nThe argument "region" is required, but was not set.\n\n"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_fmt(self, fmt_test_file):
|
|
|
|
|
tf = Terraform(working_dir=current_path, variables={"test_var": "test"})
|
|
|
|
|
ret, out, err = tf.fmt(diff=True)
|
|
|
|
|
assert ret == 0
|
|
|
|
|
|
|
|
|
|
def test_import(self, string_logger):
|
|
|
|
|
tf = Terraform(working_dir=current_path)
|
|
|
|
|
tf.import_cmd("aws_instance.foo", "i-abc1234", no_color=IsFlagged)
|
|
|
|
|
assert (
|
|
|
|
|
"Command: terraform import -no-color aws_instance.foo i-abc1234"
|
|
|
|
|
in string_logger()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_create_workspace(self, workspace_setup_teardown):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
|
with workspace_setup_teardown(workspace_name, create=False) as tf:
|
|
|
|
@ -378,25 +363,24 @@ class TestTerraform(object):
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
def test_create_workspace_with_args(self, workspace_setup_teardown, string_logger):
|
|
|
|
|
def test_create_workspace_with_args(self, workspace_setup_teardown, caplog):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
|
state_file_path = os.path.join(
|
|
|
|
|
current_path, "test_tfstate_file2", "terraform.tfstate"
|
|
|
|
|
)
|
|
|
|
|
with workspace_setup_teardown(workspace_name, create=False) as tf:
|
|
|
|
|
with workspace_setup_teardown(
|
|
|
|
|
workspace_name, create=False
|
|
|
|
|
) as tf, caplog.at_level(logging.INFO):
|
|
|
|
|
ret, out, err = tf.create_workspace(
|
|
|
|
|
"test", current_path, no_color=IsFlagged
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.replace("\n", "")
|
|
|
|
|
expected_log = "Command: terraform workspace new -no-color test {}".format(
|
|
|
|
|
current_path
|
|
|
|
|
assert (
|
|
|
|
|
f"Command: terraform workspace new -no-color test {current_path}"
|
|
|
|
|
in caplog.messages
|
|
|
|
|
)
|
|
|
|
|
assert expected_log in logs
|
|
|
|
|
|
|
|
|
|
def test_set_workspace(self, workspace_setup_teardown):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
@ -405,22 +389,21 @@ class TestTerraform(object):
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
def test_set_workspace_with_args(self, workspace_setup_teardown, string_logger):
|
|
|
|
|
def test_set_workspace_with_args(self, workspace_setup_teardown, caplog):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf, caplog.at_level(
|
|
|
|
|
logging.INFO
|
|
|
|
|
):
|
|
|
|
|
ret, out, err = tf.set_workspace(
|
|
|
|
|
workspace_name, current_path, no_color=IsFlagged
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.replace("\n", "")
|
|
|
|
|
expected_log = "Command: terraform workspace select -no-color test {}".format(
|
|
|
|
|
current_path
|
|
|
|
|
assert (
|
|
|
|
|
f"Command: terraform workspace select -no-color test {current_path}"
|
|
|
|
|
in caplog.messages
|
|
|
|
|
)
|
|
|
|
|
assert expected_log in logs
|
|
|
|
|
|
|
|
|
|
def test_show_workspace(self, workspace_setup_teardown):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
@ -429,20 +412,16 @@ class TestTerraform(object):
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
def test_show_workspace_with_no_color(
|
|
|
|
|
self, workspace_setup_teardown, string_logger
|
|
|
|
|
):
|
|
|
|
|
def test_show_workspace_with_no_color(self, workspace_setup_teardown, caplog):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf:
|
|
|
|
|
with workspace_setup_teardown(workspace_name) as tf, caplog.at_level(
|
|
|
|
|
logging.INFO
|
|
|
|
|
):
|
|
|
|
|
ret, out, err = tf.show_workspace(no_color=IsFlagged)
|
|
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.replace("\n", "")
|
|
|
|
|
expected_log = "Command: terraform workspace show -no-color"
|
|
|
|
|
assert expected_log in logs
|
|
|
|
|
assert "Command: terraform workspace show -no-color" in caplog.messages
|
|
|
|
|
|
|
|
|
|
def test_delete_workspace(self, workspace_setup_teardown):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
@ -452,9 +431,11 @@ class TestTerraform(object):
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
def test_delete_workspace_with_args(self, workspace_setup_teardown, string_logger):
|
|
|
|
|
def test_delete_workspace_with_args(self, workspace_setup_teardown, caplog):
|
|
|
|
|
workspace_name = "test"
|
|
|
|
|
with workspace_setup_teardown(workspace_name, delete=False) as tf:
|
|
|
|
|
with workspace_setup_teardown(
|
|
|
|
|
workspace_name, delete=False
|
|
|
|
|
) as tf, caplog.at_level(logging.INFO):
|
|
|
|
|
tf.set_workspace("default")
|
|
|
|
|
ret, out, err = tf.delete_workspace(
|
|
|
|
|
workspace_name, current_path, force=IsFlagged,
|
|
|
|
@ -462,10 +443,7 @@ class TestTerraform(object):
|
|
|
|
|
|
|
|
|
|
assert ret == 0
|
|
|
|
|
assert err == ""
|
|
|
|
|
|
|
|
|
|
logs = string_logger()
|
|
|
|
|
logs = logs.replace("\n", "")
|
|
|
|
|
expected_log = "Command: terraform workspace delete -force test {}".format(
|
|
|
|
|
current_path
|
|
|
|
|
assert (
|
|
|
|
|
f"Command: terraform workspace delete -force test {current_path}"
|
|
|
|
|
in caplog.messages
|
|
|
|
|
)
|
|
|
|
|
assert expected_log in logs
|
|
|
|
|