From e99514e9f12918d3f8f4dcf2e97bd62c3f5c987a Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 21 Sep 2021 15:42:01 +0200 Subject: [PATCH] Add tests to delegated role grants example --- .../delegated_role_grants/__init__.py | 13 +++++++ .../delegated_role_grants/fixture/main.tf | 22 ++++++++++++ .../fixture/variables.tf | 18 ++++++++++ .../delegated_role_grants/test_plan.py | 34 +++++++++++++++++++ tests/conftest.py | 6 +++- 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/cloud_operations/delegated_role_grants/__init__.py create mode 100644 tests/cloud_operations/delegated_role_grants/fixture/main.tf create mode 100644 tests/cloud_operations/delegated_role_grants/fixture/variables.tf create mode 100644 tests/cloud_operations/delegated_role_grants/test_plan.py diff --git a/tests/cloud_operations/delegated_role_grants/__init__.py b/tests/cloud_operations/delegated_role_grants/__init__.py new file mode 100644 index 000000000..d46dbae5e --- /dev/null +++ b/tests/cloud_operations/delegated_role_grants/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2021 Google LLC +# +# 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. diff --git a/tests/cloud_operations/delegated_role_grants/fixture/main.tf b/tests/cloud_operations/delegated_role_grants/fixture/main.tf new file mode 100644 index 000000000..f3be5be0d --- /dev/null +++ b/tests/cloud_operations/delegated_role_grants/fixture/main.tf @@ -0,0 +1,22 @@ +/** + * Copyright 2021 Google LLC + * + * 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. + */ + +module "test" { + source = "../../../../cloud-operations/delegated-role-grants" + project_create = true + project_id = var.project_id + project_administrators = ["user:user@example.com"] +} diff --git a/tests/cloud_operations/delegated_role_grants/fixture/variables.tf b/tests/cloud_operations/delegated_role_grants/fixture/variables.tf new file mode 100644 index 000000000..da4bff27f --- /dev/null +++ b/tests/cloud_operations/delegated_role_grants/fixture/variables.tf @@ -0,0 +1,18 @@ +# Copyright 2021 Google LLC +# +# 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 +# +# https://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. + +variable "project_id" { + type = string + default = "project-1" +} diff --git a/tests/cloud_operations/delegated_role_grants/test_plan.py b/tests/cloud_operations/delegated_role_grants/test_plan.py new file mode 100644 index 000000000..e6c860e88 --- /dev/null +++ b/tests/cloud_operations/delegated_role_grants/test_plan.py @@ -0,0 +1,34 @@ +# Copyright 2021 Google LLC +# +# 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 os +import sys +import pytest + + +FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixture") + + +def test_resources(e2e_plan_runner): + "Test that plan works and the numbers of resources is as expected." + modules, resources = e2e_plan_runner(FIXTURES_DIR, include_bare_resources=True) + assert len(modules) == 1 + assert len(resources) == 4 + +# TODO(jccb): test audit function (requires extending requirement for +# test suite) +# def test_audit(): +# AUDIT_PATH = os.path.join(os.path.dirname(__file__), "../../../cloud-operations/delegated-role-grants/") +# sys.path.append(AUDIT_PATH) +# import audit diff --git a/tests/conftest.py b/tests/conftest.py index b09d6c264..2a90f9256 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -54,7 +54,8 @@ def plan_runner(_plan_runner): def e2e_plan_runner(_plan_runner): "Returns a function to run Terraform plan on an end-to-end fixture." - def run_plan(fixture_path, targets=None, refresh=True, **tf_vars): + def run_plan(fixture_path, targets=None, refresh=True, + include_bare_resources=False, **tf_vars): "Runs Terraform plan on an end-to-end module using defaults, returns data." plan = _plan_runner(fixture_path, targets=targets, refresh=refresh, **tf_vars) # skip the fixture @@ -62,6 +63,9 @@ def e2e_plan_runner(_plan_runner): modules = dict((mod['address'], mod['resources']) for mod in root_module['child_modules']) resources = [r for m in modules.values() for r in m] + if include_bare_resources: + bare_resources = root_module['resources'] + resources.extend(bare_resources) return modules, resources return run_plan