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
- func Cleanup(t testing.TestingT, path string)
- func CleanupFolder(t testing.TestingT, path string)
- func CleanupFolderE(t testing.TestingT, path string) error
- func FormatPath(testFolder string, filename string) string
- func IsEmptyJSON(t testing.TestingT, bytes []byte) bool
- func IsPresent(t testing.TestingT, path string) bool
- func Load(t testing.TestingT, path string, value any)
- func Save(t testing.TestingT, path string, overwrite bool, value any)
- func SaveRedacted(t testing.TestingT, path string, overwrite bool, value any)
Constants ¶
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 CleanupFolder ¶
CleanupFolder cleans up the .test-data folder inside the given folder. If there are any errors, fail the test.
func CleanupFolderE ¶
CleanupFolderE cleans up the .test-data folder inside the given folder.
func FormatPath ¶
FormatPath formats a path to save test data with the given filename in the given test folder.
func IsEmptyJSON ¶
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 ¶
IsPresent returns true if a file exists at $path and the test data there is non-empty.
func Load ¶
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 ¶
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.
Types ¶
This section is empty.