acctest

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const RFC3339RegexPattern = `` /* 148-byte string literal not displayed */
View Source
const TestNamePrefix = "tf-acc-test-"

TestNamePrefix is a prefix for randomly generated names used during acceptance testing

View Source
const UUIDV4RegexPattern = `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`

Variables

View Source
var (

	// APIClient is a BastionZero API client.
	//
	// This can be used in testing code for API calls without requiring the use
	// of saving and referencing specific ProviderFactories instances.
	//
	// PreCheck(t) must be called before using this.
	APIClient *bzapi.Client

	// TestProtoV6ProviderFactories are used to instantiate a provider during
	// testing. The factory function will be invoked for every Terraform CLI
	// command executed to create a provider server to which the CLI can
	// reattach.
	TestProtoV6ProviderFactories map[string]func() (tfprotov6.ProviderServer, error)
)

Functions

func CheckAllPoliciesHaveGroupID

func CheckAllPoliciesHaveGroupID(namedTFResource, expectedGroupID string) resource.TestCheckFunc

CheckAllPoliciesHaveGroupID checks that all policies have at least one group that matches an expected ID. It is expected that namedTFResource has a nested list/set attribute named "policies" and the container must contain another attribute named "groups" that contains nested group objects

func CheckAllPoliciesHaveSubjectID

func CheckAllPoliciesHaveSubjectID(namedTFResource, expectedSubjectID string) resource.TestCheckFunc

CheckAllPoliciesHaveSubjectID checks that all policies have at least one subject that matches an expected ID. It is expected that namedTFResource has a nested list/set attribute named "policies" and the container must contain another attribute named "subjects" that contains nested subject objects

func CheckAllResourcesWithTypeDestroyed added in v0.1.1

func CheckAllResourcesWithTypeDestroyed[T any](resourceType string, f FindBastionZeroAPIObjectFunc[T]) resource.TestCheckFunc

CheckAllResourcesWithTypeDestroyed loops through all resources in the Terraform state and verifies that each resource of type resourceType is destroyed (no longer exists at BastionZero). Function f is used to find the API object given the resource's ID (parsed from Terraform state).

func CheckExistsAtBastionZero

func CheckExistsAtBastionZero[T any](namedTFResource string, apiObject *T, f FindBastionZeroAPIObjectFunc[T]) resource.TestCheckFunc

CheckExistsAtBastionZero attempts to load a resource/datasource with name namedTFResource from the TF state and find an API object at BastionZero, using f, with the resource's ID.

The provided pointer is set if there is no error when calling BastionZero. It can be examined to check that what exists at BastionZero matches what is actually set in the TF config/state.

func CheckListOrSetHasElements

func CheckListOrSetHasElements(namedTFResource, listOrSetAttributeName string) resource.TestCheckFunc

CheckListOrSetHasElements attempts to load a resource/datasource with name namedTFResource from the TF state, and then check that the list/set at listOrSetAttributeName has at least 1 element.

func CheckResourceAttrIsOneOf added in v0.3.0

func CheckResourceAttrIsOneOf(name, key string, values []string) resource.TestCheckFunc

CheckResourceAttrIsOneOf ensures the Terraform state value is equal to one of the supplied values.

func CheckResourceDisappears

func CheckResourceDisappears(namedTFResource string, f func(client *bzapi.Client, ctx context.Context, id string) (*http.Response, error)) resource.TestCheckFunc

CheckResourceDisappears loads namedTFResource from the Terraform state and runs f to delete the API object at BastionZero. The ID passed to f is taken from the state file

func CheckTypeSetElemNestedAttrsFromResource

func CheckTypeSetElemNestedAttrsFromResource(nameFirst string, keys []string, nameSecond string, attr string) resource.TestCheckFunc

CheckTypeSetElemNestedAttrsFromResource ensures a subset map of values is stored in state for the given name (nameSecond) and key (attr) combination of attributes nested under a list or set block. The expected subset map is built by copying one for one the key and value combinations found at nameFirst in the state.

Optionally, copy certain key and value combinations by providing a whitelist of keys. Otherwise, if keys list is empty, it is assumed all key and value pairs should be asserted to exist in one of the nested objects under a list or set block (specified by attr).

func ConfigCompose

func ConfigCompose(config ...string) string

ConfigCompose can be called to concatenate multiple strings to build test configurations

func ExpandValuesCheckMapToSingleCheck

func ExpandValuesCheckMapToSingleCheck[T any](resourceName string, apiObject *T, getValuesCheckMapFunc func(apiObject *T) map[string]string) resource.TestCheckFunc

func FindNAPIObjectsOrSkip

func FindNAPIObjectsOrSkip[APIObject any, MappedT any](
	t *testing.T,
	f func(client *bzapi.Client, ctx context.Context) ([]APIObject, *http.Response, error),
	mapF func(apiObject APIObject) MappedT,
	filterF func(apiObject APIObject) bool,
	mappedPointers ...*MappedT,
)

FindNAPIObjectsOrSkip calls f to find a list of API objects at BastionZero and sets a variadic number (n) of pointers to the first n API objects found. The API object is converted to another type, MappedT, by calling mapF (pass the identity function if you don't want to map).

Additionally, pass filterF if you wish to filter certain API objects from being included as candidates (pass nil if you don't want to filter).

If there are less than n API objects, then the current test is skipped.

func FindNBzeroTargetsOrSkip

func FindNBzeroTargetsOrSkip(t *testing.T, bzeroTargets ...*targets.BzeroTarget)

FindNBzeroTargetsOrSkip lists the Bzero targets in the BastionZero organization and sets bzeroTargets to the first n Bzero targets found. If there are less than n Bzero targets, then the current test is skipped.

If you need the targets mapped as the policy type (policies.Target), use FindNBzeroTargetsOrSkipAsPolicyTarget() instead.

func FindNBzeroTargetsOrSkipAsPolicyTarget

func FindNBzeroTargetsOrSkipAsPolicyTarget(t *testing.T, bzeroTargets ...*policies.Target)

FindNBzeroTargetsOrSkipAsPolicyTarget lists the Bzero targets in the BastionZero organization and sets bzeroTargets to the first n Bzero targets found. If there are less than n Bzero targets, then the current test is skipped.

func FindNClusterTargetsOrSkip

func FindNClusterTargetsOrSkip(t *testing.T, clusterTargets ...*targets.ClusterTarget)

FindNClusterTargetsOrSkip lists the Cluster targets in the BastionZero organization and sets clusterTargets to the first n Cluster targets found. If there are less than n Cluster targets, then the current test is skipped.

If you need the targets mapped as the policy type (policies.Cluster), use FindNClusterTargetsOrSkipAsPolicyCluster() instead.

func FindNClusterTargetsOrSkipAsPolicyCluster

func FindNClusterTargetsOrSkipAsPolicyCluster(t *testing.T, clusterTargets ...*policies.Cluster)

FindNClusterTargetsOrSkipAsPolicyCluster lists the Cluster targets in the BastionZero organization and sets clusterTargets to the first n Cluster targets found. If there are less than n Cluster targets, then the current test is skipped.

func FindNDACTargetsOrSkip

func FindNDACTargetsOrSkip(t *testing.T, dacTargets ...*targets.DynamicAccessConfiguration)

FindNDACTargetsOrSkip lists the DAC targets in the BastionZero organization and sets dacTargets to the first n DAC targets found. If there are less than n DAC targets, then the current test is skipped.

func FindNDbTargetsOrSkip

func FindNDbTargetsOrSkip(t *testing.T, dbTargets ...*targets.DatabaseTarget)

FindNDbTargetsOrSkip lists the Db targets in the BastionZero organization and sets dbTargets to the first n Db targets found. If there are less than n Db targets, then the current test is skipped.

If you need the targets mapped as the policy type (policies.Target), use FindNDbTargetsOrSkipAsPolicyTarget() instead.

func FindNDbTargetsOrSkipAsPolicyTarget

func FindNDbTargetsOrSkipAsPolicyTarget(t *testing.T, dbTargets ...*policies.Target)

FindNDbTargetsOrSkipAsPolicyTarget lists the Db targets in the BastionZero organization and sets dbTargets to the first n Db targets found. If there are less than n Db targets, then the current test is skipped.

func FindNEnvironmentsOrSkip

func FindNEnvironmentsOrSkip(t *testing.T, envs ...*environments.Environment)

FindNEnvironmentsOrSkip lists the environments in the BastionZero organization and sets envs to the first n environments found. If there are less than n environments, then the current test is skipped.

If you need the environments mapped as the policy type (policies.Environment), use FindNEnvironmentsOrSkipAsPolicyEnvironment() instead.

func FindNEnvironmentsOrSkipAsPolicyEnvironment added in v0.3.0

func FindNEnvironmentsOrSkipAsPolicyEnvironment(t *testing.T, envs ...*policies.Environment)

FindNEnvironmentsOrSkipAsPolicyEnvironment lists the environments in the BastionZero organization and sets envs to the first n environments found. If there are less than n environments, then the current test is skipped.

func FindNGroupsOrSkip

func FindNGroupsOrSkip(t *testing.T, groups ...*organization.Group)

FindNGroupsOrSkip lists the groups in the BastionZero organization and sets groups to the first n groups found. If there are less than n groups, then the current test is skipped.

If you need the groups mapped as the policy type (policies.Group), use FindNGroupsOrSkipAsPolicyGroup() instead.

func FindNGroupsOrSkipAsPolicyGroup

func FindNGroupsOrSkipAsPolicyGroup(t *testing.T, groups ...*policies.Group)

FindNGroupsOrSkipAsPolicyGroup lists the groups in the BastionZero organization and sets groups to the first n groups found. If there are less than n groups, then the current test is skipped.

func FindNKubernetesPoliciesOrSkip

func FindNKubernetesPoliciesOrSkip(t *testing.T, kubernetesPolicies ...*policies.KubernetesPolicy)

FindNKubernetesPoliciesOrSkip lists the Kubernetes policies in the BastionZero organization and sets kubernetesPolicies to the first n Kubernetes policies found. If there are less than n Kubernetes policies, then the current test is skipped.

func FindNProxyPoliciesOrSkip

func FindNProxyPoliciesOrSkip(t *testing.T, proxyPolicies ...*policies.ProxyPolicy)

FindNProxyPoliciesOrSkip lists the proxy policies in the BastionZero organization and sets proxyPolicies to the first n proxy policies found. If there are less than n proxy policies, then the current test is skipped.

func FindNServiceAccountsOrSkip

func FindNServiceAccountsOrSkip(t *testing.T, serviceAccounts ...*serviceaccounts.ServiceAccount)

FindNServiceAccountsOrSkip lists the service accounts in the BastionZero organization and sets serviceAccounts to the first n service accounts found. If there are less than n service accounts, then the current test is skipped.

func FindNTargetConnectPoliciesOrSkip

func FindNTargetConnectPoliciesOrSkip(t *testing.T, targetConnectPolicies ...*policies.TargetConnectPolicy)

FindNTargetConnectPoliciesOrSkip lists the target connect policies in the BastionZero organization and sets targetConnectPolicies to the first n target connect policies found. If there are less than n target connect policies, then the current test is skipped.

func FindNUsersOrSkip

func FindNUsersOrSkip(t *testing.T, userPointers ...*users.User)

FindNUsersOrSkip lists the users in the BastionZero organization and sets userPointers to the first n users found. If there are less than n users, then the current test is skipped.

If you need the users mapped as the policy type (policies.Subject), use FindNUsersOrSkipAsPolicySubject() instead.

func FindNUsersOrSkipAsPolicySubject

func FindNUsersOrSkipAsPolicySubject(t *testing.T, subjects ...*policies.Subject)

FindNUsersOrSkipAsPolicySubject lists the users in the BastionZero organization and sets subjects to the first n subjects found. If there are less than n users, then the current test is skipped.

func FindNWebTargetsOrSkip

func FindNWebTargetsOrSkip(t *testing.T, webTargets ...*targets.WebTarget)

FindNWebTargetsOrSkip lists the Web targets in the BastionZero organization and sets webTargets to the first n Web targets found. If there are less than n Web targets, then the current test is skipped.

func ListOrSetCount

func ListOrSetCount(resourceState *terraform.ResourceState, listOrSetAttributeName string) (int, error)

ListOrSetCount returns the number of elements in a list or set attribute

func PreCheck

func PreCheck(ctx context.Context, t *testing.T)

PreCheck verifies and sets required provider testing configuration

PreCheck makes assertions that must hold true in order to run an acceptance test. The test fails immediately if any of these assertions fails.

This PreCheck function should be present in every acceptance test. It allows test configurations to omit a provider configuration and ensures testing functions that attempt to call BastionZero APIs directly via APIClient are previously configured.

func RandomName

func RandomName(additionalNames ...string) string

RandomName creates a random name suitable for named BastionZero API objects that are created during acceptance tests.

func SafePrettyInt added in v0.3.0

func SafePrettyInt(value *int) string

func SetEnvironmentVariables

func SetEnvironmentVariables(envs map[string]string) func()

SetEnvironmentVariables sets environment variables and provides a cleanup function to reset the modified environment variables back to their original value.

Source: https://dev.to/arxeiss/auto-reset-environment-variables-when-testing-in-go-5ec

func SkipIfNotInAcceptanceTestMode

func SkipIfNotInAcceptanceTestMode(t *testing.T)

SkipIfNotInAcceptanceTestMode performs the same check that the terraform-plugin-testing library performs to see if the test should be executed or not. Its logic is duplicated here, so we can call it ourselves before the Test() block in case there are additional things that need to be done that cannot be done in PreConfig() or PreCheck()

func TerraformObjectToString added in v0.3.0

func TerraformObjectToString(o types.Object) string

TerraformObjectToString returns a human-readable representation of the Object value that can be used in Terraform configurations. This function is equivalent to basetypes.Object.String() except that null object attributes are omitted from the final string.

func ToTerraformStringList

func ToTerraformStringList(arr []string) string

Types

type FindBastionZeroAPIObjectFunc added in v0.1.1

type FindBastionZeroAPIObjectFunc[T any] func(client *bzapi.Client, ctx context.Context, id string) (*T, *http.Response, error)

FindBastionZeroAPIObjectFunc is a function that finds and returns a BastionZero API object given its ID

Jump to

Keyboard shortcuts

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