Skip to content

Commit

Permalink
Folder module tests and fixes (#38)
Browse files Browse the repository at this point in the history
* folder tests wip

* modules/folders: tests and tweaks
  • Loading branch information
ludoo authored Feb 19, 2020
1 parent c7fb026 commit b5de802
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
5 changes: 3 additions & 2 deletions modules/folders/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

locals {
folders = [for name in var.names : google_folder.folders[name]]
iam_pairs = flatten([
iam_pairs = var.iam_roles == null ? [] : flatten([
for name, roles in var.iam_roles :
[for role in roles : { name = name, role = role }]
])
iam_keypairs = {
for pair in local.iam_pairs :
"${pair.name}-${pair.role}" => pair
}
iam_members = var.iam_members == null ? {} : var.iam_members
}

resource "google_folder" "folders" {
Expand All @@ -46,7 +47,7 @@ resource "google_folder_iam_binding" "authoritative" {
folder = google_folder.folders[each.value.name].name
role = each.value.role
members = lookup(
lookup(var.iam_members, each.value.name, {}), each.value.role, []
lookup(local.iam_members, each.value.name, {}), each.value.role, []
)
}

Expand Down
4 changes: 2 additions & 2 deletions modules/folders/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
variable "iam_members" {
description = "List of IAM members keyed by folder name and role."
type = map(map(list(string)))
default = {}
default = null
}

variable "iam_roles" {
description = "List of IAM roles keyed by folder name."
type = map(list(string))
default = {}
default = null
}

variable "names" {
Expand Down
13 changes: 13 additions & 0 deletions tests/modules/folders/__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.
23 changes: 23 additions & 0 deletions tests/modules/folders/fixture/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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/folders"
parent = "organizations/12345678"
names = ["folder-a", "folder-b"]
iam_members = var.iam_members
iam_roles = var.iam_roles
}
25 changes: 25 additions & 0 deletions tests/modules/folders/fixture/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 "iam_members" {
type = map(map(list(string)))
default = null
}

variable "iam_roles" {
type = map(list(string))
default = null
}
49 changes: 49 additions & 0 deletions tests/modules/folders/test_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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_folder(plan_runner):
"Test folder resources."
_, resources = plan_runner(FIXTURES_DIR)
assert len(resources) == 2
assert set(r['type'] for r in resources) == set(['google_folder'])
assert set(r['values']['display_name'] for r in resources) == set([
'folder-a', 'folder-b'
])
assert set(r['values']['parent'] for r in resources) == set([
'organizations/12345678'
])


def test_iam_roles_only(plan_runner):
"Test folder resources with only iam roles passed."
_, resources = plan_runner(
FIXTURES_DIR, iam_roles='{folder-a = [ "roles/owner"]}')
assert len(resources) == 3


def test_iam(plan_runner):
"Test folder resources with iam roles and members."
iam_roles = '{folder-a = ["roles/owner"], folder-b = ["roles/viewer"]}'
iam_members = '{folder-a = { "roles/owner" = ["user:a@b.com"] }}'
_, resources = plan_runner(
FIXTURES_DIR, iam_roles=iam_roles, iam_members=iam_members)
assert len(resources) == 4

0 comments on commit b5de802

Please sign in to comment.