Skip to content

Commit

Permalink
Merge pull request #77 from ephur/more_test_coverage
Browse files Browse the repository at this point in the history
More test coverage
  • Loading branch information
ephur authored Jun 24, 2023
2 parents 64fab0c + a969e7b commit 83dd24f
Show file tree
Hide file tree
Showing 28 changed files with 101 additions and 45 deletions.
2 changes: 1 addition & 1 deletion tests/authenticators/test_aws_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020-2021 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/backends/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Richard Maynard ([email protected])
# Copyright 2021-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/backends/test_gcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/backends/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_terraform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Richard Maynard ([email protected])
# Copyright 2021-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -418,7 +418,7 @@ def tf_13cmd_options(rootc_options):
@pytest.fixture
def definition_odict():
one_def = {
"test": collections.OrderedDict(
"test": dict(
{
"path": "/test",
"remote_vars": {"a": 1, "b": "two"},
Expand All @@ -427,7 +427,7 @@ def definition_odict():
}
)
}
return collections.OrderedDict(one_def)
return dict(one_def)


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/test_definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_make_vars(self, definition_odict, base, expected):
12,
)
test_vars = definition.make_vars(
definition_odict[name].get("terraform_vars", collections.OrderedDict()),
definition_odict[name].get("terraform_vars", dict()),
base.get("terraform_vars"),
)
assert test_vars["c"] == expected
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
48 changes: 47 additions & 1 deletion tests/util/test_copier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -191,6 +191,7 @@ def test_check_conflicts(self, request, copier, cwp):
def test_get_destination(self, tmp_path, copier):
dpath = f"{str(tmp_path)}/destination_test)"

# test that get destination raises an error if destination is not set
with pytest.raises(ValueError):
copier.get_destination()

Expand All @@ -203,6 +204,26 @@ def test_get_destination(self, tmp_path, copier):
assert copier.get_destination(make_dir=True) == dpath
assert os.path.isdir(dpath)

def test_get_destination_path(self, tmp_path, copier):
"""Ensure the destination path is returned properly when destination is set"""
dpath_td = tempfile.TemporaryDirectory()
dpath = dpath_td.name

# ensure object is in valid state for test
with pytest.raises(AttributeError):
getattr(copier, "_destination")

assert copier.get_destination(**{"destination": dpath}) == dpath

# ensure the directory is returned properly when make_dirs is true, and no errors
# are raised when the directory already exists
rpath = copier.get_destination(**{"destination": dpath, "make_dir": True})
assert rpath == dpath
assert os.path.isdir(rpath)

# remove the temporary directory
del dpath_td


class TestGitCopier:
"""test the GitCopier copier"""
Expand Down Expand Up @@ -262,12 +283,21 @@ def test_make_and_clean_temp(self):
"""tests making the temporary directory for git clones"""
c = GitCopier("test_source")

# ensure that the temp directory is created and attributes are set
c.make_temp()
assert hasattr(c, "_temp_dir")
temp_dir = c._temp_dir
assert os.path.isdir(temp_dir)
assert hasattr(c, "_temp_dir")

# ensure that the function is idempotent
c.make_temp()
# ensure that the temp directory is the same
assert temp_dir == c._temp_dir
assert os.path.isdir(c._temp_dir)
assert hasattr(c, "_temp_dir")

# ensure that the temp directory is removed
c.clean_temp()
assert not os.path.isdir(temp_dir)
assert not hasattr(c, "_temp_dir")
Expand Down Expand Up @@ -314,7 +344,23 @@ def test_type_match(self, request):
source="/tests/fixtures/definitions/test_a",
root_path=f"{request.config.rootdir}",
)

# this should return true because the source is a valid directory
assert FileSystemCopier.type_match(source) is True
# this should return false because the full path to source does not exist inside of root_path
assert (
FileSystemCopier.type_match("/some/invalid/path")
is False
)
# this should return true because the full path to source exists inside of root_path
assert (
FileSystemCopier.type_match(
"/tests/fixtures/definitions/test_a",
**{"root_path": f"{request.config.rootdir}"},
)
is True
)
# this should return false because the source is not a valid directory
assert FileSystemCopier.type_match("/some/invalid/path") is False

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/util/test_system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/authenticators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/authenticators/aws.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/authenticators/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/backends/gcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/backends/s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tfworker/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
7 changes: 3 additions & 4 deletions tfworker/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import re
from collections import OrderedDict

import click

Expand Down Expand Up @@ -42,8 +41,8 @@ def __init__(self, rootc, deployment="undefined", limit=tuple(), **kwargs):
self._definitions = None
self._backend = None
self._plugins = None
self._terraform_vars = OrderedDict()
self._remote_vars = OrderedDict()
self._terraform_vars = dict()
self._remote_vars = dict()
self._temp_dir = rootc.temp_dir
self._repository_path = rootc.args.repository_path

Expand Down Expand Up @@ -86,7 +85,7 @@ def __init__(self, rootc, deployment="undefined", limit=tuple(), **kwargs):
self._temp_dir,
self._tf_version_major,
)
plugins_odict = OrderedDict()
plugins_odict = dict()
for provider in rootc.providers_odict:
try:
raw_version = rootc.providers_odict[provider]["requirements"]["version"]
Expand Down
2 changes: 1 addition & 1 deletion tfworker/commands/clean.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
14 changes: 14 additions & 0 deletions tfworker/commands/env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click

from tfworker.authenticators import AuthenticatorsCollection
Expand Down
3 changes: 1 addition & 2 deletions tfworker/commands/root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import re
import shutil
import tempfile
from collections import OrderedDict

import click
import hcl2
Expand Down
2 changes: 1 addition & 1 deletion tfworker/commands/terraform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
15 changes: 7 additions & 8 deletions tfworker/definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
import copy
import json
from pathlib import Path, PurePath
from typing import OrderedDict

import click
import hcl2
Expand Down Expand Up @@ -58,13 +57,13 @@ def __init__(
self._body = body
self._path = body.get("path")
self._remote_vars = self.make_vars(
body.get("remote_vars", collections.OrderedDict()), global_remote_vars
body.get("remote_vars", dict()), global_remote_vars
)
self._terraform_vars = self.make_vars(
body.get("terraform_vars", collections.OrderedDict()), global_terraform_vars
body.get("terraform_vars", dict()), global_terraform_vars
)
self._template_vars = self.make_vars(
body.get("template_vars", collections.OrderedDict()), {}
body.get("template_vars", dict()), global_template_vars
)

self._deployment = deployment
Expand Down Expand Up @@ -160,7 +159,7 @@ def quote_str(some_string):

def make_vars(self, local_vars, global_vars):
"""Make a variables dictionary based on default vars, as well as specific vars for an item."""
global_vars = global_vars or collections.OrderedDict()
global_vars = global_vars or dict()
item_vars = copy.deepcopy(global_vars)
for k, v in local_vars.items():
item_vars[k] = v
Expand Down Expand Up @@ -188,7 +187,7 @@ def vars_typer(v, inner=False):
return rval
else:
return json.dumps(rval)
elif isinstance(v, OrderedDict) or isinstance(v, dict):
elif isinstance(v, dict):
rval = {}
for k, val in v.items():
result = Definition.vars_typer(val, inner=True)
Expand Down Expand Up @@ -218,7 +217,7 @@ def __init__(
):
self._body = definitions
self._plan_for = plan_for
self._definitions = collections.OrderedDict()
self._definitions = dict()
self._limit = True if len(limit) > 0 else False
self._limit_size = len(limit)
self._root_args = rootc.args
Expand Down
2 changes: 1 addition & 1 deletion tfworker/plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
6 changes: 2 additions & 4 deletions tfworker/providers/helm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import OrderedDict

from .base import BaseProvider


Expand Down Expand Up @@ -43,7 +41,7 @@ def hcl(self):
result.append(f'provider "{self.tag}" {{')
for k, v in provider_vars.items():
# Handle special case for kubernetes block in helm provider
if k.lower() == "kubernetes" and isinstance(v, OrderedDict):
if k.lower() == "kubernetes" and isinstance(v, dict):
result.append(f" {k} {{")
for ik, iv in v.items():
if iv and '"' not in iv:
Expand Down
5 changes: 3 additions & 2 deletions tfworker/util/copier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Richard Maynard ([email protected])
# Copyright 2021-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -206,7 +206,8 @@ def type_match(source: str, **kwargs) -> bool:
def make_temp(self) -> None:
if hasattr(self, "_temp_dir"):
pass
self._temp_dir = tempfile.mkdtemp()
else:
self._temp_dir = tempfile.mkdtemp()

def clean_temp(self) -> None:
"""clean_temp removes the temporary path used by this copier"""
Expand Down
2 changes: 1 addition & 1 deletion tfworker/util/system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Richard Maynard ([email protected])
# Copyright 2020-2023 Richard Maynard ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 83dd24f

Please sign in to comment.