Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix,feat: shared plugin cache dir is a no-op, only selected tests are tracked for fixture cleanup, hide debug output behind a flag #66

Merged
merged 9 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dist/
pytestdebug.log
.venv

tests/**/.terraform.lock.hcl
# tests/**/.terraform.lock.hcl
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
hooks:
- id: black

- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-terraform"
version = "0.6.5"
version = "0.6.6"
description = "A pytest plugin for using terraform fixtures"
authors = ["Kapil Thangavelu <[email protected]>"]
license = "Apache-2.0"
Expand Down
18 changes: 12 additions & 6 deletions pytest_terraform/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import shutil
from collections import defaultdict

Expand All @@ -25,9 +24,9 @@ def pytest_configure(config):
config.addinivalue_line("markers", "terraform: tests using terraform fixtures")

cache_dir = config.getoption("dest_tf_plugin")
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
tf.LazyPluginCacheDir.value = os.path.abspath(cache_dir)

if cache_dir:
pass

tf.LazyModuleDir.value = config.getoption("dest_tf_mod_dir") or config.getini(
"terraform-mod-dir"
Expand All @@ -44,6 +43,7 @@ def pytest_configure(config):
)

tf.PytestConfig.value = config
tf.LazyTFDebug.value = config.getoption("dest_tf_debug") or False

if config.pluginmanager.hasplugin("xdist"):
config.pluginmanager.register(xdist.XDistTerraform(config))
Expand Down Expand Up @@ -72,6 +72,12 @@ def pytest_addoption(parser):
dest="dest_tf_replay",
help=("Use recorded resources instead of invoking terraform"),
)
group.addoption(
"--tf-debug",
action="store_true",
dest="dest_tf_debug",
help=("Debug terraform output and plugin output"),
)
group.addoption(
"--tf-mod-dir",
action="store",
Expand All @@ -84,8 +90,8 @@ def pytest_addoption(parser):
dest="dest_tf_plugin",
default=".tfcache",
help=(
"Use this directory for a terraform plugin cache "
"Default is to use .tfcache"
"[Depreceated] Use this directory for a terraform plugin cache "
"Default is to use .tfcache."
),
)

Expand Down
25 changes: 15 additions & 10 deletions pytest_terraform/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@


class TerraformRunner(object):

command_templates = {
"init": "init {input} {color} {plugin_dir}",
"apply": "apply {input} {color} {state} {approve} {plan}",
Expand All @@ -43,8 +42,6 @@ class TerraformRunner(object):
"approve": "-auto-approve",
}

debug = False

def __init__(
self,
work_dir,
Expand All @@ -54,7 +51,6 @@ def __init__(
stream_output=None,
tf_bin=None,
):

self.work_dir = work_dir
self.module_dir = module_dir
# use parent dir of work/data dir to avoid
Expand Down Expand Up @@ -110,14 +106,15 @@ def _get_cmd_args(self, cmd_name, tf_bin=None, env=None, **kw):
def _run_cmd(self, args, output=False):
env = dict(os.environ)
tf_env = {}
if LazyPluginCacheDir.resolve():
if LazyPluginCacheDir.resolve(False):
tf_env["TF_PLUGIN_CACHE_DIR"] = LazyPluginCacheDir.resolve()
tf_env["TF_IN_AUTOMATION"] = "yes"
if self.module_dir:
tf_env["TF_DATA_DIR"] = self.work_dir
cwd = self.module_dir or self.work_dir
env.update(tf_env)
print("run cmd", args, tf_env, cwd, file=sys.stderr)

write_log("run cmd", args, tf_env, cwd)
run_cmd = subprocess.check_call
if output:
run_cmd = subprocess.check_output
Expand Down Expand Up @@ -331,7 +328,7 @@ def __init__(self, name):
self.value = None

def resolve(self, default=None):
if not self.value and default:
if self.value is None and default is None:
raise ValueError("PlaceHolderValue %s not resolved" % self.name)
return self.value or default

Expand All @@ -341,6 +338,14 @@ def resolve(self, default=None):
LazyPluginCacheDir = PlaceHolderValue("plugin_cache")
LazyTfBin = PlaceHolderValue("tf_bin_path")
PytestConfig = PlaceHolderValue("pytestconfig")
LazyTFDebug = PlaceHolderValue("tf_debug")


def write_log(msg, *parts):
if LazyTFDebug.resolve(False):
if parts:
msg = " ".join((msg, *(map(str, parts))))
print("%s\n" % msg, file=sys.stderr)


class TerraformFixture(object):
Expand Down Expand Up @@ -389,7 +394,7 @@ def get_runner(self, module_dir, work_dir):
return TerraformRunner(
str(work_dir),
module_dir=module_dir,
plugin_cache=LazyPluginCacheDir.resolve(),
plugin_cache=LazyPluginCacheDir.resolve(False),
tf_bin=LazyTfBin.resolve(),
)

Expand All @@ -409,7 +414,7 @@ def __call__(self, request, tmpdir_factory, worker_id):
return self.create(request, module_dir)

def create(self, request, module_dir):
print("tf create %s" % self.tf_root_module, file=sys.stderr)
write_log("tf create %s" % self.tf_root_module)
self.runner.init()
if self.teardown_config != td.OFF:
request.addfinalizer(self.tear_down)
Expand All @@ -429,7 +434,7 @@ def create(self, request, module_dir):

def tear_down(self):
# config behavor on runner
print("tf teardown %s" % self.tf_root_module, file=sys.stderr)
write_log("tf teardown %s" % self.tf_root_module)
try:
self.runner.destroy()
except subprocess.CalledProcessError as e:
Expand Down
25 changes: 8 additions & 17 deletions pytest_terraform/xdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import sys

from pytest_terraform import tf
from pytest_terraform.lock import lock_create, lock_delete
Expand All @@ -34,9 +33,8 @@ def create(self, request, module_dir):

with lock_create(self.state_dir / self.name) as (success, result):
if success:
print(
"%s create %s - success: %s" % (self.wid, self.name, success),
file=sys.stderr,
tf.write_log(
"%s create %s - success: %s" % (self.wid, self.name, success)
)
tf_test_api = super(ScopedTerraformFixture, self).create(
request, module_dir
Expand All @@ -55,17 +53,13 @@ def tear_down(self):
if not success:
return
work_dir = (self.state_dir / self.name).read_text("utf8")
print(
"%s teardown %s work-dir %s" % (self.wid, self.name, success),
file=sys.stderr,
)
tf.write_log("%s teardown %s work-dir %s" % (self.wid, self.name, success))
super(ScopedTerraformFixture)
runner = self.get_runner(self.resolve_module_dir(), work_dir)
runner.destroy()


class XDistTerraform(object):

# Hooks
# https://github.com/pytest-dev/pytest-xdist/blob/master/src/xdist/newhooks.py

Expand Down Expand Up @@ -113,7 +107,7 @@ def generate_fixture_map(self, items):
return fixture_map

# worker hooks
def pytest_collection_modifyitems(self, session, config, items):
def pytest_collection_finish(self, session):
"""write out the collections of fixtures -> test ids

in xdist this is only called from the workers
Expand All @@ -123,7 +117,7 @@ def pytest_collection_modifyitems(self, session, config, items):
for t in tf.terraform.get_fixtures()
if isinstance(t, ScopedTerraformFixture)
}
self.fixture_map = self.generate_fixture_map(items)
self.fixture_map = self.generate_fixture_map(session.items)

def pytest_runtest_teardown(self, item, nextitem):
found = []
Expand All @@ -148,9 +142,8 @@ def pytest_runtest_teardown(self, item, nextitem):
# print('%s check result %s %s:%s' % (
# self.wid, completed, f, self.fixture_map[f]))
if self.completed.issuperset(self.fixture_map[f]):
print(
tf.write_log(
"%s execute test:%s teardown %s" % (self.wid, item.nodeid, f),
file=sys.stderr,
)
tf.terraform.get_fixture(f).tear_down()
self.fixture_map.pop(f)
Expand All @@ -173,14 +166,12 @@ def pytest_sessionfinish(self, exitstatus):
if f not in self.fixture_map:
continue
if self.completed.issuperset(self.fixture_map[f]):
print(
"%s worker session down cleanup %s" % (self.wid, f), file=sys.stderr
)
tf.write_log("%s worker session down cleanup %s" % (self.wid, f))
tf.terraform.get_fixture(f).tear_down()
else:
remains.append(str((f, self.fixture_map[f].difference(self.completed))))
if remains:
print("%s tf remains %s" % (self.wid, remains), file=sys.stderr)
tf.write_log("%s tf remains %s" % (self.wid, remains))

# master hooks
def pytest_report_teststatus(self, report, config):
Expand Down
6 changes: 6 additions & 0 deletions tests/data/mrofarret/local_baz/tf_resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"baz": {
"content": "baz!",
"content_base64": null,
"content_base64sha256": "GYygEuKSfhfg6WboOiMUAbr6WmzBaW3c/WCzdMKQ6Fw=",
"content_base64sha512": "+TyTAUP/jTLn89UpzicEjCK6AdmN4KBYd1WlDfKNwgAHrY9D6s+WqkuAb/ZoKd4TSDSNN+uFUt31+CFFjhCSdA==",
"content_md5": "4d36b5881759f7b1e3d65b5be9dc6790",
"content_sha1": "5fc5caaa8d04abb85be16b17953cd1a6e3ed549b",
"content_sha256": "198ca012e2927e17e0e966e83a231401bafa5a6cc1696ddcfd60b374c290e85c",
"content_sha512": "f93c930143ff8d32e7f3d529ce27048c22ba01d98de0a0587755a50df28dc20007ad8f43eacf96aa4b806ff66829de1348348d37eb8552ddf5f821458e109274",
"directory_permission": "0777",
"file_permission": "0777",
"filename": "./baz.txt",
Expand Down
21 changes: 21 additions & 0 deletions tests/terraform/local_bar/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/terraform/local_bar/tf_resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"bar": {
"content": "bar!",
"content_base64": null,
"content_base64sha256": "5oe3SfLNk2FZI6L3Bfqs5AM/NdV8z8plLNw5YWqUo8I=",
"content_base64sha512": "VsefHG45EmC85EGPSPpysV0kAveNz+q1rVoPqeeCbQQvU0uqL2FVcWPb8rOkDU9mk2y4Tj/XME5p+8h1nWC5+Q==",
"content_md5": "bb0d3e2eb8e78f4a10d55415e8e4677f",
"content_sha1": "3811e1a645e5371113584e6d18ff4843378ae590",
"content_sha256": "e687b749f2cd93615923a2f705faace4033f35d57ccfca652cdc39616a94a3c2",
"content_sha512": "56c79f1c6e391260bce4418f48fa72b15d2402f78dcfeab5ad5a0fa9e7826d042f534baa2f61557163dbf2b3a40d4f66936cb84e3fd7304e69fbc8759d60b9f9",
"directory_permission": "0777",
"file_permission": "0777",
"filename": "./bar.txt",
Expand Down
21 changes: 21 additions & 0 deletions tests/terraform/local_buz/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/terraform/local_buz/tf_resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"buz": {
"content": "fiz!",
"content_base64": null,
"content_base64sha256": "vikCmJzo1ZIiWEGfSvi3H0Oo2j+ng10mVvm7NrRW0lA=",
"content_base64sha512": "GxyZFjCjcNwXzgqwpesqGF9dHIdc4AxX+rFuTvktUHpPFLG8tXNV00zQuScillgb47xOlofyYlTX0FGZGE4rGQ==",
"content_md5": "b41ba5b9c442140644b7d2203ccb7da1",
"content_sha1": "1eb8252c1ec503dce3b41735251e12bdeadf1493",
"content_sha256": "be2902989ce8d5922258419f4af8b71f43a8da3fa7835d2656f9bb36b456d250",
"content_sha512": "1b1c991630a370dc17ce0ab0a5eb2a185f5d1c875ce00c57fab16e4ef92d507a4f14b1bcb57355d34cd0b9272296581be3bc4e9687f26254d7d05199184e2b19",
"directory_permission": "0777",
"file_permission": "0777",
"filename": "./buz.txt",
Expand Down
21 changes: 21 additions & 0 deletions tests/terraform/local_foo/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/terraform/local_foo/tf_resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"foo": {
"content": "foo!",
"content_base64": null,
"content_base64sha256": "wOCqrqBQvPO+JsDCPVj6iQwN+3nIojAWtKhs0oym6nE=",
"content_base64sha512": "TSCmPxazw2YStHurNILfRykjK/J4evgArH/2KnQuzQZodz4cq1f/ig2GeQO7mBI+Qx5jkTQEZxLCGs3mPtsB3Q==",
"content_md5": "35af8b7a9490467f75f19c1e5459f7e7",
"content_sha1": "4bf3e335199107182c6f7638efaad377acc7f452",
"content_sha256": "c0e0aaaea050bcf3be26c0c23d58fa890c0dfb79c8a23016b4a86cd28ca6ea71",
"content_sha512": "4d20a63f16b3c36612b47bab3482df4729232bf2787af800ac7ff62a742ecd0668773e1cab57ff8a0d867903bb98123e431e639134046712c21acde63edb01dd",
"directory_permission": "0777",
"file_permission": "0777",
"filename": "./foo.txt",
Expand Down
2 changes: 0 additions & 2 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


def test_lock_create(tmpdir):

path = tmpdir / "foo"

with lock_create(path) as (success, result):
Expand All @@ -18,7 +17,6 @@ def test_lock_create(tmpdir):


def test_lock_create_ctx_fail(tmpdir):

path = tmpdir / "foo2"

try:
Expand Down
1 change: 0 additions & 1 deletion tests/test_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def test_tf_statejson_update_dict():


def test_tf_statejson_update_bad():

statejson = tf.TerraformStateJson("hello")

with pytest.raises(ValueError):
Expand Down
Loading