Skip to content

Commit

Permalink
Update tests to assert ErrNotExist is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Mar 12, 2024
1 parent 6b55874 commit 1183e39
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
64 changes: 46 additions & 18 deletions distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,22 @@ func TestTerminate(t *testing.T) {
}

testCases := map[string]struct {
mockErr bool
mockErr bool
dontRegister bool

wantErr bool
wantErr bool
wantErrNotExist bool
}{
"Success": {},

// Mock-induced errors
"Error because wsl.exe returns an error": {mockErr: true, wantErr: true},
"Error because the distro is not registered": {dontRegister: true, wantErr: true, wantErrNotExist: true},
"Error because wsl.exe returns an error": {mockErr: true, wantErr: true},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
ctx, modifyMock := setupBackend(t, context.Background())
testDistro := newTestDistro(t, ctx, rootFS)
controlDistro := newTestDistro(t, ctx, rootFS)

if tc.mockErr {
modifyMock(t, func(m *mock.Backend) {
Expand All @@ -90,12 +91,23 @@ func TestTerminate(t *testing.T) {
defer modifyMock(t, (*mock.Backend).ResetErrors)
}

wakeDistroUp(t, testDistro)
controlDistro := newTestDistro(t, ctx, rootFS)

var testDistro wsl.Distro
if tc.dontRegister {
testDistro = wsl.NewDistro(ctx, uniqueDistroName(t))
} else {
testDistro = newTestDistro(t, ctx, rootFS)
wakeDistroUp(t, testDistro)
}
wakeDistroUp(t, controlDistro)

err := testDistro.Terminate()
if tc.wantErr {
require.Error(t, err, "Terminate should have returned an error")
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "Terminate error should have been ErrNotExist")
}
return
}
require.NoError(t, err, "Terminate should have returned no error")
Expand Down Expand Up @@ -182,11 +194,12 @@ func TestDistroSetAsDefault(t *testing.T) {
nonRegisteredDistro bool
wslexeError bool

wantErr bool
wantErr bool
wantErrNotExist bool
}{
"Success setting an existing distro as default": {},

"Error when setting non-existent distro as default": {nonRegisteredDistro: true, wantErr: true},
"Error when setting non-existent distro as default": {nonRegisteredDistro: true, wantErr: true, wantErrNotExist: true},

// Mock-induced errors
"Error when wsl.exe errors out": {wslexeError: true, wantErr: true},
Expand All @@ -212,6 +225,9 @@ func TestDistroSetAsDefault(t *testing.T) {
err := d.SetAsDefault()
if tc.wantErr {
require.Errorf(t, err, "Unexpected success setting non-existent distro %q as default", d.Name())
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "SetAsDefault should have returned ErrNotExist")
}
return
}
require.NoErrorf(t, err, "Unexpected error setting %q as default", d.Name())
Expand Down Expand Up @@ -281,11 +297,12 @@ func TestGUID(t *testing.T) {
distro *wsl.Distro
registryInaccessible bool

wantErr bool
wantErr bool
wantNotExistErr bool
}{
"Success with a real distro": {distro: &realDistro},

"Error with a non-registered distro": {distro: &fakeDistro, wantErr: true},
"Error with a non-registered distro": {distro: &fakeDistro, wantErr: true, wantNotExistErr: true},
"Error with an invalid distro name": {distro: &wrongDistro, wantErr: true},

// Mock-induced errors
Expand All @@ -307,6 +324,9 @@ func TestGUID(t *testing.T) {
guid, err := tc.distro.GUID()
if tc.wantErr {
require.Error(t, err, "Unexpected success obtaining GUID of non-eligible distro")
if tc.wantNotExistErr {
require.ErrorIs(t, err, wsl.ErrNotExist, "GUID error should have been ErrNotExist")
}
return
}
require.NoError(t, err, "could not obtain GUID")
Expand Down Expand Up @@ -339,30 +359,31 @@ func TestConfigurationSetters(t *testing.T) {
distro distroType
syscallError bool

wantErr bool
wantErr bool
wantErrNotExist bool
}{
// DefaultUID
"Success setting DefaultUID": {setting: DefaultUID},
"Error when setting DefaultUID: \\0 in name": {setting: DefaultUID, distro: DistroInvalidName, wantErr: true},
"Error when setting DefaultUID: not registered": {setting: DefaultUID, distro: DistroNotRegistered, wantErr: true},
"Error when setting DefaultUID: not registered": {setting: DefaultUID, distro: DistroNotRegistered, wantErr: true, wantErrNotExist: true},
"Error when setting DefaultUID: syscall errors out": {setting: DefaultUID, syscallError: true, wantErr: true},

// InteropEnabled
"Success setting InteropEnabled": {setting: InteropEnabled},
"Error when setting InteropEnabled: \\0 in name": {setting: InteropEnabled, distro: DistroInvalidName, wantErr: true},
"Error when setting InteropEnabled: not registered": {setting: InteropEnabled, distro: DistroNotRegistered, wantErr: true},
"Error when setting InteropEnabled: not registered": {setting: InteropEnabled, distro: DistroNotRegistered, wantErr: true, wantErrNotExist: true},
"Error when setting InteropEnabled: syscall errors out": {setting: InteropEnabled, syscallError: true, wantErr: true},

// PathAppended
"Success setting PathAppended": {setting: PathAppend},
"Error when setting PathAppended: \\0 in name": {setting: PathAppend, distro: DistroInvalidName, wantErr: true},
"Error when setting PathAppended: not registered": {setting: PathAppend, distro: DistroNotRegistered, wantErr: true},
"Error when setting PathAppended: not registered": {setting: PathAppend, distro: DistroNotRegistered, wantErr: true, wantErrNotExist: true},
"Error when setting PathAppended: syscall errors out": {setting: PathAppend, syscallError: true, wantErr: true},

// DriveMountingEnabled
"Success setting DriveMountingEnabled": {setting: DriveMounting},
"Error when setting DriveMountingEnabled: \\0 in name": {setting: DriveMounting, distro: DistroInvalidName, wantErr: true},
"Error when setting DriveMountingEnabled: not registered": {setting: DriveMounting, distro: DistroNotRegistered, wantErr: true},
"Error when setting DriveMountingEnabled: not registered": {setting: DriveMounting, distro: DistroNotRegistered, wantErr: true, wantErrNotExist: true},
"Error when setting DriveMountingEnabled: syscall errors out": {setting: DriveMounting, syscallError: true, wantErr: true},
}

Expand Down Expand Up @@ -439,7 +460,10 @@ func TestConfigurationSetters(t *testing.T) {
err = d.DriveMountingEnabled(false)
}
if tc.wantErr {
require.Errorf(t, err, "unexpected failure when setting config %s", details[tc.setting].name)
require.Errorf(t, err, "unexpected success when setting config %s", details[tc.setting].name)
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "expected setter to return ErrNotExist")
}
return
}
require.NoErrorf(t, err, "unexpected success when setting config %s", details[tc.setting].name)
Expand Down Expand Up @@ -488,11 +512,12 @@ func TestGetConfiguration(t *testing.T) {
distroName string // Note: distros with custom distro names will not be registered
syscallError bool

wantErr bool
wantErr bool
wantErrNotExist bool
}{
"Success": {},

"Error with non-registered distro": {distroName: "IAmNotRegistered", wantErr: true},
"Error with non-registered distro": {distroName: "IAmNotRegistered", wantErr: true, wantErrNotExist: true},
"Error with null character in name": {distroName: "MyName\x00IsNotValid", wantErr: true},

// Mock-induced errors
Expand Down Expand Up @@ -520,6 +545,9 @@ func TestGetConfiguration(t *testing.T) {

if tc.wantErr {
require.Error(t, err, "unexpected success in GetConfiguration")
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "expected GetConfiguration to return ErrNotExist")
}
return
}
require.NoError(t, err, "unexpected failure in GetConfiguration")
Expand Down
26 changes: 18 additions & 8 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ func TestCommandRun(t *testing.T) {
syscallErr bool
registryInaccessible bool

wantError bool
wantExitCode int
wantError bool
wantExitCode int
wantErrNotExist bool
}{
// Background context test cases
"Success": {cmd: "exit 0"},
"Error with a non-registered distro": {cmd: "exit 0", fakeDistro: true, wantError: true},
"Error with a non-registered distro": {cmd: "exit 0", fakeDistro: true, wantError: true, wantErrNotExist: true},
"Error when the command's exit code is non-zero": {cmd: "exit 42", wantError: true, wantExitCode: 42},
"Error when the command has invalid characters": {cmd: "echo \x00", fakeDistro: true, wantError: true},

Expand Down Expand Up @@ -124,6 +125,9 @@ func TestCommandRun(t *testing.T) {

if !tc.wantError {
require.NoError(t, err, "did not expect Run() to return an error")
if tc.wantErrNotExist {
require.ErrorIsf(t, err, wsl.ErrNotExist, "expected Run() to return an ErrNotExist error")
}
return
}

Expand Down Expand Up @@ -187,16 +191,17 @@ func TestCommandStartWait(t *testing.T) {
syscallErr bool
registryInaccessible bool

wantStdout string
wantStderr string
wantErrOn when
wantExitError int
wantStdout string
wantStderr string
wantErrOn when
wantExitError int
wantErrNotExist bool
}

testCases := map[string]testCase{
// Background context
"Success": {distro: &realDistro, cmd: "exit 0"},
"Error with a non-registered distro": {distro: &fakeDistro, cmd: "exit 0", wantErrOn: AfterStart},
"Error with a non-registered distro": {distro: &fakeDistro, cmd: "exit 0", wantErrOn: AfterStart, wantErrNotExist: true},
"Error with null char in distro name": {distro: &wrongDistro, cmd: "exit 0", wantErrOn: AfterStart},
"Error when the command's exit code is non-zero": {distro: &realDistro, cmd: "exit 42", wantErrOn: AfterWait, wantExitError: 42},

Expand Down Expand Up @@ -250,6 +255,11 @@ func TestCommandStartWait(t *testing.T) {

// Ensure that we don't get an ExitError
notErrorAsf(t, err, &target, "Unexpected error type at time %s. Expected anything but an ExitError.", whenToString(now))

if tc.wantErrNotExist {
require.ErrorIsf(t, err, wsl.ErrNotExist, "Unexpected error type at time %s. Expected ErrNotExist.", whenToString(now))
}

return true
}

Expand Down
16 changes: 12 additions & 4 deletions registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ func TestUnregister(t *testing.T) {
syscallError bool
registryInaccessible bool

wantError bool
wantError bool
wantErrNotExist bool
}{
"Success": {},

"Error with a non-registered distro": {nonRegistered: true, wantError: true},
"Error with a non-registered distro": {nonRegistered: true, wantError: true, wantErrNotExist: true},
"Error with a null char in name": {nonRegistered: true, distroname: "This Distro \x00 has a null char", wantError: true},

// Mock-induced errors
Expand Down Expand Up @@ -234,6 +235,9 @@ func TestUnregister(t *testing.T) {

if tc.wantError {
require.Errorf(t, err, "Unexpected success in unregistering distro %q.", d.Name())
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "Unregister should have returned ErrNotExist")
}
return
}
require.NoError(t, err, "Unexpected failure in unregistering distro %q.", d.Name())
Expand Down Expand Up @@ -353,13 +357,14 @@ func TestUninstall(t *testing.T) {
mockCannotOpenRegistry bool
mockOnly bool

wantError bool
wantError bool
wantErrNotExist bool
}{
"Success uninstalling an Appx-installed distro": {distroInstallType: registeredFromAppx},
"Success uninstalling an imported distro": {distroInstallType: imported},

// Usage errors
"Error uninstalling a non-registered distro": {distroInstallType: notRegistered, wantError: true},
"Error uninstalling a non-registered distro": {distroInstallType: notRegistered, wantError: true, wantErrNotExist: true},
"Error when the context is cancelled beforehand": {distroInstallType: registeredFromAppx, preCancelCtx: true, wantError: true},

// Mock-triggered errors
Expand Down Expand Up @@ -415,6 +420,9 @@ func TestUninstall(t *testing.T) {
err := d.Uninstall(uninstallCtx)
if tc.wantError {
require.Error(t, err, "Uninstall should return an error")
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "Uninstall should return ErrNotExist")
}
return
}

Expand Down
10 changes: 7 additions & 3 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ func TestShell(t *testing.T) {
distro *wsl.Distro
syscallErr bool

wantError bool
wantExitCode uint32
wantError bool
wantExitCode uint32
wantErrNotExist bool
}{
// Test with no arguments
"Success": {distro: &realDistro},
"Error with a non-registered distro": {distro: &fakeDistro, wantError: true},
"Error with a non-registered distro": {distro: &fakeDistro, wantError: true, wantErrNotExist: true},
"Error with distroname with a null character": {distro: &wrongDistro, wantError: true},

// Mock-induced errors
Expand Down Expand Up @@ -104,6 +105,9 @@ func TestShell(t *testing.T) {
var target *wsl.ShellError
if tc.wantExitCode == 0 {
notErrorAsf(t, err, &target, "unexpected ShellError, expected any other type")
if tc.wantErrNotExist {
require.ErrorIs(t, err, wsl.ErrNotExist, "unexpected error type for Shell, expected a ErrNotExist")
}
return
}

Expand Down

0 comments on commit 1183e39

Please sign in to comment.