Skip to content

Commit 2633ece

Browse files
Adapted tests to mocking mechanism
1 parent b5f21c1 commit 2633ece

8 files changed

+165
-91
lines changed

distro_test.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ import (
1515
)
1616

1717
func TestShutdown(t *testing.T) {
18-
d := newTestDistro(t, rootFs) // Will terminate
18+
ctx := testContext(context.Background())
19+
20+
d := newTestDistro(t, ctx, rootFs) // Will terminate
1921

2022
defer startTestLinuxProcess(t, &d)()
2123

22-
err := wsl.Shutdown()
24+
err := wsl.Shutdown(ctx)
2325
require.NoError(t, err, "Unexpected error attempting to shut down")
2426

2527
require.False(t, isTestLinuxProcessAlive(&d), "Process was not killed by shutting down.")
2628
}
2729

2830
func TestTerminate(t *testing.T) {
29-
sampleDistro := newTestDistro(t, rootFs) // Will terminate
30-
controlDistro := newTestDistro(t, rootFs) // Will not terminate, used to assert other distros are unaffected
31+
ctx := testContext(context.Background())
32+
33+
sampleDistro := newTestDistro(t, ctx, rootFs) // Will terminate
34+
controlDistro := newTestDistro(t, ctx, rootFs) // Will not terminate, used to assert other distros are unaffected
3135

3236
defer startTestLinuxProcess(t, &sampleDistro)()
3337
defer startTestLinuxProcess(t, &controlDistro)()
3438

35-
err := sampleDistro.Terminate()
39+
err := sampleDistro.Terminate(ctx)
3640
require.NoError(t, err, "Unexpected error attempting to terminate")
3741

3842
require.False(t, isTestLinuxProcessAlive(&sampleDistro), "Process was not killed by termination.")
@@ -75,21 +79,25 @@ func isTestLinuxProcessAlive(d *wsl.Distro) bool {
7579
}
7680

7781
func TestDefaultDistro(t *testing.T) {
78-
want := newTestDistro(t, emptyRootFs)
82+
ctx := testContext(context.Background())
83+
84+
want := newTestDistro(t, ctx, emptyRootFs)
7985

8086
//nolint:gosec // G204: Subprocess launched with a potential tainted input or cmd arguments.
8187
// No threat of code injection, want.Name() is generated by func UniqueDistroName.
8288
out, err := exec.Command("wsl.exe", "--set-default", want.Name()).CombinedOutput()
8389
require.NoErrorf(t, err, "setup: failed to set test distro as default: %s", out)
8490

85-
got, err := wsl.DefaultDistro()
91+
got, err := wsl.DefaultDistro(ctx)
8692
require.NoError(t, err, "unexpected error getting default distro %q", want.Name())
8793
require.Equal(t, want, got, "Unexpected mismatch in default distro")
8894
}
8995

9096
func TestDistroSetAsDefault(t *testing.T) {
91-
realDistro := newTestDistro(t, emptyRootFs)
92-
fakeDistro := wsl.NewDistro("This distro sure does not exist")
97+
ctx := testContext(context.Background())
98+
99+
realDistro := newTestDistro(t, ctx, emptyRootFs)
100+
fakeDistro := wsl.NewDistro(ctx, "This distro sure does not exist")
93101

94102
testCases := map[string]struct {
95103
distro wsl.Distro
@@ -102,7 +110,8 @@ func TestDistroSetAsDefault(t *testing.T) {
102110
for name, tc := range testCases {
103111
tc := tc
104112
t.Run(name, func(t *testing.T) {
105-
err := tc.distro.SetAsDefault()
113+
err := tc.distro.SetAsDefault(ctx)
114+
106115
if tc.wantErr {
107116
require.Errorf(t, err, "Unexpected success setting non-existent distro %q as default", tc.distro.Name())
108117
return
@@ -117,9 +126,11 @@ func TestDistroSetAsDefault(t *testing.T) {
117126
}
118127

119128
func TestDistroString(t *testing.T) {
120-
realDistro := newTestDistro(t, rootFs)
121-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
122-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "_\x00_invalid_name")
129+
ctx := testContext(context.Background())
130+
131+
realDistro := newTestDistro(t, ctx, rootFs)
132+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
133+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"_\x00_invalid_name")
123134

124135
realID, err := realDistro.GUID()
125136
require.NoError(t, err, "could not get the test distro's GUID")
@@ -184,10 +195,11 @@ func TestGUID(t *testing.T) {
184195
// format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", but syscall.GUID
185196
// does not have such method and prints its contents like any other
186197
// struct.
198+
ctx := testContext(context.Background())
187199

188-
realDistro := wsl.NewDistro(uniqueDistroName(t))
189-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
190-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "\x00invalidcharacter")
200+
realDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
201+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
202+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"\x00invalidcharacter")
191203

192204
err := realDistro.Register(emptyRootFs)
193205
require.NoError(t, err, "could not register empty distro")
@@ -290,6 +302,8 @@ func TestConfigurationSetters(t *testing.T) {
290302
// 1. Changes one of the default settings and asserts that it has changed, and the others have not.
291303
// 2. It changes this setting back to the default, and asserts that it has changed, and the others have not.
292304

305+
ctx := testContext(context.Background())
306+
293307
// details has info about each of the settings
294308
details := map[testedSetting]settingDetails{
295309
DefaultUID: {name: "DefaultUID", byDefault: uint32(0), want: uint32(0)},
@@ -310,13 +324,13 @@ func TestConfigurationSetters(t *testing.T) {
310324
var d wsl.Distro
311325
switch tc.distro {
312326
case DistroRegistered:
313-
d = newTestDistro(t, rootFs)
327+
d = newTestDistro(t, ctx, rootFs)
314328
err := d.Command(context.Background(), "useradd testuser").Run()
315329
require.NoError(t, err, "unexpectedly failed to add a user to the distro")
316330
case DistroNotRegistered:
317-
d = wsl.NewDistro(uniqueDistroName(t))
331+
d = wsl.NewDistro(ctx, uniqueDistroName(t))
318332
case DistroInvalidName:
319-
d = wsl.NewDistro("Wrong character \x00 in name")
333+
d = wsl.NewDistro(ctx, "Wrong character \x00 in name")
320334
}
321335

322336
// Test changing a setting
@@ -379,7 +393,8 @@ func TestConfigurationSetters(t *testing.T) {
379393
}
380394
}
381395
func TestGetConfiguration(t *testing.T) {
382-
d := newTestDistro(t, rootFs)
396+
ctx := testContext(context.Background())
397+
d := newTestDistro(t, ctx, rootFs)
383398

384399
testCases := map[string]struct {
385400
distroName string
@@ -393,7 +408,9 @@ func TestGetConfiguration(t *testing.T) {
393408
for name, tc := range testCases {
394409
tc := tc
395410
t.Run(name, func(t *testing.T) {
396-
d := wsl.NewDistro(tc.distroName)
411+
ctx := testContext(context.Background())
412+
413+
d := wsl.NewDistro(ctx, tc.distroName)
397414
c, err := d.GetConfiguration()
398415

399416
if tc.wantErr {

exec_test.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
)
1818

1919
func TestCommandRun(t *testing.T) {
20-
realDistro := newTestDistro(t, rootFs)
21-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
20+
ctx := testContext(context.Background())
21+
22+
realDistro := newTestDistro(t, ctx, rootFs)
23+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
2224

2325
// Keeping distro awake so there are no unexpected timeouts
2426
defer keepAwake(t, context.Background(), &realDistro)()
@@ -116,9 +118,11 @@ func TestCommandRun(t *testing.T) {
116118
}
117119

118120
func TestCommandStartWait(t *testing.T) {
119-
realDistro := newTestDistro(t, rootFs)
120-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
121-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "--IHaveA\x00NullChar!")
121+
ctx := testContext(context.Background())
122+
123+
realDistro := newTestDistro(t, ctx, rootFs)
124+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
125+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"--IHaveA\x00NullChar!")
122126

123127
// Keeping distro awake so there are no unexpected timeouts
124128
defer keepAwake(t, context.Background(), &realDistro)()
@@ -315,7 +319,8 @@ func bufferPipeOutput(t *testing.T, cmd *wsl.Cmd, pipeName string) *bytes.Buffer
315319
}
316320

317321
func TestCommandOutPipes(t *testing.T) {
318-
d := newTestDistro(t, rootFs)
322+
ctx := testContext(context.Background())
323+
d := newTestDistro(t, ctx, rootFs)
319324

320325
type stream int
321326
const (
@@ -398,9 +403,11 @@ func TestCommandOutPipes(t *testing.T) {
398403
}
399404

400405
func TestCommandOutput(t *testing.T) {
401-
realDistro := newTestDistro(t, rootFs)
402-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
403-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "--IHaveA\x00NullChar!")
406+
ctx := testContext(context.Background())
407+
408+
realDistro := newTestDistro(t, ctx, rootFs)
409+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
410+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"--IHaveA\x00NullChar!")
404411

405412
testCases := map[string]struct {
406413
distro *wsl.Distro
@@ -457,9 +464,11 @@ func TestCommandOutput(t *testing.T) {
457464
}
458465

459466
func TestCommandCombinedOutput(t *testing.T) {
460-
realDistro := newTestDistro(t, rootFs)
461-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
462-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "--IHaveA\x00NullChar!")
467+
ctx := testContext(context.Background())
468+
469+
realDistro := newTestDistro(t, ctx, rootFs)
470+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
471+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"--IHaveA\x00NullChar!")
463472

464473
testCases := map[string]struct {
465474
distro *wsl.Distro
@@ -520,7 +529,8 @@ func TestCommandCombinedOutput(t *testing.T) {
520529
}
521530

522531
func TestCommandStdin(t *testing.T) {
523-
d := newTestDistro(t, rootFs)
532+
ctx := testContext(context.Background())
533+
d := newTestDistro(t, ctx, rootFs)
524534

525535
const (
526536
readFromPipe int = iota

internal_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,33 @@ import (
66

77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
9+
"github.com/ubuntu/gowsl/internal/flags"
910
)
1011

1112
func TestUnpackFlags(t *testing.T) {
1213
t.Parallel()
1314

1415
tests := []struct {
1516
wants Configuration
16-
input wslFlags
17+
input flags.WslFlags
1718
}{
18-
{input: wslFlags(0x0), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
19-
{input: wslFlags(0x1), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
20-
{input: wslFlags(0x2), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
21-
{input: wslFlags(0x3), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
22-
{input: wslFlags(0x4), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
23-
{input: wslFlags(0x5), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
24-
{input: wslFlags(0x6), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
25-
{input: wslFlags(0x7), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
19+
{input: flags.WslFlags(0x0), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
20+
{input: flags.WslFlags(0x1), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
21+
{input: flags.WslFlags(0x2), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
22+
{input: flags.WslFlags(0x3), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
23+
{input: flags.WslFlags(0x4), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
24+
{input: flags.WslFlags(0x5), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
25+
{input: flags.WslFlags(0x6), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
26+
{input: flags.WslFlags(0x7), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
2627
// The following may be encountered due to an undocumented fourth flag
27-
{input: wslFlags(0x8), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
28-
{input: wslFlags(0x9), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
29-
{input: wslFlags(0xa), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
30-
{input: wslFlags(0xb), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
31-
{input: wslFlags(0xc), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
32-
{input: wslFlags(0xd), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
33-
{input: wslFlags(0xe), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
34-
{input: wslFlags(0xf), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
28+
{input: flags.WslFlags(0x8), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
29+
{input: flags.WslFlags(0x9), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
30+
{input: flags.WslFlags(0xa), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
31+
{input: flags.WslFlags(0xb), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
32+
{input: flags.WslFlags(0xc), wants: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
33+
{input: flags.WslFlags(0xd), wants: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
34+
{input: flags.WslFlags(0xe), wants: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
35+
{input: flags.WslFlags(0xf), wants: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
3536
}
3637

3738
for _, tc := range tests {
@@ -51,16 +52,16 @@ func TestPackFlags(t *testing.T) {
5152
t.Parallel()
5253
tests := []struct {
5354
input Configuration
54-
wants wslFlags
55+
wants flags.WslFlags
5556
}{
56-
{wants: wslFlags(0x0), input: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
57-
{wants: wslFlags(0x1), input: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
58-
{wants: wslFlags(0x2), input: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
59-
{wants: wslFlags(0x3), input: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
60-
{wants: wslFlags(0x4), input: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
61-
{wants: wslFlags(0x5), input: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
62-
{wants: wslFlags(0x6), input: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
63-
{wants: wslFlags(0x7), input: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
57+
{wants: flags.WslFlags(0x0), input: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}},
58+
{wants: flags.WslFlags(0x1), input: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}},
59+
{wants: flags.WslFlags(0x2), input: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}},
60+
{wants: flags.WslFlags(0x3), input: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}},
61+
{wants: flags.WslFlags(0x4), input: Configuration{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}},
62+
{wants: flags.WslFlags(0x5), input: Configuration{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}},
63+
{wants: flags.WslFlags(0x6), input: Configuration{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}},
64+
{wants: flags.WslFlags(0x7), input: Configuration{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}},
6465
}
6566

6667
for _, tc := range tests {

main_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const (
2828

2929
func TestMain(m *testing.M) {
3030
restore, err := backUpDefaultDistro()
31+
ctx := testContext(context.Background())
32+
3133
if err != nil {
3234
log.Errorf("setup: %v", err)
3335
os.Exit(1)
@@ -36,11 +38,11 @@ func TestMain(m *testing.M) {
3638

3739
exitVal := m.Run()
3840

39-
err = wsl.Shutdown()
41+
err = wsl.Shutdown(ctx)
4042
if err != nil {
4143
log.Warnf("cleanup: Failed to shutdown WSL")
4244
}
43-
cleanUpTestWslInstances()
45+
cleanUpTestWslInstances(ctx)
4446

4547
os.Exit(exitVal)
4648
}
@@ -60,9 +62,11 @@ func uniqueDistroName(t *testing.T) string {
6062
t.Helper()
6163
const maxAttempts = 10
6264
for i := 0; i < maxAttempts; i++ {
65+
ctx := testContext(context.Background())
66+
6367
//nolint:gosec
6468
// No need for this random number to be cryptographically secure
65-
d := wsl.NewDistro(sanitizeDistroName(fmt.Sprintf("%s_%s_%d", namePrefix, t.Name(), rand.Uint32())))
69+
d := wsl.NewDistro(ctx, sanitizeDistroName(fmt.Sprintf("%s_%s_%d", namePrefix, t.Name(), rand.Uint32())))
6670

6771
// Ensuring no name collision
6872
exists, err := d.IsRegistered()
@@ -81,10 +85,12 @@ func uniqueDistroName(t *testing.T) string {
8185
}
8286

8387
// newTestDistro creates and registers a new distro with a mangled name and adds it to list of distros to remove.
84-
func newTestDistro(t *testing.T, rootfs string) wsl.Distro {
88+
//
89+
//nolint: revive // No, I wont' put the context before the *testing.T.
90+
func newTestDistro(t *testing.T, ctx context.Context, rootfs string) wsl.Distro {
8591
t.Helper()
8692

87-
d := wsl.NewDistro(uniqueDistroName(t))
93+
d := wsl.NewDistro(ctx, uniqueDistroName(t))
8894
t.Logf("Setup: Registering %q\n", d.Name())
8995

9096
powershellInstallDistro(t, d.Name(), rootfs)
@@ -149,8 +155,9 @@ func powershellInstallDistro(t *testing.T, distroName string, rootfs string) {
149155
}
150156

151157
// cleanUpTestWslInstances finds all distros with a prefixed name and unregisters them.
152-
func cleanUpTestWslInstances() {
153-
testInstances, err := registeredTestWslInstances()
158+
func cleanUpTestWslInstances(ctx context.Context) {
159+
testInstances, err := registeredTestWslInstances(ctx)
160+
154161
if err != nil {
155162
return
156163
}
@@ -184,7 +191,7 @@ func cleanUpWslInstance(distro wsl.Distro) error {
184191
}
185192

186193
// registeredTestWslInstances finds all distros with a mangled name.
187-
func registeredTestWslInstances() ([]wsl.Distro, error) {
194+
func registeredTestWslInstances(ctx context.Context) ([]wsl.Distro, error) {
188195
distros := []wsl.Distro{}
189196

190197
outp, err := exec.Command("powershell.exe", "-command", "$env:WSL_UTF8=1 ; wsl.exe --list --quiet").CombinedOutput()
@@ -196,7 +203,7 @@ func registeredTestWslInstances() ([]wsl.Distro, error) {
196203
if !strings.HasPrefix(line, namePrefix) {
197204
continue
198205
}
199-
distros = append(distros, wsl.NewDistro(line))
206+
distros = append(distros, wsl.NewDistro(ctx, line))
200207
}
201208

202209
return distros, nil

0 commit comments

Comments
 (0)