Documentation ¶
Overview ¶
Package testutils implements helper functions for frequently needed functionality in tests.
Index ¶
- Constants
- func CmdExeMock(t *testing.T)
- func LandscapeConfigMock(t *testing.T)
- func MockFilesystemRoot(t *testing.T) (rootDir string)
- func ProMock(t *testing.T)
- func WslInfoMock(t *testing.T)
- func WslPathMock(t *testing.T)
- type MockWindowsAgent
- type SystemMock
- func (m *SystemMock) CmdExe(ctx context.Context, path string, args ...string) *exec.Cmd
- func (m *SystemMock) DefaultPublicDir() string
- func (m *SystemMock) GetenvWslDistroName() string
- func (m SystemMock) Hostname() (string, error)
- func (m *SystemMock) LandscapeConfigExecutable(ctx context.Context, args ...string) *exec.Cmd
- func (m *SystemMock) LookupGroup(name string) (*user.Group, error)
- func (m *SystemMock) Path(path ...string) string
- func (m *SystemMock) ProExecutable(ctx context.Context, args ...string) *exec.Cmd
- func (m *SystemMock) SetControlArg(arg controlArg)
- func (m *SystemMock) WslinfoExecutable(ctx context.Context, args ...string) *exec.Cmd
- func (m *SystemMock) WslpathExecutable(ctx context.Context, args ...string) *exec.Cmd
Constants ¶
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 ¶
CmdExeMock mocks the executable for `cmd.exe`. Add it to your package_test with:
func TestWithCmdExeMock(t *testing.T) { testutils.CmdExeMock(t) }
func LandscapeConfigMock ¶
LandscapeConfigMock mocks the executable for `landscape-config`. Add it to your package_test with:
func TestWithLanscapeConfigExeMock(t *testing.T) { testutils.LanscapeConfigMock(t) }
func MockFilesystemRoot ¶
MockFilesystemRoot sets up a skelleton filesystem with files used by the wsl-pro-service and returns its root dir.
func ProMock ¶
ProMock mocks the executable for `pro`. Add it to your package_test with:
func TestWithProMock(t *testing.T) { testutils.ProMock(t) }
func WslInfoMock ¶
WslInfoMock mocks the executable for `wslinfo`. Add it to your package_test with:
func TestWithWslInfoMock(t *testing.T) { testutils.WslInfoMock(t) }
func WslPathMock ¶
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 ¶
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) 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 ¶
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 ¶
ProExecutable mocks `pro $args...`.
func (*SystemMock) SetControlArg ¶
func (m *SystemMock) SetControlArg(arg controlArg)
SetControlArg adds control arguments to the mock executables.
func (*SystemMock) WslinfoExecutable ¶
WslinfoExecutable mocks `wslinfo $args...`.
func (*SystemMock) WslpathExecutable ¶
WslpathExecutable mocks `wslpath $args...`.