|
| 1 | +package integration_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os/exec" |
| 9 | + "time" |
| 10 | + |
| 11 | + logcache "code.cloudfoundry.org/go-log-cache/v2/rpc/logcache_v1" |
| 12 | + "code.cloudfoundry.org/log-cache/integration/integrationfakes" |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + "github.com/onsi/gomega/gexec" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("Gateway", func() { |
| 19 | + var ( |
| 20 | + fakeLogCache *integrationfakes.FakeLogCache |
| 21 | + |
| 22 | + gatewayPort int |
| 23 | + gateway *gexec.Session |
| 24 | + ) |
| 25 | + |
| 26 | + BeforeEach(func() { |
| 27 | + port := 8000 + GinkgoParallelProcess() |
| 28 | + fakeLogCache = integrationfakes.NewFakeLogCache(port, nil) |
| 29 | + |
| 30 | + gatewayPort = 8081 + GinkgoParallelProcess() |
| 31 | + }) |
| 32 | + |
| 33 | + JustBeforeEach(func() { |
| 34 | + fakeLogCache.Start() |
| 35 | + |
| 36 | + command := exec.Command(componentPaths.Gateway) |
| 37 | + envVars := map[string]string{ |
| 38 | + "ADDR": fmt.Sprintf(":%d", gatewayPort), |
| 39 | + "LOG_CACHE_ADDR": fakeLogCache.Address(), |
| 40 | + "METRICS_PORT": "0", |
| 41 | + } |
| 42 | + for k, v := range envVars { |
| 43 | + command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) |
| 44 | + } |
| 45 | + |
| 46 | + var err error |
| 47 | + gateway, err = gexec.Start(command, GinkgoWriter, GinkgoWriter) |
| 48 | + Expect(err).ShouldNot(HaveOccurred()) |
| 49 | + }) |
| 50 | + |
| 51 | + JustAfterEach(func() { |
| 52 | + gateway.Interrupt().Wait(2 * time.Second) |
| 53 | + fakeLogCache.Stop() |
| 54 | + }) |
| 55 | + |
| 56 | + Context("/api/v1/info endpoint", func() { |
| 57 | + var resp *http.Response |
| 58 | + |
| 59 | + JustBeforeEach(func() { |
| 60 | + u := fmt.Sprintf("http://localhost:%d/api/v1/info", gatewayPort) |
| 61 | + Eventually(func() error { |
| 62 | + var err error |
| 63 | + resp, err = http.Get(u) |
| 64 | + return err |
| 65 | + }, "5s").ShouldNot(HaveOccurred()) |
| 66 | + }) |
| 67 | + |
| 68 | + AfterEach(func() { |
| 69 | + resp.Body.Close() |
| 70 | + }) |
| 71 | + |
| 72 | + It("returns 200", func() { |
| 73 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 74 | + }) |
| 75 | + |
| 76 | + It("sets Content-Type header", func() { |
| 77 | + Expect(resp.Header.Get("Content-Type")).To(Equal("application/json")) |
| 78 | + }) |
| 79 | + |
| 80 | + It("sets Content-Length header", func() { |
| 81 | + Expect(resp.Header.Get("Content-Length")).To(MatchRegexp("\\d+")) |
| 82 | + }) |
| 83 | + |
| 84 | + Context("response body", func() { |
| 85 | + var body []byte |
| 86 | + |
| 87 | + JustBeforeEach(func() { |
| 88 | + var err error |
| 89 | + body, err = io.ReadAll(resp.Body) |
| 90 | + Expect(err).ToNot(HaveOccurred()) |
| 91 | + }) |
| 92 | + |
| 93 | + It("is a JSON with version and uptime information", func() { |
| 94 | + result := struct { |
| 95 | + Version string `json:"version"` |
| 96 | + VMUptime string `json:"vm_uptime"` |
| 97 | + }{} |
| 98 | + err := json.Unmarshal(body, &result) |
| 99 | + Expect(err).ToNot(HaveOccurred()) |
| 100 | + Expect(result.Version).To(Equal("1.2.3")) |
| 101 | + Expect(result.VMUptime).To(MatchRegexp("\\d+")) |
| 102 | + }) |
| 103 | + |
| 104 | + It("has a newline at the end", func() { |
| 105 | + Expect(string(body)).To(MatchRegexp(".*\\n$")) |
| 106 | + }) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + Context("api/v1/read/:sourceID endpoint", func() { |
| 111 | + DescribeTableSubtree("with valid source IDs", func(sourceID string) { |
| 112 | + var resp *http.Response |
| 113 | + |
| 114 | + JustBeforeEach(func() { |
| 115 | + u := fmt.Sprintf("http://localhost:%d/api/v1/read/%s", gatewayPort, sourceID) |
| 116 | + Eventually(func() error { |
| 117 | + var err error |
| 118 | + resp, err = http.Get(u) |
| 119 | + return err |
| 120 | + }, "5s").ShouldNot(HaveOccurred()) |
| 121 | + }) |
| 122 | + |
| 123 | + AfterEach(func() { |
| 124 | + resp.Body.Close() |
| 125 | + }) |
| 126 | + |
| 127 | + It("returns 200", func() { |
| 128 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 129 | + }) |
| 130 | + |
| 131 | + It("sets Content-Type header", func() { |
| 132 | + Expect(resp.Header.Get("Content-Type")).To(Equal("application/json")) |
| 133 | + }) |
| 134 | + |
| 135 | + It("sets Content-Length header", func() { |
| 136 | + Expect(resp.Header.Get("Content-Length")).To(MatchRegexp("\\d+")) |
| 137 | + }) |
| 138 | + |
| 139 | + PIt("forwards the request to Log Cache", func() { |
| 140 | + // Expect(fakeLogCache.requestCount).To(Equal(1)) |
| 141 | + }) |
| 142 | + |
| 143 | + Context("response body", func() { |
| 144 | + var body []byte |
| 145 | + |
| 146 | + JustBeforeEach(func() { |
| 147 | + var err error |
| 148 | + body, err = io.ReadAll(resp.Body) |
| 149 | + Expect(err).ToNot(HaveOccurred()) |
| 150 | + }) |
| 151 | + |
| 152 | + PIt("is a JSON with envelopes", func() { |
| 153 | + var rr logcache.ReadResponse |
| 154 | + err := json.Unmarshal(body, &rr) |
| 155 | + Expect(err).ToNot(HaveOccurred()) |
| 156 | + Expect(rr.Envelopes).To(HaveLen(0)) |
| 157 | + }) |
| 158 | + |
| 159 | + It("has a newline at the end", func() { |
| 160 | + Expect(string(body)).To(MatchRegexp(".*\\n$")) |
| 161 | + }) |
| 162 | + }) |
| 163 | + }, |
| 164 | + Entry("regular", "myid"), |
| 165 | + Entry("URL encoded", "my%2Fid"), |
| 166 | + Entry("with slash", "my/id"), |
| 167 | + Entry("with dash", "my-id"), |
| 168 | + ) |
| 169 | + }) |
| 170 | +}) |
0 commit comments