teststate

package
v2.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package teststate provides primitives for serializing test data to disk so that it can be created in one test stage and reused in later validation and teardown stages.

These primitives are type agnostic. Modules that own a type typically wrap them in a named helper (for example, aws.SaveEc2KeyPair or k8s.SaveKubectlOptions) so that callers do not have to spell out paths or filenames. Callers with types that have no owning module can use Save and Load directly.

This package lives in core rather than teststructure so that modules such as aws, k8s, packer, and ssh can provide their own helpers without teststructure having to import every one of them. Every t.Fatalf here is followed by an explicit return. They are unreachable with *testing.T, whose FailNow calls runtime.Goexit, but TestingT allows other harnesses, and one whose FailNow returns would otherwise carry on past the failure.

Index

Constants

View Source
const DirName = ".test-data"

DirName is the name of the folder, relative to a test folder, in which test data is stored.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup(t testing.TestingT, path string)

Cleanup cleans up the test data at the given path.

func CleanupFolder

func CleanupFolder(t testing.TestingT, path string)

CleanupFolder cleans up the .test-data folder inside the given folder. If there are any errors, fail the test.

func CleanupFolderE

func CleanupFolderE(t testing.TestingT, path string) error

CleanupFolderE cleans up the .test-data folder inside the given folder.

func FormatPath

func FormatPath(testFolder string, filename string) string

FormatPath formats a path to save test data with the given filename in the given test folder.

func IsEmptyJSON

func IsEmptyJSON(t testing.TestingT, bytes []byte) bool

IsEmptyJSON returns true if the given bytes are empty, or in a valid JSON format that can reasonably be considered empty. The types used are based on the type possibilities listed at https://golang.org/src/encoding/json/decode.go?s=4062:4110#L51

func IsPresent

func IsPresent(t testing.TestingT, path string) bool

IsPresent returns true if a file exists at $path and the test data there is non-empty.

func Load

func Load(t testing.TestingT, path string, value any)

Load loads and unserializes a value stored at the given path. The value should be a pointer to a struct into which the value will be deserialized. This allows you to reuse some sort of test data (e.g., terraform.Options) from earlier setup steps in later validation and teardown steps.

func Save

func Save(t testing.TestingT, path string, overwrite bool, value any)

Save serializes and saves a value used at test time to the given path. This allows you to create some sort of test data (e.g., terraform.Options) during setup and to reuse this data later during validation and teardown. If `overwrite` is `true`, any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.

The marshalled JSON is written to the test log. Use SaveRedacted for values that contain secrets.

func SaveRedacted

func SaveRedacted(t testing.TestingT, path string, overwrite bool, value any)

SaveRedacted behaves exactly like Save, except that the marshalled JSON is not written to the test log. Use this for values that contain secrets, such as private keys.

Types

This section is empty.

Jump to

Keyboard shortcuts

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