forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcurl.go
191 lines (166 loc) · 6.97 KB
/
curl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package assertions
import (
"context"
"fmt"
"net/http"
"time"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/solo-io/gloo/pkg/utils/kubeutils/kubectl"
"github.com/solo-io/gloo/pkg/utils/requestutils/curl"
"github.com/solo-io/gloo/test/gomega/matchers"
"github.com/solo-io/gloo/test/gomega/transforms"
"github.com/solo-io/gloo/test/kube2e/helper"
)
func (p *Provider) AssertEventualCurlReturnResponse(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedResponse *matchers.HttpResponse,
timeout ...time.Duration,
) *http.Response {
// We rely on the curlPod to execute a curl, therefore we must assert that it actually exists
p.EventuallyObjectsExist(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podOpts.Name, Namespace: podOpts.Namespace,
},
})
currentTimeout, pollingInterval := helper.GetTimeouts(timeout...)
var curlHttpResponse *http.Response
p.Gomega.Eventually(func(g Gomega) {
curlResponse, err := p.clusterContext.Cli.CurlFromPod(ctx, podOpts, curlOptions...)
fmt.Printf("want:\n%+v\nstdout:\n%s\nstderr:%s\n\n", expectedResponse, curlResponse.StdOut, curlResponse.StdErr)
g.Expect(err).NotTo(HaveOccurred())
// Do the transform in a separate step instead of a WithTransform to avoid having to do it twice
//nolint:bodyclose // The caller of this assertion should be responsible for ensuring the body close - if the response is not needed for the test, AssertEventualCurlResponse should be used instead
curlHttpResponse = transforms.WithCurlResponse(curlResponse)
g.Expect(curlHttpResponse).To(matchers.HaveHttpResponse(expectedResponse))
fmt.Printf("success: %+v", curlResponse)
}).
WithTimeout(currentTimeout).
WithPolling(pollingInterval).
WithContext(ctx).
Should(Succeed(), "failed to get expected response")
return curlHttpResponse
}
// We can't use one function and ignore the response because the response body must be closed
func (p *Provider) AssertEventualCurlResponse(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedResponse *matchers.HttpResponse,
timeout ...time.Duration,
) {
resp := p.AssertEventualCurlReturnResponse(ctx, podOpts, curlOptions, expectedResponse, timeout...)
resp.Body.Close()
}
func (p *Provider) AssertCurlReturnResponse(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedResponse *matchers.HttpResponse,
) *http.Response {
// We rely on the curlPod to execute a curl, therefore we must assert that it actually exists
p.EventuallyObjectsExist(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podOpts.Name, Namespace: podOpts.Namespace,
},
})
// Rely on default timeouts set in CurlFromPod
curlResponse, err := p.clusterContext.Cli.CurlFromPod(ctx, podOpts, curlOptions...)
fmt.Printf("want:\n%+v\nstdout:\n%s\nstderr:%s\n\n", expectedResponse, curlResponse.StdOut, curlResponse.StdErr)
Expect(err).NotTo(HaveOccurred())
// Do the transform in a separate step instead of a WithTransform to avoid having to do it twice
curlHttpResponse := transforms.WithCurlResponse(curlResponse)
Expect(curlHttpResponse).To(matchers.HaveHttpResponse(expectedResponse))
fmt.Printf("success: %+v", curlResponse)
return curlHttpResponse
}
func (p *Provider) AssertCurlResponse(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedResponse *matchers.HttpResponse,
) {
resp := p.AssertCurlReturnResponse(ctx, podOpts, curlOptions, expectedResponse)
resp.Body.Close()
}
// AssertEventuallyConsistentCurlResponse asserts that the response from a curl command
// eventually and then consistently matches the expected response
func (p *Provider) AssertEventuallyConsistentCurlResponse(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedResponse *matchers.HttpResponse,
timeout ...time.Duration,
) {
p.AssertEventualCurlResponse(ctx, podOpts, curlOptions, expectedResponse)
pollTimeout := 3 * time.Second
pollInterval := 1 * time.Second
if len(timeout) > 0 {
pollTimeout, pollInterval = helper.GetTimeouts(timeout...)
}
p.Gomega.Consistently(func(g Gomega) {
res, err := p.clusterContext.Cli.CurlFromPod(ctx, podOpts, curlOptions...)
fmt.Printf("want:\n%+v\nstdout:\n%s\nstderr:%s\n\n", expectedResponse, res.StdOut, res.StdErr)
g.Expect(err).NotTo(HaveOccurred())
expectedResponseMatcher := WithTransform(transforms.WithCurlResponse, matchers.HaveHttpResponse(expectedResponse))
g.Expect(res).To(expectedResponseMatcher)
fmt.Printf("success: %+v", res)
}).
WithTimeout(pollTimeout).
WithPolling(pollInterval).
WithContext(ctx).
Should(Succeed())
}
// AssertEventualCurlError asserts that the response from a curl command is an error such as `Failed to connect`
// as opposed to an http error from the server. This is useful when testing that a service is not reachable,
// for example to validate that a delete operation has taken effect.
func (p *Provider) AssertEventualCurlError(
ctx context.Context,
podOpts kubectl.PodExecOptions,
curlOptions []curl.Option,
expectedErrorCode int, // This is an application error code not an HTTP error code
timeout ...time.Duration,
) {
// We rely on the curlPod to execute a curl, therefore we must assert that it actually exists
p.EventuallyObjectsExist(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podOpts.Name, Namespace: podOpts.Namespace,
},
})
pollTimeout := 5 * time.Second
pollInterval := 500 * time.Millisecond
if len(timeout) > 0 {
pollTimeout, pollInterval = helper.GetTimeouts(timeout...)
}
testMessage := fmt.Sprintf("Expected curl error %d", expectedErrorCode)
p.Gomega.Eventually(func(g Gomega) {
curlResponse, err := p.clusterContext.Cli.CurlFromPod(ctx, podOpts, curlOptions...)
if err == nil {
if curlResponse == nil { // This is not expected to happen, but adding for safety/future-proofing
fmt.Printf("wanted curl error, got no error and no response\n")
testMessage = fmt.Sprintf("Expected curl error %d, got no error and no response\n", expectedErrorCode)
} else {
fmt.Printf("wanted curl error, got response:\nstdout:\n%s\nstderr:%s\n", curlResponse.StdOut, curlResponse.StdErr)
curlHttpResponse := transforms.WithCurlResponse(curlResponse)
testMessage = fmt.Sprintf("failed to get a curl error, got response code: %d", curlHttpResponse.StatusCode)
curlHttpResponse.Body.Close()
}
g.Expect(err).To(HaveOccurred())
}
if expectedErrorCode > 0 {
expectedCurlError := fmt.Sprintf("exit status %d", expectedErrorCode)
fmt.Printf("wanted curl error: %s, got error: %s\n", expectedCurlError, err.Error())
testMessage = fmt.Sprintf("Expected curl error: %s, got: %s", expectedCurlError, err.Error())
g.Expect(err.Error()).To(Equal(expectedCurlError))
} else {
fmt.Printf("wanted any curl error, got error: %s\n", err.Error())
}
}).
WithTimeout(pollTimeout).
WithPolling(pollInterval).
WithContext(ctx).
Should(Succeed(), testMessage)
}