-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain_test.go
More file actions
93 lines (83 loc) · 1.88 KB
/
main_test.go
File metadata and controls
93 lines (83 loc) · 1.88 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
package main
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"rootcause/pkg/server"
)
func TestMainSuccessFlags(t *testing.T) {
origRun := runServer
origExit := exit
origArgs := os.Args
origStderr := os.Stderr
t.Cleanup(func() {
runServer = origRun
exit = origExit
os.Args = origArgs
os.Stderr = origStderr
})
var got server.Options
runServer = func(ctx context.Context, opts server.Options) error {
got = opts
return nil
}
exit = func(code int) {
t.Fatalf("unexpected exit %d", code)
}
tmp, err := os.CreateTemp(t.TempDir(), "stderr")
if err != nil {
t.Fatalf("temp stderr: %v", err)
}
os.Stderr = tmp
os.Args = []string{
"rootcause",
"--kubeconfig", "/tmp/kubeconfig",
"--context", "demo",
"--toolsets", "k8s,istio",
"--config", "/tmp/config",
"--read-only",
"--disable-destructive",
"--log-level", "debug",
}
main()
if got.Kubeconfig != "/tmp/kubeconfig" || got.Context != "demo" {
t.Fatalf("unexpected kubeconfig/context: %#v", got)
}
if !reflect.DeepEqual(got.Toolsets, []string{"k8s", "istio"}) {
t.Fatalf("unexpected toolsets: %#v", got.Toolsets)
}
if got.ConfigPath != "/tmp/config" || !got.ReadOnly || !got.DisableDestructive || got.LogLevel != "debug" {
t.Fatalf("unexpected options: %#v", got)
}
}
func TestMainErrorExit(t *testing.T) {
origRun := runServer
origExit := exit
origArgs := os.Args
origStderr := os.Stderr
t.Cleanup(func() {
runServer = origRun
exit = origExit
os.Args = origArgs
os.Stderr = origStderr
})
runServer = func(ctx context.Context, opts server.Options) error {
return fmt.Errorf("boom")
}
exitCode := 0
exit = func(code int) {
exitCode = code
}
tmp, err := os.CreateTemp(t.TempDir(), "stderr")
if err != nil {
t.Fatalf("temp stderr: %v", err)
}
os.Stderr = tmp
os.Args = []string{"rootcause"}
main()
if exitCode != 1 {
t.Fatalf("expected exit code 1, got %d", exitCode)
}
}