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
@@ -65,13 +69,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
65
69
return nil
66
70
}
67
71
68
- if expFieldCfg .Upstream != "" && fieldExistsInUpstream (expFieldCfg , * directive ) {
72
+ if expFieldCfg .Upstream != "" && fieldExistsInUpstream (expFieldCfg , * directive , opts ... ) {
69
73
return nil
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 (
@@ -94,7 +100,16 @@ func fieldExistsInServer(
94
100
func fieldExistsInUpstream (
95
101
expFieldCfg ExpectedNginxField ,
96
102
directive Directive ,
103
+ opts ... Option ,
97
104
) bool {
105
+ options := LogOptions (opts ... )
106
+ if options .logEnabled {
107
+ GinkgoWriter .Printf (
108
+ "Checking upstream for directive %q with value %q\n " ,
109
+ expFieldCfg .Directive ,
110
+ expFieldCfg .Value ,
111
+ )
112
+ }
98
113
if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
99
114
for _ , directive := range directive .Block {
100
115
if expFieldCfg .fieldFound (directive ) {
@@ -115,15 +130,29 @@ func getServerName(serverBlock Directives) string {
115
130
return ""
116
131
}
117
132
118
- func (e ExpectedNginxField ) fieldFound (directive * Directive ) bool {
133
+ func (e ExpectedNginxField ) fieldFound (directive * Directive , opts ... Option ) bool {
134
+ options := LogOptions (opts ... )
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