8
8
"strings"
9
9
"time"
10
10
11
+ . "github.com/onsi/ginkgo/v2"
11
12
core "k8s.io/api/core/v1"
12
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
14
"k8s.io/client-go/kubernetes"
@@ -42,10 +43,13 @@ const crossplaneImageName = "nginx-crossplane:latest"
42
43
43
44
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
44
45
// and returns whether or not that field exists where it should.
45
- func ValidateNginxFieldExists (conf * Payload , expFieldCfg ExpectedNginxField ) error {
46
+ func ValidateNginxFieldExists (conf * Payload , expFieldCfg ExpectedNginxField , opts ... Option ) error {
46
47
b , err := json .Marshal (conf )
47
48
if err != nil {
48
- return fmt .Errorf ("error marshaling nginx config: %w" , err )
49
+ marshalErr := fmt .Errorf ("error marshaling nginx config: %w" , err )
50
+ GinkgoWriter .Printf ("%v\n " , marshalErr )
51
+
52
+ return marshalErr
49
53
}
50
54
51
55
for _ , config := range conf .Config {
@@ -55,7 +59,7 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
55
59
56
60
for _ , directive := range config .Parsed {
57
61
if expFieldCfg .Server == "" && expFieldCfg .Upstream == "" {
58
- if expFieldCfg .fieldFound (directive ) {
62
+ if expFieldCfg .fieldFound (directive , opts ... ) {
59
63
return nil
60
64
}
61
65
continue
@@ -70,8 +74,10 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
70
74
}
71
75
}
72
76
}
77
+ directiveErr := fmt .Errorf ("directive %s not found in: nginx config %s" , expFieldCfg .Directive , string (b ))
78
+ GinkgoWriter .Printf ("ERROR: %v\n " , directiveErr )
73
79
74
- return fmt . Errorf ( "directive %s not found in: nginx config %s" , expFieldCfg . Directive , string ( b ))
80
+ return directiveErr
75
81
}
76
82
77
83
func fieldExistsInServer (
@@ -95,6 +101,12 @@ func fieldExistsInUpstream(
95
101
expFieldCfg ExpectedNginxField ,
96
102
directive Directive ,
97
103
) bool {
104
+ GinkgoWriter .Printf (
105
+ "Checking upstream %q for directive %q with value %q\n " ,
106
+ directive .Args [0 ],
107
+ expFieldCfg .Directive ,
108
+ expFieldCfg .Value ,
109
+ )
98
110
if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
99
111
for _ , directive := range directive .Block {
100
112
if expFieldCfg .fieldFound (directive ) {
@@ -115,15 +127,32 @@ func getServerName(serverBlock Directives) string {
115
127
return ""
116
128
}
117
129
118
- func (e ExpectedNginxField ) fieldFound (directive * Directive ) bool {
130
+ func (e ExpectedNginxField ) fieldFound (directive * Directive , opts ... Option ) bool {
131
+ options := & Options {logEnabled : true }
132
+ for _ , opt := range opts {
133
+ opt (options )
134
+ }
119
135
arg := strings .Join (directive .Args , " " )
120
136
121
137
valueMatch := arg == e .Value
122
138
if e .ValueSubstringAllowed {
123
139
valueMatch = strings .Contains (arg , e .Value )
124
140
}
125
141
126
- return directive .Directive == e .Directive && valueMatch
142
+ if directive .Directive == e .Directive && valueMatch {
143
+ if options .logEnabled {
144
+ GinkgoWriter .Printf (
145
+ "Found field %q with value %q in field %q with value %q\n " ,
146
+ e .Directive ,
147
+ e .Value ,
148
+ directive .Directive ,
149
+ arg ,
150
+ )
151
+ }
152
+ return true
153
+ }
154
+
155
+ return false
127
156
}
128
157
129
158
func fieldExistsInLocation (locationDirective * Directive , expFieldCfg ExpectedNginxField ) bool {
@@ -201,7 +230,10 @@ func injectCrossplaneContainer(
201
230
202
231
podClient := k8sClient .CoreV1 ().Pods (namespace )
203
232
if _ , err := podClient .UpdateEphemeralContainers (ctx , ngfPodName , pod , metav1.UpdateOptions {}); err != nil {
204
- return fmt .Errorf ("error adding ephemeral container: %w" , err )
233
+ containerErr := fmt .Errorf ("error adding ephemeral container: %w" , err )
234
+ GinkgoWriter .Printf ("%v\n " , containerErr )
235
+
236
+ return containerErr
205
237
}
206
238
207
239
return nil
@@ -231,7 +263,10 @@ func createCrossplaneExecutor(
231
263
232
264
exec , err := remotecommand .NewSPDYExecutor (k8sConfig , http .MethodPost , req .URL ())
233
265
if err != nil {
234
- return nil , fmt .Errorf ("error creating executor: %w" , err )
266
+ executorErr := fmt .Errorf ("error creating executor: %w" , err )
267
+ GinkgoWriter .Printf ("%v\n " , executorErr )
268
+
269
+ return nil , executorErr
235
270
}
236
271
237
272
return exec , nil
0 commit comments