Skip to content

Commit

Permalink
Update terraform testing procedure (#5995)
Browse files Browse the repository at this point in the history
* test: Refactor Terraform build tests

* Change function identifier

* Fix S3 issues
  • Loading branch information
mildaniel authored Sep 27, 2023
1 parent dea2235 commit 8c0eaa8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions appveyor-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ for:
-
matrix:
only:
- configuration: DeployIntegTesting
- configuration: AllTerraformBuildTests

test_script:
- sh: "pytest -vv tests/integration/deploy -n 4 --reruns 4 --dist=loadgroup --json-report --json-report-file=TEST_REPORT-integration-deploy.json"
- sh: "pytest -vv -n 4 tests/integration/buildcmd/test_build_terraform_applications.py --json-report --json-report-file=TEST_REPORT-integration-buildcmd.json"

# Integ testing package
-
Expand Down
44 changes: 28 additions & 16 deletions tests/integration/buildcmd/test_build_terraform_applications.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import os
import logging
import shutil
Expand All @@ -19,6 +20,7 @@

from tests.integration.buildcmd.build_integ_base import BuildIntegBase
from tests.testing_utils import CI_OVERRIDE, IS_WINDOWS, RUN_BY_CANARY
from tests.testing_utils import run_command as static_run_command

LOG = logging.getLogger(__name__)
S3_SLEEP = 3
Expand Down Expand Up @@ -95,7 +97,7 @@ def build_with_prepare_hook(self):
_, stderr, return_code = self.run_command(
build_cmd_list, override_dir=self.terraform_application_execution_path, env=environment_variables
)
LOG.info(stderr)
LOG.info(stderr.decode("utf-8"))
self.assertEqual(return_code, 0)

def _verify_invoke_built_function(self, function_logical_id, overrides, expected_result):
Expand Down Expand Up @@ -152,8 +154,25 @@ def setUpClass(cls):
if not cls.pre_created_bucket:
cls.s3_bucket.create()
time.sleep(S3_SLEEP)

super().setUpClass()
cls.initialize_s3_backend()

@classmethod
def initialize_s3_backend(cls):
cls.backend_key = f"terraform-backend/{str(uuid.uuid4())}"
cls.backendconfig_path = str(Path(cls.terraform_application_execution_path) / "backend.conf")
with open(cls.backendconfig_path, "w") as f:
f.write(f'bucket="{cls.bucket_name}"\n')
f.write(f'key="{cls.backend_key}"\n')
f.write(f'region="{cls.region_name}"')

# We have to init the terraform project with specifying the S3 backend first
_, stderr, _ = static_run_command(
["terraform", "init", f"-backend-config={cls.backendconfig_path}", "-reconfigure", "-input=false"],
cwd=cls.terraform_application_execution_path
)
if stderr:
LOG.info(stderr)

@classmethod
def tearDownClass(cls):
Expand All @@ -165,19 +184,6 @@ def tearDownClass(cls):

def setUp(self):
super().setUp()
self.backend_key = f"terraform-backend/{str(uuid.uuid4())}"
self.backendconfig_path = str(Path(self.working_dir) / "backend.conf")
with open(self.backendconfig_path, "w") as f:
f.write(f'bucket="{self.bucket_name}"\n')
f.write(f'key="{self.backend_key}"\n')
f.write(f'region="{self.region_name}"')

# We have to init the terraform project with specifying the S3 backend first
_, stderr, _ = self.run_command(
["terraform", "init", f"-backend-config={self.backendconfig_path}", "-reconfigure", "-input=false"]
)
if stderr:
LOG.info(stderr)

def tearDown(self):
"""Clean up the terraform state file on S3 and remove the backendconfg locally"""
Expand All @@ -202,6 +208,7 @@ def tearDown(self):
(True,),
],
)
@pytest.mark.xdist_group(name="zip_lambda_local_backend_override")
class TestBuildTerraformApplicationsWithZipBasedLambdaFunctionAndLocalBackendWithOverride(
BuildTerraformApplicationIntegBase
):
Expand Down Expand Up @@ -297,6 +304,7 @@ def test_build_and_invoke_lambda_functions(self, function_identifier, expected_o
(True,),
],
)
@pytest.mark.xdist_group(name="zip_lambda_local_backend")
class TestBuildTerraformApplicationsWithZipBasedLambdaFunctionAndLocalBackend(BuildTerraformApplicationIntegBase):
function_identifier = "function9"
terraform_application = (
Expand Down Expand Up @@ -388,6 +396,7 @@ def test_build_and_invoke_lambda_functions(self, function_identifier, expected_o
(True,),
],
)
@pytest.mark.xdist_group(name="zip_lambda_s3_backend")
class TestBuildTerraformApplicationsWithZipBasedLambdaFunctionAndS3BackendWithOverride(
BuildTerraformApplicationS3BackendIntegBase
):
Expand Down Expand Up @@ -481,6 +490,7 @@ def test_build_and_invoke_lambda_functions(self, function_identifier, expected_o
(True,),
],
)
@pytest.mark.xdist_group(name="zip_lambda_s3_backend_override")
class TestBuildTerraformApplicationsWithZipBasedLambdaFunctionAndS3Backend(BuildTerraformApplicationS3BackendIntegBase):
function_identifier = "function9"
terraform_application = (
Expand Down Expand Up @@ -549,8 +559,10 @@ def test_build_and_invoke_lambda_functions(self, function_identifier, expected_o
command_list_parameters["build_image"] = self.docker_tag
build_cmd_list = self.get_command_list(**command_list_parameters)
LOG.info("command list: %s", build_cmd_list)
start = time.time()
_, stderr, return_code = self.run_command(build_cmd_list)
LOG.info(stderr)
end = time.time()
LOG.info(f"END: {end}, DURATION {end - start}")
self.assertEqual(return_code, 0)

self._verify_invoke_built_function(
Expand Down

0 comments on commit 8c0eaa8

Please sign in to comment.