Skip to content

Commit 29f19bd

Browse files
Adapted tests to mocking mechanism
1 parent f4a277a commit 29f19bd

File tree

5 files changed

+121
-84
lines changed

5 files changed

+121
-84
lines changed

distro_test.go

+37-24
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ 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)()
@@ -75,21 +79,23 @@ func isTestLinuxProcessAlive(d *wsl.Distro) bool {
7579
}
7680

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

80-
//nolint:gosec // G204: Subprocess launched with a potential tainted input or cmd arguments.
81-
// No threat of code injection, want.Name() is generated by func UniqueDistroName.
82-
out, err := exec.Command("wsl.exe", "--set-default", want.Name()).CombinedOutput()
83-
require.NoErrorf(t, err, "setup: failed to set test distro as default: %s", out)
84+
want := newTestDistro(t, ctx, emptyRootFs)
8485

85-
got, err := wsl.DefaultDistro()
86+
err := setDefaultDistro(ctx, want.Name())
87+
require.NoError(t, err, "Setup: could not set the default distro")
88+
89+
got, err := wsl.DefaultDistro(ctx)
8690
require.NoError(t, err, "unexpected error getting default distro %q", want.Name())
8791
require.Equal(t, want, got, "Unexpected mismatch in default distro")
8892
}
8993

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

94100
testCases := map[string]struct {
95101
distro wsl.Distro
@@ -103,23 +109,26 @@ func TestDistroSetAsDefault(t *testing.T) {
103109
tc := tc
104110
t.Run(name, func(t *testing.T) {
105111
err := tc.distro.SetAsDefault()
112+
106113
if tc.wantErr {
107114
require.Errorf(t, err, "Unexpected success setting non-existent distro %q as default", tc.distro.Name())
108115
return
109116
}
110117
require.NoErrorf(t, err, "Unexpected error setting %q as default", tc.distro.Name())
111118

112-
got, err := defaultDistro()
119+
got, err := defaultDistro(ctx)
113120
require.NoError(t, err, "unexpected error getting default distro")
114121
require.Equal(t, tc.distro.Name(), got)
115122
})
116123
}
117124
}
118125

119126
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")
127+
ctx := testContext(context.Background())
128+
129+
realDistro := newTestDistro(t, ctx, rootFs)
130+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
131+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"_\x00_invalid_name")
123132

124133
realID, err := realDistro.GUID()
125134
require.NoError(t, err, "could not get the test distro's GUID")
@@ -184,10 +193,11 @@ func TestGUID(t *testing.T) {
184193
// format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", but syscall.GUID
185194
// does not have such method and prints its contents like any other
186195
// struct.
196+
ctx := testContext(context.Background())
187197

188-
realDistro := wsl.NewDistro(uniqueDistroName(t))
189-
fakeDistro := wsl.NewDistro(uniqueDistroName(t))
190-
wrongDistro := wsl.NewDistro(uniqueDistroName(t) + "\x00invalidcharacter")
198+
realDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
199+
fakeDistro := wsl.NewDistro(ctx, uniqueDistroName(t))
200+
wrongDistro := wsl.NewDistro(ctx, uniqueDistroName(t)+"\x00invalidcharacter")
191201

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

303+
ctx := testContext(context.Background())
304+
293305
// details has info about each of the settings
294306
details := map[testedSetting]settingDetails{
295307
DefaultUID: {name: "DefaultUID", byDefault: uint32(0), want: uint32(0)},
@@ -310,13 +322,13 @@ func TestConfigurationSetters(t *testing.T) {
310322
var d wsl.Distro
311323
switch tc.distro {
312324
case DistroRegistered:
313-
d = newTestDistro(t, rootFs)
325+
d = newTestDistro(t, ctx, rootFs)
314326
err := d.Command(context.Background(), "useradd testuser").Run()
315327
require.NoError(t, err, "unexpectedly failed to add a user to the distro")
316328
case DistroNotRegistered:
317-
d = wsl.NewDistro(uniqueDistroName(t))
329+
d = wsl.NewDistro(ctx, uniqueDistroName(t))
318330
case DistroInvalidName:
319-
d = wsl.NewDistro("Wrong character \x00 in name")
331+
d = wsl.NewDistro(ctx, "Wrong character \x00 in name")
320332
}
321333

322334
// Test changing a setting
@@ -379,7 +391,8 @@ func TestConfigurationSetters(t *testing.T) {
379391
}
380392
}
381393
func TestGetConfiguration(t *testing.T) {
382-
d := newTestDistro(t, rootFs)
394+
ctx := testContext(context.Background())
395+
d := newTestDistro(t, ctx, rootFs)
383396

384397
testCases := map[string]struct {
385398
distroName string
@@ -393,7 +406,7 @@ func TestGetConfiguration(t *testing.T) {
393406
for name, tc := range testCases {
394407
tc := tc
395408
t.Run(name, func(t *testing.T) {
396-
d := wsl.NewDistro(tc.distroName)
409+
d := wsl.NewDistro(ctx, tc.distroName)
397410
c, err := d.GetConfiguration()
398411

399412
if tc.wantErr {

exec_test.go

+23-13
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

+27-26
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 {

0 commit comments

Comments
 (0)