Skip to content

Commit

Permalink
Add initial project tests (#46)
Browse files Browse the repository at this point in the history
* modules/project: make prefix optional

* initial project module tests

* modules/project: use null for unset parent
  • Loading branch information
ludoo authored Mar 4, 2020
1 parent f94f82a commit 81654f7
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 17 deletions.
16 changes: 7 additions & 9 deletions modules/project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions modules/project/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,11 @@ variable "parent" {
variable "prefix" {
description = "Prefix used to generate project id and name."
type = string
default = null
}

variable "services" {
description = "Service APIs to enable."
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
}
13 changes: 13 additions & 0 deletions tests/modules/project/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 35 additions & 0 deletions tests/modules/project/fixture/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
85 changes: 85 additions & 0 deletions tests/modules/project/fixture/variables.tf
Original file line number Diff line number Diff line change
@@ -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 = []
}
42 changes: 42 additions & 0 deletions tests/modules/project/test_plan.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 81654f7

Please sign in to comment.