-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathjson_test.go
159 lines (142 loc) · 4.69 KB
/
json_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
//
// (C) Copyright 2020-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"bytes"
"encoding/json"
"io"
"os"
"reflect"
"strings"
"testing"
"github.com/daos-stack/daos/src/control/common/test"
"github.com/daos-stack/daos/src/control/lib/control"
"github.com/daos-stack/daos/src/control/logging"
)
func walkStruct(v reflect.Value, prefix []string, visit func([]string)) {
vType := v.Type()
hasSub := false
for i := 0; i < vType.NumField(); i++ {
f := vType.Field(i)
kind := f.Type.Kind()
if kind != reflect.Struct {
continue
}
cmd := f.Tag.Get("command")
if cmd == "" {
continue
}
hasSub = true
subCmd := append(prefix, []string{cmd}...)
walkStruct(v.Field(i), subCmd, visit)
}
if !hasSub {
visit(prefix)
}
}
func TestDmg_JsonOutput(t *testing.T) {
var cmdArgs [][]string
// Use reflection to build up a list of commands in order to
// verify that they return valid JSON when invoked with valid
// arguments. This should catch new commands added without proper
// support for JSON output.
walkStruct(reflect.ValueOf(cliOptions{}), nil, func(cmd []string) {
cmdArgs = append(cmdArgs, cmd)
})
testDir, cleanup := test.CreateTestDir(t)
defer cleanup()
aclContent := "A::OWNER@:rw\nA::user1@:rw\nA:g:group1@:r\n"
aclPath := test.CreateTestFile(t, testDir, aclContent)
for _, args := range cmdArgs {
t.Run(strings.Join(args, " "), func(t *testing.T) {
testArgs := append([]string{"-i", "--json"}, args...)
switch strings.Join(args, " ") {
case "version", "telemetry config", "telemetry run", "config generate",
"manpage", "system set-prop", "support collect-log":
return
case "storage nvme-rebind":
testArgs = append(testArgs, "-l", "foo.com", "-a",
test.MockPCIAddr())
case "storage nvme-add-device":
testArgs = append(testArgs, "-l", "foo.com", "-a",
test.MockPCIAddr(), "-e", "0")
case "storage query device-health":
testArgs = append(testArgs, "-u", test.MockUUID())
case "storage set nvme-faulty":
testArgs = append(testArgs, "--force", "-u", test.MockUUID())
case "storage replace nvme":
testArgs = append(testArgs, "--old-uuid", test.MockUUID(),
"--new-uuid", test.MockUUID())
case "storage led identify", "storage led check", "storage led clear":
testArgs = append(testArgs, test.MockUUID())
case "pool create":
testArgs = append(testArgs, "-s", "1TB", "label")
case "pool destroy", "pool evict", "pool query", "pool get-acl", "pool upgrade":
testArgs = append(testArgs, test.MockUUID())
case "pool overwrite-acl", "pool update-acl":
testArgs = append(testArgs, test.MockUUID(), "-a", aclPath)
case "pool delete-acl":
testArgs = append(testArgs, test.MockUUID(), "-p", "foo@")
case "pool set-prop":
testArgs = append(testArgs, test.MockUUID(), "label:foo")
case "pool get-prop":
testArgs = append(testArgs, test.MockUUID(), "label")
case "pool extend":
testArgs = append(testArgs, test.MockUUID(), "--ranks", "0")
case "pool exclude", "pool drain", "pool reintegrate":
testArgs = append(testArgs, test.MockUUID(), "--rank", "0")
case "pool query-targets":
testArgs = append(testArgs, test.MockUUID(), "--rank", "0", "--target-idx", "1,3,5,7")
case "container set-owner":
testArgs = append(testArgs, "--user", "foo", "--pool", test.MockUUID(),
"--cont", test.MockUUID())
case "telemetry metrics list", "telemetry metrics query":
return // These commands query via http directly
case "system cleanup":
testArgs = append(testArgs, "hostname")
case "system set-attr":
testArgs = append(testArgs, "foo:bar")
case "system del-attr":
testArgs = append(testArgs, "foo")
case "system exclude":
testArgs = append(testArgs, "--ranks", "0")
case "system clear-exclude":
testArgs = append(testArgs, "--ranks", "0")
}
// replace os.Stdout so that we can verify the generated output
var result bytes.Buffer
r, w, _ := os.Pipe()
done := make(chan struct{})
go func() {
_, _ = io.Copy(&result, r)
close(done)
}()
stdout := os.Stdout
defer func() {
os.Stdout = stdout
}()
os.Stdout = w
// Use a normal logger to verify that we don't mess up JSON output.
log := logging.NewCommandLineLogger()
ctlClient := control.DefaultMockInvoker(log)
conn := newTestConn(t)
bridge := &bridgeConnInvoker{
MockInvoker: *ctlClient,
t: t,
conn: conn,
}
err := parseOpts(testArgs, &cliOptions{}, bridge, log)
if err != nil {
t.Errorf("%s: %s", strings.Join(testArgs, " "), err)
}
w.Close()
<-done
if !json.Valid(result.Bytes()) {
t.Fatalf("invalid JSON in response: %s", result.String())
}
})
}
}