-
Notifications
You must be signed in to change notification settings - Fork 990
Expand file tree
/
Copy pathlogs_command_test.go
More file actions
239 lines (217 loc) · 6.63 KB
/
logs_command_test.go
File metadata and controls
239 lines (217 loc) · 6.63 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package isolated
import (
"encoding/base64"
"fmt"
"net/http"
"runtime"
"strconv"
"time"
"code.cloudfoundry.org/cli/integration/helpers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("logs command", func() {
var server *ghttp.Server
BeforeEach(func() {
server = helpers.StartAndTargetMockServerWithAPIVersions(helpers.DefaultV2Version, helpers.DefaultV3Version)
helpers.AddLoginRoutes(server)
helpers.AddHandler(server,
http.MethodGet,
"/v3/organizations?order_by=name",
http.StatusOK,
[]byte(
`{
"total_results": 1,
"total_pages": 1,
"resources": [
{
"guid": "f3ea75ba-ea6b-439f-8889-b07abf718e6a",
"name": "some-fake-org"
}
]}`),
)
// The v6 version of this command makes the below request when logging in.
// See below for comparison with v7 version.
helpers.AddHandler(server,
http.MethodGet,
"/v3/spaces?organization_guids=f3ea75ba-ea6b-439f-8889-b07abf718e6a",
http.StatusOK,
[]byte(
`{
"total_results": 1,
"total_pages": 1,
"resources": [
{
"guid": "1704b4e7-14bb-4b7b-bc23-0b8d23a60238",
"name": "some-fake-space"
}
]}`),
)
// The v7 version of this command makes the below request when logging in,
// which is similar to the v6 version above except for the additional 'order_by'
// query parameter. Rather than split these tests across two files, we just add
// a handler for both routes (with and without 'order_by').
helpers.AddHandler(server,
http.MethodGet,
"/v3/spaces?order_by=name&organization_guids=f3ea75ba-ea6b-439f-8889-b07abf718e6a",
http.StatusOK,
[]byte(
`{
"total_results": 1,
"total_pages": 1,
"resources": [
{
"guid": "1704b4e7-14bb-4b7b-bc23-0b8d23a60238",
"name": "some-fake-space"
}
]}`),
)
helpers.AddHandler(server,
http.MethodGet,
"/v2/apps?q=name%3Asome-fake-app&q=space_guid%3A1704b4e7-14bb-4b7b-bc23-0b8d23a60238",
http.StatusOK,
[]byte(
`{
"total_results": 1,
"total_pages": 1,
"resources": [
{
"metadata": {
"guid": "d5d27772-315f-474b-8673-57e34ce2db2c"
},
"entity": {
"name": "some-fake-app"
}
}
]}`),
)
helpers.AddHandler(server,
http.MethodGet,
"/v3/apps?names=some-fake-app&space_guids=1704b4e7-14bb-4b7b-bc23-0b8d23a60238",
http.StatusOK,
[]byte(
`{
"total_results": 1,
"total_pages": 1,
"resources": [
{
"guid": "d5d27772-315f-474b-8673-57e34ce2db2c",
"name": "some-fake-app"
}
]}`),
)
helpers.AddHandler(server,
http.MethodGet,
"/api/v1/info",
http.StatusOK,
[]byte(`{"version":"2.6.8"}`),
)
})
AfterEach(func() {
server.Close()
})
Describe("streaming logs", func() {
const logMessage = "hello from log-cache"
var returnEmptyEnvelope bool
onWindows := runtime.GOOS == "windows"
BeforeEach(func() {
latestEnvelopeTimestamp := "1581447006352020890"
latestEnvelopeTimestampMinusOneSecond := "1581447005352020890"
nextEnvelopeTimestamp := "1581447009352020890"
nextEnvelopeTimestampPlusOneNanosecond := "1581447009352020891"
server.RouteToHandler(
http.MethodGet,
"/api/v1/read/d5d27772-315f-474b-8673-57e34ce2db2c",
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
switch r.URL.RawQuery {
case fmt.Sprintf("descending=true&limit=1&start_time=%s", strconv.FormatInt(time.Time{}.UnixNano(), 10)):
if returnEmptyEnvelope {
_, err := w.Write([]byte(`{}`))
Expect(err).ToNot(HaveOccurred())
returnEmptyEnvelope = false // Allow the CLI to continue after receiving an empty envelope
} else {
_, err := w.Write([]byte(fmt.Sprintf(`
{
"envelopes": {
"batch": [
{
"timestamp": "%s",
"source_id": "d5d27772-315f-474b-8673-57e34ce2db2c"
}
]
}
}`, latestEnvelopeTimestamp)))
Expect(err).ToNot(HaveOccurred())
}
case fmt.Sprintf("envelope_types=LOG&start_time=%s", latestEnvelopeTimestampMinusOneSecond):
_, err := w.Write([]byte(fmt.Sprintf(`{
"envelopes": {
"batch": [
{
"timestamp": "%s",
"source_id": "d5d27772-315f-474b-8673-57e34ce2db2c",
"tags": {
"__v1_type": "LogMessage"
},
"log": {
"payload": "%s",
"type": "OUT"
}
}
]
}
}`, nextEnvelopeTimestamp, base64.StdEncoding.EncodeToString([]byte(logMessage)))))
Expect(err).ToNot(HaveOccurred())
case fmt.Sprintf("envelope_types=LOG&start_time=%s", nextEnvelopeTimestampPlusOneNanosecond):
_, err := w.Write([]byte("{}"))
Expect(err).ToNot(HaveOccurred())
default:
Fail(fmt.Sprintf("Unhandled log-cache api query string: %s", r.URL.RawQuery))
}
})
})
When("there already is an envelope in the log cache", func() {
JustBeforeEach(func() {
returnEmptyEnvelope = false
})
It("fetches logs with a timestamp just prior to the latest log envelope", func() {
username, password := helpers.GetCredentials()
session := helpers.CF("login", "-a", server.URL(), "-u", username, "-p", password, "--skip-ssl-validation")
Eventually(session).Should(Exit(0))
session = helpers.CF("logs", "some-fake-app")
Eventually(session).Should(Say(logMessage))
if onWindows {
session.Kill()
Eventually(session).Should(Exit())
} else {
session.Interrupt()
Eventually(session).Should(Exit(0), "Interrupt should be handled and fail gracefully")
}
})
})
When("there is not yet an envelope in the log cache", func() {
JustBeforeEach(func() {
returnEmptyEnvelope = true
})
// TODO: the case where log-cache has no envelopes yet may be "special": we may want to switch to "start from your oldest envelope" approach.
It("retries until there is an initial envelope, and then fetches logs with a timestamp just prior to the latest log envelope", func() {
username, password := helpers.GetCredentials()
session := helpers.CF("login", "-a", server.URL(), "-u", username, "-p", password, "--skip-ssl-validation")
Eventually(session).Should(Exit(0))
session = helpers.CF("logs", "some-fake-app")
Eventually(session).Should(Say(logMessage))
if onWindows {
session.Kill()
Eventually(session).Should(Exit())
} else {
session.Interrupt()
Eventually(session).Should(Exit(0))
}
})
})
})
})