@@ -30,7 +30,7 @@ type ExpectedNginxField struct {
30
30
Location string
31
31
// Servers are the server names that the directive should exist in.
32
32
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.
34
34
Upstreams []string
35
35
// ValueSubstringAllowed allows the expected value to be a substring of the real value.
36
36
// 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
54
54
continue
55
55
}
56
56
57
- err := validateServerBlockDirectives (expFieldCfg , * directive )
58
- if err != nil {
57
+ if err := validateServerBlockDirectives (expFieldCfg , * directive ); err != nil {
59
58
return err
60
59
}
61
60
62
- err = validateUpstreamDirectives (expFieldCfg , directive )
63
- if err != nil {
61
+ if err := validateUpstreamDirectives (expFieldCfg , directive ); err != nil {
64
62
return err
65
63
}
66
64
}
@@ -78,29 +76,29 @@ func validateServerBlockDirectives(expFieldCfg ExpectedNginxField, directive Dir
78
76
for _ , serverName := range expFieldCfg .Servers {
79
77
if directive .Directive == "server" && getServerName (directive .Block ) == serverName {
80
78
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 )
83
81
} 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 )
86
84
}
87
85
}
88
86
}
89
87
}
90
- return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
88
+ return nil
91
89
}
92
90
93
91
func validateUpstreamDirectives (expFieldCfg ExpectedNginxField , directive * Directive ) error {
94
92
for _ , upstreamName := range expFieldCfg .Upstreams {
95
93
if directive .Directive == "upstream" && directive .Args [0 ] == upstreamName {
96
94
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 )
99
97
}
100
98
}
101
99
}
102
100
}
103
- return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
101
+ return nil
104
102
}
105
103
106
104
func getServerName (serverBlock Directives ) string {
0 commit comments