-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgateway_test.go
More file actions
179 lines (147 loc) · 4.27 KB
/
gateway_test.go
File metadata and controls
179 lines (147 loc) · 4.27 KB
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
package integration_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"time"
logcache "code.cloudfoundry.org/go-log-cache/v3/rpc/logcache_v1"
"github.com/google/go-cmp/cmp/cmpopts"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"code.cloudfoundry.org/log-cache/integration/integrationfakes"
)
var _ = Describe("Gateway", func() {
var (
fakeLogCache *integrationfakes.FakeLogCache
gatewayPort int
gateway *gexec.Session
)
BeforeEach(func() {
port := 8000 + GinkgoParallelProcess()
fakeLogCache = integrationfakes.NewFakeLogCache(port, nil)
fakeLogCache.Start()
gatewayPort = 8080 + GinkgoParallelProcess()
})
JustBeforeEach(func() {
command := exec.Command(componentPaths.Gateway)
envVars := map[string]string{
"ADDR": fmt.Sprintf(":%d", gatewayPort),
"LOG_CACHE_ADDR": fakeLogCache.Address(),
"METRICS_PORT": "0",
}
for k, v := range envVars {
command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v))
}
var err error
gateway, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
})
JustAfterEach(func() {
gateway.Interrupt().Wait(2 * time.Second)
})
AfterEach(func() {
fakeLogCache.Stop()
})
Context("/api/v1/info endpoint", func() {
var resp *http.Response
JustBeforeEach(func() {
u := fmt.Sprintf("http://localhost:%d/api/v1/info", gatewayPort)
Eventually(func() error {
var err error
resp, err = http.Get(u)
return err
}, "5s").ShouldNot(HaveOccurred())
})
AfterEach(func() {
resp.Body.Close()
})
It("returns 200", func() {
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
It("sets Content-Type header", func() {
Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
})
It("sets Content-Length header", func() {
Expect(resp.Header.Get("Content-Length")).To(MatchRegexp("\\d+"))
})
Context("response body", func() {
var body []byte
JustBeforeEach(func() {
var err error
body, err = io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
})
It("is a JSON with version and uptime information", func() {
result := struct {
Version string `json:"version"`
VMUptime string `json:"vm_uptime"`
}{}
err := json.Unmarshal(body, &result)
Expect(err).ToNot(HaveOccurred())
Expect(result.Version).To(Equal("1.2.3"))
Expect(result.VMUptime).To(MatchRegexp("\\d+"))
})
It("has a newline at the end", func() {
Expect(string(body)).To(MatchRegexp(".*\\n$"))
})
})
})
Context("api/v1/read/:sourceID endpoint", func() {
DescribeTableSubtree("with valid source IDs",
func(sourceID string) {
var resp *http.Response
JustBeforeEach(func() {
u := fmt.Sprintf("http://localhost:%d/api/v1/read/%s", gatewayPort, sourceID)
Eventually(func() error {
var err error
resp, err = http.Get(u)
return err
}, "5s").ShouldNot(HaveOccurred())
})
AfterEach(func() {
resp.Body.Close()
})
It("returns 200", func() {
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
It("sets Content-Type header", func() {
Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
})
It("sets Content-Length header", func() {
Expect(resp.Header.Get("Content-Length")).To(MatchRegexp("\\d+"))
})
It("forwards the request to Log Cache", func() {
reqs := fakeLogCache.ReadRequests()
Expect(len(reqs)).To(Equal(1))
Expect(reqs[0]).To(BeComparableTo(&logcache.ReadRequest{
SourceId: sourceID,
}, cmpopts.IgnoreUnexported(logcache.ReadRequest{})))
})
Context("response body", func() {
var body []byte
JustBeforeEach(func() {
var err error
body, err = io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
})
PIt("is a JSON with envelopes", func() {
var rr logcache.ReadResponse
err := json.Unmarshal(body, &rr)
Expect(err).ToNot(HaveOccurred())
Expect(rr.Envelopes).To(HaveLen(0))
})
It("has a newline at the end", func() {
Expect(string(body)).To(MatchRegexp(".*\\n$"))
})
})
},
Entry("regular", "myid"),
Entry("URL encoded", "my%2Fid"),
Entry("with slash", "my/id"),
Entry("with dash", "my-id"),
)
})
})