Skip to content

Commit

Permalink
Adding support for TestStep.ConfigFile field (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jul 27, 2023
1 parent 6ab7111 commit e395077
Show file tree
Hide file tree
Showing 29 changed files with 1,064 additions and 177 deletions.
26 changes: 26 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config

// TestStepConfigFunc is the callback type used with acceptance tests to
// specify a string which identifies a directory containing Terraform
// configuration files, or a file that contains Terraform configuration.
type TestStepConfigFunc func(TestStepConfigRequest) string

// TestStepConfigRequest defines the request supplied to types
// implementing TestStepConfigFunc.
type TestStepConfigRequest struct {
StepNumber int
TestName string
}

// Exec executes TestStepConfigFunc if it is not nil, otherwise an
// empty string is returned.
func (f TestStepConfigFunc) Exec(req TestStepConfigRequest) string {
if f != nil {
return f(req)
}

return ""
}
19 changes: 19 additions & 0 deletions config/directory_test.go → config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ func TestTestStepConfigFunc_Exec(t *testing.T) {
},
expected: "testdata/TestTestStepConfigFunc_Exec/1",
},
"static_file": {
testStepConfigFunc: config.StaticFile("name_of_file"),
expected: "name_of_file",
},
"test_name_file": {
testStepConfigFunc: config.TestNameFile("test.tf"),
testStepConfigRequest: config.TestStepConfigRequest{
TestName: "TestTestStepConfigFunc_Exec",
},
expected: "testdata/TestTestStepConfigFunc_Exec/test.tf",
},
"test_step_file": {
testStepConfigFunc: config.TestStepFile("test.tf"),
testStepConfigRequest: config.TestStepConfigRequest{
StepNumber: 1,
TestName: "TestTestStepConfigFunc_Exec",
},
expected: "testdata/TestTestStepConfigFunc_Exec/1/test.tf",
},
}

for name, testCase := range testCases {
Expand Down
29 changes: 2 additions & 27 deletions config/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ import (
"strconv"
)

// TestStepConfigFunc is the callback type used with acceptance tests to
// specify a string which identifies a directory containing Terraform
// configuration files.
type TestStepConfigFunc func(TestStepConfigRequest) string

// TestStepConfigRequest defines the request supplied to types
// implementing TestStepConfigFunc.
type TestStepConfigRequest struct {
StepNumber int
TestName string
}

// Exec executes TestStepConfigFunc if it is not nil, otherwise an
// empty string is returned.
func (f TestStepConfigFunc) Exec(req TestStepConfigRequest) string {
if f != nil {
return f(req)
}

return ""
}

// StaticDirectory is a helper function that returns the supplied
// directory when TestStepConfigFunc is executed.
func StaticDirectory(directory string) func(TestStepConfigRequest) string {
Expand Down Expand Up @@ -62,11 +40,8 @@ func TestNameDirectory() func(TestStepConfigRequest) string {
}
}

// TestStepDirectory returns the name of the test suffixed with an
// OS specific separator and the test step number. This facilitates
// a convention of naming directories containing Terraform
// configuration files with the test step number and nesting of
// these files within a directory with the same name as the test.
// TestStepDirectory returns the name of the test suffixed
// with the test step number.
func TestStepDirectory() func(TestStepConfigRequest) string { //nolint:paralleltest //Not a test
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName, strconv.Itoa(req.StepNumber))
Expand Down
48 changes: 48 additions & 0 deletions config/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config

import (
"path/filepath"
"strconv"
)

// StaticFile is a helper function that returns the supplied
// file when TestStepConfigFunc is executed.
func StaticFile(file string) func(TestStepConfigRequest) string {
return func(_ TestStepConfigRequest) string {
return file
}
}

// TestNameFile returns the name of the test suffixed with the supplied
// file name when TestStepConfigFunc is executed (e.g., "testdata/TestExampleCloudThing_basic/test.tf.
//
// For example, given test code:
//
// func TestExampleCloudThing_basic(t *testing.T) {
// resource.Test(t, resource.TestCase{
// Steps: []resource.TestStep{
// {
// ConfigFile: config.TestNameFile("test.tf"),
// },
// },
// })
// }
//
// The testing configuration will be expected in the
// testdata/TestExampleCloudThing_basic/test.tf file.
func TestNameFile(file string) func(TestStepConfigRequest) string {
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName, file)
}
}

// TestStepFile returns the name of the test suffixed
// with the test step number and the supplied file name.
func TestStepFile(file string) func(TestStepConfigRequest) string { //nolint:paralleltest //Not a test
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName, strconv.Itoa(req.StepNumber), file)
}
}
22 changes: 10 additions & 12 deletions helper/resource/testcase_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,17 @@ func (c TestCase) validate(ctx context.Context, t testing.T) error {
testCaseHasProviders := c.hasProviders(ctx)

for stepIndex, step := range c.Steps {
stepConfiguration, err := teststep.Configuration(
teststep.ConfigurationRequest{
Directory: teststep.Pointer(
step.ConfigDirectory.Exec(
config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
),
),
Raw: teststep.Pointer(step.Config),
configRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: step.Config,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
)
}.Exec()

stepConfiguration, err := teststep.Configuration(configRequest)

if err != nil {
return fmt.Errorf("error creating teststep.Configuration: %s", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = 8

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = 8

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

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

numeric = var.numeric
}

variable "length" {
type = number
}

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}

provider "random" {}

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

numeric = var.numeric
}

variable "length" {
type = number
}

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = 8

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

resource "random_password" "test" {
length = 8

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.2.0"
}
}
}

provider "random" {}

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

numeric = var.numeric
}

variable "length" {
type = number
}

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

terraform {
required_providers {
random = {
source = "registry.terraform.io/hashicorp/random"
version = "3.5.1"
}
}
}

provider "random" {}

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

numeric = var.numeric
}

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

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

resource "random_id" "test" {}
Loading

0 comments on commit e395077

Please sign in to comment.