@@ -54,20 +54,15 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
54
54
continue
55
55
}
56
56
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
68
60
}
69
61
70
- return validateUpstreamDirectives (expFieldCfg , directive )
62
+ err = validateUpstreamDirectives (expFieldCfg , directive )
63
+ if err != nil {
64
+ return err
65
+ }
71
66
}
72
67
}
73
68
@@ -79,6 +74,22 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
79
74
return fmt .Errorf ("field not found; expected: %+v\n NGINX conf: %s" , expFieldCfg , string (b ))
80
75
}
81
76
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
+
82
93
func validateUpstreamDirectives (expFieldCfg ExpectedNginxField , directive * Directive ) error {
83
94
for _ , upstreamName := range expFieldCfg .Upstreams {
84
95
if directive .Directive == "upstream" && directive .Args [0 ] == upstreamName {
@@ -89,7 +100,7 @@ func validateUpstreamDirectives(expFieldCfg ExpectedNginxField, directive *Direc
89
100
}
90
101
}
91
102
}
92
- return nil
103
+ return fmt . Errorf ( "field not found; expected: %+v \n NGINX conf: %s" , expFieldCfg , directive . Directive )
93
104
}
94
105
95
106
func getServerName (serverBlock Directives ) string {
0 commit comments