Skip to content

Commit

Permalink
Contiue refactoring efforts
Browse files Browse the repository at this point in the history
- fix linting errors that have accumulated over time
- refactor TerraformCommand.exec and add tests to cover all cases
- refactor TerraformCommand._check_plan and add tests
- add TerraformCommand._handle_no_plan_path with full tests
- add TerraformCommand._prepare_plan_file with full tests
- add TerraformCommand._validate_plan_path with full tests
- add TerraformCommand._run_handlers with full tests
- add TerraformCommand._should_plan with full tests
- create terraform.py for items that do not belong in TerraformCommand
- add prep_modules method function with full tests
- replace deprecated use of pkg_resources by importlib_metadata
  • Loading branch information
ephur committed Jun 9, 2024
1 parent 24ef865 commit 9ca851a
Show file tree
Hide file tree
Showing 29 changed files with 747 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[settings]
known_third_party = atlassian,boto3,botocore,click,deepdiff,google,hcl2,jinja2,mergedeep,moto,pkg_resources,pytest,pytest_lazyfixture,tenacity,yaml
known_third_party = atlassian,boto3,botocore,click,deepdiff,google,hcl2,importlib_metadata,jinja2,mergedeep,moto,pytest,tenacity,yaml
profile = black
2 changes: 1 addition & 1 deletion tests/authenticators/test_authenticatorscollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def test_collection(self, state_args):
def test_unknown_authenticator(self, state_args):
ac = tfworker.authenticators.AuthenticatorsCollection(state_args=state_args)
assert ac.get("aws") is not None
with pytest.raises(tfworker.authenticators.UnknownAuthenticator) as e:
with pytest.raises(tfworker.authenticators.UnknownAuthenticator):
ac.get("unknown")
2 changes: 0 additions & 2 deletions tests/authenticators/test_base_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
# 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 pytest

from tfworker.authenticators.base import BaseAuthenticator


Expand Down
15 changes: 7 additions & 8 deletions tests/backends/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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 os
import random
import string
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -175,12 +174,12 @@ def setup_method(self, method):
def test_no_session(self):
self.authenticators["aws"]._session = None
with pytest.raises(BackendError):
result = S3Backend(self.authenticators, self.definitions)
S3Backend(self.authenticators, self.definitions)

def test_no_backend_session(self):
self.authenticators["aws"]._backend_session = None
with pytest.raises(BackendError):
result = S3Backend(self.authenticators, self.definitions)
S3Backend(self.authenticators, self.definitions)

@patch("tfworker.backends.S3Backend._ensure_locking_table", return_value=None)
@patch("tfworker.backends.S3Backend._ensure_backend_bucket", return_value=None)
Expand Down Expand Up @@ -210,7 +209,7 @@ def test_handler_error(
mock_handler,
):
with pytest.raises(SystemExit):
result = S3Backend(self.authenticators, self.definitions)
S3Backend(self.authenticators, self.definitions)


class TestS3BackendEnsureBackendBucket:
Expand Down Expand Up @@ -262,15 +261,15 @@ def test_check_bucket_exists_error(self):
)

with pytest.raises(ClientError):
result = self.backend._check_bucket_exists(STATE_BUCKET)
self.backend._check_bucket_exists(STATE_BUCKET)
assert self.backend._s3_client.head_bucket.called

@mock_aws
def test_bucket_not_exist_no_create(self, capfd):
self.backend._authenticator.create_backend_bucket = False
self.backend._authenticator.bucket = NO_SUCH_BUCKET
with pytest.raises(BackendError):
result = self.backend._ensure_backend_bucket()
self.backend._ensure_backend_bucket()
assert (
"Backend bucket not found and --no-create-backend-bucket specified."
in capfd.readouterr().out
Expand Down Expand Up @@ -313,7 +312,7 @@ def test_create_bucket_invalid_location_constraint(self, capsys):
)

with pytest.raises(SystemExit):
result = self.backend._create_bucket(NO_SUCH_BUCKET)
self.backend._create_bucket(NO_SUCH_BUCKET)
assert "InvalidLocationConstraint" in capsys.readouterr().out

assert NO_SUCH_BUCKET not in [
Expand Down Expand Up @@ -341,7 +340,7 @@ def test_create_bucket_error(self):
)

with pytest.raises(ClientError):
result = self.backend._create_bucket(NO_SUCH_BUCKET)
self.backend._create_bucket(NO_SUCH_BUCKET)
assert self.backend._s3_client.create_bucket.called


Expand Down
11 changes: 2 additions & 9 deletions tests/commands/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from deepdiff import DeepDiff

import tfworker.commands.root
from tfworker.commands.root import get_platform
from tfworker.commands.root import get_platform, ordered_config_load


class TestMain:
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_stateargs_template_items_invalid(self, capfd):

# check templating of config_var, no environment
with pytest.raises(SystemExit) as e:
result = rc.template_items(return_as_dict=True)
rc.template_items(return_as_dict=True)
out, err = capfd.readouterr()
assert e.value.code == 1
assert "Invalid config-var" in out
Expand Down Expand Up @@ -247,13 +247,6 @@ def test_get_platform(
mock2.assert_called_once()


import io

import pytest

from tfworker.commands.root import ordered_config_load


class TestOrderedConfigLoad:
def test_ordered_config_load(self):
config = """
Expand Down
Loading

0 comments on commit 9ca851a

Please sign in to comment.