Skip to content

Commit

Permalink
fix: mutex for tft init
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Feb 12, 2024
1 parent b098e68 commit 12098ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


apiVersion: v1
kind: Pod
metadata:
Expand Down
14 changes: 14 additions & 0 deletions infra/blueprint-test/pkg/tft/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
gotest "testing"
"time"

Expand Down Expand Up @@ -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.RWMutex // Mutex to protect Terraform plugin cache
}

type tftOption func(*TFBlueprintTest)
Expand Down Expand Up @@ -481,21 +483,33 @@ func (b *TFBlueprintTest) DefaultApply(assert *assert.Assertions) {

// Init runs the default or custom init function for the blueprint.
func (b *TFBlueprintTest) Init(assert *assert.Assertions) {
// allow only single write Terraform plugin cache isn't concurrent safe
b.tftCacheMutex.Lock()
defer b.tftCacheMutex.Unlock()
b.init(assert)
}

// Apply runs the default or custom apply function for the blueprint.
func (b *TFBlueprintTest) Apply(assert *assert.Assertions) {
// allow only parallel reads as Terraform plugin cache isn't concurrent safe
b.tftCacheMutex.RLock()
defer b.tftCacheMutex.RUnlock()
b.apply(assert)
}

// Verify runs the default or custom verify function for the blueprint.
func (b *TFBlueprintTest) Verify(assert *assert.Assertions) {
// allow only parallel reads as Terraform plugin cache isn't concurrent safe
b.tftCacheMutex.RLock()
defer b.tftCacheMutex.RUnlock()
b.verify(assert)
}

// Teardown runs the default or custom teardown function for the blueprint.
func (b *TFBlueprintTest) Teardown(assert *assert.Assertions) {
// allow only parallel reads as Terraform plugin cache isn't concurrent safe
b.tftCacheMutex.RLock()
defer b.tftCacheMutex.RUnlock()
b.teardown(assert)
}

Expand Down
4 changes: 3 additions & 1 deletion infra/blueprint-test/test/krm_simple_blueprint_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"strings"
"testing"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions infra/blueprint-test/test/setup/main.tf
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 12098ee

Please sign in to comment.