Skip to content

Commit c36961a

Browse files
committed
minor reuse
1 parent 900c3c0 commit c36961a

File tree

3 files changed

+47
-155
lines changed

3 files changed

+47
-155
lines changed

test/e2e/tests/backend_upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var BackendUpgradeTest = suite.ConformanceTest{
6969

7070
tlog.Logf(t, "Starting load generation")
7171
// Run load async and continue to restart deployment
72-
go runLoadAndWait(t, &suite.TimeoutConfig, loadSuccess, aborter, reqURL.String())
72+
go runLoadAndWait(t, &suite.TimeoutConfig, loadSuccess, aborter, reqURL.String(), 3*time.Second)
7373

7474
tlog.Logf(t, "Restarting deployment")
7575
err = restartDeploymentAndWaitForNewPods(t, &suite.TimeoutConfig, suite.Client, dp)

test/e2e/tests/envoy_shutdown.go

Lines changed: 43 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import (
1111
"context"
1212
"errors"
1313
"fmt"
14-
"io"
15-
nethttp "net/http"
1614
"net/url"
17-
"sync"
1815
"testing"
1916
"time"
2017

@@ -43,56 +40,7 @@ var EnvoyShutdownTest = suite.ConformanceTest{
4340
Manifests: []string{"testdata/envoy-shutdown.yaml"},
4441
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
4542
t.Run("All requests must succeed", func(t *testing.T) {
46-
ns := "gateway-upgrade-infra"
47-
name := "ha-gateway"
48-
routeNN := types.NamespacedName{Name: "http-envoy-shutdown", Namespace: ns}
49-
gwNN := types.NamespacedName{Name: name, Namespace: ns}
50-
gwAddr := kubernetes.GatewayAndRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), &gwapiv1.HTTPRoute{}, false, routeNN)
51-
reqURL := url.URL{Scheme: "http", Host: http.CalculateHost(t, gwAddr, "http"), Path: "/envoy-shutdown"}
52-
epNN := types.NamespacedName{Name: "upgrade-config", Namespace: "envoy-gateway-system"}
53-
dp, err := getDeploymentForGateway(ns, name, suite.Client)
54-
if err != nil {
55-
t.Errorf("Failed to get proxy deployment")
56-
}
57-
58-
WaitForPods(t, suite.Client, dp.Namespace, map[string]string{"gateway.envoyproxy.io/owning-gateway-name": name}, corev1.PodRunning, &PodReady)
59-
60-
// wait for route to be programmed on envoy
61-
expectedResponse := http.ExpectedResponse{
62-
Request: http.Request{
63-
Path: "/envoy-shutdown",
64-
},
65-
Response: http.Response{
66-
StatusCodes: []int{200},
67-
},
68-
Namespace: ns,
69-
}
70-
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse)
71-
72-
// can be used to abort the test after deployment restart is complete or failed
73-
aborter := periodic.NewAborter()
74-
// will contain indication on success or failure of load test
75-
loadSuccess := make(chan bool)
76-
77-
t.Log("Starting load generation")
78-
// Run load async and continue to restart deployment
79-
go runLoadAndWait(t, &suite.TimeoutConfig, loadSuccess, aborter, reqURL.String())
80-
81-
t.Log("Rolling out proxy deployment")
82-
err = restartProxyAndWaitForRollout(t, &suite.TimeoutConfig, suite.Client, epNN, dp)
83-
84-
t.Log("Stopping load generation and collecting results")
85-
aborter.Abort(false) // abort the load either way
86-
87-
if err != nil {
88-
t.Errorf("Failed to rollout proxy deployment: %v", err)
89-
}
90-
91-
// Wait for the goroutine to finish
92-
result := <-loadSuccess
93-
if !result {
94-
t.Errorf("Load test failed")
95-
}
43+
runEnvoyShutdownCase(t, suite, "", 3*time.Second)
9644
})
9745
},
9846
}
@@ -105,118 +53,61 @@ var EnvoyShutdownLongRequestTest = suite.ConformanceTest{
10553
Manifests: []string{"testdata/envoy-shutdown.yaml"},
10654
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
10755
t.Run("All delayed requests must succeed", func(t *testing.T) {
108-
ns := "gateway-upgrade-infra"
109-
name := "ha-gateway"
110-
routeNN := types.NamespacedName{Name: "http-envoy-shutdown", Namespace: ns}
111-
gwNN := types.NamespacedName{Name: name, Namespace: ns}
112-
gwAddr := kubernetes.GatewayAndRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), &gwapiv1.HTTPRoute{}, false, routeNN)
113-
reqURL := url.URL{Scheme: "http", Host: http.CalculateHost(t, gwAddr, "http"), Path: "/envoy-shutdown"}
114-
// add a delay query param
115-
q := reqURL.Query()
116-
q.Set("delay", "3s")
117-
reqURL.RawQuery = q.Encode()
118-
epNN := types.NamespacedName{Name: "upgrade-config", Namespace: "envoy-gateway-system"}
119-
dp, err := getDeploymentForGateway(ns, name, suite.Client)
120-
if err != nil {
121-
t.Errorf("Failed to get proxy deployment")
122-
}
123-
124-
WaitForPods(t, suite.Client, dp.Namespace, map[string]string{"gateway.envoyproxy.io/owning-gateway-name": name}, corev1.PodRunning, &PodReady)
125-
126-
// wait for route to be programmed on envoy
127-
expectedResponse := http.ExpectedResponse{
128-
Request: http.Request{
129-
Path: "/envoy-shutdown",
130-
},
131-
Response: http.Response{
132-
StatusCodes: []int{200},
133-
},
134-
Namespace: ns,
135-
}
136-
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse)
137-
138-
t.Logf("Starting delayed-request load generation to %s", reqURL.String())
139-
stopCh := make(chan struct{})
140-
loadDone := execRequests(t, reqURL.String(), stopCh)
141-
142-
t.Log("Rolling out proxy deployment")
143-
err = restartProxyAndWaitForRollout(t, &suite.TimeoutConfig, suite.Client, epNN, dp)
144-
145-
t.Log("Stopping delayed-request load generation and collecting results")
146-
close(stopCh)
147-
148-
if err != nil {
149-
t.Errorf("Failed to rollout proxy deployment: %v", err)
150-
}
151-
152-
select {
153-
case ok := <-loadDone:
154-
if !ok {
155-
t.Errorf("Load test failed")
156-
}
157-
case <-time.After(30 * time.Second):
158-
t.Errorf("Timed out waiting for load generator to stop")
159-
}
56+
runEnvoyShutdownCase(t, suite, "3s", 10*time.Second)
16057
})
16158
},
16259
}
16360

164-
func execRequests(t *testing.T, targetURL string, stopCh <-chan struct{}) <-chan bool {
165-
result := make(chan bool, 1)
61+
func runEnvoyShutdownCase(t *testing.T, suite *suite.ConformanceTestSuite, delay string, perRequestTimeout time.Duration) {
62+
ns := "gateway-upgrade-infra"
63+
name := "ha-gateway"
64+
routeNN := types.NamespacedName{Name: "http-envoy-shutdown", Namespace: ns}
65+
gwNN := types.NamespacedName{Name: name, Namespace: ns}
66+
gwAddr := kubernetes.GatewayAndRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), &gwapiv1.HTTPRoute{}, false, routeNN)
67+
reqURL := url.URL{Scheme: "http", Host: http.CalculateHost(t, gwAddr, "http"), Path: "/envoy-shutdown"}
68+
if delay != "" {
69+
q := reqURL.Query()
70+
q.Set("delay", delay)
71+
reqURL.RawQuery = q.Encode()
72+
}
16673

167-
const workers = 4
74+
epNN := types.NamespacedName{Name: "upgrade-config", Namespace: "envoy-gateway-system"}
75+
dp, err := getDeploymentForGateway(ns, name, suite.Client)
76+
if err != nil {
77+
t.Errorf("Failed to get proxy deployment")
78+
}
16879

169-
go func() {
170-
defer close(result)
80+
WaitForPods(t, suite.Client, dp.Namespace, map[string]string{"gateway.envoyproxy.io/owning-gateway-name": name}, corev1.PodRunning, &PodReady)
17181

172-
cli := &nethttp.Client{
173-
Timeout: 10 * time.Second,
174-
}
82+
expectedResponse := http.ExpectedResponse{
83+
Request: http.Request{Path: "/envoy-shutdown"},
84+
Response: http.Response{
85+
StatusCodes: []int{200},
86+
},
87+
Namespace: ns,
88+
}
17589

176-
var wg sync.WaitGroup
177-
errCh := make(chan error, workers)
90+
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse)
17891

179-
for i := 0; i < workers; i++ {
180-
wg.Add(1)
181-
go func() {
182-
defer wg.Done()
183-
for {
184-
select {
185-
case <-stopCh:
186-
return
187-
default:
188-
resp, err := cli.Get(targetURL)
189-
if err != nil {
190-
errCh <- err
191-
return
192-
}
193-
_, _ = io.Copy(io.Discard, resp.Body)
194-
resp.Body.Close()
195-
if resp.StatusCode != nethttp.StatusOK {
196-
errCh <- fmt.Errorf("unexpected status code: %d", resp.StatusCode)
197-
return
198-
}
199-
time.Sleep(200 * time.Millisecond)
200-
}
201-
}
202-
}()
203-
}
92+
aborter := periodic.NewAborter()
93+
loadDone := make(chan bool)
20494

205-
go func() {
206-
wg.Wait()
207-
close(errCh)
208-
}()
95+
t.Logf("Starting load generation to %s (timeout=%s)", reqURL.String(), perRequestTimeout)
96+
go runLoadAndWait(t, &suite.TimeoutConfig, loadDone, aborter, reqURL.String(), perRequestTimeout)
20997

210-
for e := range errCh {
211-
t.Logf("request error: %v", e)
212-
result <- false
213-
return
214-
}
98+
t.Log("Rolling out proxy deployment")
99+
err = restartProxyAndWaitForRollout(t, &suite.TimeoutConfig, suite.Client, epNN, dp)
215100

216-
result <- true
217-
}()
101+
t.Log("Stopping load generation and collecting results")
102+
aborter.Abort(false)
218103

219-
return result
104+
if err != nil {
105+
t.Errorf("Failed to rollout proxy deployment: %v", err)
106+
}
107+
108+
if ok := <-loadDone; !ok {
109+
t.Errorf("Load test failed")
110+
}
220111
}
221112

222113
// gets the proxy deployment created for a gateway, assuming merge-gateways is not used

test/e2e/tests/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func AlmostEquals(actual, expect, offset int) bool {
251251
// runs a load test with options described in opts
252252
// the done channel is used to notify caller of execution result
253253
// the execution may end due to an external abort or timeout
254-
func runLoadAndWait(t *testing.T, timeoutConfig *config.TimeoutConfig, done chan bool, aborter *periodic.Aborter, reqURL string) {
254+
func runLoadAndWait(t *testing.T, timeoutConfig *config.TimeoutConfig, done chan bool, aborter *periodic.Aborter, reqURL string, reqTimeout time.Duration) {
255255
qpsVal := os.Getenv("E2E_BACKEND_UPGRADE_QPS")
256256
qps := 5000
257257
if qpsVal != "" {
@@ -273,7 +273,8 @@ func runLoadAndWait(t *testing.T, timeoutConfig *config.TimeoutConfig, done chan
273273
Out: io.Discard,
274274
},
275275
HTTPOptions: fhttp.HTTPOptions{
276-
URL: reqURL,
276+
URL: reqURL,
277+
HTTPReqTimeOut: reqTimeout,
277278
},
278279
}
279280

0 commit comments

Comments
 (0)