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
@@ -65,13 +69,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
6569 return nil
6670 }
6771
68- if expFieldCfg .Upstream != "" && fieldExistsInUpstream (expFieldCfg , * directive ) {
72+ if expFieldCfg .Upstream != "" && fieldExistsInUpstream (expFieldCfg , * directive , opts ... ) {
6973 return nil
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 (
@@ -94,7 +100,16 @@ func fieldExistsInServer(
94100func fieldExistsInUpstream (
95101 expFieldCfg ExpectedNginxField ,
96102 directive Directive ,
103+ opts ... Option ,
97104) 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+ }
98113 if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
99114 for _ , directive := range directive .Block {
100115 if expFieldCfg .fieldFound (directive ) {
@@ -115,15 +130,29 @@ func getServerName(serverBlock Directives) string {
115130 return ""
116131}
117132
118- func (e ExpectedNginxField ) fieldFound (directive * Directive ) bool {
133+ func (e ExpectedNginxField ) fieldFound (directive * Directive , opts ... Option ) bool {
134+ options := LogOptions (opts ... )
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