Skip to content

Commit

Permalink
cloud-functions v2 - fix reference to bucket_name (#951)
Browse files Browse the repository at this point in the history
* Fix reference to bucket when no bucket_config is provided

* Copy tests to check v2 cloud-functions
  • Loading branch information
wiktorn committed Nov 6, 2022
1 parent eb88d36 commit 0d80ad3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/cloud-function/main.tf
Expand Up @@ -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
}
}
Expand Down
13 changes: 13 additions & 0 deletions 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.
13 changes: 13 additions & 0 deletions 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.
31 changes: 31 additions & 0 deletions 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"]
}
}
20 changes: 20 additions & 0 deletions 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"
}
34 changes: 34 additions & 0 deletions 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'

0 comments on commit 0d80ad3

Please sign in to comment.