Skip to content

Commit 829fbd4

Browse files
committed
add functional tests for upstreamSettings policy
1 parent fe8b4dc commit 829fbd4

File tree

11 files changed

+765
-0
lines changed

11 files changed

+765
-0
lines changed

Diff for: apis/v1alpha1/policy_methods.go

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ func (p *ObservabilityPolicy) GetPolicyStatus() v1alpha2.PolicyStatus {
3131
func (p *ObservabilityPolicy) SetPolicyStatus(status v1alpha2.PolicyStatus) {
3232
p.Status = status
3333
}
34+
35+
func (p *UpstreamSettingsPolicy) GetTargetRefs() []v1alpha2.LocalPolicyTargetReference {
36+
return p.Spec.TargetRefs
37+
}

Diff for: tests/framework/crossplane.go

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ 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.
34+
Upstreams []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.
@@ -64,6 +66,8 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
6466
}
6567
}
6668
}
69+
70+
return validateUpstreamDirectives(expFieldCfg, directive)
6771
}
6872
}
6973

@@ -75,6 +79,19 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
7579
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, string(b))
7680
}
7781

82+
func validateUpstreamDirectives(expFieldCfg ExpectedNginxField, directive *Directive) error {
83+
for _, upstreamName := range expFieldCfg.Upstreams {
84+
if directive.Directive == "upstream" && directive.Args[0] == upstreamName {
85+
for _, upstreamDirective := range directive.Block {
86+
if expFieldCfg.fieldFound(upstreamDirective) {
87+
return nil
88+
}
89+
}
90+
}
91+
}
92+
return nil
93+
}
94+
7895
func getServerName(serverBlock Directives) string {
7996
for _, directive := range serverBlock {
8097
if directive.Directive == "server_name" {
+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,84 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: Gateway
3+
metadata:
4+
name: gateway-httproute-allowed
5+
spec:
6+
gatewayClassName: nginx
7+
listeners:
8+
- name: http
9+
port: 80
10+
protocol: HTTP
11+
hostname: "soda.example.com"
12+
allowedRoutes:
13+
namespaces:
14+
from: All
15+
kinds:
16+
kind: GRPCRoute
17+
---
18+
apiVersion: apps/v1
19+
kind: Deployment
20+
metadata:
21+
name: soda
22+
spec:
23+
replicas: 1
24+
selector:
25+
matchLabels:
26+
app: soda
27+
template:
28+
metadata:
29+
labels:
30+
app: soda
31+
spec:
32+
containers:
33+
- name: soda
34+
image: nginxdemos/nginx-hello:plain-text
35+
ports:
36+
- containerPort: 8080
37+
---
38+
apiVersion: v1
39+
kind: Service
40+
metadata:
41+
name: soda
42+
spec:
43+
ports:
44+
- port: 80
45+
targetPort: 8080
46+
protocol: TCP
47+
name: http
48+
selector:
49+
app: soda
50+
---
51+
apiVersion: gateway.networking.k8s.io/v1
52+
kind: HTTPRoute
53+
metadata:
54+
name: soda
55+
spec:
56+
parentRefs:
57+
- name: gateway-httproute-allowed
58+
sectionName: http
59+
hostnames:
60+
- "soda.example.com"
61+
rules:
62+
- matches:
63+
- path:
64+
type: Exact
65+
value: /soda
66+
backendRefs:
67+
- name: soda
68+
port: 80
69+
---
70+
apiVersion: gateway.nginx.org/v1alpha1
71+
kind: UpstreamSettingsPolicy
72+
metadata:
73+
name: soda-svc-usp
74+
spec:
75+
zoneSize: 512k
76+
targetRefs:
77+
- group: core
78+
kind: Service
79+
name: soda
80+
keepAlive:
81+
connections: 10
82+
requests: 3
83+
time: 10s
84+
timeout: 50s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: HTTPRoute
3+
metadata:
4+
name: coffee
5+
spec:
6+
parentRefs:
7+
- name: gateway
8+
sectionName: http
9+
hostnames:
10+
- "cafe.example.com"
11+
rules:
12+
- matches:
13+
- path:
14+
type: PathPrefix
15+
value: /coffee
16+
backendRefs:
17+
- name: coffee
18+
port: 80
19+
---
20+
apiVersion: gateway.networking.k8s.io/v1
21+
kind: GRPCRoute
22+
metadata:
23+
name: grpc-route
24+
spec:
25+
parentRefs:
26+
- name: gateway
27+
sectionName: http
28+
rules:
29+
- matches:
30+
- method:
31+
service: helloworld.Greeter
32+
method: SayHello
33+
backendRefs:
34+
- name: grpc-backend
35+
port: 8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: gateway.nginx.org/v1alpha1
2+
kind: UpstreamSettingsPolicy
3+
metadata:
4+
name: coffee-svc-usp-1
5+
spec:
6+
zoneSize: 64k
7+
targetRefs:
8+
- group: core
9+
kind: Service
10+
name: coffee
11+
---
12+
apiVersion: gateway.nginx.org/v1alpha1
13+
kind: UpstreamSettingsPolicy
14+
metadata:
15+
name: coffee-svc-usp-2
16+
spec:
17+
targetRefs:
18+
- group: core
19+
kind: Service
20+
name: coffee
21+
keepAlive:
22+
connections: 100
23+
requests: 55
24+
time: 1m
25+
timeout: 5h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: gateway.nginx.org/v1alpha1
2+
kind: UpstreamSettingsPolicy
3+
metadata:
4+
name: coffee-svc-usp-1
5+
spec:
6+
zoneSize: 64k
7+
targetRefs:
8+
- group: core
9+
kind: Service
10+
name: coffee
11+
---
12+
apiVersion: gateway.nginx.org/v1alpha1
13+
kind: UpstreamSettingsPolicy
14+
metadata:
15+
name: coffee-svc-usp-2
16+
spec:
17+
zoneSize: 128k
18+
targetRefs:
19+
- group: core
20+
kind: Service
21+
name: coffee
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: gateway.nginx.org/v1alpha1
2+
kind: UpstreamSettingsPolicy
3+
metadata:
4+
name: coffee-svc-usp
5+
spec:
6+
zoneSize: 512k
7+
targetRefs:
8+
- group: core
9+
kind: Service
10+
name: coffee
11+
keepAlive:
12+
connections: 10
13+
requests: 3
14+
time: 10s
15+
timeout: 50s
16+
---
17+
apiVersion: gateway.nginx.org/v1alpha1
18+
kind: UpstreamSettingsPolicy
19+
metadata:
20+
name: tea-multiple-svc-usp
21+
spec:
22+
zoneSize: 128k
23+
targetRefs:
24+
- group: core
25+
kind: Service
26+
name: tea
27+
- group: core
28+
kind: Service
29+
name: coffee
30+
keepAlive:
31+
connections: 12
32+
requests: 31
33+
time: 20s
34+
timeout: 40s
35+
---
36+
apiVersion: gateway.nginx.org/v1alpha1
37+
kind: UpstreamSettingsPolicy
38+
metadata:
39+
name: grpc-svc-usp
40+
spec:
41+
targetRef:
42+
group: core
43+
kind: Service
44+
name: grpc-backend
45+
keepAlive:
46+
connections: 10
47+
requests: 3
48+
time: 10s
49+
timeout: 50s

0 commit comments

Comments
 (0)