-
Notifications
You must be signed in to change notification settings - Fork 10
/
serve.go
217 lines (179 loc) · 5.63 KB
/
serve.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package matchers
import (
"errors"
"fmt"
"io"
"net/http"
"reflect"
"sort"
"strconv"
"strings"
"github.com/onsi/gomega/types"
"github.com/paketo-buildpacks/occam"
)
// Serve matches if the actual occam.Container is running AND the
// response from an HTTP request to the container's exposed port
// matches the 'expected' matcher passed as an argument.
func Serve(expected interface{}) *ServeMatcher {
return &ServeMatcher{
expected: expected,
client: http.DefaultClient,
protocol: "http",
docker: occam.NewDocker(),
}
}
type ServeMatcher struct {
expected interface{}
port int
endpoint string
protocol string
docker occam.Docker
response string
client *http.Client
responseHeaders http.Header
}
type Header struct {
key string
value string
}
func WithHeader(key, value string) Header {
return Header{
key: key,
value: value,
}
}
// OnPort sets the container port that is expected to be exposed.
func (sm *ServeMatcher) OnPort(port int) *ServeMatcher {
sm.port = port
return sm
}
// WithClient sets the http client that will be used to make the request. This
// allows for non-default client settings like custom redirect handling or
// adding a cookie jar.
func (sm *ServeMatcher) WithClient(client *http.Client) *ServeMatcher {
sm.client = client
return sm
}
// WithEndpoint sets the endpoint or subdirectory where the expected content
// should be available. For example, WithEndpoint("/health") will attempt to
// access the server's /health endpoint.
func (sm *ServeMatcher) WithEndpoint(endpoint string) *ServeMatcher {
sm.endpoint = endpoint
return sm
}
// WithProtocol sets the protocol of the request.
// For example, WithProtocol("https") will make an https request
func (sm *ServeMatcher) WithProtocol(protocol string) *ServeMatcher {
sm.protocol = protocol
return sm
}
// WithDocker sets the occam.Docker that the matcher will use to access
// the 'actual' container's metadata.
func (sm *ServeMatcher) WithDocker(docker occam.Docker) *ServeMatcher {
sm.docker = docker
return sm
}
func (sm *ServeMatcher) Match(actual interface{}) (success bool, err error) {
container, ok := actual.(occam.Container)
if !ok {
return false, fmt.Errorf("ServeMatcher expects an occam.Container, received %T", actual)
}
// no port specified, and there's only one to choose from
port := strconv.Itoa(sm.port)
if port == "0" {
if len(container.Ports) == 1 {
for p := range container.Ports {
port = p
break
}
} else {
return false, fmt.Errorf("container has multiple port mappings, but none were specified. Please specify via the OnPort method")
}
}
if _, ok := container.Ports[port]; !ok {
// EITHER: you have multiple ports and didn't specify OR you specified a bad port
message := fmt.Sprintf("ServeMatcher looking for response from container port %s which is not in container port map", port)
if logs, _ := sm.docker.Container.Logs.Execute(container.ID); logs != nil {
message = fmt.Sprintf("%s\n\nContainer logs:\n\n%s", message, logs)
}
return false, errors.New(message)
}
response, err := sm.client.Get(fmt.Sprintf("%s://%s:%s%s", sm.protocol, container.Host(), container.HostPort(port), sm.endpoint))
if err != nil {
return false, err
}
if response != nil {
sm.responseHeaders = response.Header
if header, ok := sm.expected.(Header); ok {
return response.Header.Get(header.key) == header.value, nil
}
defer response.Body.Close()
content, err := io.ReadAll(response.Body)
if err != nil {
return false, err
}
sm.response = string(content)
if response.StatusCode == http.StatusOK {
match, err := sm.compare(string(content), sm.expected)
if err != nil {
return false, err
}
return match, nil
}
}
return false, nil
}
func formatHeaders(headers http.Header) string {
var keys []string
for key := range headers {
keys = append(keys, key)
}
sort.Strings(keys)
formatted := ""
for _, key := range keys {
formatted += fmt.Sprintf("Header '%s=%s'\n\t", key, strings.Join(headers.Values(key), ", "))
}
return strings.TrimSpace(formatted)
}
func (sm *ServeMatcher) compare(actual string, expected interface{}) (bool, error) {
if m, ok := expected.(types.GomegaMatcher); ok {
match, err := m.Match(actual)
if err != nil {
return false, err
}
return match, nil
}
return reflect.DeepEqual(actual, expected), nil
}
func (sm *ServeMatcher) FailureMessage(actual interface{}) (message string) {
container := actual.(occam.Container)
response, expected := sm.response, sm.expected
if header, ok := sm.expected.(Header); ok {
response, expected = formatHeaders(sm.responseHeaders), fmt.Sprintf("Header '%s=%s'", header.key, header.value)
}
message = fmt.Sprintf("Expected the response from docker container %s:\n\n\t%s\n\nto contain:\n\n\t%s",
container.ID,
response,
expected,
)
if logs, _ := sm.docker.Container.Logs.Execute(container.ID); logs != nil {
message = fmt.Sprintf("%s\n\nContainer logs:\n\n%s", message, logs)
}
return message
}
func (sm *ServeMatcher) NegatedFailureMessage(actual interface{}) (message string) {
container := actual.(occam.Container)
response, expected := sm.response, sm.expected
if header, ok := sm.expected.(Header); ok {
response, expected = formatHeaders(sm.responseHeaders), fmt.Sprintf("Header '%s=%s'", header.key, header.value)
}
message = fmt.Sprintf("Expected the response from docker container %s:\n\n\t%s\n\nnot to contain:\n\n\t%s",
container.ID,
response,
expected,
)
if logs, _ := sm.docker.Container.Logs.Execute(container.ID); logs != nil {
message = fmt.Sprintf("%s\n\nContainer logs:\n\n%s", message, logs)
}
return message
}