terraform_module_test_helper

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 25 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 RecordVersionSnapshot added in v0.7.0

func RecordVersionSnapshot(t *testing.T, rootFolder, terraformModuleFolder string) error

func RunE2ETest

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

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 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 TerraformOutput

type TerraformOutput = map[string]interface{}

type TestVersionSnapshot added in v0.7.0

type TestVersionSnapshot struct {
	Time    time.Time
	Success bool
	Output  string
}

func GetVersion added in v0.7.0

func GetVersion(t *testing.T, rootFolder, terraformModuleFolder string) TestVersionSnapshot

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