From 54ef5f36a28785784678e0b52774f26304038467 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Fri, 9 Feb 2024 19:14:38 +0000 Subject: [PATCH] fix: mutex for tft init --- infra/blueprint-test/examples/simple_krm_blueprint/pod.yaml | 1 - infra/blueprint-test/pkg/tft/terraform.go | 5 +++++ infra/blueprint-test/test/krm_simple_blueprint_test.go | 4 +++- infra/blueprint-test/test/setup/main.tf | 4 ++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/infra/blueprint-test/examples/simple_krm_blueprint/pod.yaml b/infra/blueprint-test/examples/simple_krm_blueprint/pod.yaml index de78753e8ca8..690853cc2ab2 100644 --- a/infra/blueprint-test/examples/simple_krm_blueprint/pod.yaml +++ b/infra/blueprint-test/examples/simple_krm_blueprint/pod.yaml @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - apiVersion: v1 kind: Pod metadata: diff --git a/infra/blueprint-test/pkg/tft/terraform.go b/infra/blueprint-test/pkg/tft/terraform.go index 24647d70aa80..3ab668ff21bd 100644 --- a/infra/blueprint-test/pkg/tft/terraform.go +++ b/infra/blueprint-test/pkg/tft/terraform.go @@ -24,6 +24,7 @@ import ( "path" "path/filepath" "strings" + "sync" gotest "testing" "time" @@ -73,6 +74,7 @@ type TFBlueprintTest struct { verify func(*assert.Assertions) // verify function teardown func(*assert.Assertions) // teardown function setupOutputOverrides map[string]interface{} // override outputs from the Setup phase + tftCacheMutex sync.Mutex // Mutex to protect Terraform plugin cache } type tftOption func(*TFBlueprintTest) @@ -447,7 +449,10 @@ func (b *TFBlueprintTest) DefaultVerify(assert *assert.Assertions) { // DefaultInit runs TF init and validate on a blueprint. func (b *TFBlueprintTest) DefaultInit(assert *assert.Assertions) { + // mutex to prevent parallel calls to Terraform init as plugin cache isn't concurrent safe + b.tftCacheMutex.Lock() terraform.Init(b.t, b.GetTFOptions()) + b.tftCacheMutex.Unlock() // if vars are set for common options, this seems to trigger -var flag when calling validate // using custom tfOptions as a workaround terraform.Validate(b.t, terraform.WithDefaultRetryableErrors(b.t, &terraform.Options{ diff --git a/infra/blueprint-test/test/krm_simple_blueprint_test.go b/infra/blueprint-test/test/krm_simple_blueprint_test.go index f1098dfd80b5..38da3aa0ddaa 100644 --- a/infra/blueprint-test/test/krm_simple_blueprint_test.go +++ b/infra/blueprint-test/test/krm_simple_blueprint_test.go @@ -1,6 +1,7 @@ package test import ( + "strings" "testing" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" @@ -26,7 +27,8 @@ func TestKRMSimpleBlueprint(t *testing.T) { k8sOpts := k8s.KubectlOptions{} op, err := k8s.RunKubectlAndGetOutputE(t, &k8sOpts, "get", "pod", "simple-krm-blueprint", "--no-headers", "-o", "custom-columns=:metadata.name") assert.NoError(err) - assert.Equal("simple-krm-blueprint", op) + result := strings.Split(op, "\n") + assert.Equal("simple-krm-blueprint", result[len(result)-1]) }) networkBlueprint.Test() } diff --git a/infra/blueprint-test/test/setup/main.tf b/infra/blueprint-test/test/setup/main.tf index 0f46477fab44..ebeccc0eba60 100644 --- a/infra/blueprint-test/test/setup/main.tf +++ b/infra/blueprint-test/test/setup/main.tf @@ -1,5 +1,5 @@ /** - * Copyright 2021-2023 Google LLC + * Copyright 2021-2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,6 @@ resource "google_service_account_key" "key" { module "kubernetes-engine_example_simple_autopilot_public" { source = "terraform-google-modules/kubernetes-engine/google//examples/simple_autopilot_public" - version = "~> 26.0" + version = "~> 30.0" project_id = module.project.project_id }