diff --git a/modules/cloud-function/main.tf b/modules/cloud-function/main.tf index 1ad64f46ee..0a42490480 100644 --- a/modules/cloud-function/main.tf +++ b/modules/cloud-function/main.tf @@ -141,7 +141,7 @@ resource "google_cloudfunctions2_function" "function" { environment_variables = var.environment_variables source { storage_source { - bucket = google_storage_bucket.bucket[0].name + bucket = local.bucket object = google_storage_bucket_object.bundle.name } } diff --git a/tests/modules/cloud_function_v2/__init__.py b/tests/modules/cloud_function_v2/__init__.py new file mode 100644 index 0000000000..6d6d1266c3 --- /dev/null +++ b/tests/modules/cloud_function_v2/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2022 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/cloud_function_v2/fixture/bundle/main.py b/tests/modules/cloud_function_v2/fixture/bundle/main.py new file mode 100644 index 0000000000..6d6d1266c3 --- /dev/null +++ b/tests/modules/cloud_function_v2/fixture/bundle/main.py @@ -0,0 +1,13 @@ +# Copyright 2022 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/cloud_function_v2/fixture/main.tf b/tests/modules/cloud_function_v2/fixture/main.tf new file mode 100644 index 0000000000..db99d523b2 --- /dev/null +++ b/tests/modules/cloud_function_v2/fixture/main.tf @@ -0,0 +1,31 @@ +/** + * Copyright 2022 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/cloud-function" + project_id = "my-project" + name = "test" + bucket_name = var.bucket_name + v2 = true + bundle_config = { + source_dir = "bundle" + output_path = "bundle.zip" + excludes = null + } + iam = { + "roles/cloudfunctions.invoker" = ["allUsers"] + } +} diff --git a/tests/modules/cloud_function_v2/fixture/variables.tf b/tests/modules/cloud_function_v2/fixture/variables.tf new file mode 100644 index 0000000000..600840858c --- /dev/null +++ b/tests/modules/cloud_function_v2/fixture/variables.tf @@ -0,0 +1,20 @@ +/** + * Copyright 2022 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 "bucket_name" { + type = string + default = "test" +} diff --git a/tests/modules/cloud_function_v2/test_plan.py b/tests/modules/cloud_function_v2/test_plan.py new file mode 100644 index 0000000000..de5a06a9d5 --- /dev/null +++ b/tests/modules/cloud_function_v2/test_plan.py @@ -0,0 +1,34 @@ +# Copyright 2022 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 pytest + + +@pytest.fixture +def resources(plan_runner): + _, resources = plan_runner() + return resources + + +def test_resource_count(resources): + "Test number of resources created." + assert len(resources) == 3 + + +def test_iam(resources): + "Test IAM binding resources." + bindings = [r['values'] for r in resources if r['type'] + == 'google_cloudfunctions_function_iam_binding'] + assert len(bindings) == 1 + assert bindings[0]['role'] == 'roles/cloudfunctions.invoker'