Fix service account tests

This commit is contained in:
Julio Castillo
2020-10-20 23:25:50 +02:00
parent 13ed799a8b
commit d624295702
16 changed files with 108 additions and 98 deletions

View File

@@ -15,11 +15,11 @@
*/
module "test" {
source = "../../../../modules/iam-service-accounts"
source = "../../../../modules/iam-service-account"
project_id = var.project_id
names = ["sa-one", "sa-two", "sa-three"]
name = "sa-one"
prefix = var.prefix
generate_keys = var.generate_keys
generate_key = var.generate_key
iam_members = var.iam_members
iam_roles = var.iam_roles
iam_billing_roles = var.iam_billing_roles

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
variable "generate_keys" {
variable "generate_key" {
type = bool
default = false
}

View File

@@ -0,0 +1,56 @@
# 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_resources(plan_runner):
"Test service account resource."
_, resources = plan_runner(FIXTURES_DIR)
assert len(resources) == 1
resource = resources[0]
assert resource['type'] == 'google_service_account'
assert resource['values']['account_id'] == 'sa-one'
_, resources = plan_runner(FIXTURES_DIR, prefix='foo')
assert len(resources) == 1
resource = resources[0]
assert resource['values']['account_id'] == 'foo-sa-one'
def test_iam_roles(plan_runner):
"Test iam roles with one member."
variables = dict(
iam_roles='["roles/iam.serviceAccountUser"]',
iam_members=(
'{'
'"roles/iam.serviceAccountUser" = ["user:a@b.com"] '
'}')
)
_, resources = plan_runner(FIXTURES_DIR, **variables)
assert len(resources) == 2
iam_resources = [r for r in resources
if r['type'] != 'google_service_account']
assert len(iam_resources) == 1
iam_resource = iam_resources[0]
assert iam_resource['type'] == 'google_service_account_iam_binding'
assert iam_resource['index'] == 'roles/iam.serviceAccountUser'
assert iam_resource['values']['role'] == 'roles/iam.serviceAccountUser'
assert iam_resource['values']['members'] == ["user:a@b.com"]

View File

@@ -1,51 +0,0 @@
# 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_resources(plan_runner):
"Test service account resource."
_, resources = plan_runner(FIXTURES_DIR)
assert len(resources) == 3
assert set(r['type'] for r in resources) == set(['google_service_account'])
assert set(r['values']['account_id'] for r in resources) == set([
'sa-one', 'sa-two', 'sa-three'
])
_, resources = plan_runner(FIXTURES_DIR, prefix='foo')
assert set(r['values']['account_id'] for r in resources) == set([
'foo-sa-one', 'foo-sa-two', 'foo-sa-three'
])
def test_iam_roles(plan_runner):
"Test iam roles with no memmbers."
_, resources = plan_runner(FIXTURES_DIR,
iam_roles='["roles/iam.serviceAccountUser"]')
assert len(resources) == 6
iam_resources = [r for r in resources if r['type']
!= 'google_service_account']
assert len(iam_resources) == 3
assert set(r['type'] for r in iam_resources) == set(
['google_service_account_iam_binding'])
assert [r['index'] for r in iam_resources] == [
'sa-one-roles/iam.serviceAccountUser',
'sa-three-roles/iam.serviceAccountUser',
'sa-two-roles/iam.serviceAccountUser',
]