Skip to content

Commit a8120ca

Browse files
salonichf5Kate Osborn
authored and
Kate Osborn
committed
Functional tests for UpstreamSettings Policy (#2869)
Functional Tests for UpstreamSettings Policy.
1 parent ec7c527 commit a8120ca

12 files changed

+943
-27
lines changed

Diff for: tests/framework/crossplane.go

+46-18
Original file line numberDiff line numberDiff line change
@@ -28,8 +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
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
3335
// ValueSubstringAllowed allows the expected value to be a substring of the real value.
3436
// This makes it easier for cases when real values are complex file names or contain things we
3537
// don't care about, and we just want to check if a substring exists.
@@ -39,40 +41,66 @@ type ExpectedNginxField struct {
3941
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
4042
// and returns whether or not that field exists where it should.
4143
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
44+
b, err := json.Marshal(conf)
45+
if err != nil {
46+
return fmt.Errorf("error marshaling nginx config: %w", err)
47+
}
48+
4249
for _, config := range conf.Config {
4350
if !strings.Contains(config.File, expFieldCfg.File) {
4451
continue
4552
}
4653

4754
for _, directive := range config.Parsed {
48-
if len(expFieldCfg.Servers) == 0 {
55+
if expFieldCfg.Server == "" && expFieldCfg.Upstream == "" {
4956
if expFieldCfg.fieldFound(directive) {
5057
return nil
5158
}
5259
continue
5360
}
5461

55-
for _, serverName := range expFieldCfg.Servers {
56-
if directive.Directive == "server" && getServerName(directive.Block) == serverName {
57-
for _, serverDirective := range directive.Block {
58-
if expFieldCfg.Location == "" && expFieldCfg.fieldFound(serverDirective) {
59-
return nil
60-
} else if serverDirective.Directive == "location" &&
61-
fieldExistsInLocation(serverDirective, expFieldCfg) {
62-
return nil
63-
}
64-
}
65-
}
62+
if expFieldCfg.Server != "" && fieldExistsInServer(expFieldCfg, *directive) {
63+
return nil
64+
}
65+
66+
if expFieldCfg.Upstream != "" && fieldExistsInUpstream(expFieldCfg, *directive) {
67+
return nil
6668
}
6769
}
6870
}
6971

70-
b, err := json.Marshal(conf)
71-
if err != nil {
72-
return fmt.Errorf("error marshaling nginx config: %w", err)
72+
return fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
73+
}
74+
75+
func fieldExistsInServer(
76+
expFieldCfg ExpectedNginxField,
77+
directive Directive,
78+
) bool {
79+
if directive.Directive == "server" && getServerName(directive.Block) == expFieldCfg.Server {
80+
for _, serverDirective := range directive.Block {
81+
if expFieldCfg.Location == "" && expFieldCfg.fieldFound(serverDirective) {
82+
return true
83+
} else if serverDirective.Directive == "location" &&
84+
fieldExistsInLocation(serverDirective, expFieldCfg) {
85+
return true
86+
}
87+
}
7388
}
89+
return false
90+
}
7491

75-
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, string(b))
92+
func fieldExistsInUpstream(
93+
expFieldCfg ExpectedNginxField,
94+
directive Directive,
95+
) bool {
96+
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
97+
for _, directive := range directive.Block {
98+
if expFieldCfg.fieldFound(directive) {
99+
return true
100+
}
101+
}
102+
}
103+
return false
76104
}
77105

78106
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
{
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: coffee
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: coffee
10+
template:
11+
metadata:
12+
labels:
13+
app: coffee
14+
spec:
15+
containers:
16+
- name: coffee
17+
image: nginxdemos/nginx-hello:plain-text
18+
ports:
19+
- containerPort: 8080
20+
---
21+
apiVersion: v1
22+
kind: Service
23+
metadata:
24+
name: coffee
25+
spec:
26+
ports:
27+
- port: 80
28+
targetPort: 8080
29+
protocol: TCP
30+
name: http
31+
selector:
32+
app: coffee
33+
---
34+
apiVersion: apps/v1
35+
kind: Deployment
36+
metadata:
37+
name: tea
38+
spec:
39+
replicas: 1
40+
selector:
41+
matchLabels:
42+
app: tea
43+
template:
44+
metadata:
45+
labels:
46+
app: tea
47+
spec:
48+
containers:
49+
- name: tea
50+
image: nginxdemos/nginx-hello:plain-text
51+
ports:
52+
- containerPort: 8080
53+
---
54+
apiVersion: v1
55+
kind: Service
56+
metadata:
57+
name: tea
58+
spec:
59+
ports:
60+
- port: 80
61+
targetPort: 8080
62+
protocol: TCP
63+
name: http
64+
selector:
65+
app: tea
66+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: Gateway
3+
metadata:
4+
name: gateway
5+
spec:
6+
gatewayClassName: nginx
7+
listeners:
8+
- name: http
9+
port: 80
10+
protocol: HTTP
11+
hostname: "*.example.com"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: grpc-backend
5+
spec:
6+
selector:
7+
app: grpc-backend
8+
ports:
9+
- protocol: TCP
10+
port: 8080
11+
targetPort: 50051
12+
---
13+
apiVersion: apps/v1
14+
kind: Deployment
15+
metadata:
16+
name: grpc-backend
17+
labels:
18+
app: grpc-backend
19+
spec:
20+
replicas: 1
21+
selector:
22+
matchLabels:
23+
app: grpc-backend
24+
template:
25+
metadata:
26+
labels:
27+
app: grpc-backend
28+
spec:
29+
containers:
30+
- name: grpc-backend
31+
image: ghcr.io/nginxinc/kic-test-grpc-server:0.2.3
32+
env:
33+
- name: POD_NAME
34+
valueFrom:
35+
fieldRef:
36+
fieldPath: metadata.name
37+
resources:
38+
requests:
39+
cpu: 10m
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: gateway.nginx.org/v1alpha1
2+
kind: UpstreamSettingsPolicy
3+
metadata:
4+
name: usps-target-not-found
5+
spec:
6+
zoneSize: 512k
7+
targetRefs:
8+
- group: core
9+
kind: Service
10+
name: targeted-svc-dne
11+
keepAlive:
12+
connections: 10
13+
requests: 3
14+
time: 10s
15+
timeout: 50s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: Gateway
3+
metadata:
4+
name: gateway-not-valid
5+
spec:
6+
gatewayClassName: nginx
7+
addresses:
8+
- value: "10.0.0.1"
9+
listeners:
10+
- name: http
11+
port: 80
12+
protocol: HTTP
13+
hostname: "soda.example.com"
14+
---
15+
apiVersion: apps/v1
16+
kind: Deployment
17+
metadata:
18+
name: soda
19+
spec:
20+
replicas: 1
21+
selector:
22+
matchLabels:
23+
app: soda
24+
template:
25+
metadata:
26+
labels:
27+
app: soda
28+
spec:
29+
containers:
30+
- name: soda
31+
image: nginxdemos/nginx-hello:plain-text
32+
ports:
33+
- containerPort: 8080
34+
---
35+
apiVersion: v1
36+
kind: Service
37+
metadata:
38+
name: soda
39+
spec:
40+
ports:
41+
- port: 80
42+
targetPort: 8080
43+
protocol: TCP
44+
name: http
45+
selector:
46+
app: soda
47+
---
48+
apiVersion: gateway.networking.k8s.io/v1
49+
kind: HTTPRoute
50+
metadata:
51+
name: soda
52+
spec:
53+
parentRefs:
54+
- name: gateway-not-valid
55+
sectionName: http
56+
hostnames:
57+
- "soda.example.com"
58+
rules:
59+
- matches:
60+
- path:
61+
type: Exact
62+
value: /soda
63+
backendRefs:
64+
- name: soda
65+
port: 80
66+
---
67+
apiVersion: gateway.nginx.org/v1alpha1
68+
kind: UpstreamSettingsPolicy
69+
metadata:
70+
name: soda-svc-usp
71+
spec:
72+
zoneSize: 512k
73+
targetRefs:
74+
- group: core
75+
kind: Service
76+
name: soda
77+
keepAlive:
78+
connections: 10
79+
requests: 3
80+
time: 10s
81+
timeout: 50s

0 commit comments

Comments
 (0)