Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
308 lines (289 loc) · 9.69 KB
/
Copy pathmain_test.go
File metadata and controls
308 lines (289 loc) · 9.69 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"errors"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/giantswarm/agentlab/internal/config"
"github.com/giantswarm/agentlab/internal/lab"
"github.com/giantswarm/agentlab/internal/labs"
)
// Every command a person can discover belongs to one of the help groups —
// otherwise cobra prints it under "Additional Commands", below the groups,
// which is where a new command would silently end up.
func TestEveryCommandHasAGroup(t *testing.T) {
root := rootCmd()
ids := []string{}
for _, g := range root.Groups() {
ids = append(ids, g.ID)
}
for _, cmd := range root.Commands() {
if cmd.Hidden {
continue
}
if !slices.Contains(ids, cmd.GroupID) {
t.Errorf("command %q has group %q, want one of %v (see inGroup in main.go)", cmd.Name(), cmd.GroupID, ids)
}
}
}
// The groups a newcomer meets first, and where the everyday commands sit.
func TestGroupOrderAndPlacement(t *testing.T) {
root := rootCmd()
var titles []string
for _, g := range root.Groups() {
titles = append(titles, g.Title)
}
want := []string{"Setup", "Everyday", "Testing", "Cleanup", "Advanced"}
if !slices.Equal(titles, want) {
t.Errorf("group titles = %v, want %v (docs/cli.md carries the same order)", titles, want)
}
for name, group := range map[string]string{
"up": groupSetup,
"open": groupEveryday,
"login": groupEveryday,
"down": groupCleanup,
"test": groupTesting,
} {
cmd := find(root, name)
if cmd == nil {
t.Errorf("no %q command", name)
continue
}
if cmd.GroupID != group {
t.Errorf("%q is in group %q, want %q", name, cmd.GroupID, group)
}
}
}
// `backstage` is gone (Backstage deploys with the platform), and `browser`
// stays only as the hidden name `login --browser` had.
func TestRetiredCommands(t *testing.T) {
root := rootCmd()
if cmd := find(root, "backstage"); cmd != nil {
t.Errorf("the retired backstage command is still registered")
}
browser := find(root, "browser")
if browser == nil {
t.Fatalf("no browser command; it stays as the hidden name of `login --browser`")
}
if !browser.Hidden {
t.Errorf("the browser command should be hidden")
}
}
// `up` and `platform` carry the pre-answers for the questions they end with.
func TestBootCommandsTakeTheOfferFlags(t *testing.T) {
root := rootCmd()
for _, name := range []string{"up", "platform"} {
cmd := find(root, name)
if cmd == nil {
t.Fatalf("no %q command", name)
}
for _, flag := range []string{"trust", "open"} {
if cmd.Flags().Lookup(flag) == nil {
t.Errorf("%q has no --%s flag", name, flag)
}
}
}
}
func find(root *cobra.Command, name string) *cobra.Command {
for _, cmd := range root.Commands() {
if cmd.Name() == name {
return cmd
}
}
return nil
}
// A command run in a lab directory the registry does not know settles it:
// a lab from before the registry or the freedesktop folders, or one whose
// entry is gone, moves into its Home, is registered there and found from any
// directory again; its old directory keeps links to the moved files.
func TestEnterLabSettlesTheLabHere(t *testing.T) {
regDir := t.TempDir()
prev := labs.Dir
labs.Dir = func() (string, error) { return regDir, nil }
t.Cleanup(func() { labs.Dir = prev })
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, config.File), []byte("clusterName: unregistered\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "certs"), 0o750); err != nil {
t.Fatal(err)
}
t.Chdir(dir)
if err := enterLab(labs.Enter); err != nil {
t.Fatal(err)
}
home, err := labs.Home("unregistered")
if err != nil {
t.Fatal(err)
}
got, err := labs.List()
if err != nil || len(got) != 1 || got[0].Name != "unregistered" || got[0].Dir != home {
t.Fatalf("labs.List() = %+v, %v; want unregistered in %s", got, err, home)
}
if cwd, _ := os.Getwd(); cwd != home {
t.Errorf("the command runs in %s, want the lab's Home %s", cwd, home)
}
for _, f := range []string{config.File, "certs"} {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
t.Errorf("the old directory's %s does not resolve: %v", f, err)
}
}
}
// `render` from a copy of a registered lab's agentlab.yaml renders the copy
// into its own directory and leaves the registered lab's certs/ and state/
// untouched; a scratch lab of an unregistered name renders where it is too.
func TestRenderStaysInTheDirectoryOfItsConfig(t *testing.T) {
regDir := t.TempDir()
prev := labs.Dir
labs.Dir = func() (string, error) { return regDir, nil }
t.Cleanup(func() { labs.Dir = prev })
for _, tc := range []struct{ name, clusterName string }{
{"a copy of a registered lab's config", "registered"},
{"a scratch lab of an unregistered name", "scratch"},
} {
t.Run(tc.name, func(t *testing.T) {
registered := t.TempDir()
t.Chdir(registered)
cfg := config.Default()
cfg.ClusterName = "registered"
if err := cfg.Save(); err != nil {
t.Fatal(err)
}
if err := labs.Register(cfg.ClusterName, registered); err != nil {
t.Fatal(err)
}
copyDir := t.TempDir()
t.Chdir(copyDir)
cfg.ClusterName = tc.clusterName
if err := cfg.Save(); err != nil {
t.Fatal(err)
}
cmd := renderCmd()
cmd.SetArgs(nil)
cmd.SilenceUsage = true
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
for _, d := range []string{lab.StateDir, "certs"} {
if _, err := os.Stat(filepath.Join(registered, d)); !errors.Is(err, os.ErrNotExist) {
t.Errorf("the registered lab's %s/ was written (stat: %v)", d, err)
}
if _, err := os.Stat(filepath.Join(copyDir, d)); err != nil {
t.Errorf("the render left no %s/ in its own directory: %v", d, err)
}
}
})
}
}
// backstage-test documents --agent in its help: the flag and what it skips.
func TestBackstageTestHelpDocumentsAgent(t *testing.T) {
cmd := find(rootCmd(), "backstage-test")
if cmd.Flags().Lookup("agent") == nil {
t.Fatal("backstage-test has no --agent flag")
}
for _, want := range []string{"--agent <namespace>/<name>", "skipped", "left in place"} {
if !strings.Contains(cmd.Long, want) {
t.Errorf("backstage-test help does not say %q", want)
}
}
}
// Without a terminal `configure` refuses before its form starts, and the
// message names --defaults, the way without one.
func TestConfigureWithoutATerminalNamesDefaults(t *testing.T) {
prev := formTerminal
formTerminal = func() bool { return false }
t.Cleanup(func() { formTerminal = prev })
t.Setenv("ACCESSIBLE", "")
dir := t.TempDir()
t.Chdir(dir)
cmd := configureCmd()
cmd.SetArgs(nil)
cmd.SilenceUsage = true
cmd.SilenceErrors = true
err := cmd.Execute()
if !errors.Is(err, errHeadlessForm) {
t.Fatalf("configure without a terminal = %v, want errHeadlessForm", err)
}
if msg := err.Error(); !strings.Contains(msg, "--defaults") || strings.Contains(msg, "TTY") || strings.Contains(msg, "\n") {
t.Fatalf("message %q: want one line naming --defaults", msg)
}
if entries, _ := os.ReadDir(dir); len(entries) != 0 {
t.Fatalf("configure wrote %d entries before refusing", len(entries))
}
}
// A new lab beside a registered lab named agentlab takes the first free
// name instead of being refused; --cluster-name is validated before
// anything is discovered or written.
func TestConfigureClusterName(t *testing.T) {
regDir := t.TempDir()
prev := labs.Dir
labs.Dir = func() (string, error) { return regDir, nil }
t.Cleanup(func() { labs.Dir = prev })
other := t.TempDir()
if err := os.WriteFile(filepath.Join(other, config.File), []byte("clusterName: agentlab\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := labs.Register("agentlab", other); err != nil {
t.Fatal(err)
}
t.Chdir(t.TempDir())
t.Run("the default name moves past a registered lab", func(t *testing.T) {
cfg := config.Default()
if err := freeClusterName(cfg); err != nil {
t.Fatal(err)
}
if cfg.ClusterName != "agentlab-2" {
t.Fatalf("clusterName = %q, want agentlab-2", cfg.ClusterName)
}
})
t.Run("an invalid --cluster-name is refused before anything is written", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
cmd := configureCmd()
cmd.SetArgs([]string{"--defaults", "--cluster-name", "Not_A_Name"})
cmd.SilenceUsage = true
cmd.SilenceErrors = true
err := cmd.Execute()
if err == nil || !strings.Contains(err.Error(), "--cluster-name") {
t.Fatalf("configure --cluster-name Not_A_Name = %v, want a refusal naming the flag", err)
}
if entries, _ := os.ReadDir(dir); len(entries) != 0 {
t.Fatalf("configure wrote %d entries before refusing", len(entries))
}
})
}
// A new lab beside a registered, stopped lab moves its ports off the ones
// that lab's agentlab.yaml records: nothing listens on them while it is down,
// yet both labs could never be up together.
func TestApplyPortsAvoidsAStoppedRegisteredLab(t *testing.T) {
regDir := t.TempDir()
prev := labs.Dir
labs.Dir = func() (string, error) { return regDir, nil }
t.Cleanup(func() { labs.Dir = prev })
recorded := map[int]bool{}
for _, l := range []struct {
name string
dexPort, apiServer int
}{{"agentlab", 32000, 6443}, {"agentlab-2", 32001, 6444}} {
dir := t.TempDir()
yaml := "clusterName: " + l.name + "\ndexPort: " + strconv.Itoa(l.dexPort) + "\napiServerPort: " + strconv.Itoa(l.apiServer) + "\n"
if err := os.WriteFile(filepath.Join(dir, config.File), []byte(yaml), 0o600); err != nil {
t.Fatal(err)
}
if err := labs.Register(l.name, dir); err != nil {
t.Fatal(err)
}
recorded[l.dexPort], recorded[l.apiServer] = true, true
}
t.Chdir(t.TempDir())
cfg := config.Default()
cfg.ClusterName = "agentlab-3"
applyPorts(cfg, &lab.Discovery{})
if recorded[cfg.DexPort] || recorded[cfg.APIServerPort] {
t.Fatalf("dexPort %d, apiServerPort %d: a stopped registered lab records them", cfg.DexPort, cfg.APIServerPort)
}
}