-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconftest.py
94 lines (73 loc) · 3.01 KB
/
conftest.py
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
import pytest
import tftest
import os
import logging
def pytest_addoption(parser):
parser.addoption("--profile", action="store")
parser.addoption("--terraform-binary", action="store", default="terraform")
@pytest.fixture(scope="session")
def profile(request):
"""Return AWS profile."""
profile = request.config.getoption("--profile")
if profile is None:
raise Exception("--profile must be specified")
return profile
def terraform_examples_dir():
return os.path.join(os.getcwd(), "examples")
@pytest.fixture(scope="session")
def terraform_binary(request):
"""Return path to Terraform binary."""
return request.config.getoption("--terraform-binary")
@pytest.fixture(scope="session")
def terraform_examples():
"""Return a list of examples, i.e subdirectories in `examples/`."""
directory = terraform_examples_dir()
return [f.name for f in os.scandir(directory) if f.is_dir()]
@pytest.fixture(scope="session")
def terraform_config(terraform_binary, terraform_examples):
"""Convenience fixture for passing around config."""
config = {
"terraform_binary": terraform_binary,
"terraform_examples": terraform_examples,
}
logging.info(config)
return config
def get_tf(test_name, terraform_config, variables=None):
"""Construct and return `tftest.TerraformTest`, for executing Terraform commands."""
basedir = None
if "." in test_name: # Called with __name__, eg tests.test_examples_basic
test_name = test_name.split(".")[-1]
if test_name.startswith("test_"):
test_name = test_name[len("test_") :]
if test_name.startswith("examples_"):
basedir = terraform_examples_dir()
test_name = test_name[len("examples_") :].replace("_", "-")
logging.info(f"{basedir=} {test_name=}")
tf = tftest.TerraformTest(
tfdir=test_name, basedir=basedir, binary=terraform_config["terraform_binary"]
)
# # Populate test.auto.tfvars.json with the specified variables
# variables = variables or {}
# with open(os.path.join(basedir, test_name, "test.auto.tfvars.json"), "w") as f:
# json.dump(variables, f)
tf.setup()
return tf
def terraform_plan(test_name, terraform_config, variables=None):
"""Run `terraform plan -out`, returning the plan output."""
tf = get_tf(test_name, terraform_config, variables=variables)
yield tf.plan(output=True)
def terraform_apply_and_output(test_name, terraform_config, variables=None):
"""Run `terraform_apply` and then `terraform output`, returning the output."""
tf = get_tf(test_name, terraform_config, variables=variables)
try:
tf.apply()
yield tf.output()
finally:
tf.destroy(**{"auto_approve": True})
def terraform_apply(test_name, terraform_config, variables=None):
"""Run `terraform_apply` and then `terraform output`, returning the output."""
tf = get_tf(test_name, terraform_config, variables=variables)
try:
yield tf.apply()
finally:
tf.destroy(**{"auto_approve": True})