terraform_module_test_helper

package module
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 32 Imported by: 0

README

Azure Verified Terraform Module Test Helper

test lint

This repo contains two helper functions that were used to test Azure Verified Terraform Module.

For End-End test:

func TestExamplesStartup(t *testing.T) {
	vars := map[string]interface{}{
		"client_id":     "",
		"client_secret": "",
	}
	managedIdentityId := os.Getenv("MSI_ID")
	if managedIdentityId != "" {
		vars["managed_identity_principal_id"] = managedIdentityId
	}
	test_helper.RunE2ETest(t, "../../", "examples/startup", terraform.Options{
		Upgrade: true,
		Vars:    vars,
	}, func(t *testing.T, output test_helper.TerraformOutput) {
		aksId, ok := output["test_aks_id"].(string)
		assert.True(t, ok)
		assert.Regexp(t, regexp.MustCompile("/subscriptions/.+/resourceGroups/.+/providers/Microsoft.ContainerService/managedClusters/.+"), aksId)
	})
}

The RunE2ETest function accept module's root path, sub-folder to example code that our test want to apply, a terraform.Options argument, and an assertion callback.

In E2E test we apply the example code,then we execute terraform output and pass the json format output to this assertion callback, you can assert whether the output meets your spec there.

For Version-Upgrade Test:

func TestExampleUpgrade_startup(t *testing.T) {
	currentRoot, err := test_helper.GetCurrentModuleRootPath()
	if err != nil {
		t.FailNow()
	}
	currentMajorVersion, err := test_helper.GetCurrentMajorVersionFromEnv()
	if err != nil {
		t.FailNow()
	}
	vars := map[string]interface{}{
		"client_id":     "",
		"client_secret": "",
	}
	managedIdentityId := os.Getenv("MSI_ID")
	if managedIdentityId != "" {
		vars["managed_identity_principal_id"] = managedIdentityId
	}
	test_helper.ModuleUpgradeTest(t, "Azure", "terraform-azurerm-aks", "examples/startup", currentRoot, terraform.Options{
		Upgrade: true,
		Vars:    vars,
	}, currentMajorVersion)
}

The ModuleUpgradeTest function accept your Github repo's owner name (could be username or org name), repo name, sub-folder to example code, and module's current major version (eg: v3.0.0 major version is 3).

The ModuleUpgradeTest function will clone and checkout the latest released tag version within the major version you've passed, apply the code in a temp directory, then modify the module's source to the current path, then execute terraform plan to see if there would be any drift in the plan.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CannotTestError = fmt.Errorf("no previous tag yet or previous tag's folder structure is different than the current version, skip upgrade test")
View Source
var SkipV0Error = fmt.Errorf("v0 is meant to be unstable, skip upgrade test")

Functions

func BreakingChangesDetect added in v0.0.21

func BreakingChangesDetect(currentModulePath, owner, repo string, tag *string) (string, error)

func CompareTwoModules added in v0.0.21

func CompareTwoModules(dir1 string, dir2 string) (string, error)

func GetCurrentMajorVersionFromEnv

func GetCurrentMajorVersionFromEnv() (int, error)

func GetCurrentModuleRootPath

func GetCurrentModuleRootPath() (string, error)

func ModuleUpgradeTest

func ModuleUpgradeTest(t *testing.T, owner, repo, moduleFolderRelativeToRoot, currentModulePath string, opts terraform.Options, currentMajorVer int)

func ReadRetryableErrors added in v0.1.0

func ReadRetryableErrors(retryableCfg []byte, t *testing.T) map[string]string

func RunE2ETest

func RunE2ETest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput))

func RunE2ETestWithOption added in v0.13.0

func RunE2ETestWithOption(t *testing.T, moduleRootPath, exampleRelativePath string, testOption TestOptions)

func RunUnitTest added in v0.10.0

func RunUnitTest(t *testing.T, moduleRootPath, exampleRelativePath string, option terraform.Options, assertion func(*testing.T, TerraformOutput))

func VarsToFile added in v0.14.0

func VarsToFile(t *testing.T, vars map[string]any) string

Types

type Change added in v0.0.21

type Change struct {
	diff.Change
	Category  ChangeCategory `json:"category"`
	Name      *string        `json:"name"`
	Attribute *string        `json:"attribute"`
}

func BreakingChanges added in v0.0.21

func BreakingChanges(m1 *Module, m2 *Module) ([]Change, error)

func (Change) ToString added in v0.0.21

func (c Change) ToString() string

type ChangeCategory added in v0.0.21

type ChangeCategory = string

type KeyedMutex added in v0.9.0

type KeyedMutex struct {
	// contains filtered or unexported fields
}

func (*KeyedMutex) Lock added in v0.9.0

func (m *KeyedMutex) Lock(key string) func()

type Module added in v0.0.21

type Module struct {
	*tfconfig.Module
	OutputExts   map[string]Output
	VariableExts map[string]Variable
	// contains filtered or unexported fields
}

func NewModule added in v0.0.21

func NewModule(dir string, fs afero.Afero) (*Module, error)

func (*Module) Load added in v0.0.21

func (m *Module) Load() error

type Output added in v0.0.21

type Output struct {
	Name        string
	Description string
	Sensitive   string
	Value       string
	Range       hcl.Range
}

type StreamLogger added in v0.8.0

type StreamLogger struct {
	// contains filtered or unexported fields
}

func NewMemoryLogger added in v0.8.0

func NewMemoryLogger() *StreamLogger

func NewStreamLogger added in v0.8.0

func NewStreamLogger(stream io.ReadWriter) *StreamLogger

func (*StreamLogger) Close added in v0.8.0

func (s *StreamLogger) Close() error

func (*StreamLogger) Logf added in v0.8.0

func (s *StreamLogger) Logf(t testing.TestingT, format string, args ...interface{})

func (*StreamLogger) PipeFrom added in v0.8.0

func (s *StreamLogger) PipeFrom(srcLogger *StreamLogger) error

type TerraformOutput

type TerraformOutput = map[string]interface{}

type TestOptions added in v0.13.0

type TestOptions struct {
	TerraformOptions    terraform.Options
	Assertion           func(*testing.T, TerraformOutput)
	SkipIdempotentCheck bool
	SkipDestroy         bool
}

type TestVersionSnapshot added in v0.7.0

type TestVersionSnapshot struct {
	ModuleRootFolder        string
	SubModuleRelativeFolder string
	Time                    time.Time
	Success                 bool
	Versions                string
	ErrorMsg                string
}

func FailedTestVersionSnapshot added in v0.8.0

func FailedTestVersionSnapshot(rootFolder, exampleRelativePath, errMsg string) *TestVersionSnapshot

func SuccessTestVersionSnapshot added in v0.8.0

func SuccessTestVersionSnapshot(rootFolder, exampleRelativePath string) *TestVersionSnapshot

func (*TestVersionSnapshot) Save added in v0.8.0

func (s *TestVersionSnapshot) Save(t *testing.T) error

func (*TestVersionSnapshot) ToString added in v0.7.0

func (s *TestVersionSnapshot) ToString() string

type Variable added in v0.0.21

type Variable struct {
	Name        string
	Type        string
	Description string
	Default     string
	Sensitive   string
	Nullable    string
	Range       hcl.Range
}

Directories

Path Synopsis
bin

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL