Skip to content

Commit

Permalink
Removing any configuration or variables files from previous test step…
Browse files Browse the repository at this point in the history
…s prior to copy configuration and variables for current test step (#150)
  • Loading branch information
bendbennett committed Jul 31, 2023
1 parent 3197257 commit a8cab58
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = 8

numeric = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = 9

numeric = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

provider "random" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "random_password" "test" {
length = var.length

numeric = var.numeric
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

variable "length" {
type = number
}

variable "numeric" {
type = bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
version = "3.2.0"
}
}
}
Expand Down
56 changes: 54 additions & 2 deletions helper/resource/teststep_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2335,15 +2335,17 @@ func TestTest_ConfigDirectory_TestStepDirectory(t *testing.T) {
}

// TestTest_ConfigDirectory_TestStepDirectory_StepNotHardcoded uses a multistep test
// to prove that the test step number is not hardcoded
// to prove that the test step number is not hardcoded and to show that the
// configuration files that are copied from the test step directory in test step 1
// are removed prior to running test step 2.
func TestTest_ConfigDirectory_TestStepDirectory_StepNotHardcoded(t *testing.T) {
t.Parallel()

Test(t, TestCase{
Steps: []TestStep{
{
ConfigDirectory: config.TestStepDirectory(),
Check: TestCheckResourceAttrPtr("random_password.test", "length", teststep.Pointer("8")),
ExpectError: regexp.MustCompile(`.*An argument named "numeric" is not expected here.`),
},
{
ConfigDirectory: config.TestStepDirectory(),
Expand Down Expand Up @@ -2443,6 +2445,27 @@ func TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles(t *testing.T) {
})
}

// TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_StepNotHardcoded uses a
// multistep test to prove that the test step number is not hardcoded, and to show
// that the configuration files that are copied from the test step directory in test
// step 1 are removed prior to running test step 2.
func TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_StepNotHardcoded(t *testing.T) {
t.Parallel()

Test(t, TestCase{
Steps: []TestStep{
{
ConfigDirectory: config.TestStepDirectory(),
ExpectError: regexp.MustCompile(`.*An argument named "numeric" is not expected here.`),
},
{
ConfigDirectory: config.TestStepDirectory(),
Check: TestCheckResourceAttrPtr("random_password.test", "length", teststep.Pointer("9")),
},
},
})
}

func TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars(t *testing.T) {
t.Parallel()

Expand All @@ -2460,6 +2483,35 @@ func TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars(t *testing.T)
})
}

// TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars_StepNotHardcoded uses a
// multistep test to prove that the test step number is not hardcoded, and to show
// that the configuration files that are copied from the test step directory in test
// step 1 are removed prior to running test step 2.
func TestTest_ConfigDirectory_TestStepDirectory_MultipleFiles_Vars_StepNotHardcoded(t *testing.T) {
t.Parallel()

Test(t, TestCase{
Steps: []TestStep{
{
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: config.Variables{
"length": config.IntegerVariable(8),
"numeric": config.BoolVariable(false),
},
ExpectError: regexp.MustCompile(`.*An argument named "numeric" is not expected here.`),
},
{
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: config.Variables{
"length": config.IntegerVariable(9),
"numeric": config.BoolVariable(false),
},
Check: TestCheckResourceAttrPtr("random_password.test", "length", teststep.Pointer("9")),
},
},
})
}

// TestTest_ConfigDirectory_StaticDirectory_AttributeDoesNotExist uses Terraform
// configuration specifying a "numeric" attribute that was introduced in v3.3.0 of the
// random provider password This test confirms that the TestCase ExternalProviders
Expand Down
29 changes: 28 additions & 1 deletion internal/plugintest/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,39 @@ func (wd *WorkingDir) GetHelper() *Helper {
// Destroy to establish the configuration. Any previously-set configuration is
// discarded and any saved plan is cleared.
func (wd *WorkingDir) SetConfig(ctx context.Context, cfg teststep.Config, vars config.Variables) error {
// Remove old config and variables files first
d, err := os.Open(wd.baseDir)

if err != nil {
return err
}

defer d.Close()

fi, err := d.Readdir(-1)

if err != nil {
return err
}

for _, file := range fi {
if file.Mode().IsRegular() {
if filepath.Ext(file.Name()) == ".tf" || filepath.Ext(file.Name()) == ".json" {
err = os.Remove(filepath.Join(d.Name(), file.Name()))

if err != nil && !os.IsNotExist(err) {
return err
}
}
}
}

logging.HelperResourceTrace(ctx, "Setting Terraform configuration", map[string]any{logging.KeyTestTerraformConfiguration: cfg})

outFilename := filepath.Join(wd.baseDir, ConfigFileName)

// This file has to be written otherwise wd.Init() will return an error.
err := os.WriteFile(outFilename, nil, 0700)
err = os.WriteFile(outFilename, nil, 0700)

if err != nil {
return err
Expand Down

0 comments on commit a8cab58

Please sign in to comment.