testsacc

package
v0.20.4 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

README

testAcc

Special options

Set the following environment variables to enable special options:

Variable Description
TF_ACC_ONLY_PRINT Only print the Terraform configuration and exit.
TF_ACC_RUN_TEST Run the specified test only.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	KeyValueStore = &map[string]any{}
)

Functions

func GenerateTests

func GenerateTests(tacc TestACC) []resource.TestStep

*TestACC GenerateTests generates the acceptance tests for the resource under test.

func GetValueFromTemplate added in v0.11.0

func GetValueFromTemplate(resourceName, key string) string

GetValueFromTemplate returns the value of the given key from the key-value store.

func ImportStateIDBuilder

func ImportStateIDBuilder(resourceName string, attributeNames []string) resource.ImportStateIdFunc

* Other ImportStateIDBuilder is a function that can be used to dynamically generate the ID for the ImportState tests. It is sent the state, which can be checked to derive the attributes necessary and generate the string in the desired format. Specifie the list of attribute names to use to build the ID. Example of use: []string{"vdc_name", "edgegateway_name"} => "vdcExample.edgegatewayExample".

func NewResourceConfig added in v0.18.2

func NewResourceConfig(data TestACC) func() *ResourceConfig

Types

type ConfigName added in v0.18.2

type ConfigName string // Config name is cloudavenue_<resource_name>.<config_name>

func (ConfigName) IsValid added in v0.18.2

func (c ConfigName) IsValid() bool

IsValid checks if the given ConfigName is valid. Parse the string and check if format are prefix_resourceName.configName or data.prefix_resourceName.configName.

func (ConfigName) Parse added in v0.18.2

func (c ConfigName) Parse() (resourceName ResourceName, configName string, isDataSource bool)

Parse returns the resource name, the config name and if the config is a data source. For example, if the ConfigName is "cloudavenue_catalog.example", the resource name will be "cloudavenue_catalog" and the config name will be "example". If the ConfigName is "data.cloudavenue_catalog.example", the resource name will be "cloudavenue_catalog" and the config name will be "example" and the config will be a data source.

func (ConfigName) String added in v0.18.2

func (c ConfigName) String() string

* ConfigName String returns the string representation of the ConfigName.

type DependenciesConfigResponse added in v0.15.0

type DependenciesConfigResponse []func() map[string]TFData

func (*DependenciesConfigResponse) Append added in v0.15.0

func (d *DependenciesConfigResponse) Append(tf func() map[string]TFData)

* DependenciesConfigResponse Append appends the given dependencies config to the current one.

type ListOfDependencies added in v0.11.0

type ListOfDependencies map[ConfigName]TFData

func (*ListOfDependencies) Append added in v0.11.0

func (l *ListOfDependencies) Append(configName ConfigName, tfData TFData)

* ListOfDependencies Append appends the given dependency to the list of dependencies.

func (*ListOfDependencies) Exists added in v0.11.0

func (l *ListOfDependencies) Exists(configName ConfigName) bool

Exists checks if the given dependency exists in the list of dependencies.

func (*ListOfDependencies) Get added in v0.15.4

func (l *ListOfDependencies) Get() []string

Get returns the list of dependencies as a slice of string.

func (*ListOfDependencies) ToTFData added in v0.18.2

func (l *ListOfDependencies) ToTFData() TFData

ToTFData returns the list of dependencies as a TFData (string).

type ResourceConfig added in v0.18.2

type ResourceConfig struct {
	TestACC
}

func (ResourceConfig) GetDefaultChecks added in v0.18.2

func (r ResourceConfig) GetDefaultChecks() []resource.TestCheckFunc

GetDefaultChecks returns the checks for the test named "example".

func (ResourceConfig) GetDefaultConfig added in v0.18.2

func (r ResourceConfig) GetDefaultConfig() map[string]TFData

GetDefaultConfig returns the create configuration for the test named "example".

func (*ResourceConfig) GetSpecificChecks added in v0.18.2

func (r *ResourceConfig) GetSpecificChecks(testName string) []resource.TestCheckFunc

GetSpecificChecks returns the checks for the test named.

func (*ResourceConfig) GetSpecificConfig added in v0.18.2

func (r *ResourceConfig) GetSpecificConfig(testName string) func() map[string]TFData

GetSpecificConfig returns the create configuration for the test named "example".

type ResourceName added in v0.11.0

type ResourceName string // Resource name is cloudavenue_<resource_name>

func (ResourceName) String added in v0.11.0

func (r ResourceName) String() string

* ResourceName String returns the string representation of the ResourceName.

type TFAdvanced added in v0.12.0

type TFAdvanced struct {
	// PreConfig is called before the Config is applied to perform any per-step
	// setup that needs to happen. This is called regardless of "test mode"
	// below.
	PreConfig func()

	// Taint is a list of resource addresses to taint prior to the execution of
	// the step. Be sure to only include this at a step where the referenced
	// address will be present in state, as it will fail the test if the resource
	// is missing.
	//
	// This option is ignored on ImportState tests, and currently only works for
	// resources in the root module path.
	Taint []string

	// Destroy will create a destroy plan if set to true.
	Destroy bool

	// ExpectNonEmptyPlan can be set to true for specific types of tests that are
	// looking to verify that a diff occurs
	ExpectNonEmptyPlan bool

	// ExpectError allows the construction of test cases that we expect to fail
	// with an error. The specified regexp must match against the error for the
	// test to pass.
	ExpectError *regexp.Regexp

	// PlanOnly can be set to only run `plan` with this configuration, and not
	// actually apply it. This is useful for ensuring config changes result in
	// no-op plans
	PlanOnly bool

	// PreventDiskCleanup can be set to true for testing terraform modules which
	// require access to disk at runtime. Note that this will leave files in the
	// temp folder
	PreventDiskCleanup bool

	// PreventPostDestroyRefresh can be set to true for cases where data sources
	// are tested alongside real resources
	PreventPostDestroyRefresh bool
}

func (*TFAdvanced) IsEmpty added in v0.12.0

func (t *TFAdvanced) IsEmpty() bool

* TFAdvanced IsEmpty returns true if the TFAdvanced is empty.

type TFConfig

type TFConfig struct {
	// Checks is a Terraform checks to run for checking the resource under test.
	Checks []resource.TestCheckFunc

	// TFCongig is the Terraform configuration to use for the test.
	TFConfig TFData

	// TFAdvanced is the Terraform advanced configuration to use for the test.
	TFAdvanced TFAdvanced
}

func (TFConfig) Generate

func (t TFConfig) Generate(_ context.Context, dependencies TFData) string

Generate creates the Terraform configuration for the resource under test. It returns the Terraform configuration as a string. Concatenate the dependencies config and the resource config.

type TFData

type TFData string

func GenerateFromTemplate added in v0.11.0

func GenerateFromTemplate(resourceName, templateData string) TFData

GenerateFromTemplate generates the Terraform configuration from the given template. The template can contain placeholders that will be replaced by the given values.

Who to use:

resource "cloudavenue_catalog" "example" {
	name             = {{ get . "name" }}
	description      = {{ generate . "description" "longString"}}
	delete_recursive = true
	delete_force     = true
}

Available functions in the template:

  • generate: generates a random string and stores it in the key-value store. Generate accepts an optional argument that specifies the format of the random string (available formats: "shortString", "longString"). Default format is "shortString".
  • get: returns the value of the given key from the key-value store.

func (*TFData) Append

func (t *TFData) Append(tf TFData)

Append appends the given Terraform configuration to the current one.

func (*TFData) AppendWithoutResourceName added in v0.11.0

func (t *TFData) AppendWithoutResourceName(tf TFData)

AppendWithoutResourceName appends the given Terraform configuration to the current one.

func (TFData) Get

func (t TFData) Get() string

Get returns the Terraform configuration as a string.

func (*TFData) IsEmpty added in v0.11.0

func (t *TFData) IsEmpty() bool

is empty.

func (*TFData) Set

func (t *TFData) Set(s string)

Set sets the Terraform configuration to the given string.

func (TFData) String

func (t TFData) String() string

String returns the Terraform configuration as a string.

type TFImport

type TFImport struct {
	// ImportStateId is the ID to perform an ImportState operation with.
	// This is optional. If it isn't set, then the resource ID is automatically
	// determined by inspecting the state for ResourceName's ID.
	ImportStateID string

	// ImportStateIdFunc is a function that can be used to dynamically generate
	// the ID for the ImportState tests. It is sent the state, which can be
	// checked to derive the attributes necessary and generate the string in the
	// desired format.
	ImportStateIDFunc resource.ImportStateIdFunc

	// ImportStateIDBuilder is a function that can be used to dynamically generate
	// the ID for the ImportState tests. It is sent the state, which can be
	// checked to derive the attributes necessary and generate the string in the
	// desired format.
	// Specifie the list of attribute names to use to build the ID.
	// Example of use: []string{"vdc_name", "edgegateway_name"} => "vdcExample.edgegatewayExample"
	ImportStateIDBuilder []string

	// ImportStateVerifyIgnore is a list of prefixes of fields that should
	// not be verified to be equal. These can be set to ephemeral fields or
	// fields that can't be refreshed and don't matter.
	ImportStateVerifyIgnore []string

	// ImportStatePersist, if true, will update the persisted state with the
	// state generated by the import operation (i.e., terraform import). When
	// false (default) the state generated by the import operation is discarded
	// at the end of the test step that is verifying import behavior.
	ImportStatePersist bool

	// ImportStateVerify, if true, will also check that the state values
	// that are finally put into the state after import match for all the
	// IDs returned by the Import.
	ImportStateVerify bool

	// ImportState, if true, will test the functionality of ImportState
	// by importing the resource with ID of that resource.
	ImportState bool
}

type Test

type Test struct {
	// CommonChecks is a list of common Terraform checks applied to all tests.
	CommonChecks []resource.TestCheckFunc

	// CommonDependencies is a list of common Terraform configurations used to create any dependencies of the resource under test.
	CommonDependencies func() DependenciesConfigResponse

	// Create returns the Terraform configuration to use for the test.
	// This should be a valid Terraform configuration that can be used to create, update, and destroy resources.
	Create TFConfig

	// Update returns the Terraform configurations to use for the update test.
	// This should be a valid Terraform configuration that can be used to update the resource under test.
	Updates []TFConfig

	// Import returns the Terraform configurations to use for the import test.
	Imports []TFImport

	// Destroy will create a destroy plan if set to true.
	Destroy bool

	// CacheDependenciesConfig is used to cache the dependencies config.
	CacheDependenciesConfig TFData
	// contains filtered or unexported fields
}

func (*Test) ComputeDependenciesConfig added in v0.15.0

func (t *Test) ComputeDependenciesConfig(testACC TestACC)

Compute Dependencies config.

func (*Test) ExistInListOfDeps added in v0.15.5

func (t *Test) ExistInListOfDeps(configName string) bool

ExistInListOfDeps.

func (Test) GenerateSteps

func (t Test) GenerateSteps(ctx context.Context, testName TestName, testACC TestACC) (steps []resource.TestStep)

GenerateSteps generates the structure of the acceptance tests.

func (Test) GetChecks added in v0.18.2

func (t Test) GetChecks() []resource.TestCheckFunc

GetChecks returns the checks for the test.

type TestACC

type TestACC interface {
	// GetResourceName returns the name of the resource under test.
	GetResourceName() string

	// DependenciesConfig returns the Terraform configuration used to create any dependencies of the resource under test.
	DependenciesConfig() DependenciesConfigResponse

	// Tests returns the acceptance tests to run for the resource under test.
	// resourceName is a concatenation of the resource name and the example name. For example, "cloudavenue_catalog.example".
	Tests(context.Context) map[TestName]func(ctx context.Context, resourceName string) Test
}

type TestName

type TestName string

func (TestName) ComputeResourceName

func (e TestName) ComputeResourceName(resourceName string) string

ComputeResourceName returns the name of the resource under test. Return cloudavenue_<resource_name>.<example_name>.

func (TestName) Get

func (e TestName) Get() string

* TestName Get return the name of the example formatted as a lower camel case string.

func (TestName) String

func (e TestName) String() string

String string representation of the example name.

Jump to

Keyboard shortcuts

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