@@ -54,20 +54,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
5454 continue
5555 }
5656
57- for _ , serverName := range expFieldCfg .Servers {
58- if directive .Directive == "server" && getServerName (directive .Block ) == serverName {
59- for _ , serverDirective := range directive .Block {
60- if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
61- return nil
62- } else if serverDirective .Directive == "location" &&
63- fieldExistsInLocation (serverDirective , expFieldCfg ) {
64- return nil
65- }
66- }
67- }
57+ err := validateServerBlockDirectives (expFieldCfg , * directive )
58+ if err != nil {
59+ return err
6860 }
6961
70- return validateUpstreamDirectives (expFieldCfg , directive )
62+ err = validateUpstreamDirectives (expFieldCfg , directive )
63+ if err != nil {
64+ return err
65+ }
7166 }
7267 }
7368
@@ -79,6 +74,22 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
7974 return fmt .Errorf ("field not found; expected: %+v\n NGINX conf: %s" , expFieldCfg , string (b ))
8075}
8176
77+ func validateServerBlockDirectives (expFieldCfg ExpectedNginxField , directive Directive ) error {
78+ for _ , serverName := range expFieldCfg .Servers {
79+ if directive .Directive == "server" && getServerName (directive .Block ) == serverName {
80+ for _ , serverDirective := range directive .Block {
81+ if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
82+ return nil
83+ } else if serverDirective .Directive == "location" &&
84+ fieldExistsInLocation (serverDirective , expFieldCfg ) {
85+ return nil
86+ }
87+ }
88+ }
89+ }
90+ return fmt .Errorf ("field not found; expected: %+v\n NGINX conf: %s" , expFieldCfg , directive .Directive )
91+ }
92+
8293func validateUpstreamDirectives (expFieldCfg ExpectedNginxField , directive * Directive ) error {
8394 for _ , upstreamName := range expFieldCfg .Upstreams {
8495 if directive .Directive == "upstream" && directive .Args [0 ] == upstreamName {
@@ -89,7 +100,7 @@ func validateUpstreamDirectives(expFieldCfg ExpectedNginxField, directive *Direc
89100 }
90101 }
91102 }
92- return nil
103+ return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
93104}
94105
95106func getServerName (serverBlock Directives ) string {
0 commit comments