Skip to content

Commit 6d5008c

Browse files
Revert "Fixes invalid JSON in crictl info"
1 parent fa6c0a3 commit 6d5008c

File tree

2 files changed

+14
-162
lines changed

2 files changed

+14
-162
lines changed

cmd/crictl/util.go

+14-29
Original file line numberDiff line numberDiff line change
@@ -245,52 +245,37 @@ func outputStatusInfo(status, handlers string, info map[string]string, format st
245245
}
246246
sort.Strings(keys)
247247

248-
infoMap := map[string]interface{}{}
249-
250-
if status != "" {
251-
var statusVal map[string]interface{}
252-
err := json.Unmarshal([]byte(status), &statusVal)
253-
if err != nil {
254-
return err
255-
}
256-
infoMap["status"] = statusVal
257-
}
258-
248+
jsonInfo := "{" + "\"status\":" + status + ","
259249
if handlers != "" {
260-
var handlersVal []*interface{}
261-
err := json.Unmarshal([]byte(handlers), &handlersVal)
262-
if err != nil {
263-
return err
264-
}
265-
if handlersVal != nil {
266-
infoMap["runtimeHandlers"] = handlersVal
267-
}
250+
jsonInfo += "\"runtimeHandlers\":" + handlers + ","
268251
}
269-
270252
for _, k := range keys {
271-
infoMap[k] = strings.Trim(info[k], "\"")
272-
}
273-
274-
jsonInfo, err := json.Marshal(infoMap)
275-
if err != nil {
276-
return err
253+
var res interface{}
254+
// We attempt to convert key into JSON if possible else use it directly
255+
if err := json.Unmarshal([]byte(info[k]), &res); err != nil {
256+
jsonInfo += "\"" + k + "\"" + ":" + "\"" + info[k] + "\","
257+
} else {
258+
jsonInfo += "\"" + k + "\"" + ":" + info[k] + ","
259+
}
277260
}
261+
jsonInfo = jsonInfo[:len(jsonInfo)-1]
262+
jsonInfo += "}"
278263

279264
switch format {
280265
case "yaml":
281-
yamlInfo, err := yaml.JSONToYAML(jsonInfo)
266+
yamlInfo, err := yaml.JSONToYAML([]byte(jsonInfo))
282267
if err != nil {
283268
return err
284269
}
285270
fmt.Println(string(yamlInfo))
286271
case "json":
287272
var output bytes.Buffer
288-
if err := json.Indent(&output, jsonInfo, "", " "); err != nil {
273+
if err := json.Indent(&output, []byte(jsonInfo), "", " "); err != nil {
289274
return err
290275
}
291276
fmt.Println(output.String())
292277
case "go-template":
293-
output, err := tmplExecuteRawJSON(tmplStr, string(jsonInfo))
278+
output, err := tmplExecuteRawJSON(tmplStr, jsonInfo)
294279
if err != nil {
295280
return err
296281
}

cmd/crictl/util_test.go

-133
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"io"
21-
"os"
22-
"strings"
2320
"testing"
24-
25-
. "github.com/onsi/gomega"
2621
)
2722

2823
func TestNameFilterByRegex(t *testing.T) {
@@ -69,135 +64,7 @@ func TestNameFilterByRegex(t *testing.T) {
6964
if r != tc.isMatch {
7065
t.Errorf("expected matched to be %v; actual result is %v", tc.isMatch, r)
7166
}
72-
})
73-
}
74-
}
75-
76-
func TestOutputStatusInfo(t *testing.T) {
77-
const (
78-
statusResponse = `{"conditions":[
79-
{
80-
"message": "no network config found in C:\\Program Files",
81-
"reason": "NetworkPluginNotReady",
82-
"status": false,
83-
"type": "NetworkReady"
84-
}
85-
]}`
86-
handlerResponse = `[
87-
{
88-
"features": {
89-
"recursive_read_only_mounts": true
90-
},
91-
"name": "runc"
92-
},
93-
{
94-
"features": {
95-
"recursive_read_only_mounts": true,
96-
"user_namespaces": true
97-
},
98-
"name": "crun"
99-
}
100-
]`
101-
emptyResponse = ""
102-
)
103-
testCases := []struct {
104-
name string
105-
status string
106-
handlers string
107-
info map[string]string
108-
format string
109-
tmplStr string
110-
expectedOut string
111-
}{
112-
{
113-
name: "YAML format",
114-
status: statusResponse,
115-
handlers: handlerResponse,
116-
info: map[string]string{"key1": "value1", "key2": "/var/lib"},
117-
format: "yaml",
118-
tmplStr: "",
119-
expectedOut: "key1: value1\nkey2: /var/lib\nruntimeHandlers:\n- features:\n recursive_read_only_mounts: true\n name: runc\n- features:\n recursive_read_only_mounts: true\n user_namespaces: true\n name: crun\nstatus:\n conditions:\n - message: no network config found in C:\\Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady",
120-
},
121-
{
122-
name: "YAML format with empty status response",
123-
status: emptyResponse,
124-
handlers: handlerResponse,
125-
info: map[string]string{"key1": "value1", "key2": "/var/lib"},
126-
format: "yaml",
127-
tmplStr: "",
128-
expectedOut: "key1: value1\nkey2: /var/lib\nruntimeHandlers:\n- features:\n recursive_read_only_mounts: true\n name: runc\n- features:\n recursive_read_only_mounts: true\n user_namespaces: true\n name: crun",
129-
},
130-
{
131-
name: "YAML format with empty handlers response",
132-
status: statusResponse,
133-
handlers: emptyResponse,
134-
info: map[string]string{"key1": "value1", "key2": "/var/lib"},
135-
format: "yaml",
136-
tmplStr: "",
137-
expectedOut: "key1: value1\nkey2: /var/lib\nstatus:\n conditions:\n - message: no network config found in C:\\Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady",
138-
},
139-
{
140-
name: "JSON format",
141-
status: statusResponse,
142-
handlers: handlerResponse,
143-
info: map[string]string{"key1": "\"value1\"", "key2": "\"C:\\ProgramFiles\""},
144-
format: "json",
145-
tmplStr: "",
146-
expectedOut: "{\n \"key1\": \"value1\",\n \"key2\": \"C:\\\\ProgramFiles\",\n \"runtimeHandlers\": [\n {\n \"features\": {\n \"recursive_read_only_mounts\": true\n },\n \"name\": \"runc\"\n },\n {\n \"features\": {\n \"recursive_read_only_mounts\": true,\n \"user_namespaces\": true\n },\n \"name\": \"crun\"\n }\n ],\n \"status\": {\n \"conditions\": [\n {\n \"message\": \"no network config found in C:\\\\Program Files\",\n \"reason\": \"NetworkPluginNotReady\",\n \"status\": false,\n \"type\": \"NetworkReady\"\n }\n ]\n }\n}",
147-
},
148-
{
149-
name: "Go template format",
150-
status: statusResponse,
151-
handlers: handlerResponse,
152-
info: map[string]string{"key1": "value1", "key2": "value2"},
153-
format: "go-template",
154-
tmplStr: `NetworkReady: {{ (index .status.conditions 0).status }}`,
155-
expectedOut: "NetworkReady: false",
156-
},
157-
}
158-
159-
// Run tests
160-
for _, tc := range testCases {
161-
t.Run(tc.name, func(t *testing.T) {
162-
captureOutput := func(f func() error) (string, error) {
163-
var err error
164-
old := os.Stdout
165-
166-
r, w, _ := os.Pipe()
167-
os.Stdout = w
168-
defer func() {
169-
os.Stdout = old
170-
}()
171-
172-
err = f()
173-
if err != nil {
174-
return "", err
175-
}
176-
177-
err = w.Close()
178-
if err != nil {
179-
return "", err
180-
}
181-
182-
out, err := io.ReadAll(r)
183-
return strings.TrimRight(string(out), "\n"), err
184-
}
185-
186-
outStr, err := captureOutput(func() error {
187-
err := outputStatusInfo(tc.status, tc.handlers, tc.info, tc.format, tc.tmplStr)
188-
if err != nil {
189-
t.Errorf("Unexpected error: %v", err)
190-
}
191-
return nil
192-
})
19367

194-
if err != nil {
195-
Expect(err).To(BeNil())
196-
}
197-
198-
if outStr != tc.expectedOut {
199-
t.Errorf("Expected output:\n%s\nGot:\n%s", tc.expectedOut, outStr)
200-
}
20168
})
20269
}
20370
}

0 commit comments

Comments
 (0)