diff --git a/modules/pubsub/README.md b/modules/pubsub/README.md index 09fa1b3a..21fb38e4 100644 --- a/modules/pubsub/README.md +++ b/modules/pubsub/README.md @@ -16,7 +16,7 @@ module "pubsub" { "roles/pubsub.subscriber" = ["user:user1@example.com"] } } -# tftest modules=1 resources=3 +# tftest modules=1 resources=3 inventory=simple.yaml ``` ### Topic with schema @@ -48,7 +48,7 @@ module "topic_with_schema" { }) } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=schema.yaml ``` ### Subscriptions @@ -74,7 +74,7 @@ module "pubsub" { } } } -# tftest modules=1 resources=3 +# tftest modules=1 resources=3 inventory=subscriptions.yaml ``` ### Push subscriptions diff --git a/tests/modules/pubsub/examples/bigquery-subscription.yaml b/tests/modules/pubsub/examples/bigquery-subscription.yaml new file mode 100644 index 00000000..c0c20e74 --- /dev/null +++ b/tests/modules/pubsub/examples/bigquery-subscription.yaml @@ -0,0 +1,31 @@ +# Copyright 2023 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. + +values: + module.pubsub.google_pubsub_subscription.default["test-bigquery"]: + bigquery_config: + - drop_unknown_fields: true + table: my_project_id:my_dataset.my_table + use_topic_schema: true + write_metadata: false + name: test-bigquery + project: my-project + topic: my-topic + module.pubsub.google_pubsub_topic.default: + name: my-topic + project: my-project + +counts: + google_pubsub_subscription: 1 + google_pubsub_topic: 1 diff --git a/tests/modules/pubsub/examples/push-subscription.yaml b/tests/modules/pubsub/examples/push-subscription.yaml new file mode 100644 index 00000000..efa3cc1b --- /dev/null +++ b/tests/modules/pubsub/examples/push-subscription.yaml @@ -0,0 +1,30 @@ +# Copyright 2023 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. + +values: + module.pubsub.google_pubsub_subscription.default["test-push"]: + name: test-push + project: my-project + push_config: + - attributes: null + oidc_token: [] + push_endpoint: https://example.com/foo + topic: my-topic + module.pubsub.google_pubsub_topic.default: + name: my-topic + project: my-project + +counts: + google_pubsub_subscription: 1 + google_pubsub_topic: 1 diff --git a/tests/modules/pubsub/examples/schema.yaml b/tests/modules/pubsub/examples/schema.yaml new file mode 100644 index 00000000..7021d110 --- /dev/null +++ b/tests/modules/pubsub/examples/schema.yaml @@ -0,0 +1,29 @@ +# Copyright 2023 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. + +values: + module.topic_with_schema.google_pubsub_schema.default[0]: + definition: '{"fields":[{"name":"StringField","type":"string"},{"name":"FloatField","type":"float"},{"name":"BooleanField","type":"boolean"}],"name":"Avro","type":"record"}' + name: my-topic-schema + project: my-project + type: AVRO + module.topic_with_schema.google_pubsub_topic.default: + name: my-topic + project: my-project + schema_settings: + - encoding: JSON + +counts: + google_pubsub_schema: 1 + google_pubsub_topic: 1 diff --git a/tests/modules/pubsub/examples/simple.yaml b/tests/modules/pubsub/examples/simple.yaml new file mode 100644 index 00000000..51094a51 --- /dev/null +++ b/tests/modules/pubsub/examples/simple.yaml @@ -0,0 +1,36 @@ +# Copyright 2023 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. + +values: + module.pubsub.google_pubsub_topic.default: + name: my-topic + project: my-project + module.pubsub.google_pubsub_topic_iam_binding.default["roles/pubsub.subscriber"]: + condition: [] + members: + - user:user1@example.com + project: my-project + role: roles/pubsub.subscriber + topic: my-topic + module.pubsub.google_pubsub_topic_iam_binding.default["roles/pubsub.viewer"]: + condition: [] + members: + - group:foo@example.com + project: my-project + role: roles/pubsub.viewer + topic: my-topic + +counts: + google_pubsub_topic: 1 + google_pubsub_topic_iam_binding: 2 diff --git a/tests/modules/pubsub/__init__.py b/tests/modules/pubsub/examples/subscription-iam.yaml similarity index 58% rename from tests/modules/pubsub/__init__.py rename to tests/modules/pubsub/examples/subscription-iam.yaml index 6d6d1266..d0fa9fb6 100644 --- a/tests/modules/pubsub/__init__.py +++ b/tests/modules/pubsub/examples/subscription-iam.yaml @@ -1,4 +1,4 @@ -# Copyright 2022 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,3 +11,17 @@ # 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. + +values: + module.pubsub.google_pubsub_subscription_iam_binding.default["test-1.roles/pubsub.subscriber"]: + condition: [] + members: + - user:user1@ludomagno.net + project: my-project + role: roles/pubsub.subscriber + subscription: test-1 + +counts: + google_pubsub_subscription: 1 + google_pubsub_subscription_iam_binding: 1 + google_pubsub_topic: 1 diff --git a/tests/modules/pubsub/examples/subscriptions.yaml b/tests/modules/pubsub/examples/subscriptions.yaml new file mode 100644 index 00000000..a87a6d47 --- /dev/null +++ b/tests/modules/pubsub/examples/subscriptions.yaml @@ -0,0 +1,51 @@ +# Copyright 2023 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. + +values: + module.pubsub.google_pubsub_subscription.default["test-pull"]: + bigquery_config: [] + dead_letter_policy: [] + enable_exactly_once_delivery: null + enable_message_ordering: null + filter: null + labels: null + message_retention_duration: 604800s + name: test-pull + project: my-project + push_config: [] + retain_acked_messages: null + retry_policy: [] + topic: my-topic + module.pubsub.google_pubsub_subscription.default["test-pull-override"]: + bigquery_config: [] + dead_letter_policy: [] + enable_exactly_once_delivery: null + enable_message_ordering: null + filter: null + labels: + test: override + message_retention_duration: 604800s + name: test-pull-override + project: my-project + push_config: [] + retain_acked_messages: true + retry_policy: [] + topic: my-topic + module.pubsub.google_pubsub_topic.default: + name: my-topic + project: my-project + +counts: + google_pubsub_subscription: 2 + google_pubsub_topic: 1 diff --git a/tests/modules/pubsub/fixture/main.tf b/tests/modules/pubsub/fixture/main.tf deleted file mode 100644 index ef068206..00000000 --- a/tests/modules/pubsub/fixture/main.tf +++ /dev/null @@ -1,34 +0,0 @@ -/** - * 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/pubsub" - project_id = "my-project" - regions = ["europe-west1"] - name = "test" - iam = { - "roles/pubsub.publisher" = ["user:me@example.com"] - } - subscriptions = { - test = null - } - subscription_iam = { - test = { - "roles/pubsub.subscriber" = ["user:me@example.com"] - } - } - labels = var.labels -} diff --git a/tests/modules/pubsub/fixture/variables.tf b/tests/modules/pubsub/fixture/variables.tf deleted file mode 100644 index 8371c619..00000000 --- a/tests/modules/pubsub/fixture/variables.tf +++ /dev/null @@ -1,20 +0,0 @@ -/** - * 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 "labels" { - type = map(string) - default = {} -} diff --git a/tests/modules/pubsub/test_plan.py b/tests/modules/pubsub/test_plan.py deleted file mode 100644 index 9fc2ac14..00000000 --- a/tests/modules/pubsub/test_plan.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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) == 4 - - -def test_iam(resources): - "Test IAM binding resources." - bindings = [r['values'] for r in resources if r['type'] - == 'google_pubsub_topic_iam_binding'] - assert len(bindings) == 1 - assert bindings[0]['role'] == 'roles/pubsub.publisher' - - -def test_subscriptions(resources): - "Test subscription resources." - subs = [r['values'] for r in resources if r['type'] - == 'google_pubsub_subscription'] - assert len(subs) == 1 - assert set(s['name'] for s in subs) == set(['test']) - - -def test_subscription_iam(resources): - "Test subscription IAM binding resources." - bindings = [r['values'] for r in resources if r['type'] - == 'google_pubsub_subscription_iam_binding'] - assert len(bindings) == 1 - assert set(b['role'] for b in bindings) == set(['roles/pubsub.subscriber'])