Skip to content

Commit ca42375

Browse files
committed
update tests as needed
1 parent 1580e84 commit ca42375

9 files changed

+304
-312
lines changed

Diff for: tests/framework/crossplane.go

+44-34
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ type ExpectedNginxField struct {
2828
File string
2929
// Location is the location name that the directive should exist in.
3030
Location string
31-
// Servers are the server names that the directive should exist in.
32-
Servers []string
33-
// Upstreams are the upstream names that the directive should exist in.
34-
Upstreams []string
31+
// Server is the server name that the directive should exist in.
32+
Server string
33+
// Upstream is the upstream name that the directive should exist in.
34+
Upstream 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
3737
// don't care about, and we just want to check if a substring exists.
@@ -41,64 +41,74 @@ type ExpectedNginxField struct {
4141
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
4242
// and returns whether or not that field exists where it should.
4343
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
44+
var directiveFoundInServer, directiveFoundInUpstream bool
45+
46+
b, err := json.Marshal(conf)
47+
if err != nil {
48+
return fmt.Errorf("error marshaling nginx config: %w", err)
49+
}
50+
4451
for _, config := range conf.Config {
4552
if !strings.Contains(config.File, expFieldCfg.File) {
4653
continue
4754
}
4855

4956
for _, directive := range config.Parsed {
50-
if len(expFieldCfg.Servers) == 0 {
57+
if len(expFieldCfg.Server) == 0 && len(expFieldCfg.Upstream) == 0 {
5158
if expFieldCfg.fieldFound(directive) {
5259
return nil
5360
}
5461
continue
5562
}
5663

57-
if err := validateServerBlockDirectives(expFieldCfg, *directive); err != nil {
58-
return err
64+
directiveFoundInServer = validateServerBlockDirectives(expFieldCfg, *directive)
65+
66+
directiveFoundInUpstream = validateUpstreamDirectives(expFieldCfg, *directive)
67+
68+
if len(expFieldCfg.Server) > 0 && directiveFoundInServer {
69+
return nil
5970
}
6071

61-
if err := validateUpstreamDirectives(expFieldCfg, directive); err != nil {
62-
return err
72+
if len(expFieldCfg.Upstream) > 0 && directiveFoundInUpstream {
73+
return nil
6374
}
6475
}
6576
}
6677

67-
b, err := json.Marshal(conf)
68-
if err != nil {
69-
return fmt.Errorf("error marshaling nginx config: %w", err)
70-
}
71-
72-
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, string(b))
78+
return fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
7379
}
7480

75-
func validateServerBlockDirectives(expFieldCfg ExpectedNginxField, directive Directive) error {
76-
for _, serverName := range expFieldCfg.Servers {
77-
if directive.Directive == "server" && getServerName(directive.Block) == serverName {
78-
for _, serverDirective := range directive.Block {
79-
if expFieldCfg.Location == "" && !expFieldCfg.fieldFound(serverDirective) {
80-
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, serverDirective.Directive)
81-
} else if serverDirective.Directive == "location" &&
82-
!fieldExistsInLocation(serverDirective, expFieldCfg) {
83-
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, serverDirective.Directive)
84-
}
81+
func validateServerBlockDirectives(
82+
expFieldCfg ExpectedNginxField,
83+
directive Directive,
84+
) bool {
85+
var fieldFound bool
86+
if directive.Directive == "server" && getServerName(directive.Block) == expFieldCfg.Server {
87+
for _, serverDirective := range directive.Block {
88+
if expFieldCfg.Location == "" && expFieldCfg.fieldFound(serverDirective) {
89+
fieldFound = true
90+
} else if serverDirective.Directive == "location" &&
91+
fieldExistsInLocation(serverDirective, expFieldCfg) {
92+
fieldFound = true
8593
}
8694
}
8795
}
88-
return nil
96+
return fieldFound
8997
}
9098

91-
func validateUpstreamDirectives(expFieldCfg ExpectedNginxField, directive *Directive) error {
92-
for _, upstreamName := range expFieldCfg.Upstreams {
93-
if directive.Directive == "upstream" && directive.Args[0] == upstreamName {
94-
for _, upstreamDirective := range directive.Block {
95-
if !expFieldCfg.fieldFound(upstreamDirective) {
96-
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, upstreamDirective.Directive)
97-
}
99+
func validateUpstreamDirectives(
100+
expFieldCfg ExpectedNginxField,
101+
directive Directive,
102+
) bool {
103+
var fieldFound bool
104+
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
105+
for _, directive := range directive.Block {
106+
if expFieldCfg.fieldFound(directive) {
107+
fieldFound = true
98108
}
99109
}
100110
}
101-
return nil
111+
return fieldFound
102112
}
103113

104114
func getServerName(serverBlock Directives) string {

Diff for: tests/suite/client_settings_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
117117
Directive: "include",
118118
Value: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
119119
File: "http.conf",
120-
Servers: []string{"*.example.com", "cafe.example.com"},
120+
Server: "*.example.com",
121+
},
122+
{
123+
Directive: "include",
124+
Value: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
125+
File: "http.conf",
126+
Server: "cafe.example.com",
121127
},
122128
{
123129
Directive: "client_max_body_size",
@@ -150,7 +156,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
150156
Directive: "include",
151157
Value: fmt.Sprintf("%s_coffee-route-csp.conf", filePrefix),
152158
File: "http.conf",
153-
Servers: []string{"cafe.example.com"},
159+
Server: "cafe.example.com",
154160
Location: "/coffee",
155161
},
156162
{
@@ -164,7 +170,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
164170
Directive: "include",
165171
Value: fmt.Sprintf("%s_tea-route-csp.conf", filePrefix),
166172
File: "http.conf",
167-
Servers: []string{"cafe.example.com"},
173+
Server: "cafe.example.com",
168174
Location: "/tea",
169175
},
170176
{
@@ -178,7 +184,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
178184
Directive: "include",
179185
Value: fmt.Sprintf("%s_soda-route-csp.conf", filePrefix),
180186
File: "http.conf",
181-
Servers: []string{"cafe.example.com"},
187+
Server: "cafe.example.com",
182188
Location: "/soda",
183189
},
184190
{
@@ -192,7 +198,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
192198
Directive: "include",
193199
Value: fmt.Sprintf("%s_grpc-route-csp.conf", filePrefix),
194200
File: "http.conf",
195-
Servers: []string{"*.example.com"},
201+
Server: "*.example.com",
196202
Location: "/helloworld.Greeter/SayHello",
197203
},
198204
{

Diff for: tests/suite/manifests/upstream-settings-policy/invalid-target-usps.yaml

+28-27
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ metadata:
44
name: gateway-not-valid
55
spec:
66
gatewayClassName: nginx
7-
addresses: "10.0.0.1"
7+
addresses:
8+
- value: "10.0.0.1"
89
listeners:
9-
- name: http
10-
port: 80
11-
protocol: HTTP
12-
hostname: "soda.example.com"
10+
- name: http
11+
port: 80
12+
protocol: HTTP
13+
hostname: "soda.example.com"
1314
---
1415
apiVersion: apps/v1
1516
kind: Deployment
@@ -26,42 +27,42 @@ spec:
2627
app: soda
2728
spec:
2829
containers:
29-
- name: soda
30-
image: nginxdemos/nginx-hello:plain-text
31-
ports:
32-
- containerPort: 8080
30+
- name: soda
31+
image: nginxdemos/nginx-hello:plain-text
32+
ports:
33+
- containerPort: 8080
3334
---
3435
apiVersion: v1
3536
kind: Service
3637
metadata:
3738
name: soda
3839
spec:
3940
ports:
40-
- port: 80
41-
targetPort: 8080
42-
protocol: TCP
43-
name: http
41+
- port: 80
42+
targetPort: 8080
43+
protocol: TCP
44+
name: http
4445
selector:
4546
app: soda
4647
---
4748
apiVersion: gateway.networking.k8s.io/v1
4849
kind: HTTPRoute
4950
metadata:
50-
name: soda
51+
name: sode
5152
spec:
5253
parentRefs:
53-
- name: gateway-httproute-allowed
54-
sectionName: http
54+
- name: gateway-not-valid
55+
sectionName: http
5556
hostnames:
56-
- "soda.example.com"
57+
- "soda.example.com"
5758
rules:
58-
- matches:
59-
- path:
60-
type: Exact
61-
value: /soda
62-
backendRefs:
63-
- name: soda
64-
port: 80
59+
- matches:
60+
- path:
61+
type: Exact
62+
value: /soda
63+
backendRefs:
64+
- name: soda
65+
port: 80
6566
---
6667
apiVersion: gateway.nginx.org/v1alpha1
6768
kind: UpstreamSettingsPolicy
@@ -70,9 +71,9 @@ metadata:
7071
spec:
7172
zoneSize: 512k
7273
targetRefs:
73-
- group: core
74-
kind: Service
75-
name: soda
74+
- group: core
75+
kind: Service
76+
name: soda
7677
keepAlive:
7778
connections: 10
7879
requests: 3

Diff for: tests/suite/manifests/upstream-settings-policy/routes.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ spec:
1818
port: 80
1919
---
2020
apiVersion: gateway.networking.k8s.io/v1
21+
kind: HTTPRoute
22+
metadata:
23+
name: tea
24+
spec:
25+
parentRefs:
26+
- name: gateway
27+
sectionName: http
28+
hostnames:
29+
- "cafe.example.com"
30+
rules:
31+
- matches:
32+
- path:
33+
type: Exact
34+
value: /tea
35+
backendRefs:
36+
- name: tea
37+
port: 80
38+
---
39+
apiVersion: gateway.networking.k8s.io/v1
2140
kind: GRPCRoute
2241
metadata:
2342
name: grpc-route
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
apiVersion: gateway.nginx.org/v1alpha1
22
kind: UpstreamSettingsPolicy
33
metadata:
4-
name: coffee-svc-usp-1
4+
name: merge-usp-1
55
spec:
6-
zoneSize: 1g
76
targetRefs:
87
- group: core
98
kind: Service
109
name: coffee
10+
keepAlive:
11+
time: 1m
12+
timeout: 5h
1113
---
1214
apiVersion: gateway.nginx.org/v1alpha1
1315
kind: UpstreamSettingsPolicy
1416
metadata:
15-
name: coffee-svc-usp-2
17+
name: merge-usp-2
1618
spec:
1719
targetRefs:
1820
- group: core
@@ -21,5 +23,25 @@ spec:
2123
keepAlive:
2224
connections: 100
2325
requests: 55
24-
time: 1m
25-
timeout: 5h
26+
---
27+
apiVersion: gateway.nginx.org/v1alpha1
28+
kind: UpstreamSettingsPolicy
29+
metadata:
30+
name: z-usp-wins
31+
spec:
32+
zoneSize: 64k
33+
targetRefs:
34+
- group: core
35+
kind: Service
36+
name: tea
37+
---
38+
apiVersion: gateway.nginx.org/v1alpha1
39+
kind: UpstreamSettingsPolicy
40+
metadata:
41+
name: a-usp
42+
spec:
43+
zoneSize: 128k
44+
targetRefs:
45+
- group: core
46+
kind: Service
47+
name: tea

Diff for: tests/suite/manifests/upstream-settings-policy/valid-usps-first-wins.yaml

-21
This file was deleted.

Diff for: tests/suite/manifests/upstream-settings-policy/valid-usps.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ kind: UpstreamSettingsPolicy
2121
metadata:
2222
name: grpc-svc-usp
2323
spec:
24-
targetRef:
25-
group: core
26-
kind: Service
27-
name: grpc-backend
24+
targetRefs:
25+
- group: core
26+
kind: Service
27+
name: grpc-backend
2828
zoneSize: 64k
2929
keepAlive:
3030
connections: 100

0 commit comments

Comments
 (0)