testutil

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Created ContainerState = 0
	Started ContainerState = 1

	TestUtilization uint64 = 424242
)

Variables

This section is empty.

Functions

func Abs

func Abs(t *testing.T, path string) string

func CheckContains

func CheckContains(t *testing.T, expected, actual string)

func CheckDeepEqual

func CheckDeepEqual(t *testing.T, expected, actual interface{}, opts ...cmp.Option)

func CheckElementsMatch

func CheckElementsMatch(t *testing.T, expected, actual interface{})

CheckElementsMatch validates that two given slices contain the same elements while disregarding their order. Elements of both slices have to be comparable by '=='

func CheckError

func CheckError(t *testing.T, shouldErr bool, err error)

func CheckErrorAndDeepEqual

func CheckErrorAndDeepEqual(t *testing.T, shouldErr bool, err error, expected, actual interface{}, opts ...cmp.Option)

func CheckErrorAndExitCode

func CheckErrorAndExitCode(t *testing.T, expectedCode int, err error)

func CheckErrorAndFailNow

func CheckErrorAndFailNow(t *testing.T, shouldErr bool, err error)

func CheckFileExistAndContent

func CheckFileExistAndContent(t *testing.T, path string, expectedContent []byte)

func CheckMapsMatch

func CheckMapsMatch(t *testing.T, expected, actual interface{})

CheckMapsMatch validates that two given maps contain the same key-value pairs

func CheckNotContains

func CheckNotContains(t *testing.T, excluded, actual string)

func CheckRegex

func CheckRegex(t *testing.T, pattern, actual string)

func CreateFakeImageTar

func CreateFakeImageTar(ref string, path string) error

func EnsureTestPanicked

func EnsureTestPanicked(t *testing.T)

func ReadRefFromFakeTar

func ReadRefFromFakeTar(input io.Reader) (string, error)

func Run

func Run(t *testing.T, name string, f func(t *T))

func ServeFile

func ServeFile(t *testing.T, content []byte) string

ServeFile serves a file with http. Returns the url to the file and a teardown function that should be called to properly stop the server.

func SetupFakeWatcher

func SetupFakeWatcher(w watch.Interface) func(a fake_testing.Action) (handled bool, ret watch.Interface, err error)

SetupFakeWatcher helps set up a fake Kubernetes watcher

func TempFile

func TempFile(t *testing.T, prefix string, content []byte) string

TempFile creates a temporary file with a given content. Returns the file name.

func YamlObj added in v2.2.0

func YamlObj(t *testing.T) cmp.Option

YamlObj used in cmp.Diff, cmp.Equal to convert input from yaml string to map[string]any before comparison

Types

type ContainerState

type ContainerState int

type FakeAPIClient

type FakeAPIClient struct {
	client.CommonAPIClient

	ErrImageBuild   bool
	ErrImageInspect bool
	ErrImagePush    bool
	ErrImagePull    bool
	ErrImageList    bool
	ErrImageRemove  bool

	ErrStream  bool
	ErrVersion bool
	// will return the "test error" error on first <DUFails> DiskUsage calls
	DUFails int

	Built []types.ImageBuildOptions
	// ref -> [id]
	LocalImages map[string][]string
	// contains filtered or unexported fields
}

func (*FakeAPIClient) Add

func (f *FakeAPIClient) Add(tag, imageID string) *FakeAPIClient

func (*FakeAPIClient) Close

func (f *FakeAPIClient) Close() error

func (*FakeAPIClient) DiskUsage

func (*FakeAPIClient) DistributionInspect

func (f *FakeAPIClient) DistributionInspect(ctx context.Context, ref, encodedRegistryAuth string) (registry.DistributionInspect, error)

func (*FakeAPIClient) ImageBuild

func (*FakeAPIClient) ImageHistory added in v2.7.1

func (f *FakeAPIClient) ImageHistory(ctx context.Context, image string) ([]image.HistoryResponseItem, error)

func (*FakeAPIClient) ImageInspectWithRaw

func (f *FakeAPIClient) ImageInspectWithRaw(_ context.Context, refOrID string) (types.ImageInspect, []byte, error)

func (*FakeAPIClient) ImageList

func (*FakeAPIClient) ImageLoad

func (f *FakeAPIClient) ImageLoad(ctx context.Context, input io.Reader, quiet bool) (types.ImageLoadResponse, error)

func (*FakeAPIClient) ImagePull

func (*FakeAPIClient) ImagePush

func (*FakeAPIClient) ImageRemove

func (*FakeAPIClient) ImageTag

func (f *FakeAPIClient) ImageTag(_ context.Context, image, ref string) error

func (*FakeAPIClient) Info

func (*FakeAPIClient) Pulled

func (f *FakeAPIClient) Pulled() []string

func (*FakeAPIClient) Pushed

func (f *FakeAPIClient) Pushed() map[string]string

func (*FakeAPIClient) ServerVersion

func (f *FakeAPIClient) ServerVersion(ctx context.Context) (types.Version, error)

type FakeCmd

type FakeCmd struct {
	// contains filtered or unexported fields
}

func CmdRun

func CmdRun(command string) *FakeCmd

func CmdRunDirOut

func CmdRunDirOut(command string, dir string, output string) *FakeCmd

func CmdRunEnv

func CmdRunEnv(command string, env []string) *FakeCmd

func CmdRunErr

func CmdRunErr(command string, err error) *FakeCmd

func CmdRunInputOut

func CmdRunInputOut(command string, input string, output string) *FakeCmd

func CmdRunOut

func CmdRunOut(command string, output string) *FakeCmd

func CmdRunOutErr

func CmdRunOutErr(command string, output string, err error) *FakeCmd

func CmdRunOutOnce added in v2.2.0

func CmdRunOutOnce(command string, output string) *FakeCmd

func CmdRunWithOutput

func CmdRunWithOutput(command, output string) *FakeCmd

CmdRunWithOutput programs the fake runner with a command and expected output

func (*FakeCmd) AndRun

func (c *FakeCmd) AndRun(command string) *FakeCmd

func (*FakeCmd) AndRunDirOut

func (c *FakeCmd) AndRunDirOut(command string, dir string, output string) *FakeCmd

func (*FakeCmd) AndRunEnv

func (c *FakeCmd) AndRunEnv(command string, env []string) *FakeCmd

func (*FakeCmd) AndRunErr

func (c *FakeCmd) AndRunErr(command string, err error) *FakeCmd

func (*FakeCmd) AndRunInput

func (c *FakeCmd) AndRunInput(command, input string) *FakeCmd

func (*FakeCmd) AndRunInputOut

func (c *FakeCmd) AndRunInputOut(command string, input string, output string) *FakeCmd

func (*FakeCmd) AndRunOut

func (c *FakeCmd) AndRunOut(command string, output string) *FakeCmd

func (*FakeCmd) AndRunOutErr

func (c *FakeCmd) AndRunOutErr(command string, output string, err error) *FakeCmd

func (*FakeCmd) AndRunOutOnce added in v2.2.0

func (c *FakeCmd) AndRunOutOnce(command string, output string) *FakeCmd

func (*FakeCmd) AndRunWithOutput

func (c *FakeCmd) AndRunWithOutput(command, output string) *FakeCmd

AndRunWithOutput takes a command and an expected output. It expected to match up with a call to RunCmd, and pipes the provided output to RunCmd's exec.Cmd's stdout.

func (*FakeCmd) ForTest

func (c *FakeCmd) ForTest(t *testing.T)

func (*FakeCmd) RunCmd

func (c *FakeCmd) RunCmd(ctx context.Context, cmd *exec.Cmd) error

func (*FakeCmd) RunCmdOut

func (c *FakeCmd) RunCmdOut(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

func (*FakeCmd) RunCmdOutOnce added in v2.2.0

func (c *FakeCmd) RunCmdOutOnce(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

func (*FakeCmd) TimesCalled

func (c *FakeCmd) TimesCalled() int

type FakeFileSystem

type FakeFileSystem struct {
	Files map[string][]byte
}

func (FakeFileSystem) Open

func (f FakeFileSystem) Open(name string) (fs.File, error)

func (FakeFileSystem) ReadFile

func (f FakeFileSystem) ReadFile(name string) ([]byte, error)

type ForTester

type ForTester interface {
	ForTest(t *testing.T)
}

type T

type T struct {
	*testing.T
}

func (*T) Chdir

func (t *T) Chdir(dir string)

func (*T) CheckContains

func (t *T) CheckContains(expected, actual string)

func (*T) CheckDeepEqual

func (t *T) CheckDeepEqual(expected, actual interface{}, opts ...cmp.Option)

func (*T) CheckElementsMatch

func (t *T) CheckElementsMatch(expected, actual interface{})

CheckElementsMatch validates that two given slices contain the same elements while disregarding their order. Elements of both slices have to be comparable by '=='

func (*T) CheckEmpty

func (t *T) CheckEmpty(actual interface{})

func (*T) CheckError

func (t *T) CheckError(shouldErr bool, err error)

func (*T) CheckErrorAndDeepEqual

func (t *T) CheckErrorAndDeepEqual(shouldErr bool, err error, expected, actual interface{}, opts ...cmp.Option)

func (*T) CheckErrorAndExitCode

func (t *T) CheckErrorAndExitCode(expectedCode int, err error)

func (*T) CheckErrorAndFailNow

func (t *T) CheckErrorAndFailNow(shouldErr bool, err error)

CheckErrorAndFailNow checks that the provided error complies with whether or not we expect an error and fails the test execution immediately if it does not. Useful for testing functions which return (obj interface{}, e error) and subsequent checks operate on `obj` assuming that it is not nil.

func (*T) CheckErrorContains

func (t *T) CheckErrorContains(message string, err error)

CheckErrorContains checks that an error is not nil and contains a given message.

func (*T) CheckFalse

func (t *T) CheckFalse(actual bool)

func (*T) CheckFileExistAndContent

func (t *T) CheckFileExistAndContent(filePath string, expectedContent []byte)

CheckFileExistAndContent checks if the given file path exists and the content is expected.

func (*T) CheckMapsMatch

func (t *T) CheckMapsMatch(expected, actual interface{})

CheckMapsMatch validates that two given maps contain the same key value pairs.

func (*T) CheckMatches

func (t *T) CheckMatches(pattern, actual string)

func (*T) CheckNil

func (t *T) CheckNil(actual interface{})

func (*T) CheckNoError

func (t *T) CheckNoError(err error)

func (*T) CheckNotNil

func (t *T) CheckNotNil(actual interface{})

func (*T) CheckTrue

func (t *T) CheckTrue(actual bool)

func (*T) CheckTypeEquality

func (t *T) CheckTypeEquality(expected, actual interface{})

func (*T) NewTempDir

func (t *T) NewTempDir() *TempDir

func (*T) Override

func (t *T) Override(dest, tmp interface{})

func (*T) RequireNoError

func (t *T) RequireNoError(err error)

func (*T) RequireNonNilResult

func (t *T) RequireNonNilResult(x interface{}, err error) interface{}

func (*T) SetArgs

func (t *T) SetArgs(args []string)

SetArgs override os.Args for the duration of a test.

func (*T) SetEnvs

func (t *T) SetEnvs(envs map[string]string)

SetEnvs takes a map of key values to set using t.Setenv and restore the environment variable to its original value after the test.

func (*T) SetStdin

func (t *T) SetStdin(content []byte)

SetStdin replaces os.Stdin with a given content.

func (*T) SetupFakeKubernetesContext

func (t *T) SetupFakeKubernetesContext(config api.Config)

SetupFakeKubernetesContext replaces the current Kubernetes configuration file to setup a fixed current context.

func (*T) TempFile

func (t *T) TempFile(prefix string, content []byte) string

func (*T) UnsetEnv added in v2.4.0

func (t *T) UnsetEnv(key string)

type TempDir

type TempDir struct {
	// contains filtered or unexported fields
}

TempDir offers actions on a temp directory.

func NewTempDir

func NewTempDir(t *testing.T) *TempDir

NewTempDir creates a temporary directory and a teardown function that should be called to properly delete the directory content.

func (*TempDir) Chdir

func (h *TempDir) Chdir() *TempDir

Chdir changes current directory to this temp directory.

func (*TempDir) Chtimes

func (h *TempDir) Chtimes(file string, t time.Time) *TempDir

Chtimes changes the times for a file in the temp directory.

func (*TempDir) List

func (h *TempDir) List() ([]string, error)

List lists all the files in the temp directory.

func (*TempDir) Mkdir

func (h *TempDir) Mkdir(dir string) *TempDir

Mkdir makes a sub-directory in the temp directory.

func (*TempDir) Path

func (h *TempDir) Path(file string) string

Path returns the path to a file in the temp directory.

func (*TempDir) Paths

func (h *TempDir) Paths(files ...string) []string

Paths returns the paths to a list of files in the temp directory.

func (*TempDir) Remove

func (h *TempDir) Remove(file string) *TempDir

Remove deletes a file from the temp directory.

func (*TempDir) Rename

func (h *TempDir) Rename(oldName, newName string) *TempDir

Rename renames a file from oldname to newname

func (*TempDir) Root

func (h *TempDir) Root() string

Root returns the temp directory.

func (h *TempDir) Symlink(dst, src string) *TempDir

Symlink creates a symlink.

func (*TempDir) Touch

func (h *TempDir) Touch(files ...string) *TempDir

Touch creates a list of empty files in the temp directory.

func (*TempDir) Write

func (h *TempDir) Write(file, content string) *TempDir

Write write content to a file in the temp directory.

func (*TempDir) WriteFiles

func (h *TempDir) WriteFiles(files map[string]string) *TempDir

WriteFiles write a list of files (path->content) in the temp directory.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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