Skip to content

Commit

Permalink
Support testing of configurations in JSON syntax.
Browse files Browse the repository at this point in the history
This implements the proposal described in hashicorp#721
  • Loading branch information
rudo-thomas committed Mar 9, 2021
1 parent d0dcf42 commit 47857b0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ENHANCEMENTS

* `helper/resource.TestStep` now has a `ConfigIsJSON` bool flag that allows tests to use configuration in the JSON syntax. ([#721](https://github.com/hashicorp/terraform-plugin-sdk/issues/721))

# 2.4.4 (February 24, 2021)

NOTES
Expand Down
6 changes: 5 additions & 1 deletion helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ type TestStep struct {
// Config a string of the configuration to give to Terraform. If this
// is set, then the TestCase will execute this step with the same logic
// as a `terraform apply`.
Config string
//
// ConfigIsJSON, if true, will assume that the configuration in Config,
// if any, uses the JSON syntax rather than the native syntax.
Config string
ConfigIsJSON bool

// Check is called after the Config is applied. Use this step to
// make your own API calls to check the status of things, and to
Expand Down
3 changes: 3 additions & 0 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func runNewTest(t testing.T, c TestCase, helper *plugintest.Helper) {
t.Fatal(err)
}

wd.SetConfigIsJSON(false)
err = wd.SetConfig(providerCfg)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
Expand Down Expand Up @@ -196,11 +197,13 @@ func testIDRefresh(c TestCase, t testing.T, wd *plugintest.WorkingDir, step Test
if err != nil {
return err
}
wd.SetConfigIsJSON(false)
err = wd.SetConfig(cfg)
if err != nil {
t.Fatalf("Error setting import test config: %s", err)
}
defer func() {
wd.SetConfigIsJSON(step.ConfigIsJSON)
err = wd.SetConfig(step.Config)
if err != nil {
t.Fatalf("Error resetting test config: %s", err)
Expand Down
1 change: 1 addition & 0 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func testStepNewConfig(t testing.T, c TestCase, wd *plugintest.WorkingDir, step
}
}

wd.SetConfigIsJSON(step.ConfigIsJSON)
err := wd.SetConfig(step.Config)
if err != nil {
return fmt.Errorf("Error setting config: %w", err)
Expand Down
1 change: 1 addition & 0 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func testStepNewImportState(t testing.T, c TestCase, helper *plugintest.Helper,
}
importWd := helper.RequireNewWorkingDir(t)
defer importWd.Close()
importWd.SetConfigIsJSON(step.ConfigIsJSON)
err = importWd.SetConfig(step.Config)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
Expand Down
36 changes: 29 additions & 7 deletions internal/plugintest/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

const (
ConfigFileName = "terraform_plugin_test.tf"
PlanFileName = "tfplan"
ConfigFileNameNative = "terraform_plugin_test.tf"
ConfigFileNameJSON = ConfigFileNameNative + ".json"
PlanFileName = "tfplan"
)

// WorkingDir represents a distinct working directory that can be used for
Expand All @@ -42,6 +43,11 @@ type WorkingDir struct {
reattachInfo tfexec.ReattachInfo

env map[string]string

// configIsJSON is a flag that affects the assumed syntax of the
// configuration passed to SetConfig. If true, SetConfig assumes the
// configuration is in the JSON syntax rather than the native syntax.
configIsJSON bool
}

// Close deletes the directories and files created to represent the receiving
Expand Down Expand Up @@ -77,14 +83,26 @@ func (wd *WorkingDir) GetHelper() *Helper {
return wd.h
}

// SetConfigIsJSON sets the configIsJSON flag, which affects all subsequent
// calls to SetConfig.
func (wd *WorkingDir) SetConfigIsJSON(configIsJSON bool) {
wd.configIsJSON = configIsJSON
}

// SetConfig sets a new configuration for the working directory.
//
// This must be called at least once before any call to Init, Plan, Apply, or
// Destroy to establish the configuration. Any previously-set configuration is
// discarded and any saved plan is cleared.
//
// The assumed syntax of the configuration can be changed by calling
// SetConfigIsJSON prior to calling SetConfig.
func (wd *WorkingDir) SetConfig(cfg string) error {
configFilename := filepath.Join(wd.baseDir, ConfigFileName)
err := ioutil.WriteFile(configFilename, []byte(cfg), 0700)
err := os.Remove(wd.configFilename(!wd.configIsJSON))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
err = ioutil.WriteFile(wd.configFilename(wd.configIsJSON), []byte(cfg), 0700)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,15 +153,19 @@ func (wd *WorkingDir) ClearPlan() error {
// Init runs "terraform init" for the given working directory, forcing Terraform
// to use the current version of the plugin under test.
func (wd *WorkingDir) Init() error {
if _, err := os.Stat(wd.configFilename()); err != nil {
if _, err := os.Stat(wd.configFilename(wd.configIsJSON)); err != nil {
return fmt.Errorf("must call SetConfig before Init")
}

return wd.tf.Init(context.Background(), tfexec.Reattach(wd.reattachInfo))
}

func (wd *WorkingDir) configFilename() string {
return filepath.Join(wd.baseDir, ConfigFileName)
func (wd *WorkingDir) configFilename(configIsJson bool) string {
if configIsJson {
return filepath.Join(wd.baseDir, ConfigFileNameJSON)
} else {
return filepath.Join(wd.baseDir, ConfigFileNameNative)
}
}

func (wd *WorkingDir) planFilename() string {
Expand Down

0 comments on commit 47857b0

Please sign in to comment.