From cb13d481d6c645f606ed0ed661ffaddcd4d55c12 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 15:51:05 +0200 Subject: [PATCH] Migrate cloud-function tests --- tests/fixtures.py | 1 + .../{fixture => }/bundle/main.py | 0 tests/modules/cloud_function/common.tfvars | 11 ++++++++ .../cloud_function/fixture/common.tfvars | 12 +++++++++ tests/modules/cloud_function/test_plan.py | 26 +++++++++---------- 5 files changed, 37 insertions(+), 13 deletions(-) rename tests/modules/cloud_function/{fixture => }/bundle/main.py (100%) create mode 100644 tests/modules/cloud_function/common.tfvars create mode 100644 tests/modules/cloud_function/fixture/common.tfvars diff --git a/tests/fixtures.py b/tests/fixtures.py index 381e161e..c142a610 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -143,6 +143,7 @@ def plan_summary_fixture(request): **tf_vars): if basedir is None: basedir = Path(request.fspath).parent + print(f"{basedir=}") return plan_summary(module_path=module_path, basedir=basedir, tf_var_files=tf_var_files, extra_files=extra_files, **tf_vars) diff --git a/tests/modules/cloud_function/fixture/bundle/main.py b/tests/modules/cloud_function/bundle/main.py similarity index 100% rename from tests/modules/cloud_function/fixture/bundle/main.py rename to tests/modules/cloud_function/bundle/main.py diff --git a/tests/modules/cloud_function/common.tfvars b/tests/modules/cloud_function/common.tfvars new file mode 100644 index 00000000..d7c7350c --- /dev/null +++ b/tests/modules/cloud_function/common.tfvars @@ -0,0 +1,11 @@ +project_id = "my-project" +name = "test" +bucket_name = "mybucket" +bundle_config = { + source_dir = "../../tests/modules/cloud_function/bundle" + output_path = "bundle.zip" + excludes = null +} +iam = { + "roles/cloudfunctions.invoker" = ["allUsers"] +} diff --git a/tests/modules/cloud_function/fixture/common.tfvars b/tests/modules/cloud_function/fixture/common.tfvars new file mode 100644 index 00000000..f9fb11f4 --- /dev/null +++ b/tests/modules/cloud_function/fixture/common.tfvars @@ -0,0 +1,12 @@ +project_id = "my-project" +name = "test" +bucket_name = var.bucket_name +v2 = var.v2 +bundle_config = { + source_dir = "bundle" + output_path = "bundle.zip" + excludes = null +} +iam = { + "roles/cloudfunctions.invoker" = ["allUsers"] +} diff --git a/tests/modules/cloud_function/test_plan.py b/tests/modules/cloud_function/test_plan.py index 019d69ce..79f91a20 100644 --- a/tests/modules/cloud_function/test_plan.py +++ b/tests/modules/cloud_function/test_plan.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,29 +16,29 @@ import pytest @pytest.fixture -def resources(plan_runner, version): +def resources(plan_summary, plan_runner, version): # convert `version` to a boolean suitable for the `v2` variable v2 = {'v1': 'false', 'v2': 'true'}[version] - _, resources = plan_runner(v2=v2) - return resources + #_, resources = plan_runner(v2=v2) + summary = plan_summary('modules/cloud-function', + tf_var_files=['common.tfvars'], v2=v2) + return summary @pytest.mark.parametrize('version', ['v1', 'v2']) def test_resource_count(resources): "Test number of resources created." - assert len(resources) == 3 + assert resources.counts['resources'] == 3 @pytest.mark.parametrize('version', ['v1', 'v2']) def test_iam(resources, version): "Test IAM binding resources." - - types = { + type = { 'v1': 'google_cloudfunctions_function_iam_binding', 'v2': 'google_cloudfunctions2_function_iam_binding' - } - - bindings = [r['values'] for r in resources if r['type'] == types[version]] - assert len(bindings) == 1 - assert bindings[0]['role'] == 'roles/cloudfunctions.invoker' - assert bindings[0]['members'] == ['allUsers'] + }[version] + key = f'{type}.default["roles/cloudfunctions.invoker"]' + binding = resources.values[key] + assert binding['role'] == 'roles/cloudfunctions.invoker' + assert binding['members'] == ['allUsers']