From c819305a42aab60876c2c95a8ac746c186829db8 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Thu, 20 Apr 2023 11:48:14 +0200 Subject: [PATCH 01/11] Migrate apigee tests --- tests/modules/apigee/__init__.py | 13 --- .../{fixture/test.all.tfvars => all.tfvars} | 0 tests/modules/apigee/all.yaml | 83 ++++++++++++++ ...tfvars => endpoint_attachment_only.tfvars} | 0 .../apigee/endpoint_attachment_only.yaml} | 12 +- .../test.env_only.tfvars => env_only.tfvars} | 0 tests/modules/apigee/env_only.yaml | 32 ++++++ ...rs => env_only_with_api_proxy_type.tfvars} | 0 .../apigee/env_only_with_api_proxy_type.yaml | 31 ++++++ ...s => env_only_with_deployment_type.tfvars} | 0 .../apigee/env_only_with_deployment_type.yaml | 34 ++++++ ...group_only.tfvars => envgroup_only.tfvars} | 0 .../apigee/envgroup_only.yaml} | 12 +- tests/modules/apigee/fixture/main.tf | 25 ----- tests/modules/apigee/fixture/variables.tf | 81 -------------- ...tance_only.tfvars => instance_only.tfvars} | 0 .../apigee/instance_only.yaml} | 15 ++- ...o_instances.tfvars => no_instances.tfvars} | 0 tests/modules/apigee/no_instances.yaml | 51 +++++++++ ...n_only.tfvars => organization_only.tfvars} | 0 .../apigee/organization_only.yaml} | 17 ++- tests/modules/apigee/test_plan.py | 95 ---------------- .../test_plan.py => apigee/tftest.yaml} | 22 ++-- tests/modules/binauthz/__init__.py | 13 --- tests/modules/binauthz/fixture/main.tf | 23 ---- tests/modules/binauthz/fixture/variables.tf | 103 ------------------ 26 files changed, 295 insertions(+), 367 deletions(-) delete mode 100644 tests/modules/apigee/__init__.py rename tests/modules/apigee/{fixture/test.all.tfvars => all.tfvars} (100%) create mode 100644 tests/modules/apigee/all.yaml rename tests/modules/apigee/{fixture/test.endpoint_attachment_only.tfvars => endpoint_attachment_only.tfvars} (100%) rename tests/{fast/stages_multitenant/__init__.py => modules/apigee/endpoint_attachment_only.yaml} (60%) rename tests/modules/apigee/{fixture/test.env_only.tfvars => env_only.tfvars} (100%) create mode 100644 tests/modules/apigee/env_only.yaml rename tests/modules/apigee/{fixture/test.env_only_with_api_proxy_type.tfvars => env_only_with_api_proxy_type.tfvars} (100%) create mode 100644 tests/modules/apigee/env_only_with_api_proxy_type.yaml rename tests/modules/apigee/{fixture/test.env_only_with_deployment_type.tfvars => env_only_with_deployment_type.tfvars} (100%) create mode 100644 tests/modules/apigee/env_only_with_deployment_type.yaml rename tests/modules/apigee/{fixture/test.envgroup_only.tfvars => envgroup_only.tfvars} (100%) rename tests/{blueprints/factories/__init__.py => modules/apigee/envgroup_only.yaml} (72%) delete mode 100644 tests/modules/apigee/fixture/main.tf delete mode 100644 tests/modules/apigee/fixture/variables.tf rename tests/modules/apigee/{fixture/test.instance_only.tfvars => instance_only.tfvars} (100%) rename tests/{fast/stages_multitenant/s1_resman_tenant/__init__.py => modules/apigee/instance_only.yaml} (54%) rename tests/modules/apigee/{fixture/test.no_instances.tfvars => no_instances.tfvars} (100%) create mode 100644 tests/modules/apigee/no_instances.yaml rename tests/modules/apigee/{fixture/test.organization_only.tfvars => organization_only.tfvars} (100%) rename tests/{fast/stages_multitenant/s0_bootstrap_tenant/__init__.py => modules/apigee/organization_only.yaml} (57%) delete mode 100644 tests/modules/apigee/test_plan.py rename tests/modules/{binauthz/test_plan.py => apigee/tftest.yaml} (69%) delete mode 100644 tests/modules/binauthz/__init__.py delete mode 100644 tests/modules/binauthz/fixture/main.tf delete mode 100644 tests/modules/binauthz/fixture/variables.tf diff --git a/tests/modules/apigee/__init__.py b/tests/modules/apigee/__init__.py deleted file mode 100644 index 6d6d1266c3..0000000000 --- a/tests/modules/apigee/__init__.py +++ /dev/null @@ -1,13 +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. diff --git a/tests/modules/apigee/fixture/test.all.tfvars b/tests/modules/apigee/all.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.all.tfvars rename to tests/modules/apigee/all.tfvars diff --git a/tests/modules/apigee/all.yaml b/tests/modules/apigee/all.yaml new file mode 100644 index 0000000000..587d6ff589 --- /dev/null +++ b/tests/modules/apigee/all.yaml @@ -0,0 +1,83 @@ +# 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: + google_apigee_endpoint_attachment.endpoint_attachments["endpoint-backend-1"]: + endpoint_attachment_id: endpoint-backend-1 + location: europe-west1 + service_attachment: projects/my-project-1/serviceAttachments/gkebackend1 + google_apigee_endpoint_attachment.endpoint_attachments["endpoint-backend-2"]: + endpoint_attachment_id: endpoint-backend-2 + location: europe-west1 + service_attachment: projects/my-project-2/serviceAttachments/gkebackend2 + google_apigee_envgroup.envgroups["prod"]: + hostnames: + - prod.example.com + name: prod + google_apigee_envgroup.envgroups["test"]: + hostnames: + - test.example.com + name: test + google_apigee_envgroup_attachment.envgroup_attachments["apis-prod-prod"]: + environment: apis-prod + google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]: + environment: apis-test + google_apigee_environment.environments["apis-prod"]: + description: APIs prod + display_name: APIs prod + name: apis-prod + google_apigee_environment.environments["apis-test"]: + description: APIs Test + display_name: APIs test + name: apis-test + google_apigee_environment_iam_binding.binding["apis-prod-roles/viewer"]: + condition: [] + env_id: apis-prod + members: + - group:devops@myorg.com + role: roles/viewer + google_apigee_instance.instances["instance-prod-ew3"]: + description: Terraform-managed + disk_encryption_key_name: null + display_name: null + ip_range: 10.0.6.0/22,10.1.0.16/28 + location: europe-west3 + name: instance-prod-ew3 + google_apigee_instance.instances["instance-test-ew1"]: + description: Terraform-managed + disk_encryption_key_name: null + display_name: null + ip_range: 10.0.4.0/22,10.1.0.0/28 + location: europe-west1 + name: instance-test-ew1 + google_apigee_organization.organization[0]: + analytics_region: europe-west1 + authorized_network: my-vpc + billing_type: Pay-as-you-go + description: null + display_name: null + project_id: my-project + retention: DELETION_RETENTION_UNSPECIFIED + runtime_database_encryption_key_name: '123456789' + runtime_type: CLOUD + +counts: + google_apigee_endpoint_attachment: 2 + google_apigee_envgroup: 2 + google_apigee_envgroup_attachment: 2 + google_apigee_environment: 2 + google_apigee_environment_iam_binding: 1 + google_apigee_instance: 2 + google_apigee_instance_attachment: 2 + google_apigee_organization: 1 diff --git a/tests/modules/apigee/fixture/test.endpoint_attachment_only.tfvars b/tests/modules/apigee/endpoint_attachment_only.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.endpoint_attachment_only.tfvars rename to tests/modules/apigee/endpoint_attachment_only.tfvars diff --git a/tests/fast/stages_multitenant/__init__.py b/tests/modules/apigee/endpoint_attachment_only.yaml similarity index 60% rename from tests/fast/stages_multitenant/__init__.py rename to tests/modules/apigee/endpoint_attachment_only.yaml index 6d6d1266c3..aa67380f4e 100644 --- a/tests/fast/stages_multitenant/__init__.py +++ b/tests/modules/apigee/endpoint_attachment_only.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,13 @@ # 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: + google_apigee_endpoint_attachment.endpoint_attachments["endpoint-backend-1"]: + endpoint_attachment_id: endpoint-backend-1 + location: europe-west1 + org_id: organizations/my-project + service_attachment: projects/my-project-1/serviceAttachments/gkebackend1 + +counts: + google_apigee_endpoint_attachment: 1 diff --git a/tests/modules/apigee/fixture/test.env_only.tfvars b/tests/modules/apigee/env_only.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.env_only.tfvars rename to tests/modules/apigee/env_only.tfvars diff --git a/tests/modules/apigee/env_only.yaml b/tests/modules/apigee/env_only.yaml new file mode 100644 index 0000000000..3a5e1ec233 --- /dev/null +++ b/tests/modules/apigee/env_only.yaml @@ -0,0 +1,32 @@ +# 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: + google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]: + envgroup_id: test + environment: apis-test + timeouts: null + google_apigee_environment.environments["apis-test"]: + description: APIs Test + display_name: APIs test + name: apis-test + node_config: + - max_node_count: '5' + min_node_count: '2' + org_id: organizations/my-project + timeouts: null + +counts: + google_apigee_envgroup_attachment: 1 + google_apigee_environment: 1 diff --git a/tests/modules/apigee/fixture/test.env_only_with_api_proxy_type.tfvars b/tests/modules/apigee/env_only_with_api_proxy_type.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.env_only_with_api_proxy_type.tfvars rename to tests/modules/apigee/env_only_with_api_proxy_type.tfvars diff --git a/tests/modules/apigee/env_only_with_api_proxy_type.yaml b/tests/modules/apigee/env_only_with_api_proxy_type.yaml new file mode 100644 index 0000000000..ba9371ab2d --- /dev/null +++ b/tests/modules/apigee/env_only_with_api_proxy_type.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: + google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]: + envgroup_id: test + environment: apis-test + google_apigee_environment.environments["apis-test"]: + api_proxy_type: PROGRAMMABLE + description: APIs Test + display_name: APIs test + name: apis-test + node_config: + - max_node_count: '5' + min_node_count: '2' + org_id: organizations/my-project + +counts: + google_apigee_envgroup_attachment: 1 + google_apigee_environment: 1 diff --git a/tests/modules/apigee/fixture/test.env_only_with_deployment_type.tfvars b/tests/modules/apigee/env_only_with_deployment_type.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.env_only_with_deployment_type.tfvars rename to tests/modules/apigee/env_only_with_deployment_type.tfvars diff --git a/tests/modules/apigee/env_only_with_deployment_type.yaml b/tests/modules/apigee/env_only_with_deployment_type.yaml new file mode 100644 index 0000000000..f315f46335 --- /dev/null +++ b/tests/modules/apigee/env_only_with_deployment_type.yaml @@ -0,0 +1,34 @@ +# 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: + google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]: + envgroup_id: test + environment: apis-test + timeouts: null + google_apigee_environment.environments["apis-test"]: + deployment_type: ARCHIVE + description: APIs Test + display_name: APIs test + name: apis-test + node_config: + - max_node_count: '5' + min_node_count: '2' + org_id: organizations/my-project + timeouts: null + +counts: + google_apigee_envgroup_attachment: 1 + google_apigee_environment: 1 + diff --git a/tests/modules/apigee/fixture/test.envgroup_only.tfvars b/tests/modules/apigee/envgroup_only.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.envgroup_only.tfvars rename to tests/modules/apigee/envgroup_only.tfvars diff --git a/tests/blueprints/factories/__init__.py b/tests/modules/apigee/envgroup_only.yaml similarity index 72% rename from tests/blueprints/factories/__init__.py rename to tests/modules/apigee/envgroup_only.yaml index 6d6d1266c3..58ee34c085 100644 --- a/tests/blueprints/factories/__init__.py +++ b/tests/modules/apigee/envgroup_only.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,13 @@ # 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: + google_apigee_envgroup.envgroups["test"]: + hostnames: + - test.example.com + name: test + org_id: organizations/my-project + +counts: + google_apigee_envgroup: 1 diff --git a/tests/modules/apigee/fixture/main.tf b/tests/modules/apigee/fixture/main.tf deleted file mode 100644 index 7ab25f733a..0000000000 --- a/tests/modules/apigee/fixture/main.tf +++ /dev/null @@ -1,25 +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/apigee" - project_id = var.project_id - organization = var.organization - envgroups = var.envgroups - environments = var.environments - instances = var.instances - endpoint_attachments = var.endpoint_attachments -} diff --git a/tests/modules/apigee/fixture/variables.tf b/tests/modules/apigee/fixture/variables.tf deleted file mode 100644 index 00961aac2c..0000000000 --- a/tests/modules/apigee/fixture/variables.tf +++ /dev/null @@ -1,81 +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 "endpoint_attachments" { - description = "Endpoint attachments." - type = map(object({ - region = string - service_attachment = string - })) - default = null -} - -variable "envgroups" { - description = "Environment groups (NAME => [HOSTNAMES])." - type = map(list(string)) - default = null -} - -variable "environments" { - description = "Environments." - type = map(object({ - display_name = optional(string) - description = optional(string, "Terraform-managed") - deployment_type = optional(string) - api_proxy_type = optional(string) - node_config = optional(object({ - min_node_count = optional(number) - max_node_count = optional(number) - })) - iam = optional(map(list(string))) - envgroups = list(string) - })) - default = null -} - -variable "instances" { - description = "Instances." - type = map(object({ - display_name = optional(string) - description = optional(string, "Terraform-managed") - region = string - environments = list(string) - runtime_ip_cidr_range = string - troubleshooting_ip_cidr_range = string - disk_encryption_key = optional(string) - consumer_accept_list = optional(list(string)) - })) - default = null -} - -variable "organization" { - description = "Apigee organization. If set to null the organization must already exist." - type = object({ - display_name = optional(string) - description = optional(string, "Terraform-managed") - authorized_network = optional(string) - runtime_type = optional(string, "CLOUD") - billing_type = optional(string) - database_encryption_key = optional(string) - analytics_region = optional(string, "europe-west1") - }) - default = null -} - -variable "project_id" { - description = "Project ID." - type = string -} diff --git a/tests/modules/apigee/fixture/test.instance_only.tfvars b/tests/modules/apigee/instance_only.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.instance_only.tfvars rename to tests/modules/apigee/instance_only.tfvars diff --git a/tests/fast/stages_multitenant/s1_resman_tenant/__init__.py b/tests/modules/apigee/instance_only.yaml similarity index 54% rename from tests/fast/stages_multitenant/s1_resman_tenant/__init__.py rename to tests/modules/apigee/instance_only.yaml index 6d6d1266c3..5150e979cd 100644 --- a/tests/fast/stages_multitenant/s1_resman_tenant/__init__.py +++ b/tests/modules/apigee/instance_only.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,16 @@ # 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: + google_apigee_instance.instances["instance-test-ew1"]: + ip_range: 10.0.4.0/22,10.1.1.0.0/28 + location: europe-west1 + name: instance-test-ew1 + org_id: organizations/my-project + google_apigee_instance_attachment.instance_attachments["instance-test-ew1-apis-test"]: + environment: organizations/my-project/environments/apis-test + +counts: + google_apigee_instance: 1 + google_apigee_instance_attachment: 1 diff --git a/tests/modules/apigee/fixture/test.no_instances.tfvars b/tests/modules/apigee/no_instances.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.no_instances.tfvars rename to tests/modules/apigee/no_instances.tfvars diff --git a/tests/modules/apigee/no_instances.yaml b/tests/modules/apigee/no_instances.yaml new file mode 100644 index 0000000000..ce509047b4 --- /dev/null +++ b/tests/modules/apigee/no_instances.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: + google_apigee_envgroup.envgroups["prod"]: + hostnames: + - prod.example.com + name: prod + google_apigee_envgroup.envgroups["test"]: + hostnames: + - test.example.com + name: test + google_apigee_envgroup_attachment.envgroup_attachments["apis-prod-prod"]: + environment: apis-prod + google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]: + environment: apis-test + google_apigee_environment.environments["apis-prod"]: + description: APIs prod + display_name: APIs prod + name: apis-prod + google_apigee_environment.environments["apis-test"]: + description: APIs Test + display_name: APIs test + name: apis-test + google_apigee_organization.organization[0]: + analytics_region: europe-west1 + authorized_network: my-vpc + billing_type: PAYG + description: null + display_name: null + project_id: my-project + retention: DELETION_RETENTION_UNSPECIFIED + runtime_database_encryption_key_name: '123456789' + runtime_type: CLOUD + +counts: + google_apigee_envgroup: 2 + google_apigee_envgroup_attachment: 2 + google_apigee_environment: 2 + google_apigee_organization: 1 diff --git a/tests/modules/apigee/fixture/test.organization_only.tfvars b/tests/modules/apigee/organization_only.tfvars similarity index 100% rename from tests/modules/apigee/fixture/test.organization_only.tfvars rename to tests/modules/apigee/organization_only.tfvars diff --git a/tests/fast/stages_multitenant/s0_bootstrap_tenant/__init__.py b/tests/modules/apigee/organization_only.yaml similarity index 57% rename from tests/fast/stages_multitenant/s0_bootstrap_tenant/__init__.py rename to tests/modules/apigee/organization_only.yaml index 6d6d1266c3..8eee04fcc0 100644 --- a/tests/fast/stages_multitenant/s0_bootstrap_tenant/__init__.py +++ b/tests/modules/apigee/organization_only.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,18 @@ # 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: + google_apigee_organization.organization[0]: + analytics_region: europe-west1 + authorized_network: my-vpc + billing_type: PAYG + description: null + display_name: null + project_id: my-project + retention: DELETION_RETENTION_UNSPECIFIED + runtime_database_encryption_key_name: '123456789' + runtime_type: CLOUD + +counts: + google_apigee_organization: 1 diff --git a/tests/modules/apigee/test_plan.py b/tests/modules/apigee/test_plan.py deleted file mode 100644 index d16ef2963b..0000000000 --- a/tests/modules/apigee/test_plan.py +++ /dev/null @@ -1,95 +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 collections - -def test_all(plan_runner): - "Test that creates all resources." - _, resources = plan_runner(tf_var_file='test.all.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_organization.organization': 1, - 'google_apigee_envgroup.envgroups': 2, - 'google_apigee_environment.environments': 2, - 'google_apigee_envgroup_attachment.envgroup_attachments': 2, - 'google_apigee_instance.instances': 2, - 'google_apigee_instance_attachment.instance_attachments': 2, - 'google_apigee_endpoint_attachment.endpoint_attachments': 2, - 'google_apigee_environment_iam_binding.binding': 1 - } - -def test_organization_only(plan_runner): - "Test that creates only an organization." - _, resources = plan_runner(tf_var_file='test.organization_only.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_organization.organization': 1 - } - -def test_envgroup_only(plan_runner): - "Test that creates only an environment group in an existing organization." - _, resources = plan_runner(tf_var_file='test.envgroup_only.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_envgroup.envgroups': 1, - } - -def test_env_only(plan_runner): - "Test that creates an environment in an existing environment group." - _, resources = plan_runner(tf_var_file='test.env_only.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_environment.environments': 1, - 'google_apigee_envgroup_attachment.envgroup_attachments': 1, - } - -def test_env_only_with_deployment_type(plan_runner): - "Test that creates an environment in an existing environment group, with deployment_type set." - _, resources = plan_runner(tf_var_file='test.env_only_with_deployment_type.tfvars') - assert [r['values'].get('deployment_type') for r in resources - ] == [None, 'ARCHIVE'] - -def test_env_only_with_api_proxy_type(plan_runner): - "Test that creates an environment in an existing environment group, with api_proxy_type set." - _, resources = plan_runner(tf_var_file='test.env_only_with_api_proxy_type.tfvars') - assert [r['values'].get('api_proxy_type') for r in resources - ] == [None, 'PROGRAMMABLE'] - -def test_instance_only(plan_runner): - "Test that creates only an instance." - _, resources = plan_runner(tf_var_file='test.instance_only.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_instance.instances': 1, - 'google_apigee_instance_attachment.instance_attachments': 1 - } - -def test_endpoint_attachment_only(plan_runner): - "Test that creates only an instance." - _, resources = plan_runner(tf_var_file='test.endpoint_attachment_only.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_endpoint_attachment.endpoint_attachments': 1, - } - -def test_no_instances(plan_runner): - "Test that creates everything but the instances." - _, resources = plan_runner(tf_var_file='test.no_instances.tfvars') - counts = collections.Counter(f'{r["type"]}.{r["name"]}' for r in resources) - assert counts == { - 'google_apigee_organization.organization': 1, - 'google_apigee_envgroup.envgroups': 2, - 'google_apigee_environment.environments': 2, - 'google_apigee_envgroup_attachment.envgroup_attachments': 2, - } diff --git a/tests/modules/binauthz/test_plan.py b/tests/modules/apigee/tftest.yaml similarity index 69% rename from tests/modules/binauthz/test_plan.py rename to tests/modules/apigee/tftest.yaml index 9555042581..9b381d15d2 100644 --- a/tests/modules/binauthz/test_plan.py +++ b/tests/modules/apigee/tftest.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. @@ -12,13 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest +module: modules/apigee -@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 +tests: + all: + endpoint_attachment_only: + env_only: + env_only_with_api_proxy_type: + env_only_with_deployment_type: + envgroup_only: + instance_only: + no_instances: + organization_only: diff --git a/tests/modules/binauthz/__init__.py b/tests/modules/binauthz/__init__.py deleted file mode 100644 index 6d6d1266c3..0000000000 --- a/tests/modules/binauthz/__init__.py +++ /dev/null @@ -1,13 +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. diff --git a/tests/modules/binauthz/fixture/main.tf b/tests/modules/binauthz/fixture/main.tf deleted file mode 100644 index 95f76d634f..0000000000 --- a/tests/modules/binauthz/fixture/main.tf +++ /dev/null @@ -1,23 +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/binauthz" - project_id = var.project_id - global_policy_evaluation_mode = var.global_policy_evaluation_mode - default_admission_rule = var.default_admission_rule - attestors_config = var.attestors_config -} diff --git a/tests/modules/binauthz/fixture/variables.tf b/tests/modules/binauthz/fixture/variables.tf deleted file mode 100644 index 327ced2523..0000000000 --- a/tests/modules/binauthz/fixture/variables.tf +++ /dev/null @@ -1,103 +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 "project_id" { - type = string - default = "my_project" -} - -variable "global_policy_evaluation_mode" { - type = string - default = null -} - -variable "admission_whitelist_patterns" { - type = list(string) - default = [ - "gcr.io/google_containers/*" - ] -} - -variable "default_admission_rule" { - type = object({ - evaluation_mode = string - enforcement_mode = string - attestors = list(string) - }) - default = { - evaluation_mode = "ALWAYS_ALLOW" - enforcement_mode = "ENFORCED_BLOCK_AND_AUDIT_LOG" - attestors = null - } -} - -variable "cluster_admission_rules" { - type = map(object({ - evaluation_mode = string - enforcement_mode = string - attestors = list(string) - })) - default = { - "europe-west1-c.cluster" = { - evaluation_mode = "REQUIRE_ATTESTATION" - enforcement_mode = "ENFORCED_BLOCK_AND_AUDIT_LOG" - attestors = ["test"] - } - } -} - -variable "attestors_config" { - description = "Attestors configuration" - type = map(object({ - note_reference = string - iam = map(list(string)) - pgp_public_keys = list(string) - pkix_public_keys = list(object({ - id = string - public_key_pem = string - signature_algorithm = string - })) - })) - default = { - "test" : { - note_reference = null - pgp_public_keys = [ - < Date: Fri, 21 Apr 2023 14:18:15 +0200 Subject: [PATCH 02/11] Migrate net-ilb tests --- tests/modules/net_ilb/defaults.tfvars | 12 ++++ tests/modules/net_ilb/defaults.yaml | 63 +++++++++++++++++++ tests/modules/net_ilb/fixture/main.tf | 35 ----------- tests/modules/net_ilb/fixture/variables.tf | 61 ------------------ tests/modules/net_ilb/forwarding-rule.tfvars | 14 +++++ tests/modules/net_ilb/forwarding-rule.yaml | 25 ++++++++ tests/modules/net_ilb/test_plan.py | 51 --------------- .../net_ilb/{__init__.py => tftest.yaml} | 8 ++- 8 files changed, 121 insertions(+), 148 deletions(-) create mode 100644 tests/modules/net_ilb/defaults.tfvars create mode 100644 tests/modules/net_ilb/defaults.yaml delete mode 100644 tests/modules/net_ilb/fixture/main.tf delete mode 100644 tests/modules/net_ilb/fixture/variables.tf create mode 100644 tests/modules/net_ilb/forwarding-rule.tfvars create mode 100644 tests/modules/net_ilb/forwarding-rule.yaml delete mode 100644 tests/modules/net_ilb/test_plan.py rename tests/modules/net_ilb/{__init__.py => tftest.yaml} (85%) diff --git a/tests/modules/net_ilb/defaults.tfvars b/tests/modules/net_ilb/defaults.tfvars new file mode 100644 index 0000000000..3671a69dcf --- /dev/null +++ b/tests/modules/net_ilb/defaults.tfvars @@ -0,0 +1,12 @@ +project_id = "my-project" +region = "europe-west1" +name = "ilb-test" +vpc_config = { + network = "default" + subnetwork = "default" +} +backends = [{ + balancing_mode = "CONNECTION" + group = "foo" + failover = false +}] diff --git a/tests/modules/net_ilb/defaults.yaml b/tests/modules/net_ilb/defaults.yaml new file mode 100644 index 0000000000..f66ea2a888 --- /dev/null +++ b/tests/modules/net_ilb/defaults.yaml @@ -0,0 +1,63 @@ +# 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: + google_compute_forwarding_rule.default: + all_ports: true + ip_protocol: TCP + labels: null + load_balancing_scheme: INTERNAL + name: ilb-test + network: default + project: my-project + region: europe-west1 + subnetwork: default + google_compute_health_check.default[0]: + check_interval_sec: 5 + name: ilb-test + project: my-project + tcp_health_check: + - port: null + port_name: null + port_specification: USE_SERVING_PORT + proxy_header: NONE + request: null + response: null + timeout_sec: 5 + unhealthy_threshold: 2 + google_compute_region_backend_service.default: + backend: + - balancing_mode: CONNECTION + capacity_scaler: null + description: Terraform managed. + failover: false + group: foo + max_connections: null + max_connections_per_endpoint: null + max_connections_per_instance: null + max_rate: null + max_rate_per_endpoint: null + max_rate_per_instance: null + max_utilization: null + load_balancing_scheme: INTERNAL + name: ilb-test + network: default + project: my-project + protocol: TCP + region: europe-west1 + +counts: + google_compute_forwarding_rule: 1 + google_compute_health_check: 1 + google_compute_region_backend_service: 1 diff --git a/tests/modules/net_ilb/fixture/main.tf b/tests/modules/net_ilb/fixture/main.tf deleted file mode 100644 index 66346efcba..0000000000 --- a/tests/modules/net_ilb/fixture/main.tf +++ /dev/null @@ -1,35 +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/net-ilb" - project_id = "my-project" - region = "europe-west1" - name = "ilb-test" - vpc_config = { - network = "default" - subnetwork = "default" - } - address = var.address - backend_service_config = var.backend_service_config - backends = var.backends - description = var.description - global_access = var.global_access - group_configs = var.group_configs - ports = var.ports - protocol = var.protocol - service_label = var.service_label -} diff --git a/tests/modules/net_ilb/fixture/variables.tf b/tests/modules/net_ilb/fixture/variables.tf deleted file mode 100644 index 2c2c2fb4ea..0000000000 --- a/tests/modules/net_ilb/fixture/variables.tf +++ /dev/null @@ -1,61 +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 "address" { - type = string - default = null -} - -variable "backend_service_config" { - description = "Backend service level configuration." - type = any - default = {} -} - -variable "backends" { - type = any - default = [] -} - -variable "description" { - type = string - default = "Terraform managed." -} - -variable "global_access" { - type = bool - default = null -} - -variable "group_configs" { - type = any - default = {} -} - -variable "ports" { - type = list(string) - default = null -} - -variable "protocol" { - type = string - default = "TCP" -} - -variable "service_label" { - type = string - default = null -} diff --git a/tests/modules/net_ilb/forwarding-rule.tfvars b/tests/modules/net_ilb/forwarding-rule.tfvars new file mode 100644 index 0000000000..c204090e88 --- /dev/null +++ b/tests/modules/net_ilb/forwarding-rule.tfvars @@ -0,0 +1,14 @@ +project_id = "my-project" +region = "europe-west1" +name = "ilb-test" +vpc_config = { + network = "default" + subnetwork = "default" +} +backends = [{ + balancing_mode = "CONNECTION" + group = "foo" + failover = false +}] +global_access = true +ports = [80] diff --git a/tests/modules/net_ilb/forwarding-rule.yaml b/tests/modules/net_ilb/forwarding-rule.yaml new file mode 100644 index 0000000000..3608afcd16 --- /dev/null +++ b/tests/modules/net_ilb/forwarding-rule.yaml @@ -0,0 +1,25 @@ +# 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: + google_compute_forwarding_rule.default: + all_ports: null + allow_global_access: true + ports: + - '80' + +counts: + google_compute_forwarding_rule: 1 + google_compute_health_check: 1 + google_compute_region_backend_service: 1 diff --git a/tests/modules/net_ilb/test_plan.py b/tests/modules/net_ilb/test_plan.py deleted file mode 100644 index 9956b331b0..0000000000 --- a/tests/modules/net_ilb/test_plan.py +++ /dev/null @@ -1,51 +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. - -_BACKENDS = '[{balancing_mode="CONNECTION", group="foo", failover=false}]' - - -def test_defaults(plan_runner): - "Test variable defaults." - _, resources = plan_runner(backends=_BACKENDS) - assert len(resources) == 3 - resources = dict((r['type'], r['values']) for r in resources) - fwd_rule = resources['google_compute_forwarding_rule'] - assert fwd_rule['load_balancing_scheme'] == 'INTERNAL' - assert fwd_rule['all_ports'] - assert fwd_rule['allow_global_access'] is None - backend = resources['google_compute_region_backend_service'] - assert len(backend['backend']) == 1 - assert backend['backend'][0]['group'] == 'foo' - health_check = resources['google_compute_health_check'] - for k, v in health_check.items(): - if k == 'tcp_health_check': - assert len(v) == 1 - assert v[0]['port_specification'] == 'USE_SERVING_PORT' - elif k.endswith('_health_check'): - assert len(v) == 0 - - -def test_forwarding_rule(plan_runner): - "Test forwarding rule variables." - _, resources = plan_runner(backends=_BACKENDS, global_access='true', - ports="[80]") - assert len(resources) == 3 - values = [ - r['values'] - for r in resources - if r['type'] == 'google_compute_forwarding_rule' - ][0] - assert not values['all_ports'] - assert values['ports'] == ['80'] - assert values['allow_global_access'] diff --git a/tests/modules/net_ilb/__init__.py b/tests/modules/net_ilb/tftest.yaml similarity index 85% rename from tests/modules/net_ilb/__init__.py rename to tests/modules/net_ilb/tftest.yaml index 6d6d1266c3..aa1b8e4a5d 100644 --- a/tests/modules/net_ilb/__init__.py +++ b/tests/modules/net_ilb/tftest.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,9 @@ # 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: modules/net-ilb + +tests: + defaults: + forwarding-rule: From 924230eb12e52672e9a23753c2d3a4ea5c1fd813 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 14:29:48 +0200 Subject: [PATCH 03/11] Migrate gke-hub tests --- modules/gke-hub/README.md | 4 +- tests/modules/gke_hub/__init__.py | 13 --- tests/modules/gke_hub/examples/full.yaml | 71 ++++++++++++++++ .../gke_hub/examples/multi-cluster.yaml | 70 ++++++++++++++++ tests/modules/gke_hub/fixture/main.tf | 25 ------ tests/modules/gke_hub/fixture/variables.tf | 72 ---------------- tests/modules/gke_hub/test_plan.py | 82 ------------------- 7 files changed, 143 insertions(+), 194 deletions(-) delete mode 100644 tests/modules/gke_hub/__init__.py create mode 100644 tests/modules/gke_hub/examples/full.yaml create mode 100644 tests/modules/gke_hub/examples/multi-cluster.yaml delete mode 100644 tests/modules/gke_hub/fixture/main.tf delete mode 100644 tests/modules/gke_hub/fixture/variables.tf delete mode 100644 tests/modules/gke_hub/test_plan.py diff --git a/modules/gke-hub/README.md b/modules/gke-hub/README.md index f895f013a3..8f801f934d 100644 --- a/modules/gke-hub/README.md +++ b/modules/gke-hub/README.md @@ -54,7 +54,7 @@ module "cluster_1" { network = module.vpc.self_link subnetwork = module.vpc.subnet_self_links["europe-west1/cluster-1"] master_authorized_ranges = { - fc1918_10_8 = "10.0.0.0/8" + rfc1918_10_8 = "10.0.0.0/8" } master_ipv4_cidr_block = "192.168.0.0/28" } @@ -119,7 +119,7 @@ module "hub" { } } -# tftest modules=4 resources=16 +# tftest modules=4 resources=16 inventory=full.yaml ``` ## Multi-cluster mesh on GKE diff --git a/tests/modules/gke_hub/__init__.py b/tests/modules/gke_hub/__init__.py deleted file mode 100644 index 6d6d1266c3..0000000000 --- a/tests/modules/gke_hub/__init__.py +++ /dev/null @@ -1,13 +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. diff --git a/tests/modules/gke_hub/examples/full.yaml b/tests/modules/gke_hub/examples/full.yaml new file mode 100644 index 0000000000..8d0861e9ad --- /dev/null +++ b/tests/modules/gke_hub/examples/full.yaml @@ -0,0 +1,71 @@ +# 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.hub.google_gke_hub_feature.default["configmanagement"]: + location: global + name: configmanagement + project: gkehub-test + module.hub.google_gke_hub_feature_membership.default["cluster-1"]: + configmanagement: + - binauthz: [] + config_sync: + - git: + - gcp_service_account_email: null + https_proxy: null + policy_dir: configsync + secret_type: none + sync_branch: main + sync_repo: https://github.com/danielmarzini/configsync-platform-example + sync_rev: null + sync_wait_secs: null + oci: [] + prevent_drift: false + source_format: hierarchy + hierarchy_controller: + - enable_hierarchical_resource_quota: true + enable_pod_tree_labels: true + enabled: true + policy_controller: + - audit_interval_seconds: '120' + enabled: true + exemptable_namespaces: [] + log_denies_enabled: true + mutation_enabled: null + referential_rules_enabled: true + template_library_installed: true + version: v1 + feature: configmanagement + location: global + membership: cluster-1 + mesh: [] + project: gkehub-test + module.hub.google_gke_hub_membership.default["cluster-1"]: + authority: [] + endpoint: + - gke_cluster: + - {} + membership_id: cluster-1 + project: gkehub-test + +counts: + google_compute_network: 1 + google_compute_subnetwork: 1 + google_container_cluster: 1 + google_gke_hub_feature: 1 + google_gke_hub_feature_membership: 1 + google_gke_hub_membership: 1 + google_project: 1 + google_project_service: 7 + google_project_service_identity: 2 diff --git a/tests/modules/gke_hub/examples/multi-cluster.yaml b/tests/modules/gke_hub/examples/multi-cluster.yaml new file mode 100644 index 0000000000..beae2f6785 --- /dev/null +++ b/tests/modules/gke_hub/examples/multi-cluster.yaml @@ -0,0 +1,70 @@ +# 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.hub.google_gke_hub_feature.default["servicemesh"]: + location: global + name: servicemesh + project: gkehub-test + module.hub.google_gke_hub_feature_membership.servicemesh["cluster-1"]: + configmanagement: [] + feature: servicemesh + location: global + membership: cluster-1 + mesh: + - control_plane: null + management: MANAGEMENT_AUTOMATIC + project: gkehub-test + module.hub.google_gke_hub_feature_membership.servicemesh["cluster-2"]: + configmanagement: [] + feature: servicemesh + location: global + membership: cluster-2 + mesh: + - control_plane: null + management: MANAGEMENT_AUTOMATIC + project: gkehub-test + module.hub.google_gke_hub_membership.default["cluster-1"]: + authority: + - {} + endpoint: + - gke_cluster: + - {} + membership_id: cluster-1 + project: gkehub-test + module.hub.google_gke_hub_membership.default["cluster-2"]: + authority: + - {} + endpoint: + - gke_cluster: + - {} + membership_id: cluster-2 + project: gkehub-test + +counts: + google_compute_firewall: 6 + google_compute_network: 1 + google_compute_subnetwork: 3 + google_container_cluster: 2 + google_container_node_pool: 2 + google_gke_hub_feature: 1 + google_gke_hub_feature_membership: 2 + google_gke_hub_membership: 2 + google_project: 1 + google_project_iam_member: 1 + google_project_service: 7 + google_project_service_identity: 2 + google_service_account: 2 + modules: 8 + resources: 32 diff --git a/tests/modules/gke_hub/fixture/main.tf b/tests/modules/gke_hub/fixture/main.tf deleted file mode 100644 index 7ebd010eb3..0000000000 --- a/tests/modules/gke_hub/fixture/main.tf +++ /dev/null @@ -1,25 +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 "hub" { - source = "../../../../modules/gke-hub" - project_id = var.project_id - clusters = var.clusters - features = var.features - configmanagement_templates = var.configmanagement_templates - configmanagement_clusters = var.configmanagement_clusters - workload_identity_clusters = var.workload_identity_clusters -} diff --git a/tests/modules/gke_hub/fixture/variables.tf b/tests/modules/gke_hub/fixture/variables.tf deleted file mode 100644 index 1d76d4f978..0000000000 --- a/tests/modules/gke_hub/fixture/variables.tf +++ /dev/null @@ -1,72 +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 "project_id" { - default = "my-project" -} - -variable "clusters" { - default = { - cluster-1 = "projects/myproject/locations/europe-west1-b/clusters/cluster-1" - cluster-2 = "projects/myproject/locations/europe-west1-b/clusters/cluster-2" - } -} - -variable "features" { - default = { - appdevexperience = false - configmanagement = true - identityservice = false - multiclusteringress = null - servicemesh = true - multiclusterservicediscovery = false - } -} - -variable "configmanagement_templates" { - default = { - default = { - binauthz = false - config_sync = { - git = { - gcp_service_account_email = null - https_proxy = null - policy_dir = "configsync" - secret_type = "ssh" - sync_branch = "main" - sync_repo = "https://github.com/danielmarzini/configsync-platform-example" - sync_rev = null - sync_wait_secs = null - } - prevent_drift = false - source_format = "hierarchy" - } - hierarchy_controller = null - policy_controller = null - version = "1.10.2" - } - } -} - -variable "configmanagement_clusters" { - default = { - default = ["cluster-1", "cluster-2"] - } -} - -variable "workload_identity_clusters" { - default = ["mycluster1", "mycluster2"] -} diff --git a/tests/modules/gke_hub/test_plan.py b/tests/modules/gke_hub/test_plan.py deleted file mode 100644 index 3b847e397f..0000000000 --- a/tests/modules/gke_hub/test_plan.py +++ /dev/null @@ -1,82 +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) == 8 - assert sorted(r['address'] for r in resources) == [ - 'module.hub.google_gke_hub_feature.default["configmanagement"]', - 'module.hub.google_gke_hub_feature.default["servicemesh"]', - 'module.hub.google_gke_hub_feature_membership.default["cluster-1"]', - 'module.hub.google_gke_hub_feature_membership.default["cluster-2"]', - 'module.hub.google_gke_hub_feature_membership.servicemesh["cluster-1"]', - 'module.hub.google_gke_hub_feature_membership.servicemesh["cluster-2"]', - 'module.hub.google_gke_hub_membership.default["cluster-1"]', - 'module.hub.google_gke_hub_membership.default["cluster-2"]' - ] - - -def test_configmanagement_setup(resources): - "Test configuration of configmanagement." - resources = {r['address']: r['values'] for r in resources} - - expected_configmanagement = [{ - 'binauthz': [], - 'config_sync': [{ - 'git': [{ - 'gcp_service_account_email': - None, - 'https_proxy': - None, - 'policy_dir': - 'configsync', - 'secret_type': - 'ssh', - 'sync_branch': - 'main', - 'sync_repo': - 'https://github.com/danielmarzini/configsync-platform-example', - 'sync_rev': - None, - 'sync_wait_secs': - None - }], - 'oci': [], - 'prevent_drift': False, - 'source_format': 'hierarchy' - }], - 'hierarchy_controller': [], - 'policy_controller': [], - 'version': '1.10.2' - }] - - for cluster in ['cluster-1', 'cluster-2']: - membership_key = f'module.hub.google_gke_hub_membership.default["{cluster}"]' - membership = resources[membership_key] - link = membership['endpoint'][0]['gke_cluster'][0]['resource_link'] - assert link == f'//container.googleapis.com/projects/myproject/locations/europe-west1-b/clusters/{cluster}' - - fm_key = f'module.hub.google_gke_hub_feature_membership.default["{cluster}"]' - fm = resources[fm_key] - print(fm['configmanagement']) - assert fm['configmanagement'] == expected_configmanagement From 230a1034ecede5c9653309f6d3af6bf53dba7198 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 14:36:11 +0200 Subject: [PATCH 04/11] Migrate net-address tests --- modules/net-address/README.md | 12 ++-- .../net_address/examples/external.yaml | 35 ++++++++++ .../net_address/examples/internal.yaml | 38 +++++++++++ .../{__init__.py => examples/psa.yaml} | 15 ++++- tests/modules/net_address/examples/psc.yaml | 33 ++++++++++ tests/modules/net_address/fixture/main.tf | 24 ------- tests/modules/net_address/fixture/outputs.tf | 19 ------ .../modules/net_address/fixture/variables.tf | 40 ----------- tests/modules/net_address/test_plan.py | 66 ------------------- 9 files changed, 126 insertions(+), 156 deletions(-) create mode 100644 tests/modules/net_address/examples/external.yaml create mode 100644 tests/modules/net_address/examples/internal.yaml rename tests/modules/net_address/{__init__.py => examples/psa.yaml} (61%) create mode 100644 tests/modules/net_address/examples/psc.yaml delete mode 100644 tests/modules/net_address/fixture/main.tf delete mode 100644 tests/modules/net_address/fixture/outputs.tf delete mode 100644 tests/modules/net_address/fixture/variables.tf delete mode 100644 tests/modules/net_address/test_plan.py diff --git a/modules/net-address/README.md b/modules/net-address/README.md index 23e947cd45..32b660146e 100644 --- a/modules/net-address/README.md +++ b/modules/net-address/README.md @@ -11,12 +11,12 @@ module "addresses" { source = "./fabric/modules/net-address" project_id = var.project_id external_addresses = { - nat-1 = var.region - vpn-remote = var.region + one = "europe-west1" + two = "europe-west2" } global_addresses = ["app-1", "app-2"] } -# tftest modules=1 resources=4 +# tftest modules=1 resources=4 inventory=external.yaml ``` ### Internal addresses @@ -38,7 +38,7 @@ module "addresses" { } } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=internal.yaml ``` ### PSA addresses @@ -55,7 +55,7 @@ module "addresses" { } } } -# tftest modules=1 resources=1 +# tftest modules=1 resources=1 inventory=psa.yaml ``` ### PSC addresses @@ -75,7 +75,7 @@ module "addresses" { } } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=psc.yaml ``` diff --git a/tests/modules/net_address/examples/external.yaml b/tests/modules/net_address/examples/external.yaml new file mode 100644 index 0000000000..f099faf9eb --- /dev/null +++ b/tests/modules/net_address/examples/external.yaml @@ -0,0 +1,35 @@ +# 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.addresses.google_compute_address.external["one"]: + address_type: EXTERNAL + name: one + project: project-id + region: europe-west1 + module.addresses.google_compute_address.external["two"]: + address_type: EXTERNAL + name: two + project: project-id + region: europe-west2 + module.addresses.google_compute_global_address.global["app-1"]: + name: app-1 + project: project-id + module.addresses.google_compute_global_address.global["app-2"]: + name: app-2 + project: project-id + +counts: + google_compute_address: 2 + google_compute_global_address: 2 diff --git a/tests/modules/net_address/examples/internal.yaml b/tests/modules/net_address/examples/internal.yaml new file mode 100644 index 0000000000..ab369cff84 --- /dev/null +++ b/tests/modules/net_address/examples/internal.yaml @@ -0,0 +1,38 @@ +# 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.addresses.google_compute_address.internal["ilb-1"]: + address_type: INTERNAL + labels: null + name: ilb-1 + network: null + prefix_length: null + project: project-id + purpose: SHARED_LOADBALANCER_VIP + region: region + subnetwork: subnet_self_link + module.addresses.google_compute_address.internal["ilb-2"]: + address: 10.0.0.2 + address_type: INTERNAL + labels: null + name: ilb-2 + network: null + prefix_length: null + project: project-id + region: region + subnetwork: subnet_self_link + +counts: + google_compute_address: 2 diff --git a/tests/modules/net_address/__init__.py b/tests/modules/net_address/examples/psa.yaml similarity index 61% rename from tests/modules/net_address/__init__.py rename to tests/modules/net_address/examples/psa.yaml index 6d6d1266c3..d0db33095a 100644 --- a/tests/modules/net_address/__init__.py +++ b/tests/modules/net_address/examples/psa.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,16 @@ # 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.addresses.google_compute_global_address.psa["cloudsql-mysql"]: + address: 10.10.10.0 + address_type: INTERNAL + name: cloudsql-mysql + network: projects/xxx/global/networks/aaa + prefix_length: 24 + project: project-id + purpose: VPC_PEERING + +counts: + google_compute_global_address: 1 diff --git a/tests/modules/net_address/examples/psc.yaml b/tests/modules/net_address/examples/psc.yaml new file mode 100644 index 0000000000..bf998a0b91 --- /dev/null +++ b/tests/modules/net_address/examples/psc.yaml @@ -0,0 +1,33 @@ +# 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.addresses.google_compute_global_address.psc["one"]: + address_type: INTERNAL + name: one + network: projects/xxx/global/networks/aaa + prefix_length: null + project: project-id + purpose: PRIVATE_SERVICE_CONNECT + module.addresses.google_compute_global_address.psc["two"]: + address: 10.0.0.32 + address_type: INTERNAL + name: two + network: projects/xxx/global/networks/aaa + prefix_length: null + project: project-id + purpose: PRIVATE_SERVICE_CONNECT + +counts: + google_compute_global_address: 2 diff --git a/tests/modules/net_address/fixture/main.tf b/tests/modules/net_address/fixture/main.tf deleted file mode 100644 index 4d41b09634..0000000000 --- a/tests/modules/net_address/fixture/main.tf +++ /dev/null @@ -1,24 +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/net-address" - external_addresses = var.external_addresses - global_addresses = var.global_addresses - internal_addresses = var.internal_addresses - psa_addresses = var.psa_addresses - project_id = var.project_id -} diff --git a/tests/modules/net_address/fixture/outputs.tf b/tests/modules/net_address/fixture/outputs.tf deleted file mode 100644 index 523c5274c0..0000000000 --- a/tests/modules/net_address/fixture/outputs.tf +++ /dev/null @@ -1,19 +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. - */ - -output "module" { - value = module.test -} diff --git a/tests/modules/net_address/fixture/variables.tf b/tests/modules/net_address/fixture/variables.tf deleted file mode 100644 index ebc90814f0..0000000000 --- a/tests/modules/net_address/fixture/variables.tf +++ /dev/null @@ -1,40 +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 "external_addresses" { - type = any - default = {} -} - -variable "global_addresses" { - type = any - default = [] -} - -variable "internal_addresses" { - type = any - default = {} -} - -variable "project_id" { - type = string - default = "my-project" -} - -variable "psa_addresses" { - type = any - default = {} -} diff --git a/tests/modules/net_address/test_plan.py b/tests/modules/net_address/test_plan.py deleted file mode 100644 index 2274da6fa7..0000000000 --- a/tests/modules/net_address/test_plan.py +++ /dev/null @@ -1,66 +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. - - -def test_external_addresses(plan_runner): - addresses = '{one = "europe-west1", two = "europe-west2"}' - _, resources = plan_runner(external_addresses=addresses) - assert [r['values']['name'] for r in resources] == ['one', 'two'] - assert set(r['values']['address_type'] for r in resources) == set( - ['EXTERNAL']) - assert [r['values']['region'] for r in resources - ] == ['europe-west1', 'europe-west2'] - - -def test_global_addresses(plan_runner): - _, resources = plan_runner(global_addresses='["one", "two"]') - assert [r['values']['name'] for r in resources] == ['one', 'two'] - assert set(r['values']['address_type'] for r in resources) == set([None]) - - -def test_internal_addresses(plan_runner): - addresses = ('{one = {region = "europe-west1", subnetwork = "foobar"}, ' - 'two = {region = "europe-west2", subnetwork = "foobarz"}}') - _, resources = plan_runner(internal_addresses=addresses) - assert [r['values']['name'] for r in resources] == ['one', 'two'] - assert set(r['values']['address_type'] for r in resources) == set( - ['INTERNAL']) - assert [r['values']['region'] for r in resources - ] == ['europe-west1', 'europe-west2'] - - -def test_internal_addresses_config(plan_runner): - addresses = '''{ - one = { - region = "europe-west1" - subnetwork = "foobar" - address = "10.0.0.2" - purpose = "SHARED_LOADBALANCER_VIP" - }, - two = {region = "europe-west2", subnetwork = "foobarz"} - }''' - _, resources = plan_runner(internal_addresses=addresses) - assert [r['values']['name'] for r in resources] == ['one', 'two'] - assert set(r['values']['address_type'] for r in resources) == set( - ['INTERNAL']) - assert [r['values'].get('address') for r in resources] == ['10.0.0.2', None] - assert [r['values'].get('purpose') for r in resources - ] == ['SHARED_LOADBALANCER_VIP', None] - - -def test_psa_config(plan_runner): - psa_addresses = '{cloudsql-mysql={address="10.199.0.0", network="foobar", prefix_length = 24}}' - _, resources = plan_runner(psa_addresses=psa_addresses) - assert set(r['values']['purpose'] for r in resources) == set(['VPC_PEERING']) - assert set(r['values']['address'] for r in resources) == set(['10.199.0.0']) From 1c7f8f5907eecb02cd2b000f705f00c22f8ebea6 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 14:43:53 +0200 Subject: [PATCH 05/11] Migrate bigquery-dataset tests --- modules/bigquery-dataset/README.md | 12 ++--- .../bigquery_dataset/examples/iam.yaml | 29 ++++++++++++ .../{test_plan.py => examples/options.yaml} | 23 +++++----- .../partitioning.yaml} | 17 ++++++- .../bigquery_dataset/examples/simple.yaml | 46 +++++++++++++++++++ .../bigquery_dataset/examples/tables.yaml | 39 ++++++++++++++++ .../bigquery_dataset/examples/views.yaml | 35 ++++++++++++++ .../modules/bigquery_dataset/fixture/main.tf | 21 --------- 8 files changed, 182 insertions(+), 40 deletions(-) create mode 100644 tests/modules/bigquery_dataset/examples/iam.yaml rename tests/modules/bigquery_dataset/{test_plan.py => examples/options.yaml} (63%) rename tests/modules/bigquery_dataset/{__init__.py => examples/partitioning.yaml} (58%) create mode 100644 tests/modules/bigquery_dataset/examples/simple.yaml create mode 100644 tests/modules/bigquery_dataset/examples/tables.yaml create mode 100644 tests/modules/bigquery_dataset/examples/views.yaml delete mode 100644 tests/modules/bigquery_dataset/fixture/main.tf diff --git a/modules/bigquery-dataset/README.md b/modules/bigquery-dataset/README.md index e7b9333b62..a1b2b2dd07 100644 --- a/modules/bigquery-dataset/README.md +++ b/modules/bigquery-dataset/README.md @@ -35,7 +35,7 @@ module "bigquery-dataset" { view_1 = "my-project|my-dataset|my-table" } } -# tftest modules=1 resources=5 +# tftest modules=1 resources=5 inventory=simple.yaml ``` ### IAM roles @@ -51,7 +51,7 @@ module "bigquery-dataset" { "roles/bigquery.dataOwner" = ["user:user1@example.org"] } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=iam.yaml ``` ### Dataset options @@ -70,7 +70,7 @@ module "bigquery-dataset" { max_time_travel_hours = 168 } } -# tftest modules=1 resources=1 +# tftest modules=1 resources=1 inventory=options.yaml ``` ### Tables and views @@ -100,7 +100,7 @@ module "bigquery-dataset" { } } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=tables.yaml ``` If partitioning is needed, populate the `partitioning` variable using either the `time` or `range` attribute. @@ -132,7 +132,7 @@ module "bigquery-dataset" { } } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=partitioning.yaml ``` To create views use the `view` variable. If you're querying a table created by the same module `terraform apply` will initially fail and eventually succeed once the underlying table has been created. You can probably also use the module's output in the view's query to create a dependency on the table. @@ -170,7 +170,7 @@ module "bigquery-dataset" { } } -# tftest modules=1 resources=3 +# tftest modules=1 resources=3 inventory=views.yaml ``` diff --git a/tests/modules/bigquery_dataset/examples/iam.yaml b/tests/modules/bigquery_dataset/examples/iam.yaml new file mode 100644 index 0000000000..5135ecc69a --- /dev/null +++ b/tests/modules/bigquery_dataset/examples/iam.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.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my-dataset + project: my-project + module.bigquery-dataset.google_bigquery_dataset_iam_binding.bindings["roles/bigquery.dataOwner"]: + condition: [] + dataset_id: my-dataset + members: + - user:user1@example.org + project: my-project + role: roles/bigquery.dataOwner + +counts: + google_bigquery_dataset: 1 + google_bigquery_dataset_iam_binding: 1 diff --git a/tests/modules/bigquery_dataset/test_plan.py b/tests/modules/bigquery_dataset/examples/options.yaml similarity index 63% rename from tests/modules/bigquery_dataset/test_plan.py rename to tests/modules/bigquery_dataset/examples/options.yaml index 362ccca40a..95f2da92d0 100644 --- a/tests/modules/bigquery_dataset/test_plan.py +++ b/tests/modules/bigquery_dataset/examples/options.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. @@ -12,15 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest +values: + module.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my-dataset + default_partition_expiration_ms: null + default_table_expiration_ms: 3600000 + delete_contents_on_destroy: false + location: EU + project: my-project - -@pytest.fixture -def resources(plan_runner): - _, resources = plan_runner() - return resources - - -def test_resource_count(resources): - "Test number of resources created." - assert len(resources) == 1 +counts: + google_bigquery_dataset: 1 diff --git a/tests/modules/bigquery_dataset/__init__.py b/tests/modules/bigquery_dataset/examples/partitioning.yaml similarity index 58% rename from tests/modules/bigquery_dataset/__init__.py rename to tests/modules/bigquery_dataset/examples/partitioning.yaml index 6d6d1266c3..ac7992ea2d 100644 --- a/tests/modules/bigquery_dataset/__init__.py +++ b/tests/modules/bigquery_dataset/examples/partitioning.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,18 @@ # 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.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my-dataset + location: EU + project: my-project + module.bigquery-dataset.google_bigquery_table.default["table_a"]: + time_partitioning: + - field: null + require_partition_filter: null + type: DAY + +counts: + google_bigquery_dataset: 1 + google_bigquery_table: 1 diff --git a/tests/modules/bigquery_dataset/examples/simple.yaml b/tests/modules/bigquery_dataset/examples/simple.yaml new file mode 100644 index 0000000000..acf8e819c6 --- /dev/null +++ b/tests/modules/bigquery_dataset/examples/simple.yaml @@ -0,0 +1,46 @@ +# 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.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my-dataset + description: Terraform managed. + location: EU + project: my-project + module.bigquery-dataset.google_bigquery_dataset_access.group_by_email["reader-group"]: + dataset_id: my-dataset + group_by_email: playground-test@ludomagno.net + project: my-project + role: READER + module.bigquery-dataset.google_bigquery_dataset_access.special_group["project_owners"]: + dataset_id: my-dataset + project: my-project + role: OWNER + special_group: projectOwners + module.bigquery-dataset.google_bigquery_dataset_access.user_by_email["owner"]: + dataset_id: my-dataset + project: my-project + role: OWNER + user_by_email: ludo@ludomagno.net + module.bigquery-dataset.google_bigquery_dataset_access.views["view_1"]: + dataset_id: my-dataset + project: my-project + view: + - dataset_id: my-dataset + project_id: my-project + table_id: my-table + +counts: + google_bigquery_dataset: 1 + google_bigquery_dataset_access: 4 diff --git a/tests/modules/bigquery_dataset/examples/tables.yaml b/tests/modules/bigquery_dataset/examples/tables.yaml new file mode 100644 index 0000000000..c3917788a2 --- /dev/null +++ b/tests/modules/bigquery_dataset/examples/tables.yaml @@ -0,0 +1,39 @@ +# 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.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my_dataset + project: my-project + module.bigquery-dataset.google_bigquery_table.default["countries"]: + clustering: null + dataset_id: my_dataset + deletion_protection: true + description: Terraform managed. + friendly_name: Countries + materialized_view: [] + project: my-project + range_partitioning: [] + schema: '[{"name":"country","type":"STRING"},{"name":"population","type":"INT64"}]' + table_id: countries + time_partitioning: [] + view: [] + +counts: + google_bigquery_dataset: 1 + google_bigquery_table: 1 + modules: 1 + resources: 2 + +outputs: {} diff --git a/tests/modules/bigquery_dataset/examples/views.yaml b/tests/modules/bigquery_dataset/examples/views.yaml new file mode 100644 index 0000000000..0927e574a6 --- /dev/null +++ b/tests/modules/bigquery_dataset/examples/views.yaml @@ -0,0 +1,35 @@ +# 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.bigquery-dataset.google_bigquery_dataset.default: + dataset_id: my_dataset + location: EU + project: my-project + module.bigquery-dataset.google_bigquery_table.default["countries"]: + dataset_id: my_dataset + friendly_name: Countries + module.bigquery-dataset.google_bigquery_table.views["population"]: + dataset_id: my_dataset + deletion_protection: true + friendly_name: Population + project: my-project + table_id: population + view: + - query: SELECT SUM(population) FROM my_dataset.countries + use_legacy_sql: false + +counts: + google_bigquery_dataset: 1 + google_bigquery_table: 2 diff --git a/tests/modules/bigquery_dataset/fixture/main.tf b/tests/modules/bigquery_dataset/fixture/main.tf deleted file mode 100644 index a331411838..0000000000 --- a/tests/modules/bigquery_dataset/fixture/main.tf +++ /dev/null @@ -1,21 +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/bigquery-dataset" - project_id = "my-project" - id = "test" -} From 1649787905c5ca2c955d8ffa72da1da856dfd5ff Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 14:47:09 +0200 Subject: [PATCH 06/11] Migrate kms tests --- modules/kms/README.md | 2 +- tests/modules/kms/__init__.py | 13 --- tests/modules/kms/examples/basic.yaml | 66 ++++++++++++++++ tests/modules/kms/examples/purpose.yaml | 39 +++++++++ tests/modules/kms/fixture/main.tf | 27 ------- tests/modules/kms/fixture/outputs.tf | 19 ----- tests/modules/kms/fixture/variables.tf | 101 ------------------------ tests/modules/kms/test_plan.py | 25 ------ 8 files changed, 106 insertions(+), 186 deletions(-) delete mode 100644 tests/modules/kms/__init__.py create mode 100644 tests/modules/kms/examples/basic.yaml create mode 100644 tests/modules/kms/examples/purpose.yaml delete mode 100644 tests/modules/kms/fixture/main.tf delete mode 100644 tests/modules/kms/fixture/outputs.tf delete mode 100644 tests/modules/kms/fixture/variables.tf delete mode 100644 tests/modules/kms/test_plan.py diff --git a/modules/kms/README.md b/modules/kms/README.md index 3398a595d2..9565c3a9de 100644 --- a/modules/kms/README.md +++ b/modules/kms/README.md @@ -56,7 +56,7 @@ module "kms" { key-c = { rotation_period = null, labels = { env = "test" } } } } -# tftest modules=1 resources=9 +# tftest modules=1 resources=9 inventory=basic.yaml ``` ### Crypto key purpose diff --git a/tests/modules/kms/__init__.py b/tests/modules/kms/__init__.py deleted file mode 100644 index 6d6d1266c3..0000000000 --- a/tests/modules/kms/__init__.py +++ /dev/null @@ -1,13 +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. diff --git a/tests/modules/kms/examples/basic.yaml b/tests/modules/kms/examples/basic.yaml new file mode 100644 index 0000000000..acd7ac2905 --- /dev/null +++ b/tests/modules/kms/examples/basic.yaml @@ -0,0 +1,66 @@ +# 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.kms.google_kms_crypto_key.default["key-a"]: + labels: null + name: key-a + purpose: ENCRYPT_DECRYPT + rotation_period: null + skip_initial_version_creation: null + module.kms.google_kms_crypto_key.default["key-b"]: + labels: null + name: key-b + purpose: ENCRYPT_DECRYPT + rotation_period: 604800s + skip_initial_version_creation: null + module.kms.google_kms_crypto_key.default["key-c"]: + labels: + env: test + name: key-c + purpose: ENCRYPT_DECRYPT + rotation_period: null + skip_initial_version_creation: null + module.kms.google_kms_crypto_key_iam_binding.default["key-a.roles/cloudkms.admin"]: + condition: [] + members: + - user:user3@example.com + role: roles/cloudkms.admin + module.kms.google_kms_crypto_key_iam_member.default["key-b.roles/cloudkms.cryptoKeyEncrypterDecrypteruser:user4@example.com"]: + condition: [] + member: user:user4@example.com + role: roles/cloudkms.cryptoKeyEncrypterDecrypter + module.kms.google_kms_crypto_key_iam_member.default["key-b.roles/cloudkms.cryptoKeyEncrypterDecrypteruser:user5@example.com"]: + condition: [] + member: user:user5@example.com + role: roles/cloudkms.cryptoKeyEncrypterDecrypter + module.kms.google_kms_key_ring.default[0]: + location: europe-west1 + name: test + project: my-project + module.kms.google_kms_key_ring_iam_member.default["roles/cloudkms.cryptoKeyEncrypterDecrypteruser:user1@example.com"]: + condition: [] + member: user:user1@example.com + role: roles/cloudkms.cryptoKeyEncrypterDecrypter + module.kms.google_kms_key_ring_iam_member.default["roles/cloudkms.cryptoKeyEncrypterDecrypteruser:user2@example.com"]: + condition: [] + member: user:user2@example.com + role: roles/cloudkms.cryptoKeyEncrypterDecrypter + +counts: + google_kms_crypto_key: 3 + google_kms_crypto_key_iam_binding: 1 + google_kms_crypto_key_iam_member: 2 + google_kms_key_ring: 1 + google_kms_key_ring_iam_member: 2 diff --git a/tests/modules/kms/examples/purpose.yaml b/tests/modules/kms/examples/purpose.yaml new file mode 100644 index 0000000000..c08779b220 --- /dev/null +++ b/tests/modules/kms/examples/purpose.yaml @@ -0,0 +1,39 @@ +# 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.kms.google_kms_crypto_key.default["key-a"]: + name: key-a + purpose: ENCRYPT_DECRYPT + module.kms.google_kms_crypto_key.default["key-b"]: + name: key-b + purpose: ENCRYPT_DECRYPT + module.kms.google_kms_crypto_key.default["key-c"]: + name: key-c + purpose: ASYMMETRIC_SIGN + version_template: + - algorithm: EC_SIGN_P384_SHA384 + protection_level: SOFTWARE + module.kms.google_kms_key_ring.default[0]: + location: europe-west1 + name: test + project: my-project + +counts: + google_kms_crypto_key: 3 + google_kms_key_ring: 1 + modules: 1 + resources: 4 + +outputs: {} diff --git a/tests/modules/kms/fixture/main.tf b/tests/modules/kms/fixture/main.tf deleted file mode 100644 index 42132f46a2..0000000000 --- a/tests/modules/kms/fixture/main.tf +++ /dev/null @@ -1,27 +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/kms" - iam = var.iam - key_iam = var.key_iam - key_purpose = var.key_purpose - key_purpose_defaults = var.key_purpose_defaults - keyring = var.keyring - keyring_create = var.keyring_create - keys = var.keys - project_id = var.project_id -} diff --git a/tests/modules/kms/fixture/outputs.tf b/tests/modules/kms/fixture/outputs.tf deleted file mode 100644 index 523c5274c0..0000000000 --- a/tests/modules/kms/fixture/outputs.tf +++ /dev/null @@ -1,19 +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. - */ - -output "module" { - value = module.test -} diff --git a/tests/modules/kms/fixture/variables.tf b/tests/modules/kms/fixture/variables.tf deleted file mode 100644 index aa03fd834f..0000000000 --- a/tests/modules/kms/fixture/variables.tf +++ /dev/null @@ -1,101 +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 "iam" { - type = map(list(string)) - default = { - "roles/owner" = ["user:ludo@ludomagno.net"] - } -} - -variable "key_iam" { - type = map(map(list(string))) - default = { - key-a = { - "roles/owner" = ["user:ludo@ludomagno.net"] - } - } -} - -variable "key_purpose" { - type = map(object({ - purpose = string - version_template = object({ - algorithm = string - protection_level = string - }) - })) - default = { - key-b = { - purpose = "ENCRYPT_DECRYPT" - version_template = null - } - key-c = { - purpose = "ASYMMETRIC_SIGN" - version_template = { - algorithm = "EC_SIGN_P384_SHA384" - protection_level = null - } - } - } -} - -variable "key_purpose_defaults" { - type = object({ - purpose = string - version_template = object({ - algorithm = string - protection_level = string - }) - }) - default = { - purpose = null - version_template = null - } -} - -variable "keyring" { - type = object({ - location = string - name = string - }) - default = { - location = "europe-west1" - name = "test-module" - } -} - -variable "keyring_create" { - type = bool - default = true -} - -variable "keys" { - type = map(object({ - rotation_period = string - labels = map(string) - })) - default = { - key-a = null - key-b = { rotation_period = "604800s", labels = null } - key-c = { rotation_period = null, labels = { env = "test" } } - } -} - -variable "project_id" { - type = string - default = "my-project" -} diff --git a/tests/modules/kms/test_plan.py b/tests/modules/kms/test_plan.py deleted file mode 100644 index 79626f3444..0000000000 --- a/tests/modules/kms/test_plan.py +++ /dev/null @@ -1,25 +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. - -def test_resources(plan_runner): - "Test module resources." - _, resources = plan_runner() - assert sorted(r['type'] for r in resources) == [ - 'google_kms_crypto_key', - 'google_kms_crypto_key', - 'google_kms_crypto_key', - 'google_kms_crypto_key_iam_binding', - 'google_kms_key_ring', - 'google_kms_key_ring_iam_binding' - ] From eab9053e166e5059b1a7f8c574a05da33e511dde Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 14:55:24 +0200 Subject: [PATCH 07/11] Migrate billing-budget tests --- modules/billing-budget/README.md | 4 +- tests/modules/billing_budget/__init__.py | 13 ---- .../billing_budget/examples/email.yaml | 58 ++++++++++++++++ .../billing_budget/examples/pubsub.yaml | 39 +++++++++++ tests/modules/billing_budget/fixture/main.tf | 30 -------- .../billing_budget/fixture/variables.tf | 69 ------------------- tests/modules/billing_budget/test_plan.py | 63 ----------------- 7 files changed, 99 insertions(+), 177 deletions(-) delete mode 100644 tests/modules/billing_budget/__init__.py create mode 100644 tests/modules/billing_budget/examples/email.yaml create mode 100644 tests/modules/billing_budget/examples/pubsub.yaml delete mode 100644 tests/modules/billing_budget/fixture/main.tf delete mode 100644 tests/modules/billing_budget/fixture/variables.tf delete mode 100644 tests/modules/billing_budget/test_plan.py diff --git a/modules/billing-budget/README.md b/modules/billing-budget/README.md index d995c0e0ac..3db5faa325 100644 --- a/modules/billing-budget/README.md +++ b/modules/billing-budget/README.md @@ -32,7 +32,7 @@ module "budget" { emails = ["user@example.com"] } } -# tftest modules=1 resources=2 +# tftest modules=1 resources=2 inventory=email.yaml ``` ### Pubsub notification @@ -59,7 +59,7 @@ module "pubsub" { name = "budget-topic" } -# tftest modules=2 resources=2 +# tftest modules=2 resources=2 inventory=pubsub.yaml ``` diff --git a/tests/modules/billing_budget/__init__.py b/tests/modules/billing_budget/__init__.py deleted file mode 100644 index 6d6d1266c3..0000000000 --- a/tests/modules/billing_budget/__init__.py +++ /dev/null @@ -1,13 +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. diff --git a/tests/modules/billing_budget/examples/email.yaml b/tests/modules/billing_budget/examples/email.yaml new file mode 100644 index 0000000000..87c088268c --- /dev/null +++ b/tests/modules/billing_budget/examples/email.yaml @@ -0,0 +1,58 @@ +# 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.budget.google_billing_budget.budget: + all_updates_rule: + - disable_default_iam_recipients: false + pubsub_topic: null + schema_version: '1.0' + amount: + - last_period_amount: null + specified_amount: + - nanos: null + units: '100' + billing_account: 123456-123456-123456 + budget_filter: + - calendar_period: null + credit_types_treatment: INCLUDE_ALL_CREDITS + custom_period: [] + projects: + - projects/123456789000 + - projects/123456789111 + display_name: $100 budget + threshold_rules: + - spend_basis: CURRENT_SPEND + threshold_percent: 0.5 + - spend_basis: CURRENT_SPEND + threshold_percent: 0.75 + - spend_basis: CURRENT_SPEND + threshold_percent: 1 + - spend_basis: FORECASTED_SPEND + threshold_percent: 1 + module.budget.google_monitoring_notification_channel.email_channels["user@example.com"]: + description: null + display_name: $100 budget budget email notification (user@example.com) + enabled: true + force_delete: false + labels: + email_address: user@example.com + project: my-project + sensitive_labels: [] + type: email + user_labels: null + +counts: + google_billing_budget: 1 + google_monitoring_notification_channel: 1 diff --git a/tests/modules/billing_budget/examples/pubsub.yaml b/tests/modules/billing_budget/examples/pubsub.yaml new file mode 100644 index 0000000000..8d09d8a2da --- /dev/null +++ b/tests/modules/billing_budget/examples/pubsub.yaml @@ -0,0 +1,39 @@ +# 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.budget.google_billing_budget.budget: + all_updates_rule: + - disable_default_iam_recipients: false + monitoring_notification_channels: [] + pubsub_topic: projects/project-id/topics/budget-topic + schema_version: '1.0' + amount: + - last_period_amount: true + specified_amount: [] + billing_account: 123456-123456-123456 + budget_filter: + - calendar_period: null + credit_types_treatment: INCLUDE_ALL_CREDITS + custom_period: [] + projects: null + display_name: previous period budget + threshold_rules: + - spend_basis: CURRENT_SPEND + threshold_percent: 1 + timeouts: null + +counts: + google_billing_budget: 1 + google_pubsub_topic: 1 diff --git a/tests/modules/billing_budget/fixture/main.tf b/tests/modules/billing_budget/fixture/main.tf deleted file mode 100644 index 25cd25f577..0000000000 --- a/tests/modules/billing_budget/fixture/main.tf +++ /dev/null @@ -1,30 +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 "budget" { - source = "../../../../modules/billing-budget" - billing_account = "123456-123456-123456" - name = "my budget" - projects = var.projects - services = var.services - notify_default_recipients = var.notify_default_recipients - amount = var.amount - credit_treatment = var.credit_treatment - pubsub_topic = var.pubsub_topic - notification_channels = var.notification_channels - thresholds = var.thresholds - email_recipients = var.email_recipients -} diff --git a/tests/modules/billing_budget/fixture/variables.tf b/tests/modules/billing_budget/fixture/variables.tf deleted file mode 100644 index 0cbcb447f4..0000000000 --- a/tests/modules/billing_budget/fixture/variables.tf +++ /dev/null @@ -1,69 +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 "amount" { - type = number - default = 0 -} - -variable "credit_treatment" { - type = string - default = "INCLUDE_ALL_CREDITS" -} - -variable "email_recipients" { - type = object({ - project_id = string - emails = list(string) - }) - default = null -} - -variable "notification_channels" { - type = list(string) - default = null -} - -variable "notify_default_recipients" { - type = bool - default = false -} - -variable "projects" { - type = list(string) - default = null -} - -variable "pubsub_topic" { - type = string - default = null -} - -variable "services" { - type = list(string) - default = null -} - -variable "thresholds" { - type = object({ - current = list(number) - forecasted = list(number) - }) - default = { - current = [0.5, 1.0] - forecasted = [1.0] - } -} diff --git a/tests/modules/billing_budget/test_plan.py b/tests/modules/billing_budget/test_plan.py deleted file mode 100644 index 6907ab0656..0000000000 --- a/tests/modules/billing_budget/test_plan.py +++ /dev/null @@ -1,63 +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. - -def test_pubsub(plan_runner): - "Test number of resources created." - _, resources = plan_runner(pubsub_topic='topic') - assert len(resources) == 1 - resource = resources[0] - assert resource['values']['all_updates_rule'] == [ - {'disable_default_iam_recipients': False, - 'monitoring_notification_channels': [], - 'pubsub_topic': 'topic', - 'schema_version': '1.0'} - ] - - -def test_channel(plan_runner): - _, resources = plan_runner(notification_channels='["channel"]') - assert len(resources) == 1 - resource = resources[0] - assert resource['values']['all_updates_rule'] == [ - {'disable_default_iam_recipients': True, - 'monitoring_notification_channels': ['channel'], - 'pubsub_topic': None, - 'schema_version': '1.0'} - ] - - -def test_emails(plan_runner): - email_recipients = '{project_id = "project", emails = ["a@b.com", "c@d.com"]}' - _, resources = plan_runner(email_recipients=email_recipients) - assert len(resources) == 3 - - -def test_absolute_amount(plan_runner): - "Test absolute amount budget." - _, resources = plan_runner(pubsub_topic='topic', amount="100") - assert len(resources) == 1 - resource = resources[0] - - amount = resource['values']['amount'][0] - assert amount['last_period_amount'] is None - assert amount['specified_amount'] == [{'nanos': None, 'units': '100'}] - - assert resource['values']['threshold_rules'] == [ - {'spend_basis': 'CURRENT_SPEND', - 'threshold_percent': 0.5}, - {'spend_basis': 'CURRENT_SPEND', - 'threshold_percent': 1}, - {'spend_basis': 'FORECASTED_SPEND', - 'threshold_percent': 1} - ] From cb13d481d6c645f606ed0ed661ffaddcd4d55c12 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 15:51:05 +0200 Subject: [PATCH 08/11] Migrate cloud-function tests --- tests/fixtures.py | 1 + .../{fixture => }/bundle/main.py | 0 tests/modules/cloud_function/common.tfvars | 11 ++++++++ .../cloud_function/fixture/common.tfvars | 12 +++++++++ tests/modules/cloud_function/test_plan.py | 26 +++++++++---------- 5 files changed, 37 insertions(+), 13 deletions(-) rename tests/modules/cloud_function/{fixture => }/bundle/main.py (100%) create mode 100644 tests/modules/cloud_function/common.tfvars create mode 100644 tests/modules/cloud_function/fixture/common.tfvars diff --git a/tests/fixtures.py b/tests/fixtures.py index 381e161e4e..c142a61029 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -143,6 +143,7 @@ def inner(module_path, basedir=None, tf_var_files=None, extra_files=None, **tf_vars): if basedir is None: basedir = Path(request.fspath).parent + print(f"{basedir=}") return plan_summary(module_path=module_path, basedir=basedir, tf_var_files=tf_var_files, extra_files=extra_files, **tf_vars) diff --git a/tests/modules/cloud_function/fixture/bundle/main.py b/tests/modules/cloud_function/bundle/main.py similarity index 100% rename from tests/modules/cloud_function/fixture/bundle/main.py rename to tests/modules/cloud_function/bundle/main.py diff --git a/tests/modules/cloud_function/common.tfvars b/tests/modules/cloud_function/common.tfvars new file mode 100644 index 0000000000..d7c7350ceb --- /dev/null +++ b/tests/modules/cloud_function/common.tfvars @@ -0,0 +1,11 @@ +project_id = "my-project" +name = "test" +bucket_name = "mybucket" +bundle_config = { + source_dir = "../../tests/modules/cloud_function/bundle" + output_path = "bundle.zip" + excludes = null +} +iam = { + "roles/cloudfunctions.invoker" = ["allUsers"] +} diff --git a/tests/modules/cloud_function/fixture/common.tfvars b/tests/modules/cloud_function/fixture/common.tfvars new file mode 100644 index 0000000000..f9fb11f4dd --- /dev/null +++ b/tests/modules/cloud_function/fixture/common.tfvars @@ -0,0 +1,12 @@ +project_id = "my-project" +name = "test" +bucket_name = var.bucket_name +v2 = var.v2 +bundle_config = { + source_dir = "bundle" + output_path = "bundle.zip" + excludes = null +} +iam = { + "roles/cloudfunctions.invoker" = ["allUsers"] +} diff --git a/tests/modules/cloud_function/test_plan.py b/tests/modules/cloud_function/test_plan.py index 019d69ceb8..79f91a2063 100644 --- a/tests/modules/cloud_function/test_plan.py +++ b/tests/modules/cloud_function/test_plan.py @@ -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. @@ -16,29 +16,29 @@ @pytest.fixture -def resources(plan_runner, version): +def resources(plan_summary, plan_runner, version): # convert `version` to a boolean suitable for the `v2` variable v2 = {'v1': 'false', 'v2': 'true'}[version] - _, resources = plan_runner(v2=v2) - return resources + #_, resources = plan_runner(v2=v2) + summary = plan_summary('modules/cloud-function', + tf_var_files=['common.tfvars'], v2=v2) + return summary @pytest.mark.parametrize('version', ['v1', 'v2']) def test_resource_count(resources): "Test number of resources created." - assert len(resources) == 3 + assert resources.counts['resources'] == 3 @pytest.mark.parametrize('version', ['v1', 'v2']) def test_iam(resources, version): "Test IAM binding resources." - - types = { + type = { 'v1': 'google_cloudfunctions_function_iam_binding', 'v2': 'google_cloudfunctions2_function_iam_binding' - } - - bindings = [r['values'] for r in resources if r['type'] == types[version]] - assert len(bindings) == 1 - assert bindings[0]['role'] == 'roles/cloudfunctions.invoker' - assert bindings[0]['members'] == ['allUsers'] + }[version] + key = f'{type}.default["roles/cloudfunctions.invoker"]' + binding = resources.values[key] + assert binding['role'] == 'roles/cloudfunctions.invoker' + assert binding['members'] == ['allUsers'] From df7cf3d278a49bea3ce4cf2a256c0a3f1977e241 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 16:02:34 +0200 Subject: [PATCH 09/11] Migrate compute-mig tests --- modules/compute-mig/README.md | 8 +- tests/modules/cloud_function/test_plan.py | 3 +- .../compute_mig/examples/autoscaling.yaml | 37 +++++ .../compute_mig/examples/health-check.yaml | 43 ++++++ .../{__init__.py => examples/simple.yaml} | 14 +- .../compute_mig/examples/stateful.yaml | 37 +++++ tests/modules/compute_mig/fixture/main.tf | 41 ------ .../modules/compute_mig/fixture/variables.tf | 95 ------------- tests/modules/compute_mig/test_plan.py | 134 ------------------ 9 files changed, 135 insertions(+), 277 deletions(-) create mode 100644 tests/modules/compute_mig/examples/autoscaling.yaml create mode 100644 tests/modules/compute_mig/examples/health-check.yaml rename tests/modules/compute_mig/{__init__.py => examples/simple.yaml} (63%) create mode 100644 tests/modules/compute_mig/examples/stateful.yaml delete mode 100644 tests/modules/compute_mig/fixture/main.tf delete mode 100644 tests/modules/compute_mig/fixture/variables.tf delete mode 100644 tests/modules/compute_mig/test_plan.py diff --git a/modules/compute-mig/README.md b/modules/compute-mig/README.md index cdcec16f6f..5851b6ad2f 100644 --- a/modules/compute-mig/README.md +++ b/modules/compute-mig/README.md @@ -46,7 +46,7 @@ module "nginx-mig" { target_size = 2 instance_template = module.nginx-template.template.self_link } -# tftest modules=2 resources=2 +# tftest modules=2 resources=2 inventory=simple.yaml ``` ### Multiple versions @@ -149,7 +149,7 @@ module "nginx-mig" { } } } -# tftest modules=2 resources=3 +# tftest modules=2 resources=3 inventory=health-check.yaml ``` ### Autoscaling @@ -202,7 +202,7 @@ module "nginx-mig" { } } } -# tftest modules=2 resources=3 +# tftest modules=2 resources=3 inventory=autoscaling.yaml ``` ### Update policy @@ -408,7 +408,7 @@ module "nginx-mig" { } } } -# tftest modules=2 resources=4 +# tftest modules=2 resources=4 inventory=stateful.yaml ``` diff --git a/tests/modules/cloud_function/test_plan.py b/tests/modules/cloud_function/test_plan.py index 79f91a2063..cd2eab9e41 100644 --- a/tests/modules/cloud_function/test_plan.py +++ b/tests/modules/cloud_function/test_plan.py @@ -16,10 +16,9 @@ @pytest.fixture -def resources(plan_summary, plan_runner, version): +def resources(plan_summary, version): # convert `version` to a boolean suitable for the `v2` variable v2 = {'v1': 'false', 'v2': 'true'}[version] - #_, resources = plan_runner(v2=v2) summary = plan_summary('modules/cloud-function', tf_var_files=['common.tfvars'], v2=v2) return summary diff --git a/tests/modules/compute_mig/examples/autoscaling.yaml b/tests/modules/compute_mig/examples/autoscaling.yaml new file mode 100644 index 0000000000..79e85aa04a --- /dev/null +++ b/tests/modules/compute_mig/examples/autoscaling.yaml @@ -0,0 +1,37 @@ +# 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.nginx-mig.google_compute_autoscaler.default[0]: + autoscaling_policy: + - cooldown_period: 30 + cpu_utilization: + - predictive_method: NONE + target: 0.65 + load_balancing_utilization: [] + max_replicas: 3 + metric: [] + min_replicas: 1 + mode: 'ON' + scale_in_control: [] + scaling_schedules: [] + name: mig-test + project: my-project + timeouts: null + zone: europe-west1-b + +counts: + google_compute_autoscaler: 1 + google_compute_instance_group_manager: 1 + google_compute_instance_template: 1 diff --git a/tests/modules/compute_mig/examples/health-check.yaml b/tests/modules/compute_mig/examples/health-check.yaml new file mode 100644 index 0000000000..569e7ddf28 --- /dev/null +++ b/tests/modules/compute_mig/examples/health-check.yaml @@ -0,0 +1,43 @@ +# 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.nginx-mig.google_compute_health_check.default[0]: + check_interval_sec: 5 + grpc_health_check: [] + healthy_threshold: 2 + http2_health_check: [] + http_health_check: + - host: null + port: 80 + port_name: null + port_specification: null + proxy_header: NONE + request_path: / + response: null + https_health_check: [] + log_config: + - enable: true + name: mig-test + project: my-project + ssl_health_check: [] + tcp_health_check: [] + timeout_sec: 5 + timeouts: null + unhealthy_threshold: 2 + +counts: + google_compute_health_check: 1 + google_compute_instance_group_manager: 1 + google_compute_instance_template: 1 diff --git a/tests/modules/compute_mig/__init__.py b/tests/modules/compute_mig/examples/simple.yaml similarity index 63% rename from tests/modules/compute_mig/__init__.py rename to tests/modules/compute_mig/examples/simple.yaml index 6d6d1266c3..92671f0e3f 100644 --- a/tests/modules/compute_mig/__init__.py +++ b/tests/modules/compute_mig/examples/simple.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,15 @@ # 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.nginx-mig.google_compute_instance_group_manager.default[0]: + base_instance_name: mig-test + name: mig-test + project: my-project + target_size: 2 + zone: europe-west1-b + +counts: + google_compute_instance_group_manager: 1 + google_compute_instance_template: 1 diff --git a/tests/modules/compute_mig/examples/stateful.yaml b/tests/modules/compute_mig/examples/stateful.yaml new file mode 100644 index 0000000000..5fcd7ff46c --- /dev/null +++ b/tests/modules/compute_mig/examples/stateful.yaml @@ -0,0 +1,37 @@ +# 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.nginx-mig.google_compute_per_instance_config.default["instance-1"]: + minimal_action: NONE + most_disruptive_allowed_action: REPLACE + name: instance-1 + preserved_state: + - disk: + - delete_rule: NEVER + device_name: persistent-disk-1 + mode: READ_WRITE + source: test-disk + metadata: + foo: bar + project: my-project + remove_instance_state_on_destroy: false + timeouts: null + zone: europe-west1-b + +counts: + google_compute_autoscaler: 1 + google_compute_instance_group_manager: 1 + google_compute_instance_template: 1 + google_compute_per_instance_config: 1 diff --git a/tests/modules/compute_mig/fixture/main.tf b/tests/modules/compute_mig/fixture/main.tf deleted file mode 100644 index b91c0140eb..0000000000 --- a/tests/modules/compute_mig/fixture/main.tf +++ /dev/null @@ -1,41 +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. - */ - -# Used in stateful disk test -resource "google_compute_disk" "default" { - name = "test-disk" - type = "pd-ssd" - zone = "europe-west1-c" - image = "debian-9-stretch-v20200805" - physical_block_size_bytes = 4096 -} - -module "test" { - source = "../../../../modules/compute-mig" - project_id = "my-project" - name = "test-mig" - target_size = 2 - default_version_name = "foo" - instance_template = "foo-template" - location = var.location - autoscaler_config = var.autoscaler_config - health_check_config = var.health_check_config - named_ports = var.named_ports - stateful_config = var.stateful_config - stateful_disks = var.stateful_disks - update_policy = var.update_policy - versions = var.versions -} diff --git a/tests/modules/compute_mig/fixture/variables.tf b/tests/modules/compute_mig/fixture/variables.tf deleted file mode 100644 index 70117838da..0000000000 --- a/tests/modules/compute_mig/fixture/variables.tf +++ /dev/null @@ -1,95 +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 "all_instances_config" { - type = any - default = null -} - -variable "auto_healing_policies" { - type = any - default = null -} - -variable "autoscaler_config" { - type = any - default = null -} - -variable "default_version_name" { - type = any - default = "default" -} - -variable "description" { - type = any - default = "Terraform managed." -} - -variable "distribution_policy" { - type = any - default = null -} - -variable "health_check_config" { - type = any - default = null -} - -variable "location" { - type = any - default = "europe-west1-b" -} - -variable "named_ports" { - type = any - default = null -} - -variable "stateful_disks" { - type = any - default = {} -} - -variable "stateful_config" { - type = any - default = {} -} - -variable "target_pools" { - type = any - default = [] -} - -variable "target_size" { - type = any - default = null -} - -variable "update_policy" { - type = any - default = null -} - -variable "versions" { - type = any - default = {} -} - -variable "wait_for_instances" { - type = any - default = null -} diff --git a/tests/modules/compute_mig/test_plan.py b/tests/modules/compute_mig/test_plan.py deleted file mode 100644 index 7fec3c1bd2..0000000000 --- a/tests/modules/compute_mig/test_plan.py +++ /dev/null @@ -1,134 +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. - - -def test_defaults(plan_runner): - "Test variable defaults." - _, resources = plan_runner() - assert len(resources) == 1 - print(resources[0]['type']) - mig = resources[0] - assert mig['type'] == 'google_compute_instance_group_manager' - assert mig['values']['target_size'] == 2 - assert mig['values']['zone'] - _, resources = plan_runner(location='"europe-west1"') - assert len(resources) == 1 - mig = resources[0] - assert mig['type'] == 'google_compute_region_instance_group_manager' - assert mig['values']['target_size'] == 2 - assert mig['values']['region'] - - -def test_health_check(plan_runner): - "Test health check resource." - health_check_config = '''{ - enable_logging = true - tcp = { - port = 80 - } - }''' - _, resources = plan_runner(health_check_config=health_check_config) - assert len(resources) == 2 - assert any(r['type'] == 'google_compute_health_check' for r in resources) - - -def test_autoscaler(plan_runner): - "Test autoscaler resource." - autoscaler_config = '''{ - colldown_period = 60 - max_replicas = 3 - min_replicas = 1 - scaling_signals = { - cpu_utilization = { - target = 65 - } - } - }''' - _, resources = plan_runner(autoscaler_config=autoscaler_config) - assert len(resources) == 2 - autoscaler = resources[0] - assert autoscaler['type'] == 'google_compute_autoscaler' - assert autoscaler['values']['autoscaling_policy'] == [{ - 'cooldown_period': 60, - 'cpu_utilization': [{ - 'predictive_method': 'NONE', - 'target': 65 - }], - 'load_balancing_utilization': [], - 'max_replicas': 3, - 'metric': [], - 'min_replicas': 1, - 'mode': 'ON', - 'scale_in_control': [], - 'scaling_schedules': [], - }] - _, resources = plan_runner(autoscaler_config=autoscaler_config, - location='"europe-west1"') - assert len(resources) == 2 - autoscaler = resources[0] - assert autoscaler['type'] == 'google_compute_region_autoscaler' - - -def test_stateful_mig(plan_runner): - "Test stateful instances - mig." - - stateful_disks = '''{ - persistent-disk-1 = false - }''' - _, resources = plan_runner(stateful_disks=stateful_disks) - assert len(resources) == 1 - statefuldisk = resources[0] - assert statefuldisk['type'] == 'google_compute_instance_group_manager' - assert statefuldisk['values']['stateful_disk'] == [{ - 'device_name': 'persistent-disk-1', - 'delete_rule': 'NEVER', - }] - - -def test_stateful_instance(plan_runner): - "Test stateful instances - instance." - stateful_config = '''{ - instance-1 = { - most_disruptive_action = "REPLACE", - preserved_state = { - disks = { - persistent-disk-1 = { - source = "test-disk" - } - } - metadata = { foo = "bar" } - } - } - }''' - _, resources = plan_runner(stateful_config=stateful_config) - assert len(resources) == 2 - instanceconfig = resources[0] - assert instanceconfig['type'] == 'google_compute_instance_group_manager' - instanceconfig = resources[1] - assert instanceconfig['type'] == 'google_compute_per_instance_config' - - assert instanceconfig['values']['preserved_state'] == [{ - 'disk': [{ - 'device_name': 'persistent-disk-1', - 'delete_rule': 'NEVER', - 'source': 'test-disk', - 'mode': 'READ_WRITE', - }], - 'metadata': { - 'foo': 'bar' - } - }] - assert instanceconfig['values']['minimal_action'] == 'NONE' - assert instanceconfig['values']['most_disruptive_allowed_action'] == 'REPLACE' - assert instanceconfig['values']['remove_instance_state_on_destroy'] == False From 016a4e08ae0307edcb0f4da987199e5850dfabc9 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 17:09:10 +0200 Subject: [PATCH 10/11] fix fast tftest directives --- fast/stages-multitenant/0-bootstrap-tenant/README.md | 5 ++--- fast/stages-multitenant/1-resman-tenant/README.md | 2 +- fast/stages/1-resman/README.md | 2 +- fast/stages/2-networking-a-peering/README.md | 5 ++--- fast/stages/2-networking-b-vpn/README.md | 5 ++--- fast/stages/2-networking-c-nva/README.md | 5 ++--- fast/stages/2-networking-d-separate-envs/README.md | 5 ++--- fast/stages/2-networking-e-nva-bgp/README.md | 5 ++--- fast/stages/2-security/README.md | 2 +- 9 files changed, 15 insertions(+), 21 deletions(-) diff --git a/fast/stages-multitenant/0-bootstrap-tenant/README.md b/fast/stages-multitenant/0-bootstrap-tenant/README.md index 76e36795ec..9dae91d925 100644 --- a/fast/stages-multitenant/0-bootstrap-tenant/README.md +++ b/fast/stages-multitenant/0-bootstrap-tenant/README.md @@ -85,7 +85,7 @@ As shown in the script output above, the provider file is a template used as a s Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [org-level bootstrap stage documentation](../../stages/0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -97,7 +97,7 @@ The globals variable file linked above contains definition which were set for th The tenant configuration resides in the `tenant_config` variable, this is an example configuration for a tenant with comments explaining the different choices that need to be made: -```hcl +```tfvars tenant_config = { # used for the top-level folder name descriptive_name = "My First Tenant" @@ -142,7 +142,6 @@ tenant_config = { # logging = "folders/0123456789" # } } -# tftest skip ``` Configure the tenant variable in a tfvars file for this stage. A few minor points worth noting: diff --git a/fast/stages-multitenant/1-resman-tenant/README.md b/fast/stages-multitenant/1-resman-tenant/README.md index 1b5df86996..9e1188bba6 100644 --- a/fast/stages-multitenant/1-resman-tenant/README.md +++ b/fast/stages-multitenant/1-resman-tenant/README.md @@ -116,7 +116,7 @@ Once that is done, stage-level configuration variables are the same as the corre Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [org-level bootstrap stage documentation](../../stages/0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` diff --git a/fast/stages/1-resman/README.md b/fast/stages/1-resman/README.md index 6ef900f1b8..c2c32dda21 100644 --- a/fast/stages/1-resman/README.md +++ b/fast/stages/1-resman/README.md @@ -112,7 +112,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` diff --git a/fast/stages/2-networking-a-peering/README.md b/fast/stages/2-networking-a-peering/README.md index 9b043eb5e0..f3c7c9511b 100644 --- a/fast/stages/2-networking-a-peering/README.md +++ b/fast/stages/2-networking-a-peering/README.md @@ -253,7 +253,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -315,7 +315,7 @@ This stage includes basic support for an HA VPN connecting the landing zone in t Support for the onprem VPN is disabled by default so that no resources are created, this is an example of how to configure the variable to enable the VPN: -```hcl +```tfvars vpn_onprem_primary_config = { peer_external_gateways = { default = { @@ -357,7 +357,6 @@ vpn_onprem_primary_config = { } } } -# tftest skip ``` ### Adding an environment diff --git a/fast/stages/2-networking-b-vpn/README.md b/fast/stages/2-networking-b-vpn/README.md index ed52715df3..ab1b9734cb 100644 --- a/fast/stages/2-networking-b-vpn/README.md +++ b/fast/stages/2-networking-b-vpn/README.md @@ -267,7 +267,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -329,7 +329,7 @@ This stage includes basic support for an HA VPN connecting the landing zone in t Support for the onprem VPN is disabled by default so that no resources are created, this is an example of how to configure the variable to enable the VPN: -```hcl +```tfvars vpn_onprem_primary_config = { peer_external_gateways = { default = { @@ -371,7 +371,6 @@ vpn_onprem_primary_config = { } } } -# tftest skip ``` ### Adding an environment diff --git a/fast/stages/2-networking-c-nva/README.md b/fast/stages/2-networking-c-nva/README.md index 43bc5daf82..5aeefa29fb 100644 --- a/fast/stages/2-networking-c-nva/README.md +++ b/fast/stages/2-networking-c-nva/README.md @@ -335,7 +335,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -397,7 +397,7 @@ This stage includes basic support for an HA VPN connecting the landing zone in t Support for the onprem VPNs is disabled by default so that no resources are created, this is an example of how to configure one variable to enable the VPN in the primary region: -```hcl +```tfvars vpn_onprem_primary_config = { peer_external_gateways = { default = { @@ -439,7 +439,6 @@ vpn_onprem_primary_config = { } } } -# tftest skip ``` ### Adding an environment diff --git a/fast/stages/2-networking-d-separate-envs/README.md b/fast/stages/2-networking-d-separate-envs/README.md index 596d427a9a..a718cda8a5 100644 --- a/fast/stages/2-networking-d-separate-envs/README.md +++ b/fast/stages/2-networking-d-separate-envs/README.md @@ -215,7 +215,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -270,7 +270,7 @@ This stage includes basic support for an HA VPN connecting each environment land Support for the onprem VPNs is disabled by default so that no resources are created, this is an example of how to configure one variable to enable the VPN for dev in the primary region: -```hcl +```tfvars vpn_onprem_dev_primary_config = { peer_external_gateways = { default = { @@ -312,7 +312,6 @@ vpn_onprem_dev_primary_config = { } } } -# tftest skip ``` ### Changing default regions diff --git a/fast/stages/2-networking-e-nva-bgp/README.md b/fast/stages/2-networking-e-nva-bgp/README.md index adb68d8977..ea1e2ec56e 100644 --- a/fast/stages/2-networking-e-nva-bgp/README.md +++ b/fast/stages/2-networking-e-nva-bgp/README.md @@ -357,7 +357,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` @@ -419,7 +419,7 @@ This stage includes basic support for an HA VPN connecting the landing zone in t Support for the onprem VPNs is disabled by default so that no resources are created, this is an example of how to configure one variable to enable the VPN in the primary region: -```hcl +```tfvars vpn_onprem_primary_config = { peer_external_gateways = { default = { @@ -461,7 +461,6 @@ vpn_onprem_primary_config = { } } } -# tftest skip ``` ### Adding an environment diff --git a/fast/stages/2-security/README.md b/fast/stages/2-security/README.md index f6c966b8dc..66f726b5f1 100644 --- a/fast/stages/2-security/README.md +++ b/fast/stages/2-security/README.md @@ -110,7 +110,7 @@ The latter set is explained in the [Customization](#customizations) sections bel Note that the `outputs_location` variable is disabled by default, you need to explicitly set it in your `terraform.tfvars` file if you want output files to be generated by this stage. This is a sample `terraform.tfvars` that configures it, refer to the [bootstrap stage documentation](../0-bootstrap/README.md#output-files-and-cross-stage-variables) for more details: -```hcl +```tfvars outputs_location = "~/fast-config" ``` From e61df0aa3bd1406205b678cece0cd299f713d61c Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Fri, 21 Apr 2023 17:52:30 +0200 Subject: [PATCH 11/11] Deprecate plan runner fixture and all its variants --- tests/legacy_fixtures.py | 70 +--------------------------------------- 1 file changed, 1 insertion(+), 69 deletions(-) diff --git a/tests/legacy_fixtures.py b/tests/legacy_fixtures.py index 8b23ea5eee..593709e430 100644 --- a/tests/legacy_fixtures.py +++ b/tests/legacy_fixtures.py @@ -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. @@ -28,74 +28,6 @@ BASEDIR = os.path.dirname(os.path.dirname(__file__)) -@pytest.fixture(scope='session') -def _plan_runner(): - 'Return a function to run Terraform plan on a fixture.' - - def run_plan(fixture_path=None, extra_files=None, tf_var_file=None, - targets=None, refresh=True, tmpdir=True, **tf_vars): - 'Run Terraform plan and returns parsed output.' - if fixture_path is None: - # find out the fixture directory from the caller's directory - caller = inspect.stack()[2] - fixture_path = os.path.join(os.path.dirname(caller.filename), 'fixture') - - fixture_parent = os.path.dirname(fixture_path) - fixture_prefix = os.path.basename(fixture_path) + '_' - with tempfile.TemporaryDirectory(prefix=fixture_prefix, - dir=fixture_parent) as tmp_path: - # copy fixture to a temporary directory so we can execute - # multiple tests in parallel - if tmpdir: - shutil.copytree(fixture_path, tmp_path, dirs_exist_ok=True) - tf = tftest.TerraformTest(tmp_path if tmpdir else fixture_path, BASEDIR, - os.environ.get('TERRAFORM', 'terraform')) - tf.setup(extra_files=extra_files, upgrade=True) - plan = tf.plan(output=True, refresh=refresh, tf_var_file=tf_var_file, - tf_vars=tf_vars, targets=targets) - return plan - - return run_plan - - -@pytest.fixture(scope='session') -def plan_runner(_plan_runner): - 'Return a function to run Terraform plan on a module fixture.' - - def run_plan(fixture_path=None, extra_files=None, tf_var_file=None, - targets=None, **tf_vars): - 'Run Terraform plan and returns plan and module resources.' - plan = _plan_runner(fixture_path, extra_files=extra_files, - tf_var_file=tf_var_file, targets=targets, **tf_vars) - # skip the fixture - root_module = plan.root_module['child_modules'][0] - return plan, root_module['resources'] - - return run_plan - - -@pytest.fixture(scope='session') -def e2e_plan_runner(_plan_runner): - 'Return a function to run Terraform plan on an end-to-end fixture.' - - def run_plan(fixture_path=None, tf_var_file=None, targets=None, refresh=True, - include_bare_resources=False, **tf_vars): - 'Run Terraform plan on an end-to-end module using defaults, returns data.' - plan = _plan_runner(fixture_path, tf_var_file=tf_var_file, targets=targets, - refresh=refresh, **tf_vars) - # skip the fixture - root_module = plan.root_module['child_modules'][0] - modules = dict((mod['address'], mod['resources']) - for mod in root_module['child_modules']) - resources = [r for m in modules.values() for r in m] - if include_bare_resources: - bare_resources = root_module['resources'] - resources.extend(bare_resources) - return modules, resources - - return run_plan - - @pytest.fixture(scope='session') def apply_runner(): 'Return a function to run Terraform apply on a fixture.'