Skip to content

Commit

Permalink
feat: new test strategy for redeploy validation (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
bharathkkb committed Nov 18, 2022
1 parent 53ae656 commit de5d509
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions infra/blueprint-test/examples/simple_pet_module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "random_pet" "hello" {
}
20 changes: 20 additions & 0 deletions infra/blueprint-test/pkg/tft/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,23 @@ func (b *TFBlueprintTest) Test() {
utils.RunStage("apply", func() { b.Apply(a) })
utils.RunStage("verify", func() { b.Verify(a) })
}

// RedeployTest deploys the test n times in separate workspaces before teardown.
func (b *TFBlueprintTest) RedeployTest(n int) {
if b.ShouldSkip() {
b.logger.Logf(b.t, "Skipping test due to config %s", b.Path)
b.t.SkipNow()
return
}
a := assert.New(b.t)
for i := 1; i <= n; i++ {
ws := terraform.WorkspaceSelectOrNew(b.t, b.GetTFOptions(), fmt.Sprintf("test-%d", i))
utils.RunStage("init", func() { b.Init(a) })
defer func() {
terraform.WorkspaceSelectOrNew(b.t, b.GetTFOptions(), ws)
utils.RunStage("teardown", func() { b.Teardown(a) })
}()
utils.RunStage("apply", func() { b.Apply(a) })
utils.RunStage("verify", func() { b.Verify(a) })
}
}
36 changes: 36 additions & 0 deletions infra/blueprint-test/test/terraform_redeploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.
*/

package test

import (
"testing"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestRedeploy(t *testing.T) {
nt := tft.NewTFBlueprintTest(t,
tft.WithTFDir("../examples/simple_pet_module"),
tft.WithSetupPath(""),
)
nt.RedeployTest(3)
expectedWorkspaces := []string{"test-1", "test-2", "test-3"}
for _, ws := range expectedWorkspaces {
terraform.RunTerraformCommand(t, nt.GetTFOptions(), "workspace", "select", ws)
}
}

0 comments on commit de5d509

Please sign in to comment.