forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_test.go
443 lines (398 loc) · 10.9 KB
/
dump_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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
func Test_logDumperNode_findFiles(t *testing.T) {
grid := []struct {
dir string
command string
stdout string
stderr string
expected []string
}{
{
dir: "/var/log",
command: "sudo find /var/log -print0",
stdout: "/var/log/a\x00/var/log/b\x00",
expected: []string{
"/var/log/a",
"/var/log/b",
},
},
{
dir: "/var/log",
command: "sudo find /var/log -print0",
stdout: "/var/log/a\x00",
expected: []string{
"/var/log/a",
},
},
{
dir: "/var/log",
command: "sudo find /var/log -print0",
expected: []string{},
},
}
for _, g := range grid {
client := &mockSSHClient{}
n := logDumperNode{
client: client,
}
client.commands = append(client.commands, &mockCommand{
command: g.command,
stdout: []byte(g.stdout),
stderr: []byte(g.stderr),
})
actual, err := n.findFiles(context.Background(), g.dir)
if err != nil {
t.Errorf("unexpected error from findFiles: %v", err)
continue
}
if !reflect.DeepEqual(actual, g.expected) {
t.Errorf("unexpected files. actual=%v, expected=%v", actual, g.expected)
continue
}
}
}
func Test_logDumperNode_listSystemdUnits(t *testing.T) {
grid := []struct {
command string
stdout string
stderr string
expected []string
}{
{
command: "sudo systemctl list-units -t service --no-pager --no-legend --all",
stdout: "accounts-daemon.service loaded active running Accounts Service\n" +
"acpid.service loaded active running ACPI event daemon\n" +
"atd.service loaded active running Deferred execution scheduler\n" +
"avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack\n" +
"bluetooth.service loaded active running Bluetooth service\n" +
"cameras.service loaded failed failed cameras\n" +
"colord.service loaded active running Manage, Install and Generate Color Profiles\n",
expected: []string{
"accounts-daemon.service",
"acpid.service",
"atd.service",
"avahi-daemon.service",
"bluetooth.service",
"cameras.service",
"colord.service",
},
},
}
for _, g := range grid {
client := &mockSSHClient{}
n := logDumperNode{
client: client,
}
client.commands = append(client.commands, &mockCommand{
command: g.command,
stdout: []byte(g.stdout),
stderr: []byte(g.stderr),
})
actual, err := n.listSystemdUnits(context.Background())
if err != nil {
t.Errorf("unexpected error from listSystemdUnits: %v", err)
continue
}
if !reflect.DeepEqual(actual, g.expected) {
t.Errorf("unexpected systemdUnits. actual=%v, expected=%v", actual, g.expected)
continue
}
}
}
func Test_logDumperNode_shellToFile(t *testing.T) {
grid := []struct {
command string
stdout []byte
stderr []byte
}{
{
command: "cat something",
stdout: []byte("hello"),
},
}
for _, g := range grid {
client := &mockSSHClient{}
n := logDumperNode{
client: client,
}
client.commands = append(client.commands, &mockCommand{
command: g.command,
stdout: []byte(g.stdout),
stderr: []byte(g.stderr),
})
tmpfile, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("error creating temp file: %v", err)
continue
}
defer func() {
if err := os.Remove(tmpfile.Name()); err != nil {
t.Errorf("error removing temp file: %v", err)
}
}()
err = n.shellToFile(context.Background(), "cat something", tmpfile.Name())
if err != nil {
t.Errorf("unexpected error from shellToFile: %v", err)
continue
}
if err := tmpfile.Close(); err != nil {
t.Errorf("unexpected error closing file: %v", err)
continue
}
actual, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Errorf("unexpected error reading file: %v", err)
continue
}
if !reflect.DeepEqual(actual, g.stdout) {
t.Errorf("unexpected systemdUnits. actual=%q, expected=%q", string(actual), string(g.stdout))
continue
}
}
}
func Test_logDumperNode_dump(t *testing.T) {
tmpdir := t.TempDir()
host1Client := &mockSSHClient{}
host1Client.commands = append(host1Client.commands,
&mockCommand{
command: "sudo journalctl --output=short-precise -k",
},
&mockCommand{
command: "sudo journalctl --output=short-precise",
},
&mockCommand{
command: "sudo sysctl --all",
},
&mockCommand{
command: "sudo systemctl list-units -t service --no-pager --no-legend --all",
stdout: []byte(
"kubelet.service loaded active running kubelet daemon\n" +
"atd.service loaded active running Deferred execution scheduler\n" +
"avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack\n" +
"bluetooth.service loaded active running Bluetooth service\n" +
"cameras.service loaded failed failed cameras\n" +
"colord.service loaded active running Manage, Install and Generate Color Profiles\n",
),
},
&mockCommand{
command: "sudo find /var/log -print0",
stdout: []byte(strings.Join([]string{
"/var/log",
"/var/log/kube-controller-manager.log",
"/var/log/kube-controller-manager.log.1",
"/var/log/kube-controller-manager.log.2.gz",
"/var/log/other.log",
}, "\x00")),
},
&mockCommand{
command: "sudo journalctl --output=cat -u kubelet.service",
},
&mockCommand{
command: "sudo cat /var/log/kube-controller-manager.log",
},
&mockCommand{
command: "sudo cat /var/log/kube-controller-manager.log.1",
},
&mockCommand{
command: "sudo cat /var/log/kube-controller-manager.log.2.gz",
},
)
mockSSHClientFactory := &mockSSHClientFactory{
clients: map[string]sshClient{
"host1": host1Client,
},
}
dumper, err := newLogDumper(mockSSHClientFactory, tmpdir)
if err != nil {
t.Errorf("error building logDumper: %v", err)
}
dumper.DumpSysctls = true
n, err := dumper.connectToNode(context.Background(), "nodename1", "host1")
if err != nil {
t.Errorf("error from connectToNode: %v", err)
}
errors := n.dump(context.Background())
if len(errors) != 0 {
t.Errorf("unexpected errors from dump: %v", errors)
return
}
actual := []string{}
err = filepath.Walk(tmpdir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
p := strings.TrimPrefix(strings.TrimPrefix(path, tmpdir), "/")
if p == "" {
return nil
}
if info.IsDir() {
p += "/"
}
actual = append(actual, p)
return nil
})
if err != nil {
t.Errorf("unexpected error walking output tree: %v", err)
return
}
expected := []string{
"nodename1/",
"nodename1/kern.log",
"nodename1/journal.log",
"nodename1/kubelet.log",
"nodename1/sysctl.conf",
"nodename1/kube-controller-manager.log",
"nodename1/kube-controller-manager.log.1",
"nodename1/kube-controller-manager.log.2.gz",
}
sort.Strings(actual)
sort.Strings(expected)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("unexpected files found in dump: actual=%v, expected=%v", actual, expected)
return
}
}
// mockCommand is an expected command and canned response
type mockCommand struct {
command string
stdout []byte
stderr []byte
err error
}
// mockSSHClient is a mock implementation of sshClient
type mockSSHClient struct {
// commands holds the canned commands and responses we expect
commands []*mockCommand
// closed is set if the mockSSHClient is closed
closed bool
}
var _ sshClient = &mockSSHClient{}
// mockSSHClientFactory is a mock implementation of sshClientFactory
type mockSSHClientFactory struct {
clients map[string]sshClient
}
var _ sshClientFactory = &mockSSHClientFactory{}
func (f *mockSSHClientFactory) Dial(ctx context.Context, host string) (sshClient, error) {
client := f.clients[host]
if client == nil {
return nil, fmt.Errorf("host %q not registered in mockSSHClientFactory", host)
}
return client, nil
}
// Close implements sshClient::Close. It records that the client was closed; future calls will fail
func (m *mockSSHClient) Close() error {
if m.closed {
return fmt.Errorf("mockSSHClient::Close called on Closed mockSSHClient")
}
m.closed = true
return nil
}
// ExecPiped implements sshClient::ExecPiped. It scans the configured commands, and returns the result if one is found.
// If no command is found, it returns an error.
func (m *mockSSHClient) ExecPiped(ctx context.Context, command string, stdout io.Writer, stderr io.Writer) error {
if m.closed {
return fmt.Errorf("mockSSHClient::ExecPiped called on Closed mockSSHClient")
}
for i := range m.commands {
c := m.commands[i]
if c == nil {
continue
}
if c.command == command {
if _, err := stdout.Write(c.stdout); err != nil {
return fmt.Errorf("error writing to stdout: %w", err)
}
if _, err := stderr.Write(c.stderr); err != nil {
return fmt.Errorf("error writing to stderr: %w", err)
}
m.commands[i] = nil
return c.err
}
}
return fmt.Errorf("unexpected command: %s", command)
}
func TestFindInstancesNotDumped(t *testing.T) {
n1 := &node{
Status: nodeStatus{
Addresses: []nodeAddress{{Address: "10.0.0.1"}},
},
}
n2 := &node{
Status: nodeStatus{
Addresses: []nodeAddress{{Address: "10.0.0.2"}},
},
}
n3 := &node{
Status: nodeStatus{
Addresses: []nodeAddress{
{Address: "10.0.0.3"},
{Address: "10.0.3.3"},
},
},
}
grid := []struct {
ips []string
dumped []*node
expected []string
}{
{
ips: nil,
dumped: nil,
expected: nil,
},
{
ips: []string{"10.0.0.1"},
dumped: nil,
expected: []string{"10.0.0.1"},
},
{
ips: []string{"10.0.0.1"},
dumped: []*node{n1},
expected: nil,
},
{
ips: []string{"10.0.0.1", "10.0.0.2"},
dumped: []*node{n1},
expected: []string{"10.0.0.2"},
},
{
ips: []string{"10.0.0.1", "10.0.0.2", "10.0.3.3"},
dumped: []*node{n1, n2, n3},
expected: nil,
},
{
ips: []string{"10.0.0.1", "10.0.0.2", "10.0.3.3"},
dumped: []*node{n1, n2},
expected: []string{"10.0.3.3"},
},
}
for _, g := range grid {
actual := findInstancesNotDumped(g.ips, g.dumped)
if !reflect.DeepEqual(actual, g.expected) {
t.Errorf("unexpected result from findInstancesNotDumped. actual=%v, expected=%v", actual, g.expected)
}
}
}