-
Notifications
You must be signed in to change notification settings - Fork 6
/
manual_token_input_test.go
112 lines (90 loc) · 3.34 KB
/
manual_token_input_test.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
package endtoend_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
wsl "github.com/ubuntu/gowsl"
)
func TestManualTokenInput(t *testing.T) {
// TODO: Remove this line when cloud-init support for UP4W is released.
// Follow this PR for more information: https://github.com/canonical/cloud-init/pull/5116
t.Skip("This test depends on cloud-init support for UP4W being released.")
type whenToken int
const (
never whenToken = iota
beforeDistroRegistration
afterDistroRegistration
)
// Let's be lazy and don't fall into the risk of changing the function name without updating the places where its name is used.
currentFuncName := t.Name()
testCases := map[string]struct {
whenToken whenToken
overrideTokenEnv string
wantAttached bool
}{
"Success when applying pro token before registration": {whenToken: beforeDistroRegistration, wantAttached: true},
"Success when applying pro token after registration": {whenToken: afterDistroRegistration, wantAttached: true},
"Error with invalid token": {whenToken: afterDistroRegistration, overrideTokenEnv: fmt.Sprintf("%s=%s", proTokenEnv, "CJd8MMN8wXSWsv7wJT8c8dDK")},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
testSetup(t)
defer logWindowsAgentOnError(t)
landscape := NewLandscape(t, ctx)
writeUbuntuProRegistry(t, "LandscapeConfig", landscape.ClientConfig)
serverDone := make(chan struct{})
go func() {
defer close(serverDone)
landscape.Serve()
}()
t.Cleanup(func() {
landscape.Stop()
<-serverDone
})
defer landscape.LogOnError(t)
hostname, err := os.Hostname()
require.NoError(t, err, "Setup: could not test machine's hostname")
// Either runs the ubuntupro app before...
if tc.whenToken == beforeDistroRegistration {
cleanup := startAgent(t, ctx, currentFuncName, tc.overrideTokenEnv)
defer cleanup()
}
// Distro setup
name := registerFromTestImage(t, ctx)
d := wsl.NewDistro(ctx, name)
defer logWslProServiceOnError(t, ctx, d)
out, err := d.Command(ctx, "cloud-init status --wait").CombinedOutput()
require.NoErrorf(t, err, "Setup: could not wake distro up: %v. %s", err, out)
// ... or after registration, but never both.
if tc.whenToken == afterDistroRegistration {
cleanup := startAgent(t, ctx, currentFuncName, tc.overrideTokenEnv)
defer cleanup()
out, err = d.Command(ctx, "exit 0").CombinedOutput()
require.NoErrorf(t, err, "Setup: could not wake distro up: %v. %s", err, out)
}
const maxTimeout = time.Minute
if !tc.wantAttached {
time.Sleep(maxTimeout)
proCtx, cancel := context.WithTimeout(ctx, maxTimeout)
defer cancel()
attached, err := distroIsProAttached(t, proCtx, d)
require.NoError(t, err, "could not determine if distro is attached")
require.False(t, attached, "distro should not have been Pro attached")
return
}
require.Eventually(t, func() bool {
attached, err := distroIsProAttached(t, ctx, d)
if err != nil {
t.Logf("could not determine if distro is attached: %v", err)
}
return attached
}, maxTimeout, time.Second, "distro should have been Pro attached")
info := landscape.RequireReceivedInfo(t, os.Getenv(proTokenEnv), []wsl.Distro{d}, hostname)
landscape.RequireUninstallCommand(t, ctx, d, info)
})
}
}