From 81654f71d40e4bc102341fcc8e24bb3b8bd7c9a7 Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Wed, 4 Mar 2020 08:16:10 +0100 Subject: [PATCH] Add initial project tests (#46) * modules/project: make prefix optional * initial project module tests * modules/project: use null for unset parent --- modules/project/main.tf | 16 ++-- modules/project/variables.tf | 9 +-- tests/modules/project/__init__.py | 13 ++++ tests/modules/project/fixture/main.tf | 35 +++++++++ tests/modules/project/fixture/variables.tf | 85 ++++++++++++++++++++++ tests/modules/project/test_plan.py | 42 +++++++++++ 6 files changed, 183 insertions(+), 17 deletions(-) create mode 100644 tests/modules/project/__init__.py create mode 100644 tests/modules/project/fixture/main.tf create mode 100644 tests/modules/project/fixture/variables.tf create mode 100644 tests/modules/project/test_plan.py diff --git a/modules/project/main.tf b/modules/project/main.tf index 0a5e0a5ffb..b684ed0cb0 100644 --- a/modules/project/main.tf +++ b/modules/project/main.tf @@ -31,18 +31,16 @@ locals { } parent_type = split("/", var.parent)[0] parent_id = split("/", var.parent)[1] + prefix = var.prefix == null ? "" : "${var.prefix}-" } resource "google_project" "project" { - org_id = local.parent_type == "organizations" ? local.parent_id : "" - folder_id = local.parent_type == "folders" ? local.parent_id : "" - project_id = "${var.prefix}-${var.name}" - name = "${var.prefix}-${var.name}" - billing_account = var.billing_account - # TODO: Once terraform-providers/terraform-provider-google#3582 is - # fixed, we remove the condition and just use - # var.auto_create_network - auto_create_network = var.prevent_default_network_deletion ? null : var.auto_create_network + org_id = local.parent_type == "organizations" ? local.parent_id : null + folder_id = local.parent_type == "folders" ? local.parent_id : null + project_id = "${local.prefix}${var.name}" + name = "${local.prefix}${var.name}" + billing_account = var.billing_account + auto_create_network = var.auto_create_network labels = var.labels } diff --git a/modules/project/variables.tf b/modules/project/variables.tf index 6cdf2963c3..fb71debd6a 100644 --- a/modules/project/variables.tf +++ b/modules/project/variables.tf @@ -99,6 +99,7 @@ variable "parent" { variable "prefix" { description = "Prefix used to generate project id and name." type = string + default = null } variable "services" { @@ -106,11 +107,3 @@ variable "services" { type = list(string) default = [] } - -# TODO: Once terraform-providers/terraform-provider-google#3582 is -# fixed, we can remove this variable -variable "prevent_default_network_deletion" { - description = "Prevent deletion of default network (use this if your organization has skipDefaultNetworkCreation enforced)" - type = bool - default = false -} diff --git a/tests/modules/project/__init__.py b/tests/modules/project/__init__.py new file mode 100644 index 0000000000..6913f02e36 --- /dev/null +++ b/tests/modules/project/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2020 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/modules/project/fixture/main.tf b/tests/modules/project/fixture/main.tf new file mode 100644 index 0000000000..07a883e99c --- /dev/null +++ b/tests/modules/project/fixture/main.tf @@ -0,0 +1,35 @@ +/** + * Copyright 2020 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 = "../../../../modules/project" + name = "my-project" + billing_account = "12345-12345-12345" + auto_create_network = var.auto_create_network + custom_roles = var.custom_roles + iam_members = var.iam_members + iam_roles = var.iam_roles + iam_nonauth_members = var.iam_nonauth_members + iam_nonauth_roles = var.iam_nonauth_roles + labels = var.labels + lien_reason = var.lien_reason + oslogin = var.oslogin + oslogin_admins = var.oslogin_admins + oslogin_users = var.oslogin_users + parent = var.parent + prefix = var.prefix + services = var.services +} diff --git a/tests/modules/project/fixture/variables.tf b/tests/modules/project/fixture/variables.tf new file mode 100644 index 0000000000..a159af39a7 --- /dev/null +++ b/tests/modules/project/fixture/variables.tf @@ -0,0 +1,85 @@ +/** + * Copyright 2020 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. + */ + +variable "auto_create_network" { + type = bool + default = false +} + +variable "custom_roles" { + type = map(list(string)) + default = {} +} + +variable "iam_members" { + type = map(list(string)) + default = {} +} + +variable "iam_roles" { + type = list(string) + default = [] +} + +variable "iam_nonauth_members" { + type = map(list(string)) + default = {} +} + +variable "iam_nonauth_roles" { + type = list(string) + default = [] +} + +variable "labels" { + type = map(string) + default = {} +} + +variable "lien_reason" { + type = string + default = "" +} + +variable "oslogin" { + type = bool + default = false +} + +variable "oslogin_admins" { + type = list(string) + default = [] +} + +variable "oslogin_users" { + type = list(string) + default = [] +} + +variable "parent" { + type = string + default = "folders/12345678" +} + +variable "prefix" { + type = string + default = null +} + +variable "services" { + type = list(string) + default = [] +} diff --git a/tests/modules/project/test_plan.py b/tests/modules/project/test_plan.py new file mode 100644 index 0000000000..4c5aba825d --- /dev/null +++ b/tests/modules/project/test_plan.py @@ -0,0 +1,42 @@ +# Copyright 2020 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 pytest + + +FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture') + + +def test_prefix(plan_runner): + "Test project id prefix." + _, resources = plan_runner(FIXTURES_DIR) + assert len(resources) == 1 + assert resources[0]['values']['name'] == 'my-project' + _, resources = plan_runner(FIXTURES_DIR, prefix='foo') + assert len(resources) == 1 + assert resources[0]['values']['name'] == 'foo-my-project' + + +def test_parent(plan_runner): + "Test project parent." + _, resources = plan_runner(FIXTURES_DIR) + assert len(resources) == 1 + assert resources[0]['values']['folder_id'] == '12345678' + assert resources[0]['values'].get('org_id') == None + _, resources = plan_runner(FIXTURES_DIR, parent='organizations/12345678') + assert len(resources) == 1 + assert resources[0]['values']['org_id'] == '12345678' + assert resources[0]['values'].get('folder_id') == None