testutils

package
v0.0.0-...-f952c9f Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package testutils implements helper functions for frequently needed functionality in tests.

Index

Constants

View Source
const (
	ProStatusErr      = "UP4W_PRO_STATUS_ERR"
	ProStatusBadJSON  = "UP4W_PRO_STATUS_BAD_JSON"
	ProStatusAttached = "UP4W_PRO_STATUS_ATTACHED"

	ProAttachErr = "UP4W_PRO_ATTACH_ERR"

	ProDetachBadJSON = "UP4W_PRO_DETACH_BAD_JSON"

	ProDetachErrAlreadyDetached = "UP4W_PRO_DETACH_ERR_ALREADY_DETACHED"
	ProDetachErrGeneric         = "UP4W_PRO_DETACH_ERR_GENERIC"
	ProDetachErrNoReason        = "UP4W_PRO_DETACH_ERR_UNKNOWN"

	LandscapeEnableErr  = "UP4W_LANDSCAPE_ENABLE_ERR"
	LandscapeDisableErr = "UP4W_LANDSCAPE_DISABLE_ERR"

	WslpathErr             = "UP4W_WSLPATH_ERR"
	WslpathBadOutput       = "UP4W_WSLPATH_BAD_OUTPUT"
	EmptyUserprofileEnvVar = "UP4W_EMPTY_USERPROFILE_ENV_VAR"

	CmdExeErr = "UP4W_CMDEXE_ERR"

	WslInfoErr   = "UP4W_WSLINFO_ERR"
	WslInfoIsNAT = "UP4W_WSLINFO_IS_NAT"

	// FileSystemRoot contains the path to the mocked filesystem root.
	FileSystemRoot = "UP4W_FILE_SYSTEM_ROOT"
)

Arguments that control how the mocked executable will behave. If none are provided, the mock will copy the behaviour of the real thing.

Variables

This section is empty.

Functions

func CmdExeMock

func CmdExeMock(t *testing.T)

CmdExeMock mocks the executable for `cmd.exe`. Add it to your package_test with:

func TestWithCmdExeMock(t *testing.T) { testutils.CmdExeMock(t) }

func LandscapeConfigMock

func LandscapeConfigMock(t *testing.T)

LandscapeConfigMock mocks the executable for `landscape-config`. Add it to your package_test with:

func TestWithLanscapeConfigExeMock(t *testing.T) { testutils.LanscapeConfigMock(t) }

func MockFilesystemRoot

func MockFilesystemRoot(t *testing.T) (rootDir string)

MockFilesystemRoot sets up a skelleton filesystem with files used by the wsl-pro-service and returns its root dir.

func ProMock

func ProMock(t *testing.T)

ProMock mocks the executable for `pro`. Add it to your package_test with:

func TestWithProMock(t *testing.T) { testutils.ProMock(t) }

func WslInfoMock

func WslInfoMock(t *testing.T)

WslInfoMock mocks the executable for `wslinfo`. Add it to your package_test with:

func TestWithWslInfoMock(t *testing.T) { testutils.WslInfoMock(t) }

func WslPathMock

func WslPathMock(t *testing.T)

WslPathMock mocks the executable for `wslpath`. Add it to your package_test with:

func TestWithWslPathMock(t *testing.T) { testutils.WslPathMock(t) }

Types

type MockWindowsAgent

type MockWindowsAgent struct {
	Server   *grpc.Server
	Service  *mockWSLInstanceService
	Listener net.Listener

	ClientCredentials credentials.TransportCredentials

	Started chan struct{}
	Stopped chan struct{}
}

MockWindowsAgent mocks the windows agent server.

func NewMockWindowsAgent

func NewMockWindowsAgent(t *testing.T, ctx context.Context, publicDir string) *MockWindowsAgent

MockWindowsAgent mocks the windows-agent. It starts a GRPC service that will perform the port dance and stay connected. It'll write the port file as well. For simplicity's sake, it only suports one WSL distro at a time.

You can stop it manually, otherwise it'll stop during cleanup.

func (*MockWindowsAgent) Stop

func (m *MockWindowsAgent) Stop()

Stop releases all resources associated with the MockWindowsAgent.

type SystemMock

type SystemMock struct {
	// FsRoot is the path to what will be used as root for the test filesystem
	FsRoot string

	// DistroHostname is the hostname of the distro. Make nil to cause an error.
	DistroHostname *string

	// WslDistroNameEnv is the value that the mocked Getenv(WSL_DISTRO_NAME) or wslpath -w / will display
	WslDistroName string

	// WslDistroNameEnvEnabled sets the mocked WSL_DISTRO_NAME to $WslDistroName when true, and to an empty
	// string when false
	WslDistroNameEnvEnabled bool

	// LookupGroupError makes the LookupGroup function fail.
	LandscapeGroupGID string
	// contains filtered or unexported fields
}

SystemMock is used to override system's behaviour. Its control parameters are not thread safe. You can modify them in test setup, but after that you risk a race.

func MockSystem

func MockSystem(t *testing.T) (*system.System, *SystemMock)

MockSystem sets up a few mocks: - filesystem and mock executables for wslpath, pro.

func (*SystemMock) CmdExe

func (m *SystemMock) CmdExe(ctx context.Context, path string, args ...string) *exec.Cmd

CmdExe mocks `cmd.exe $args...`.

func (*SystemMock) DefaultPublicDir

func (m *SystemMock) DefaultPublicDir() string

DefaultPublicDir is the location where a mocked system will expect the addr file to be located, and its containing directory will be created in New().

func (*SystemMock) GetenvWslDistroName

func (m *SystemMock) GetenvWslDistroName() string

GetenvWslDistroName mocks os.GetEnv("WSL_DISTRO_NAME").

func (SystemMock) Hostname

func (m SystemMock) Hostname() (string, error)

Hostname returns a mock hostname.

func (*SystemMock) LandscapeConfigExecutable

func (m *SystemMock) LandscapeConfigExecutable(ctx context.Context, args ...string) *exec.Cmd

LandscapeConfigExecutable mocks `landscape-config $q`.

func (*SystemMock) LookupGroup

func (m *SystemMock) LookupGroup(name string) (*user.Group, error)

LookupGroup mocks the user.LookupGroup function.

func (*SystemMock) Path

func (m *SystemMock) Path(path ...string) string

Path prepends FsRoot to a path.

func (*SystemMock) ProExecutable

func (m *SystemMock) ProExecutable(ctx context.Context, args ...string) *exec.Cmd

ProExecutable mocks `pro $args...`.

func (*SystemMock) SetControlArg

func (m *SystemMock) SetControlArg(arg controlArg)

SetControlArg adds control arguments to the mock executables.

func (*SystemMock) WslinfoExecutable

func (m *SystemMock) WslinfoExecutable(ctx context.Context, args ...string) *exec.Cmd

WslinfoExecutable mocks `wslinfo $args...`.

func (*SystemMock) WslpathExecutable

func (m *SystemMock) WslpathExecutable(ctx context.Context, args ...string) *exec.Cmd

WslpathExecutable mocks `wslpath $args...`.

Jump to

Keyboard shortcuts

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