88 "strings"
99 "time"
1010
11+ . "github.com/onsi/ginkgo/v2"
1112 core "k8s.io/api/core/v1"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 "k8s.io/client-go/kubernetes"
@@ -42,10 +43,13 @@ const crossplaneImageName = "nginx-crossplane:latest"
4243
4344// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
4445// 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 {
4647 b , err := json .Marshal (conf )
4748 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
4953 }
5054
5155 for _ , config := range conf .Config {
@@ -55,7 +59,7 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
5559
5660 for _ , directive := range config .Parsed {
5761 if expFieldCfg .Server == "" && expFieldCfg .Upstream == "" {
58- if expFieldCfg .fieldFound (directive ) {
62+ if expFieldCfg .fieldFound (directive , opts ... ) {
5963 return nil
6064 }
6165 continue
@@ -70,8 +74,10 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
7074 }
7175 }
7276 }
77+ directiveErr := fmt .Errorf ("directive %s not found in: nginx config %s" , expFieldCfg .Directive , string (b ))
78+ GinkgoWriter .Printf ("ERROR: %v\n " , directiveErr )
7379
74- return fmt . Errorf ( "directive %s not found in: nginx config %s" , expFieldCfg . Directive , string ( b ))
80+ return directiveErr
7581}
7682
7783func fieldExistsInServer (
@@ -95,6 +101,12 @@ func fieldExistsInUpstream(
95101 expFieldCfg ExpectedNginxField ,
96102 directive Directive ,
97103) 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+ )
98110 if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
99111 for _ , directive := range directive .Block {
100112 if expFieldCfg .fieldFound (directive ) {
@@ -115,15 +127,32 @@ func getServerName(serverBlock Directives) string {
115127 return ""
116128}
117129
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+ }
119135 arg := strings .Join (directive .Args , " " )
120136
121137 valueMatch := arg == e .Value
122138 if e .ValueSubstringAllowed {
123139 valueMatch = strings .Contains (arg , e .Value )
124140 }
125141
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
127156}
128157
129158func fieldExistsInLocation (locationDirective * Directive , expFieldCfg ExpectedNginxField ) bool {
@@ -201,7 +230,10 @@ func injectCrossplaneContainer(
201230
202231 podClient := k8sClient .CoreV1 ().Pods (namespace )
203232 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
205237 }
206238
207239 return nil
@@ -231,7 +263,10 @@ func createCrossplaneExecutor(
231263
232264 exec , err := remotecommand .NewSPDYExecutor (k8sConfig , http .MethodPost , req .URL ())
233265 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
235270 }
236271
237272 return exec , nil
0 commit comments