@@ -17,7 +17,12 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
+ "io"
21
+ "os"
22
+ "strings"
20
23
"testing"
24
+
25
+ . "github.com/onsi/gomega"
21
26
)
22
27
23
28
func TestNameFilterByRegex (t * testing.T ) {
@@ -64,7 +69,135 @@ func TestNameFilterByRegex(t *testing.T) {
64
69
if r != tc .isMatch {
65
70
t .Errorf ("expected matched to be %v; actual result is %v" , tc .isMatch , r )
66
71
}
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\n key2: /var/lib\n runtimeHandlers:\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\n status:\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\n key2: /var/lib\n runtimeHandlers:\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\n key2: /var/lib\n status:\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
+ })
67
193
194
+ if err != nil {
195
+ Expect (err ).To (BeNil ())
196
+ }
197
+
198
+ if outStr != tc .expectedOut {
199
+ t .Errorf ("Expected output:\n %s\n Got:\n %s" , tc .expectedOut , outStr )
200
+ }
68
201
})
69
202
}
70
203
}
0 commit comments