Skip to content

Commit

Permalink
Address code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed Sep 9, 2024
1 parent a9c9bcf commit 2785b47
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 74 deletions.
1 change: 1 addition & 0 deletions internal/mode/static/nginx/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
)

// ConfigFolders is a list of folders where NGINX configuration files are stored.
// Volumes here also need to be added to our crossplane ephemeral test container.
var ConfigFolders = []string{httpFolder, secretsFolder, includesFolder, modulesIncludesFolder, streamFolder}

// Generator generates NGINX configuration files.
Expand Down
33 changes: 20 additions & 13 deletions tests/framework/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package framework

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
Expand All @@ -18,9 +19,10 @@ import (
// ExpectedNginxField contains an nginx directive key and value,
// and the expected file, server, and location block that it should exist in.
type ExpectedNginxField struct {
// Key is the directive name.
Key string
// Value is the value for the directive. Can be the full value or a substring.
// Directive is the directive name.
Directive string
// Value is the value for the directive. Can be the full value or a substring. If it's a substring,
// then ValueSubstringAllowed should be true.
Value string
// File is the file name that should contain the directive. Can be a full filename or a substring.
File string
Expand All @@ -36,7 +38,7 @@ 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) bool {
func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) error {
for _, config := range conf.Config {
if !strings.Contains(config.File, expFieldCfg.File) {
continue
Expand All @@ -45,7 +47,7 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) boo
for _, directive := range config.Parsed {
if len(expFieldCfg.Servers) == 0 {
if expFieldCfg.fieldFound(directive) {
return true
return nil
}
continue
}
Expand All @@ -54,18 +56,23 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) boo
if directive.Directive == "server" && getServerName(directive.Block) == serverName {
for _, serverDirective := range directive.Block {
if expFieldCfg.Location == "" && expFieldCfg.fieldFound(serverDirective) {
return true
return nil
} else if serverDirective.Directive == "location" &&
fieldExistsInLocation(serverDirective, expFieldCfg) {
return true
return nil
}
}
}
}
}
}

return false
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))
}

func getServerName(serverBlock Directives) string {
Expand All @@ -86,15 +93,15 @@ func (e ExpectedNginxField) fieldFound(directive *Directive) bool {
valueMatch = strings.Contains(arg, e.Value)
}

return directive.Directive == e.Key && valueMatch
return directive.Directive == e.Directive && valueMatch
}

func fieldExistsInLocation(serverDirective *Directive, expFieldCfg ExpectedNginxField) bool {
func fieldExistsInLocation(locationDirective *Directive, expFieldCfg ExpectedNginxField) bool {
// location could start with '=', so get the last element which is the path
loc := serverDirective.Args[len(serverDirective.Args)-1]
loc := locationDirective.Args[len(locationDirective.Args)-1]
if loc == expFieldCfg.Location {
for _, locDirective := range serverDirective.Block {
if expFieldCfg.fieldFound(locDirective) {
for _, directive := range locationDirective.Block {
if expFieldCfg.fieldFound(directive) {
return true
}
}
Expand Down
114 changes: 53 additions & 61 deletions tests/suite/client_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"

Context("nginx config", func() {
var conf *framework.Payload
filePrefix := fmt.Sprintf("/etc/nginx/includes/ClientSettingsPolicy_%s", namespace)

BeforeAll(func() {
podNames, err := framework.GetReadyNGFPodNames(k8sClient, ngfNamespace, releaseName, timeoutConfig.GetTimeout)
Expand All @@ -108,105 +109,96 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
DescribeTable("is set properly for",
func(expCfgs []framework.ExpectedNginxField) {
for _, expCfg := range expCfgs {
failureMsg := fmt.Sprintf(
"directive '%s' with value '%s' not found in expected place",
expCfg.Key, expCfg.Value,
)
Expect(framework.ValidateNginxFieldExists(conf, expCfg)).To(BeTrue(), failureMsg)
Expect(framework.ValidateNginxFieldExists(conf, expCfg)).To(Succeed())
}
},
Entry("gateway policy", []framework.ExpectedNginxField{
{
Key: "include",
Value: "gw-csp.conf",
ValueSubstringAllowed: true,
File: "http.conf",
Servers: []string{"*.example.com", "cafe.example.com"},
Directive: "include",
Value: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"*.example.com", "cafe.example.com"},
},
{
Key: "client_max_body_size",
Value: "1000",
File: "gw-csp.conf",
Directive: "client_max_body_size",
Value: "1000",
File: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
},
{
Key: "client_body_timeout",
Value: "30s",
File: "gw-csp.conf",
Directive: "client_body_timeout",
Value: "30s",
File: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
},
{
Key: "keepalive_requests",
Value: "100",
File: "gw-csp.conf",
Directive: "keepalive_requests",
Value: "100",
File: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
},
{
Key: "keepalive_time",
Value: "5s",
File: "gw-csp.conf",
Directive: "keepalive_time",
Value: "5s",
File: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
},
{
Key: "keepalive_timeout",
Value: "2s 1s",
File: "gw-csp.conf",
Directive: "keepalive_timeout",
Value: "2s 1s",
File: fmt.Sprintf("%s_gw-csp.conf", filePrefix),
},
}),
Entry("coffee route policy", []framework.ExpectedNginxField{
{
Key: "include",
Value: "coffee-route-csp.conf",
ValueSubstringAllowed: true,
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/coffee",
Directive: "include",
Value: fmt.Sprintf("%s_coffee-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/coffee",
},
{
Key: "client_max_body_size",
Value: "2000",
File: "coffee-route-csp.conf",
Directive: "client_max_body_size",
Value: "2000",
File: fmt.Sprintf("%s_coffee-route-csp.conf", filePrefix),
},
}),
Entry("tea route policy", []framework.ExpectedNginxField{
{
Key: "include",
Value: "tea-route-csp.conf",
ValueSubstringAllowed: true,
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/tea",
Directive: "include",
Value: fmt.Sprintf("%s_tea-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/tea",
},
{
Key: "keepalive_requests",
Value: "200",
File: "tea-route-csp.conf",
Directive: "keepalive_requests",
Value: "200",
File: fmt.Sprintf("%s_tea-route-csp.conf", filePrefix),
},
}),
Entry("soda route policy", []framework.ExpectedNginxField{
{
Key: "include",
Value: "soda-route-csp.conf",
ValueSubstringAllowed: true,
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/soda",
Directive: "include",
Value: fmt.Sprintf("%s_soda-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"cafe.example.com"},
Location: "/soda",
},
{
Key: "client_max_body_size",
Value: "3000",
File: "soda-route-csp.conf",
Directive: "client_max_body_size",
Value: "3000",
File: fmt.Sprintf("%s_soda-route-csp.conf", filePrefix),
},
}),
Entry("grpc route policy", []framework.ExpectedNginxField{
{
Key: "include",
Value: "grpc-route-csp.conf",
ValueSubstringAllowed: true,
File: "http.conf",
Servers: []string{"*.example.com"},
Location: "/helloworld.Greeter/SayHello",
Directive: "include",
Value: fmt.Sprintf("%s_grpc-route-csp.conf", filePrefix),
File: "http.conf",
Servers: []string{"*.example.com"},
Location: "/helloworld.Greeter/SayHello",
},
{
Key: "client_max_body_size",
Value: "0",
File: "grpc-route-csp.conf",
Directive: "client_max_body_size",
Value: "0",
File: fmt.Sprintf("%s_grpc-route-csp.conf", filePrefix),
},
}),
)
Expand Down

0 comments on commit 2785b47

Please sign in to comment.