-
Notifications
You must be signed in to change notification settings - Fork 4
/
wsl.go
141 lines (110 loc) · 3.91 KB
/
wsl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package testutils
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
wsl "github.com/ubuntu/gowsl"
)
const (
testDistroPrefix = "testDistro_UP4W"
)
// NonRegisteredDistro generates a random distroName and GUID but does not register them.
func NonRegisteredDistro(t *testing.T) (distroName string, GUID string) {
t.Helper()
distroName = RandomDistroName(t)
guid, err := uuid.NewRandom()
require.NoError(t, err, "Setup: could not generate a GUID for the non-registered distro")
GUID = fmt.Sprintf("{%s}", guid.String())
return distroName, GUID
}
// RandomDistroName generates a distroName that is not registered.
func RandomDistroName(t *testing.T) (name string) {
t.Helper()
testFullNormalized := normalizeName(t, strings.ReplaceAll(t.Name(), "/", "--"))
//nolint:gosec // No need to be cryptographically secure
return fmt.Sprintf("%s_%s_%d", testDistroPrefix, testFullNormalized, rand.Uint64())
}
// DistroState returns the state of the distro as specified by wsl.exe. Possible states:
// - Installing
// - Running
// - Stopped
// - Unregistered.
//
//nolint:revive // The context is better after the testing.T
func DistroState(t *testing.T, ctx context.Context, distroName string) string {
t.Helper()
d := wsl.NewDistro(ctx, distroName)
s, err := d.State()
require.NoError(t, err) // Error message is explanatory enough
return s.String()
}
// requireIsTestDistro requires a distroName to match those generated by the testutils.
// It is intended to protect other distros in the machine.
func requireIsTestDistro(t *testing.T, distroName string) {
t.Helper()
if !strings.HasPrefix(distroName, testDistroPrefix) {
require.Fail(t, "testutils can only be used with test distros", "Requested distro: %s", distroName)
}
}
// RegisterDistro registers a distro and returns its randomly-generated name and its GUID.
//
//nolint:revive // The context is better after the testing.T
func RegisterDistro(t *testing.T, ctx context.Context, realDistro bool) (distroName string, GUID string) {
t.Helper()
distroName = RandomDistroName(t)
guid := registerDistro(t, ctx, distroName, realDistro)
return distroName, guid
}
// UnregisterDistro unregisters a WSL distro. Errors are ignored.
//
//nolint:revive // The context is better after the testing.T
func UnregisterDistro(t *testing.T, ctx context.Context, distroName string) {
t.Helper()
requireIsTestDistro(t, distroName)
d := wsl.NewDistro(ctx, distroName)
_ = d.Unregister()
}
// ReregisterDistro unregister, then registers the same distro again.
//
//nolint:revive // The context is better after the testing.T
func ReregisterDistro(t *testing.T, ctx context.Context, distroName string, realDistro bool) (GUID string) {
t.Helper()
UnregisterDistro(t, ctx, distroName)
return registerDistro(t, ctx, distroName, realDistro)
}
// TerminateDistro shuts down that distro in particular.
// Wrapper for `wsl -t distro`.
//
//nolint:revive // The context is better after the testing.T
func TerminateDistro(t *testing.T, ctx context.Context, distroName string) {
t.Helper()
requireIsTestDistro(t, distroName)
d := wsl.NewDistro(ctx, distroName)
_ = d.Terminate()
}
//nolint:revive // The context is better after the testing.T
func registerDistro(t *testing.T, ctx context.Context, distroName string, realDistro bool) (GUID string) {
t.Helper()
if !wsl.MockAvailable() {
return powershellInstallDistro(t, ctx, distroName, realDistro)
}
t.Cleanup(func() {
UnregisterDistro(t, ctx, distroName)
})
dir := t.TempDir()
rootfs := filepath.Join(dir, "empty.tar.gz")
err := os.WriteFile(rootfs, []byte{}, 0600)
require.NoError(t, err, "could not write empty fake rootfs")
d := wsl.NewDistro(ctx, distroName)
err = d.Register(rootfs)
require.NoError(t, err) // Error messsage is explanatory enough
guid, err := d.GUID()
require.NoError(t, err, "Setup: could not get distro GUID")
return guid.String()
}