Skip to content

Commit 96944ec

Browse files
committed
add functional tests for upstream settings policy
1 parent caf722e commit 96944ec

18 files changed

+1049
-73
lines changed

tests/framework/logging.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ func WithLoggingDisabled() Option {
1313
}
1414

1515
func LogOptions(opts ...Option) *Options {
16-
options := &Options{logEnabled: true}
16+
options := &Options{
17+
logEnabled: true,
18+
}
1719
for _, opt := range opts {
1820
opt(options)
1921
}

tests/framework/ngf.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ type InstallationConfig struct {
3939

4040
// InstallGatewayAPI installs the specified version of the Gateway API resources.
4141
func InstallGatewayAPI(apiVersion string) ([]byte, error) {
42-
apiPath := fmt.Sprintf("%s/v%s/standard-install.yaml", gwInstallBasePath, apiVersion)
43-
GinkgoWriter.Printf("Installing Gateway API version %q at API path %q\n", apiVersion, apiPath)
42+
apiPath := fmt.Sprintf("%s/v%s/experimental-install.yaml", gwInstallBasePath, apiVersion)
43+
GinkgoWriter.Printf("Installing Gateway API CRDs from experimental channel %q", apiVersion, apiPath)
4444

4545
cmd := exec.CommandContext(
4646
context.Background(),
47-
"kubectl", "apply", "-f", apiPath,
47+
"kubectl", "apply", "--server-side", "-f", apiPath,
4848
)
4949
output, err := cmd.CombinedOutput()
5050
if err != nil {
@@ -60,7 +60,7 @@ func InstallGatewayAPI(apiVersion string) ([]byte, error) {
6060
// UninstallGatewayAPI uninstalls the specified version of the Gateway API resources.
6161
func UninstallGatewayAPI(apiVersion string) ([]byte, error) {
6262
apiPath := fmt.Sprintf("%s/v%s/standard-install.yaml", gwInstallBasePath, apiVersion)
63-
GinkgoWriter.Printf("Uninstalling Gateway API version %q at API path %q\n", apiVersion, apiPath)
63+
GinkgoWriter.Printf("Installing Gateway API CRDs from experimental channel for version %q\n", apiVersion)
6464

6565
output, err := exec.CommandContext(context.Background(), "kubectl", "delete", "-f", apiPath).CombinedOutput()
6666
if err != nil && !strings.Contains(string(output), "not found") {
@@ -84,6 +84,7 @@ func InstallNGF(cfg InstallationConfig, extraArgs ...string) ([]byte, error) {
8484
"--namespace", cfg.Namespace,
8585
"--wait",
8686
"--set", "nginxGateway.snippetsFilters.enable=true",
87+
"--set", "nginxGateway.gwAPIExperimentalFeatures.enable=true",
8788
}
8889
if cfg.ChartVersion != "" {
8990
args = append(args, "--version", cfg.ChartVersion)

tests/framework/prometheus.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log/slog"
9+
"net/http"
910
"os"
1011
"os/exec"
1112
"time"
@@ -542,7 +543,12 @@ func CreateResponseChecker(url, address string, requestTimeout time.Duration, op
542543
}
543544

544545
return func() error {
545-
status, _, err := Get(url, address, requestTimeout, nil, nil, opts...)
546+
request := Request{
547+
URL: url,
548+
Address: address,
549+
Timeout: requestTimeout,
550+
}
551+
resp, err := Get(request, opts...)
546552
if err != nil {
547553
badReqErr := fmt.Errorf("bad response: %w", err)
548554
if options.logEnabled {
@@ -552,8 +558,8 @@ func CreateResponseChecker(url, address string, requestTimeout time.Duration, op
552558
return badReqErr
553559
}
554560

555-
if status != 200 {
556-
statusErr := fmt.Errorf("unexpected status code: %d", status)
561+
if resp.StatusCode != http.StatusOK {
562+
statusErr := fmt.Errorf("unexpected status code: %d", resp.StatusCode)
557563
if options.logEnabled {
558564
GinkgoWriter.Printf("ERROR during creating response checker: %v\n", statusErr)
559565
}

tests/framework/request.go

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ import (
1515
. "github.com/onsi/ginkgo/v2"
1616
)
1717

18+
type Response struct {
19+
Headers http.Header
20+
Body string
21+
StatusCode int
22+
}
23+
24+
type Request struct {
25+
Body io.Reader
26+
Headers map[string]string
27+
QueryParams map[string]string
28+
URL string
29+
Address string
30+
Timeout time.Duration
31+
}
32+
1833
// Get sends a GET request to the specified url.
1934
// It resolves to the specified address instead of using DNS.
20-
// The status and body of the response is returned, or an error.
21-
func Get(
22-
url, address string,
23-
timeout time.Duration,
24-
headers, queryParams map[string]string,
25-
opts ...Option,
26-
) (int, string, error) {
35+
// It returns the response body, headers, and status code.
36+
func Get(request Request, opts ...Option) (Response, error) {
2737
options := LogOptions(opts...)
2838

29-
resp, err := makeRequest(http.MethodGet, url, address, nil, timeout, headers, queryParams, opts...)
39+
resp, err := makeRequest(http.MethodGet, request, opts...)
3040
if err != nil {
3141
if options.logEnabled {
3242
GinkgoWriter.Printf(
@@ -35,46 +45,39 @@ func Get(
3545
)
3646
}
3747

38-
return 0, "", err
48+
return Response{StatusCode: 0}, err
3949
}
4050
defer resp.Body.Close()
4151

4252
body := new(bytes.Buffer)
4353
_, err = body.ReadFrom(resp.Body)
4454
if err != nil {
4555
GinkgoWriter.Printf("ERROR in Body content: %v returning body: ''\n", err)
46-
return resp.StatusCode, "", err
56+
return Response{StatusCode: resp.StatusCode}, err
4757
}
4858
if options.logEnabled {
4959
GinkgoWriter.Printf("Successfully received response and parsed body: %s\n", body.String())
5060
}
5161

52-
return resp.StatusCode, body.String(), nil
62+
return Response{
63+
Body: body.String(),
64+
Headers: resp.Header,
65+
StatusCode: resp.StatusCode,
66+
}, nil
5367
}
5468

5569
// Post sends a POST request to the specified url with the body as the payload.
5670
// It resolves to the specified address instead of using DNS.
57-
func Post(
58-
url, address string,
59-
body io.Reader,
60-
timeout time.Duration,
61-
headers, queryParams map[string]string,
62-
) (*http.Response, error) {
63-
response, err := makeRequest(http.MethodPost, url, address, body, timeout, headers, queryParams)
71+
func Post(request Request) (*http.Response, error) {
72+
response, err := makeRequest(http.MethodPost, request)
6473
if err != nil {
6574
GinkgoWriter.Printf("ERROR occurred during getting response, error: %s\n", err)
6675
}
6776

6877
return response, err
6978
}
7079

71-
func makeRequest(
72-
method, url, address string,
73-
body io.Reader,
74-
timeout time.Duration,
75-
headers, queryParams map[string]string,
76-
opts ...Option,
77-
) (*http.Response, error) {
80+
func makeRequest(method string, request Request, opts ...Option) (*http.Response, error) {
7881
dialer := &net.Dialer{}
7982

8083
transport, ok := http.DefaultTransport.(*http.Transport)
@@ -90,50 +93,52 @@ func makeRequest(
9093
) (net.Conn, error) {
9194
split := strings.Split(addr, ":")
9295
port := split[len(split)-1]
93-
return dialer.DialContext(ctx, network, fmt.Sprintf("%s:%s", address, port))
96+
return dialer.DialContext(ctx, network, fmt.Sprintf("%s:%s", request.Address, port))
9497
}
9598

96-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
99+
ctx, cancel := context.WithTimeout(context.Background(), request.Timeout)
97100
defer cancel()
98101

99102
options := LogOptions(opts...)
100103
if options.logEnabled {
101104
requestDetails := fmt.Sprintf(
102105
"Method: %s, URL: %s, Address: %s, Headers: %v, QueryParams: %v\n",
103106
strings.ToUpper(method),
104-
url,
105-
address,
106-
headers,
107-
queryParams,
107+
request.URL,
108+
request.Address,
109+
request.Headers,
110+
request.QueryParams,
108111
)
109112
GinkgoWriter.Printf("Sending request: %s", requestDetails)
110113
}
111114

112-
req, err := http.NewRequestWithContext(ctx, method, url, body)
115+
req, err := http.NewRequestWithContext(ctx, method, request.URL, request.Body)
113116
if err != nil {
114117
return nil, err
115118
}
116119

117-
for key, value := range headers {
120+
for key, value := range request.Headers {
118121
req.Header.Add(key, value)
119122
}
120123

121-
if queryParams != nil {
124+
if request.QueryParams != nil {
122125
q := req.URL.Query()
123-
for key, value := range queryParams {
126+
for key, value := range request.QueryParams {
124127
q.Add(key, value)
125128
}
126129
req.URL.RawQuery = q.Encode()
127130
}
128131

129132
var resp *http.Response
130-
if strings.HasPrefix(url, "https") {
133+
if strings.HasPrefix(request.URL, "https") {
131134
// similar to how in our examples with https requests we run our curl command
132135
// we turn off verification of the certificate, we do the same here
133136
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // for https test traffic
134137
}
135138

136-
client := &http.Client{Transport: customTransport}
139+
client := &http.Client{
140+
Transport: customTransport,
141+
}
137142
resp, err = client.Do(req)
138143
if err != nil {
139144
return nil, err

tests/suite/advanced_routing_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,28 @@ func expectRequestToRespondFromExpectedServer(
120120
headers, queryParams map[string]string,
121121
) error {
122122
GinkgoWriter.Printf("Expecting request to respond from the server %q\n", expServerName)
123-
status, body, err := framework.Get(appURL, address, timeoutConfig.RequestTimeout, headers, queryParams)
123+
124+
request := framework.Request{
125+
URL: appURL,
126+
Address: address,
127+
Timeout: timeoutConfig.RequestTimeout,
128+
Headers: headers,
129+
QueryParams: queryParams,
130+
}
131+
132+
resp, err := framework.Get(request)
124133
if err != nil {
125134
return err
126135
}
127136

128-
if status != http.StatusOK {
137+
if resp.StatusCode != http.StatusOK {
129138
statusErr := errors.New("http status was not 200")
130139
GinkgoWriter.Printf("ERROR: %v\n", statusErr)
131140

132141
return statusErr
133142
}
134143

135-
actualServerName, err := extractServerName(body)
144+
actualServerName, err := extractServerName(resp.Body)
136145
if err != nil {
137146
GinkgoWriter.Printf("ERROR extracting server name from response body: %v\n", err)
138147

tests/suite/client_settings_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,13 @@ var _ = Describe("ClientSettingsPolicy", Ordered, Label("functional", "cspolicy"
254254
_, err := rand.Read(payload)
255255
Expect(err).ToNot(HaveOccurred())
256256

257-
resp, err := framework.Post(url, address, bytes.NewReader(payload), timeoutConfig.RequestTimeout, nil, nil)
257+
request := framework.Request{
258+
URL: url,
259+
Address: address,
260+
Body: bytes.NewReader(payload),
261+
Timeout: timeoutConfig.RequestTimeout,
262+
}
263+
resp, err := framework.Post(request)
258264
Expect(err).ToNot(HaveOccurred())
259265
Expect(resp).To(HaveHTTPStatus(expStatus))
260266

tests/suite/graceful_recovery_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,19 @@ var _ = Describe("Graceful Recovery test", Ordered, FlakeAttempts(2), Label("gra
555555
})
556556

557557
func expectRequestToSucceed(appURL, address string, responseBodyMessage string) error {
558-
status, body, err := framework.Get(appURL, address, timeoutConfig.RequestTimeout, nil, nil)
558+
request := framework.Request{
559+
URL: appURL,
560+
Address: address,
561+
Timeout: timeoutConfig.RequestTimeout,
562+
}
563+
resp, err := framework.Get(request)
559564

560-
if status != http.StatusOK {
561-
return fmt.Errorf("http status was not 200, got %d: %w", status, err)
565+
if resp.StatusCode != http.StatusOK {
566+
return fmt.Errorf("http status was not 200, got %d: %w", resp.StatusCode, err)
562567
}
563568

564-
if !strings.Contains(body, responseBodyMessage) {
565-
return fmt.Errorf("expected response body to contain correct body message, got: %s", body)
569+
if !strings.Contains(resp.Body, responseBodyMessage) {
570+
return fmt.Errorf("expected response body to contain correct body message, got: %s", resp.Body)
566571
}
567572

568573
return err
@@ -577,13 +582,18 @@ func expectRequestToSucceed(appURL, address string, responseBodyMessage string)
577582
// We only want an error returned from this particular function if it does not appear that NGINX has
578583
// stopped serving traffic.
579584
func expectRequestToFail(appURL, address string) error {
580-
status, body, err := framework.Get(appURL, address, timeoutConfig.RequestTimeout, nil, nil)
581-
if status != 0 {
585+
request := framework.Request{
586+
URL: appURL,
587+
Address: address,
588+
Timeout: timeoutConfig.RequestTimeout,
589+
}
590+
resp, err := framework.Get(request)
591+
if resp.StatusCode != 0 {
582592
return errors.New("expected http status to be 0")
583593
}
584594

585-
if body != "" {
586-
return fmt.Errorf("expected response body to be empty, instead received: %s", body)
595+
if resp.Body != "" {
596+
return fmt.Errorf("expected response body to be empty, instead received: %s", resp.Body)
587597
}
588598

589599
if err == nil {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: coffee
5+
spec:
6+
replicas: 3
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: 3
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

0 commit comments

Comments
 (0)