Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option to use Terraform Configuration in TestStep #153

Merged
merged 59 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
0b590e7
Adding ConfigDirectory to TestStep (#150)
bendbennett Jul 17, 2023
91b0ce7
Add teststep.Config interface so that logic that examines the content…
bendbennett Jul 17, 2023
3937312
Update TestStep.Validate() to use teststep.Config interface implement…
bendbennett Jul 17, 2023
6838303
Switching to using a teststep.ConfigurationRequest struct for use in …
bendbennett Jul 17, 2023
5634313
Refactoring to use type implementing teststep.Config interface (#150)
bendbennett Jul 17, 2023
18a3602
Refactoring testStepNewImportState() to use type implementing testste…
bendbennett Jul 17, 2023
f9f0b1b
Switching to using type implementing teststep.Config interface (#150)
bendbennett Jul 17, 2023
1e1b53c
Moved TestStep.configHasProviderBlock(), TestStep.configHasTerraformB…
bendbennett Jul 18, 2023
da96c02
Fix validation error message to include ConfigDirectory (#150)
bendbennett Jul 18, 2023
4230bb1
Added testCaseProviderConfig and testStepProviderConfig as fields on …
bendbennett Jul 18, 2023
d46d517
Add initial implementation of copying files from ConfigDirectory into…
bendbennett Jul 18, 2023
80c09f5
Switch to using WriteDirectory() method on configuration to make mana…
bendbennett Jul 19, 2023
516e68b
Adding tests to verify that the ExternalProviders specified in a Test…
bendbennett Jul 19, 2023
6a8f3da
Extending implementation of HasProviderBlock() to include detection o…
bendbennett Jul 19, 2023
6e04f0c
Moving writing of raw config or copy of config directory files intern…
bendbennett Jul 19, 2023
a258bfa
Removing writing of configuration files for external providers (conta…
bendbennett Jul 20, 2023
0966142
Adding HasConfigurationFiles() func to Config interface to be able to…
bendbennett Jul 20, 2023
ce6b5a3
Reinstating TestStep.mergedConfig() method as terraform and provider …
bendbennett Jul 20, 2023
a800d8e
Adding a couple of tests to verify behaviour when using multiple terr…
bendbennett Jul 20, 2023
f07f6aa
Switching to using TestStepConfigFunc type for TestStep.ConfigDirecto…
bendbennett Jul 21, 2023
2877b6b
Adding acceptance test coverage for StaticDirectory(), TestNameDirect…
bendbennett Jul 21, 2023
ea3044c
Adding Exec() method to TestStepConfigFunc type and removing ExecuteT…
bendbennett Jul 24, 2023
7b0ac16
Adding ConfigVariables to allow defining of Terraform variables withi…
bendbennett Jul 24, 2023
4aa11b7
Adding docs for config directory and variables (#150)
bendbennett Jul 24, 2023
136e47e
Adding tests for TestStepConfigFunc.Exec() method (#150)
bendbennett Jul 24, 2023
a98a036
Adding tests for config<Type>Variable and Variables.Write() func (#150)
bendbennett Jul 24, 2023
88066ac
Adding copyright headers (#150)
bendbennett Jul 24, 2023
36409db
Unmarshalling before comparing (#150)
bendbennett Jul 24, 2023
3f6a544
Apply suggestions from code review
bendbennett Jul 25, 2023
5a59a64
Adding TestName to TestStepConfigRequest and updating TestStepDirecto…
bendbennett Jul 25, 2023
f7ce370
Switching to supplying t.Name() to TestStepConfigRequest (#150)
bendbennett Jul 25, 2023
d0524a8
Marshalling Variables directly (#150)
bendbennett Jul 25, 2023
c84a366
Adding comment to explain the usage of Unwrap() (#150)
bendbennett Jul 25, 2023
fa4af4e
Adding FloatVariable and IntegerVariable and removing NumberVariable …
bendbennett Jul 25, 2023
90907b8
Changing ConfigurationRequest fields Directory and Raw to *string to …
bendbennett Jul 25, 2023
647801c
Adding Validate() func to ConfigurationRequest along with test covera…
bendbennett Jul 26, 2023
2155db2
Adding configurationDirectory and configurationString types (#150)
bendbennett Jul 26, 2023
6ab7111
Renaming raw.go to string.go to match type defined therein (#150)
bendbennett Jul 26, 2023
e395077
Adding support for TestStep.ConfigFile field (#150)
bendbennett Jul 27, 2023
c9e3014
Updating docs for config pkg (#150)
bendbennett Jul 27, 2023
3cfc6c9
Adding documentation into teststep/config.go and tests for exported m…
bendbennett Jul 27, 2023
0c4a1fe
Merge remote-tracking branch 'origin/main' into bendbennett/issues-150
bendbennett Jul 27, 2023
36dd090
Adding checks for absolute filepath in configurationDirectory and con…
bendbennett Jul 28, 2023
28af0cc
Adding tests configurationDirectory, configurationFile and configurat…
bendbennett Jul 28, 2023
6fbfd10
Adding test coverage for error conditions and configurationDirectory,…
bendbennett Jul 28, 2023
5da5a2a
Adding empty dir for tests to git (#150)
bendbennett Jul 28, 2023
5cab687
Adding a page on usage of Terraform configuration to the website docs…
bendbennett Jul 28, 2023
51576a0
Adding changelog entries (#150)
bendbennett Jul 28, 2023
a95efe7
Adding test to demonstrate that the TestStep number is being used dyn…
bendbennett Jul 31, 2023
711dac6
Removing unneeded elsif conditional (#150)
bendbennett Jul 31, 2023
3197257
Updating docs (#150)
bendbennett Jul 31, 2023
a8cab58
Removing any configuration or variables files from previous test step…
bendbennett Jul 31, 2023
48fabe2
Updating docs (#150)
bendbennett Jul 31, 2023
cba81dc
Merge remote-tracking branch 'origin/main' into bendbennett/issues-150
bendbennett Aug 9, 2023
2117343
Apply suggestions from code review
bendbennett Aug 29, 2023
29dec70
Update .changes/unreleased/FEATURES-20230728-152822.yaml
bendbennett Aug 29, 2023
76b0526
Removing unneeded changelog entries (#150)
bendbennett Aug 29, 2023
9f31393
Reusing stepNumber variable (#150)
bendbennett Aug 29, 2023
19c39ec
Merge remote-tracking branch 'origin/main' into bendbennett/issues-150
bendbennett Aug 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20230728-143814.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'config: Introduced new `config` package which contains interfaces and helper
functions for working with native Terraform configuration and variables'
time: 2023-07-28T14:38:14.006499+01:00
custom:
Issue: "153"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20230728-152737.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'helper/resource: Added `TestStep.ConfigDirectory` to allow specifying a directory
containing Terraform configuration for use during acceptance tests'
time: 2023-07-28T15:27:37.944964+01:00
custom:
Issue: "153"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20230728-152822.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'helper/resource: Added `TestStep.ConfigFile` to allow specifying a file containing
Terraform configuration for use during acceptance tests'
time: 2023-07-28T15:28:22.204411+01:00
custom:
Issue: "153"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20230728-152917.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'helper/resource: Added `TestStep.ConfigVariables` to allow specifying Terraform variables
for use with Terraform configuration during acceptance tests'
time: 2023-07-28T15:29:17.02183+01:00
custom:
Issue: "153"
38 changes: 38 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 either 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. StepNumber is one-based
// and is used in the predefined helper functions:
//
// - [config.TestStepDirectory]
// - [config.TestStepFile].
//
// TestName is used in the predefined helper functions:
//
// - [config.TestNameDirectory]
// - [config.TestStepDirectory]
// - [config.TestNameFile]
// - [config.TestStepFile]
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 ""
}
63 changes: 63 additions & 0 deletions config/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config

import (
"path/filepath"
"strconv"
)

// StaticDirectory returns the supplied directory.
func StaticDirectory(directory string) func(TestStepConfigRequest) string {
return func(_ TestStepConfigRequest) string {
return directory
}
}

// TestNameDirectory returns the name of the test prefixed with
// "testdata".
//
// For example, given test code:
//
// func TestExampleCloudThing_basic(t *testing.T) {
// resource.Test(t, resource.TestCase{
// Steps: []resource.TestStep{
// {
// ConfigDirectory: config.TestNameDirectory(),
// },
// },
// })
// }
//
// The testing configurations will be expected in the
// testdata/TestExampleCloudThing_basic/ directory.
func TestNameDirectory() func(TestStepConfigRequest) string {
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName)
}
}

// TestStepDirectory returns the name of the test suffixed with the
// test step number and prefixed with "testdata".
//
// For example, given test code:
//
// func TestExampleCloudThing_basic(t *testing.T) {
// resource.Test(t, resource.TestCase{
// Steps: []resource.TestStep{
// {
// ConfigDirectory: config.TestStepDirectory(),
// },
// },
// })
// }
//
// The testing configurations will be expected in the
// testdata/TestExampleCloudThing_basic/1 directory as
// TestStepConfigRequest.StepNumber is one-based.
func TestStepDirectory() func(TestStepConfigRequest) string {
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName, strconv.Itoa(req.StepNumber))
}
}
54 changes: 54 additions & 0 deletions config/directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/config"
)

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

testCases := map[string]struct {
testStepConfigFunc config.TestStepConfigFunc
testStepConfigRequest config.TestStepConfigRequest
expected string
}{
"static_directory": {
testStepConfigFunc: config.StaticDirectory("name_of_directory"),
expected: "name_of_directory",
},
"test_name_directory": {
testStepConfigFunc: config.TestNameDirectory(),
testStepConfigRequest: config.TestStepConfigRequest{
TestName: "TestTestStepConfigFunc_Exec",
},
expected: "testdata/TestTestStepConfigFunc_Exec",
},
"test_step_directory": {
testStepConfigFunc: config.TestStepDirectory(),
testStepConfigRequest: config.TestStepConfigRequest{
StepNumber: 1,
TestName: "TestTestStepConfigFunc_Exec",
},
expected: "testdata/TestTestStepConfigFunc_Exec/1",
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.testStepConfigFunc.Exec(testCase.testStepConfigRequest)

if testCase.expected != got {
t.Errorf("expected %s, got %s", testCase.expected, got)
}
})
}
}
6 changes: 6 additions & 0 deletions config/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Package config implements functionality for supporting native
// Terraform configuration and variables for testing purposes.
package config
63 changes: 63 additions & 0 deletions config/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config

import (
"path/filepath"
"strconv"
)

// StaticFile returns the supplied file.
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 and prefixed with "testdata".
//
// 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, and prefixed with "testdata".
//
// For example, given test code:
//
// func TestExampleCloudThing_basic(t *testing.T) {
// resource.Test(t, resource.TestCase{
// Steps: []resource.TestStep{
// {
// ConfigFile: config.TestStepFile("test.tf"),
// },
// },
// })
// }
//
// The testing configuration will be expected in the
// testdata/TestExampleCloudThing_basic/1/test.tf file
// as TestStepConfigRequest.StepNumber is one-based.
func TestStepFile(file string) func(TestStepConfigRequest) string {
return func(req TestStepConfigRequest) string {
return filepath.Join("testdata", req.TestName, strconv.Itoa(req.StepNumber), file)
}
}
54 changes: 54 additions & 0 deletions config/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package config_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/config"
)

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

testCases := map[string]struct {
testStepConfigFunc config.TestStepConfigFunc
testStepConfigRequest config.TestStepConfigRequest
expected string
}{
"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 {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.testStepConfigFunc.Exec(testCase.testStepConfigRequest)

if testCase.expected != got {
t.Errorf("expected %s, got %s", testCase.expected, got)
}
})
}
}
Loading