@@ -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,131 @@ 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
+ }
67
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" : `{"foo": "bar"}` , "key2" : `{"bar": "baz"}` },
117
+ format : "yaml" ,
118
+ tmplStr : "" ,
119
+ expectedOut : "key1:\n foo: bar\n key2:\n bar: baz\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
+ format : "yaml" ,
126
+ tmplStr : "" ,
127
+ expectedOut : "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" ,
128
+ },
129
+ {
130
+ name : "YAML format with empty handlers response" ,
131
+ status : statusResponse ,
132
+ handlers : emptyResponse ,
133
+ format : "yaml" ,
134
+ tmplStr : "" ,
135
+ expectedOut : "status:\n conditions:\n - message: no network config found in C:\\ Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady" ,
136
+ },
137
+ {
138
+ name : "JSON format" ,
139
+ status : statusResponse ,
140
+ handlers : handlerResponse ,
141
+ format : "json" ,
142
+ tmplStr : "" ,
143
+ expectedOut : "{\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 }" ,
144
+ },
145
+ {
146
+ name : "Go template format" ,
147
+ status : statusResponse ,
148
+ handlers : handlerResponse ,
149
+ format : "go-template" ,
150
+ tmplStr : `NetworkReady: {{ (index .status.conditions 0).status }}` ,
151
+ expectedOut : "NetworkReady: false" ,
152
+ },
153
+ }
154
+
155
+ // Run tests
156
+ for _ , tc := range testCases {
157
+ t .Run (tc .name , func (t * testing.T ) {
158
+ captureOutput := func (f func () error ) (string , error ) {
159
+ var err error
160
+ old := os .Stdout
161
+
162
+ r , w , _ := os .Pipe ()
163
+ os .Stdout = w
164
+ defer func () {
165
+ os .Stdout = old
166
+ }()
167
+
168
+ err = f ()
169
+ if err != nil {
170
+ return "" , err
171
+ }
172
+
173
+ err = w .Close ()
174
+ if err != nil {
175
+ return "" , err
176
+ }
177
+
178
+ out , err := io .ReadAll (r )
179
+ return strings .TrimRight (string (out ), "\n " ), err
180
+ }
181
+
182
+ outStr , err := captureOutput (func () error {
183
+ err := outputStatusInfo (tc .status , tc .handlers , tc .info , tc .format , tc .tmplStr )
184
+ if err != nil {
185
+ t .Errorf ("Unexpected error: %v" , err )
186
+ }
187
+ return nil
188
+ })
189
+
190
+ if err != nil {
191
+ Expect (err ).To (BeNil ())
192
+ }
193
+
194
+ if outStr != tc .expectedOut {
195
+ t .Errorf ("Expected output:\n %s\n Got:\n %s" , tc .expectedOut , outStr )
196
+ }
68
197
})
69
198
}
70
199
}
0 commit comments