Add tests to delegated role grants example

This commit is contained in:
Julio Castillo
2021-09-21 15:42:01 +02:00
parent 48cfef86dd
commit e99514e9f1
5 changed files with 92 additions and 1 deletions

View File

@@ -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.

View File

@@ -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"]
}

View File

@@ -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"
}

View File

@@ -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

View File

@@ -54,7 +54,8 @@ def plan_runner(_plan_runner):
def e2e_plan_runner(_plan_runner): def e2e_plan_runner(_plan_runner):
"Returns a function to run Terraform plan on an end-to-end fixture." "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." "Runs Terraform plan on an end-to-end module using defaults, returns data."
plan = _plan_runner(fixture_path, targets=targets, refresh=refresh, **tf_vars) plan = _plan_runner(fixture_path, targets=targets, refresh=refresh, **tf_vars)
# skip the fixture # skip the fixture
@@ -62,6 +63,9 @@ def e2e_plan_runner(_plan_runner):
modules = dict((mod['address'], mod['resources']) modules = dict((mod['address'], mod['resources'])
for mod in root_module['child_modules']) for mod in root_module['child_modules'])
resources = [r for m in modules.values() for r in m] 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 modules, resources
return run_plan return run_plan