Skip to content

Commit

Permalink
update tests as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
salonichf5 committed Dec 19, 2024
1 parent 1580e84 commit 723d1c6
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 311 deletions.
78 changes: 44 additions & 34 deletions tests/framework/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type ExpectedNginxField struct {
File string
// Location is the location name that the directive should exist in.
Location string
// Servers are the server names that the directive should exist in.
Servers []string
// Upstreams are the upstream names that the directive should exist in.
Upstreams []string
// Server is the server name that the directive should exist in.
Server string
// Upstream is the upstream name that the directive should exist in.
Upstream string
// ValueSubstringAllowed allows the expected value to be a substring of the real value.
// This makes it easier for cases when real values are complex file names or contain things we
// don't care about, and we just want to check if a substring exists.
Expand All @@ -41,64 +41,74 @@ type ExpectedNginxField struct {
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
// and returns whether or not that field exists where it should.
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
var directiveFoundInServer, directiveFoundInUpstream bool

b, err := json.Marshal(conf)
if err != nil {
return fmt.Errorf("error marshaling nginx config: %w", err)
}

for _, config := range conf.Config {
if !strings.Contains(config.File, expFieldCfg.File) {
continue
}

for _, directive := range config.Parsed {
if len(expFieldCfg.Servers) == 0 {
if len(expFieldCfg.Server) == 0 && len(expFieldCfg.Upstream) == 0 {
if expFieldCfg.fieldFound(directive) {
return nil
}
continue
}

if err := validateServerBlockDirectives(expFieldCfg, *directive); err != nil {
return err
directiveFoundInServer = validateServerBlockDirectives(expFieldCfg, *directive)

directiveFoundInUpstream = validateUpstreamDirectives(expFieldCfg, *directive)

if len(expFieldCfg.Server) > 0 && directiveFoundInServer {
return nil
}

if err := validateUpstreamDirectives(expFieldCfg, directive); err != nil {
return err
if len(expFieldCfg.Upstream) > 0 && directiveFoundInUpstream {
return nil
}
}
}

b, err := json.Marshal(conf)
if err != nil {
return fmt.Errorf("error marshaling nginx config: %w", err)
}

return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, string(b))
return fmt.Errorf("directive %s not found in: nginx config %s", expFieldCfg.Directive, string(b))
}

func validateServerBlockDirectives(expFieldCfg ExpectedNginxField, directive Directive) error {
for _, serverName := range expFieldCfg.Servers {
if directive.Directive == "server" && getServerName(directive.Block) == serverName {
for _, serverDirective := range directive.Block {
if expFieldCfg.Location == "" && !expFieldCfg.fieldFound(serverDirective) {
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, serverDirective.Directive)
} else if serverDirective.Directive == "location" &&
!fieldExistsInLocation(serverDirective, expFieldCfg) {
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, serverDirective.Directive)
}
func validateServerBlockDirectives(
expFieldCfg ExpectedNginxField,
directive Directive,
) bool {
var fieldFound bool
if directive.Directive == "server" && getServerName(directive.Block) == expFieldCfg.Server {
for _, serverDirective := range directive.Block {
if expFieldCfg.Location == "" && expFieldCfg.fieldFound(serverDirective) {
fieldFound = true
} else if serverDirective.Directive == "location" &&
fieldExistsInLocation(serverDirective, expFieldCfg) {
fieldFound = true
}
}
}
return nil
return fieldFound
}

func validateUpstreamDirectives(expFieldCfg ExpectedNginxField, directive *Directive) error {
for _, upstreamName := range expFieldCfg.Upstreams {
if directive.Directive == "upstream" && directive.Args[0] == upstreamName {
for _, upstreamDirective := range directive.Block {
if !expFieldCfg.fieldFound(upstreamDirective) {
return fmt.Errorf("field not found; expected: %+v\nNGINX conf: %s", expFieldCfg, upstreamDirective.Directive)
}
func validateUpstreamDirectives(
expFieldCfg ExpectedNginxField,
directive Directive,
) bool {
var fieldFound bool
if directive.Directive == "upstream" && directive.Args[0] == expFieldCfg.Upstream {
for _, directive := range directive.Block {
if expFieldCfg.fieldFound(directive) {
fieldFound = true
}
}
}
return nil
return fieldFound
}

func getServerName(serverBlock Directives) string {
Expand Down
16 changes: 11 additions & 5 deletions tests/suite/client_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
Directive: "include",
Value: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"*.example.com", "cafe.example.com"},
Server: "*.example.com",
},
{
Directive: "include",
Value: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
File: "http.conf",
Server: "cafe.example.com",
},
{
Directive: "client_max_body_size",
Expand Down Expand Up @@ -150,7 +156,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
Directive: "include",
Value: fmt.Sprintf("%s_coffee-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Server: "cafe.example.com",
Location: "/coffee",
},
{
Expand All @@ -164,7 +170,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
Directive: "include",
Value: fmt.Sprintf("%s_tea-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Server: "cafe.example.com",
Location: "/tea",
},
{
Expand All @@ -178,7 +184,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
Directive: "include",
Value: fmt.Sprintf("%s_soda-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Server: "cafe.example.com",
Location: "/soda",
},
{
Expand All @@ -192,7 +198,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
Directive: "include",
Value: fmt.Sprintf("%s_grpc-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"*.example.com"},
Server: "*.example.com",
Location: "/helloworld.Greeter/SayHello",
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ metadata:
name: gateway-not-valid
spec:
gatewayClassName: nginx
addresses: "10.0.0.1"
addresses:
- value: "10.0.0.1"
listeners:
- name: http
port: 80
protocol: HTTP
hostname: "soda.example.com"
- name: http
port: 80
protocol: HTTP
hostname: "soda.example.com"
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -26,42 +27,42 @@ spec:
app: soda
spec:
containers:
- name: soda
image: nginxdemos/nginx-hello:plain-text
ports:
- containerPort: 8080
- name: soda
image: nginxdemos/nginx-hello:plain-text
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: soda
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: soda
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: soda
name: sode
spec:
parentRefs:
- name: gateway-httproute-allowed
sectionName: http
- name: gateway-not-valid
sectionName: http
hostnames:
- "soda.example.com"
- "soda.example.com"
rules:
- matches:
- path:
type: Exact
value: /soda
backendRefs:
- name: soda
port: 80
- matches:
- path:
type: Exact
value: /soda
backendRefs:
- name: soda
port: 80
---
apiVersion: gateway.nginx.org/v1alpha1
kind: UpstreamSettingsPolicy
Expand All @@ -70,9 +71,9 @@ metadata:
spec:
zoneSize: 512k
targetRefs:
- group: core
kind: Service
name: soda
- group: core
kind: Service
name: soda
keepAlive:
connections: 10
requests: 3
Expand Down
19 changes: 19 additions & 0 deletions tests/suite/manifests/upstream-settings-policy/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ spec:
port: 80
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: tea
spec:
parentRefs:
- name: gateway
sectionName: http
hostnames:
- "cafe.example.com"
rules:
- matches:
- path:
type: Exact
value: /tea
backendRefs:
- name: tea
port: 80
---
apiVersion: gateway.networking.k8s.io/v1
kind: GRPCRoute
metadata:
name: grpc-route
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
apiVersion: gateway.nginx.org/v1alpha1
kind: UpstreamSettingsPolicy
metadata:
name: coffee-svc-usp-1
name: merge-usp-1
spec:
zoneSize: 1g
targetRefs:
- group: core
kind: Service
name: coffee
keepAlive:
time: 1m
timeout: 5h
---
apiVersion: gateway.nginx.org/v1alpha1
kind: UpstreamSettingsPolicy
metadata:
name: coffee-svc-usp-2
name: merge-usp-2
spec:
targetRefs:
- group: core
Expand All @@ -21,5 +23,25 @@ spec:
keepAlive:
connections: 100
requests: 55
time: 1m
timeout: 5h
---
apiVersion: gateway.nginx.org/v1alpha1
kind: UpstreamSettingsPolicy
metadata:
name: z-usp-wins
spec:
zoneSize: 64k
targetRefs:
- group: core
kind: Service
name: tea
---
apiVersion: gateway.nginx.org/v1alpha1
kind: UpstreamSettingsPolicy
metadata:
name: a-usp
spec:
zoneSize: 128k
targetRefs:
- group: core
kind: Service
name: tea

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ kind: UpstreamSettingsPolicy
metadata:
name: grpc-svc-usp
spec:
targetRef:
group: core
kind: Service
name: grpc-backend
targetRefs:
- group: core
kind: Service
name: grpc-backend
zoneSize: 64k
keepAlive:
connections: 100
Expand Down
Loading

0 comments on commit 723d1c6

Please sign in to comment.