|
1 | 1 | package k8s |
2 | 2 |
|
3 | | -// import ( |
4 | | -// "errors" |
5 | | -// "kool-dev/kool/core/shell" |
6 | | -// "kool-dev/kool/services/cloud/api" |
7 | | -// "os" |
8 | | -// "strings" |
9 | | -// "testing" |
10 | | -// ) |
11 | | - |
12 | | -// // fake api.ExecCall |
13 | | -// type fakeExecCall struct { |
14 | | -// api.DefaultEndpoint |
15 | | - |
16 | | -// err error |
17 | | -// resp *api.DeployExecResponse |
18 | | -// } |
19 | | - |
20 | | -// func (d *fakeExecCall) Call() (*api.ExecResponse, error) { |
21 | | -// return d.resp, d.err |
22 | | -// } |
23 | | - |
24 | | -// func newFakeExecCall() *fakeExecCall { |
25 | | -// return &fakeExecCall{ |
26 | | -// DefaultEndpoint: *api.NewDefaultEndpoint(""), |
27 | | -// } |
28 | | -// } |
29 | | - |
30 | | -// // fake shell.OutputWritter |
31 | | -// type fakeOutputWritter struct { |
32 | | -// warned []interface{} |
33 | | -// } |
34 | | - |
35 | | -// func (*fakeOutputWritter) Println(args ...interface{}) { |
36 | | -// } |
37 | | - |
38 | | -// func (*fakeOutputWritter) Printf(s string, args ...interface{}) { |
39 | | -// } |
40 | | - |
41 | | -// func (f *fakeOutputWritter) Warning(args ...interface{}) { |
42 | | -// f.warned = append(f.warned, args...) |
43 | | -// } |
44 | | - |
45 | | -// func (*fakeOutputWritter) Success(args ...interface{}) { |
46 | | -// } |
47 | | - |
48 | | -// func (*fakeOutputWritter) Info(args ...interface{}) { |
49 | | -// } |
50 | | - |
51 | | -// func TestNewDefaultK8S(t *testing.T) { |
52 | | -// k := NewDefaultK8S() |
53 | | -// if _, ok := k.apiExec.(*api.DefaultExecCall); !ok { |
54 | | -// t.Error("invalid type on apiExec") |
55 | | -// } |
56 | | -// } |
57 | | - |
58 | | -// func TestAuthenticate(t *testing.T) { |
59 | | -// k := &DefaultK8S{ |
60 | | -// apiExec: newFakeExecCall(), |
61 | | -// } |
62 | | - |
63 | | -// expectedErr := errors.New("call error") |
64 | | -// k.apiExec.(*fakeExecCall).err = expectedErr |
65 | | - |
66 | | -// if _, err := k.Authenticate("foo", "bar"); !errors.Is(err, expectedErr) { |
67 | | -// t.Error("unexpected error return from Authenticate") |
68 | | -// } |
69 | | - |
70 | | -// k.apiExec.(*fakeExecCall).err = nil |
71 | | -// k.apiExec.(*fakeExecCall).resp = &api.ExecResponse{ |
72 | | -// Server: "server", |
73 | | -// Namespace: "ns", |
74 | | -// Path: "path", |
75 | | -// Token: "", |
76 | | -// CA: "ca", |
77 | | -// } |
78 | | - |
79 | | -// if _, err := k.Authenticate("foo", "bar"); !strings.Contains(err.Error(), "failed to generate access credentials") { |
80 | | -// t.Errorf("unexpected error from DeployExec call: %v", err) |
81 | | -// } |
82 | | - |
83 | | -// k.apiExec.(*fakeExecCall).resp.Token = "token" |
84 | | -// authTempPath = t.TempDir() |
85 | | - |
86 | | -// if cloudService, err := k.Authenticate("foo", "bar"); err != nil { |
87 | | -// t.Errorf("unexpected error from Authenticate call: %v", err) |
88 | | -// } else if cloudService != "path" { |
89 | | -// t.Errorf("unexpected cloudService return: %s", cloudService) |
90 | | -// } |
91 | | -// } |
92 | | - |
93 | | -// func TestTempCAPath(t *testing.T) { |
94 | | -// k := NewDefaultK8S() |
95 | | - |
96 | | -// authTempPath = "fake-path" |
97 | | - |
98 | | -// if !strings.Contains(k.getTempCAPath(), authTempPath) { |
99 | | -// t.Error("missing authTempPath from temp CA path") |
100 | | -// } |
101 | | -// } |
102 | | - |
103 | | -// func TestCleanup(t *testing.T) { |
104 | | -// k := NewDefaultK8S() |
105 | | - |
106 | | -// authTempPath = t.TempDir() |
107 | | -// if err := os.WriteFile(k.getTempCAPath(), []byte("ca"), os.ModePerm); err != nil { |
108 | | -// t.Fatal(err) |
109 | | -// } |
110 | | - |
111 | | -// fakeOut := &fakeOutputWritter{} |
112 | | - |
113 | | -// k.Cleanup(fakeOut) |
114 | | - |
115 | | -// if len(fakeOut.warned) != 0 { |
116 | | -// t.Error("should not have warned on removing the file") |
117 | | -// } |
118 | | - |
119 | | -// authTempPath = t.TempDir() + "test" |
120 | | -// k.Cleanup(fakeOut) |
121 | | - |
122 | | -// if len(fakeOut.warned) != 2 { |
123 | | -// t.Error("should have warned on removing the file once") |
124 | | -// } |
125 | | -// } |
126 | | - |
127 | | -// func TestKubectl(t *testing.T) { |
128 | | -// authTempPath = t.TempDir() |
129 | | - |
130 | | -// k := &DefaultK8S{ |
131 | | -// apiExec: newFakeExecCall(), |
132 | | -// } |
133 | | - |
134 | | -// k.apiExec.(*fakeExecCall).resp = &api.ExecResponse{ |
135 | | -// Server: "server", |
136 | | -// Namespace: "ns", |
137 | | -// Path: "path", |
138 | | -// Token: "token", |
139 | | -// CA: "ca", |
140 | | -// } |
141 | | - |
142 | | -// fakeShell := &shell.FakeShell{} |
143 | | - |
144 | | -// if _, err := k.Kubectl(fakeShell); !strings.Contains(err.Error(), "but did not auth") { |
145 | | -// t.Error("should get error before authenticating") |
146 | | -// } |
147 | | - |
148 | | -// _, _ = k.Authenticate("foo", "bar") |
149 | | - |
150 | | -// if cmd, _ := k.Kubectl(fakeShell); cmd.Cmd() != "kubectl" { |
151 | | -// t.Error("should use kubectl") |
152 | | -// } |
153 | | - |
154 | | -// fakeShell.MockLookPath = errors.New("err") |
155 | | - |
156 | | -// if cmd, _ := k.Kubectl(fakeShell); cmd.Cmd() != "kool" { |
157 | | -// t.Error("should use kool") |
158 | | -// } |
159 | | -// } |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "kool-dev/kool/core/shell" |
| 6 | + "kool-dev/kool/services/cloud/api" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAuthenticate(t *testing.T) { |
| 13 | + k := NewDefaultK8S() |
| 14 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).Fake() |
| 15 | + |
| 16 | + expectedErr := errors.New("call error") |
| 17 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).MockErr(expectedErr) |
| 18 | + |
| 19 | + if _, err := k.Authenticate("foo", "bar"); !errors.Is(err, expectedErr) { |
| 20 | + t.Error("unexpected error return from Authenticate") |
| 21 | + } |
| 22 | + |
| 23 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).MockErr(nil) |
| 24 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).MockResp(&api.DeployExecResponse{ |
| 25 | + Server: "server", |
| 26 | + Namespace: "ns", |
| 27 | + Path: "path", |
| 28 | + Token: "", |
| 29 | + CA: "ca", |
| 30 | + }) |
| 31 | + |
| 32 | + if _, err := k.Authenticate("foo", "bar"); !strings.Contains(err.Error(), "failed to generate access credentials") { |
| 33 | + t.Errorf("unexpected error from DeployExec call: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).MockResp(&api.DeployExecResponse{ |
| 37 | + Server: "server", |
| 38 | + Namespace: "ns", |
| 39 | + Path: "path", |
| 40 | + Token: "token", |
| 41 | + CA: "ca", |
| 42 | + }) |
| 43 | + |
| 44 | + authTempPath = t.TempDir() |
| 45 | + |
| 46 | + if cloudService, err := k.Authenticate("foo", "bar"); err != nil { |
| 47 | + t.Errorf("unexpected error from Authenticate call: %v", err) |
| 48 | + } else if cloudService != "path" { |
| 49 | + t.Errorf("unexpected cloudService return: %s", cloudService) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestTempCAPath(t *testing.T) { |
| 54 | + k := NewDefaultK8S() |
| 55 | + |
| 56 | + authTempPath = "fake-path" |
| 57 | + |
| 58 | + if !strings.Contains(k.getTempCAPath(), authTempPath) { |
| 59 | + t.Error("missing authTempPath from temp CA path") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestCleanup(t *testing.T) { |
| 64 | + k := NewDefaultK8S() |
| 65 | + |
| 66 | + authTempPath = t.TempDir() |
| 67 | + if err := os.WriteFile(k.getTempCAPath(), []byte("ca"), os.ModePerm); err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + |
| 71 | + fakeShell := &shell.FakeShell{} |
| 72 | + k.Cleanup(fakeShell) |
| 73 | + |
| 74 | + if fakeShell.CalledWarning { |
| 75 | + t.Error("should not have warned on removing the file") |
| 76 | + } |
| 77 | + |
| 78 | + authTempPath = t.TempDir() + "test" |
| 79 | + fakeShell = &shell.FakeShell{} |
| 80 | + k.Cleanup(fakeShell) |
| 81 | + |
| 82 | + if !fakeShell.CalledWarning || len(fakeShell.WarningOutput) != 2 { |
| 83 | + t.Error("should have warned on removing the file once") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestKubectl(t *testing.T) { |
| 88 | + authTempPath = t.TempDir() |
| 89 | + |
| 90 | + k := NewDefaultK8S() |
| 91 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).Fake() |
| 92 | + |
| 93 | + k.deployExec.Endpoint.(*api.DefaultEndpoint).MockResp(&api.DeployExecResponse{ |
| 94 | + Server: "server", |
| 95 | + Namespace: "ns", |
| 96 | + Path: "path", |
| 97 | + Token: "token", |
| 98 | + CA: "ca", |
| 99 | + }) |
| 100 | + |
| 101 | + fakeShell := &shell.FakeShell{} |
| 102 | + |
| 103 | + if _, err := k.Kubectl(fakeShell); !strings.Contains(err.Error(), "but did not auth") { |
| 104 | + t.Error("should get error before authenticating") |
| 105 | + } |
| 106 | + |
| 107 | + _, _ = k.Authenticate("foo", "bar") |
| 108 | + |
| 109 | + if cmd, _ := k.Kubectl(fakeShell); cmd.Cmd() != "kubectl" { |
| 110 | + t.Error("should use kubectl") |
| 111 | + } |
| 112 | + |
| 113 | + fakeShell.MockLookPath = errors.New("err") |
| 114 | + |
| 115 | + if cmd, _ := k.Kubectl(fakeShell); cmd.Cmd() != "kool" { |
| 116 | + t.Error("should use kool") |
| 117 | + } |
| 118 | +} |
0 commit comments