@@ -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,99 @@ 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
+ "message": "no network config found in C:\\Program Files",
80
+ "reason": "NetworkPluginNotReady",
81
+ "status": false,
82
+ "type": "NetworkReady"
83
+ }]}`
84
+ )
85
+ testCases := []struct {
86
+ name string
87
+ status string
88
+ handlers string
89
+ info map [string ]string
90
+ format string
91
+ tmplStr string
92
+ expectedOut string
93
+ }{
94
+ {
95
+ name : "YAML format" ,
96
+ status : statusResponse ,
97
+ handlers : `{"handlers":["handler1","handler2"]}` ,
98
+ info : map [string ]string {"key1" : "value1" , "key2" : "/var/lib" },
99
+ format : "yaml" ,
100
+ tmplStr : "" ,
101
+ expectedOut : "key1: value1\n key2: /var/lib\n runtimeHandlers:\n handlers:\n - handler1\n - handler2\n status:\n conditions:\n - message: no network config found in C:\\ Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady" ,
102
+ },
103
+ {
104
+ name : "JSON format" ,
105
+ status : statusResponse ,
106
+ handlers : `{"handlers":["handler1","handler2"]}` ,
107
+ info : map [string ]string {"key1" : "\" value1\" " , "key2" : "\" C:\\ ProgramFiles\" " },
108
+ format : "json" ,
109
+ tmplStr : "" ,
110
+ expectedOut : "{\n \" key1\" : \" value1\" ,\n \" key2\" : \" C:\\ \\ ProgramFiles\" ,\n \" runtimeHandlers\" : {\n \" handlers\" : [\n \" handler1\" ,\n \" handler2\" \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 }" ,
111
+ },
112
+ {
113
+ name : "Go template format" ,
114
+ status : statusResponse ,
115
+ handlers : `{"handlers":["handler1","handler2"]}` ,
116
+ info : map [string ]string {"key1" : "value1" , "key2" : "value2" },
117
+ format : "go-template" ,
118
+ tmplStr : `NetworkReady: {{ (index .status.conditions 0).status }}` ,
119
+ expectedOut : "NetworkReady: false" ,
120
+ },
121
+ }
122
+
123
+ // Run tests
124
+ for _ , tc := range testCases {
125
+ t .Run (tc .name , func (t * testing.T ) {
126
+ captureOutput := func (f func () error ) (string , error ) {
127
+ var err error
128
+ old := os .Stdout
129
+
130
+ r , w , _ := os .Pipe ()
131
+ os .Stdout = w
132
+ defer func () {
133
+ os .Stdout = old
134
+ }()
135
+
136
+ err = f ()
137
+ if err != nil {
138
+ return "" , err
139
+ }
140
+
141
+ err = w .Close ()
142
+ if err != nil {
143
+ return "" , err
144
+ }
145
+
146
+ out , err := io .ReadAll (r )
147
+ return strings .TrimRight (string (out ), "\n " ), err
148
+ }
149
+
150
+ outStr , err := captureOutput (func () error {
151
+ err := outputStatusInfo (tc .status , tc .handlers , tc .info , tc .format , tc .tmplStr )
152
+ if err != nil {
153
+ t .Errorf ("Unexpected error: %v" , err )
154
+ }
155
+ return nil
156
+ })
157
+
158
+ if err != nil {
159
+ Expect (err ).To (BeNil ())
160
+ }
161
+
162
+ if outStr != tc .expectedOut {
163
+ t .Errorf ("Expected output:\n %s\n Got:\n %s" , tc .expectedOut , outStr )
164
+ }
68
165
})
69
166
}
70
167
}
0 commit comments