Add tests for busines-units example

This commit is contained in:
Aleksandr Averbukh 2019-09-12 00:43:40 +02:00
parent 73cd9c5ff5
commit f17b4a9b04
7 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,13 @@
# Copyright 2019 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.

View File

@ -0,0 +1,34 @@
# Copyright 2019 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.
"Plan fixture."
import os
import pytest
import tftest
_ABSPATH = os.path.dirname(os.path.abspath(__file__)).split(os.path.sep)
_TFDIR = os.path.sep.join(_ABSPATH[-2:])
# TODO(ludoo): generalize and put in top-level package
@pytest.fixture(scope='session')
def plan():
tf = tftest.TerraformTest(_TFDIR, os.path.sep.join(_ABSPATH[:-3]),
os.environ.get('TERRAFORM', 'terraform'))
tf.setup(extra_files=['tests/{}/terraform.tfvars'.format(_TFDIR)])
return tf.plan_out(parsed=True)

View File

@ -0,0 +1,9 @@
billing_account_id = "012345-ABCDEF-012345"
business_unit_1_name = "infra"
business_unit_2_name = "analytics"
business_unit_3_name = "data"
environments = ["dev", "test"]
generate_service_account_keys = true
organization_id = "012345678919"
prefix = "fabric-org-env-3"
root_node = "folders/0123456789"

View File

@ -0,0 +1,38 @@
# Copyright 2019 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.
"Test shared and business-units folders"
import pytest
def test_shared_folder(plan):
"Shared folder resource attributes must match variables."
root_node = plan.variables['root_node']
resource = plan.modules['module.shared-folder']['module.shared-folder.google_folder.folders[0]']
assert resource['values']['parent'] == root_node
assert resource['values']['display_name'] == 'shared'
def test_business_unit_folders(plan):
"Business Unit folder resource attributes must match variables."
folder_resource_addresses = ['module.business-unit-%s-folders.module.business-unit-folder.google_folder.folders[0]' %
num for num in (1,2,3)]
root_node = plan.variables['root_node']
business_unit_names = [plan.variables[name] for name in ('business_unit_1_name', 'business_unit_2_name', 'business_unit_3_name')]
for address in folder_resource_addresses:
resource = plan.resource_changes[address]
assert resource['change']['after']['parent'] == root_node
assert resource['change']['after']['display_name'] in business_unit_names

View File

@ -0,0 +1,48 @@
# Copyright 2019 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.
"Test root module outputs."
def test_project_ids(plan):
"Project ids should use prefix and match expected values."
prefix = plan.variables['prefix']
assert plan.outputs['audit_logs_project'] == prefix + '-audit'
assert plan.outputs['shared_resources_project'] == prefix + '-shared'
assert plan.outputs['terraform_project'] == prefix + '-terraform'
def test_bucket_names(plan):
"GCS bucket names should use prefix and location and match expected values."
location = plan.variables['gcs_location'].lower()
prefix = plan.variables['prefix']
bootstrap_bucket = plan.outputs['bootstrap_tf_gcs_bucket']
assert bootstrap_bucket.startswith(prefix)
assert bootstrap_bucket.endswith('tf-bootstrap')
assert '-%s-' % location in bootstrap_bucket
def test_environment_buckets(plan):
"One GCS bucket should be created for each environment."
buckets = plan.outputs['environment_tf_gcs_buckets']
for environment in plan.variables['environments']:
assert environment in buckets
assert buckets[environment].endswith(environment)
def test_bq_dataset(plan):
"Bigquery dataset should be named in the following way 'logs_audit_<ROOT_NODE_TYPE>_<ROOT NODE NUMERIC ID>'"
root_node_type = plan.variables['root_node'].split("/")[0][:-1]
root_node_numeric_id = plan.variables['root_node'].split("/")[1]
assert plan.outputs['audit_logs_bq_dataset'] == 'logs_audit_' + root_node_type + '_' + root_node_numeric_id

View File

@ -0,0 +1,44 @@
# Copyright 2019 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.
"Test project creation in root module."
import pytest
@pytest.fixture(scope='module')
def project_modules(plan):
names = ['module.project-%s' %
name for name in ('audit', 'shared-resources', 'tf')]
return dict((name, plan.modules[name]) for name in names)
def test_project_resource(plan, project_modules):
"Project resource attributes must match variables."
billing_account = plan.variables['billing_account_id']
project_names = ['%s-%s' %
(plan.variables['prefix'], name) for name in ('shared', 'terraform', 'audit')]
for name, mod in project_modules.items():
resource = mod['%s.google_project.project' % name]
assert resource['values']['billing_account'] == billing_account
assert resource['values']['name'] in project_names
def test_project_services(plan, project_modules):
"Project service resource must enable APIs specified in the variable."
services = plan.variables['project_services']
for name, mod in project_modules.items():
resource = mod['%s.google_project_services.services[0]' % name]
assert resource['values']['services'] == services

View File

@ -0,0 +1,33 @@
# Copyright 2019 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.
"Test service account creation in root module."
import pytest
@pytest.fixture(scope='module')
def mod(plan):
return plan.modules['module.service-accounts-tf-environments']
def test_accounts(plan, mod):
"One service account per environment should be created."
environments = plan.variables['environments']
prefix = plan.variables['prefix']
resources = [v for k, v in mod.items() if '.google_service_account.' in k]
assert len(resources) == len(environments)
assert sorted([res['values']['account_id'] for res in resources]) == sorted([
'%s-%s' % (prefix, env) for env in environments])