@@ -30,7 +30,7 @@ type ExpectedNginxField struct {
3030 Location string
3131 // Servers are the server names that the directive should exist in.
3232 Servers []string
33- // Upstream are the upstream names that the directive should exist in.
33+ // Upstreams are the upstream names that the directive should exist in.
3434 Upstreams []string
3535 // ValueSubstringAllowed allows the expected value to be a substring of the real value.
3636 // This makes it easier for cases when real values are complex file names or contain things we
@@ -54,13 +54,11 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
5454 continue
5555 }
5656
57- err := validateServerBlockDirectives (expFieldCfg , * directive )
58- if err != nil {
57+ if err := validateServerBlockDirectives (expFieldCfg , * directive ); err != nil {
5958 return err
6059 }
6160
62- err = validateUpstreamDirectives (expFieldCfg , directive )
63- if err != nil {
61+ if err := validateUpstreamDirectives (expFieldCfg , directive ); err != nil {
6462 return err
6563 }
6664 }
@@ -78,29 +76,29 @@ func validateServerBlockDirectives(expFieldCfg ExpectedNginxField, directive Dir
7876 for _ , serverName := range expFieldCfg .Servers {
7977 if directive .Directive == "server" && getServerName (directive .Block ) == serverName {
8078 for _ , serverDirective := range directive .Block {
81- if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
82- return nil
79+ if expFieldCfg .Location == "" && ! expFieldCfg .fieldFound (serverDirective ) {
80+ return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , serverDirective . Directive )
8381 } else if serverDirective .Directive == "location" &&
84- fieldExistsInLocation (serverDirective , expFieldCfg ) {
85- return nil
82+ ! fieldExistsInLocation (serverDirective , expFieldCfg ) {
83+ return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , serverDirective . Directive )
8684 }
8785 }
8886 }
8987 }
90- return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
88+ return nil
9189}
9290
9391func validateUpstreamDirectives (expFieldCfg ExpectedNginxField , directive * Directive ) error {
9492 for _ , upstreamName := range expFieldCfg .Upstreams {
9593 if directive .Directive == "upstream" && directive .Args [0 ] == upstreamName {
9694 for _ , upstreamDirective := range directive .Block {
97- if expFieldCfg .fieldFound (upstreamDirective ) {
98- return nil
95+ if ! expFieldCfg .fieldFound (upstreamDirective ) {
96+ return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , upstreamDirective . Directive )
9997 }
10098 }
10199 }
102100 }
103- return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
101+ return nil
104102}
105103
106104func getServerName (serverBlock Directives ) string {
0 commit comments