/
logs-dash-c.ts
261 lines (216 loc) · 8.35 KB
/
logs-dash-c.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 2019 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Common, Selectors, SidecarExpect } from '@kui-shell/test'
import {
createNS,
allocateNS,
deleteNS,
waitForTerminalText,
deletePodByName
} from '@kui-shell/plugin-kubectl/tests/lib/k8s/utils'
import { create, get, clickRetry, wait } from './helpers'
import { readFileSync } from 'fs'
import { dirname, join } from 'path'
const ROOT = dirname(require.resolve('@kui-shell/plugin-kubectl/tests/package.json'))
const inputBuffer1 = readFileSync(join(ROOT, 'data/k8s/kubectl-logs-two-containers.yaml'))
const inputEncoded1 = inputBuffer1.toString('base64')
const inputBuffer2 = readFileSync(join(ROOT, 'data/k8s/bunch/pod.yaml'))
const inputEncoded2 = inputBuffer2.toString('base64')
const sleepTime = 3
/** sleep for N seconds */
function sleep(N: number) {
return new Promise(resolve => setTimeout(resolve, N * 1000))
}
const wdescribe = !process.env.USE_WATCH_PANE ? describe : xdescribe
wdescribe(`kubectl Logs tab ${process.env.MOCHA_RUN_TARGET || ''}`, function(this: Common.ISuite) {
before(Common.before(this))
after(Common.after(this))
const ns: string = createNS()
allocateNS(this, ns)
const waitForLogText = waitForTerminalText.bind(this)
const createPodWithoutWaiting = create.bind(this, ns)
const waitForPod = wait.bind(this, ns)
const getPodViaClick = get.bind(this, ns)
const click = clickRetry.bind(this)
const podName1 = 'kui-two-containers'
const allContainers = 'All Containers'
const containerName1 = 'nginx'
const containerName2 = 'vim'
const podName2 = 'nginx'
const testLogsContent = (show: string[], notShow?: string[]) => {
if (show) {
show.forEach(showInLog => {
it(`should show ${showInLog} in log output`, async () => {
try {
await sleep(sleepTime)
await waitForLogText((text: string) => text.indexOf(showInLog) !== -1)
} catch (err) {
return Common.oops(this, true)(err)
}
})
})
}
if (notShow) {
notShow.forEach(notShowInLog => {
it(`should not show ${notShowInLog} in log output`, async () => {
try {
await sleep(sleepTime)
await waitForLogText((text: string) => text.indexOf(notShowInLog) === -1)
} catch (err) {
return Common.oops(this, true)(err)
}
})
})
}
}
const doRetry = (showInLog: string[], toolbar: { text: string; type: string }) => {
it('should hit retry', async () => {
try {
await click()
await SidecarExpect.toolbarText({ text: toolbar.text, type: toolbar.type, exact: false })(this.app)
testLogsContent(showInLog)
} catch (err) {
return Common.oops(this, true)(err)
}
})
}
const switchToLogsTab = (showInLog: string[], toolbar: { text: string; type: string }) => {
it('should show logs tab', async () => {
try {
await this.app.client.waitForVisible(Selectors.SIDECAR_MODE_BUTTON('logs'))
await this.app.client.click(Selectors.SIDECAR_MODE_BUTTON('logs'))
await this.app.client.waitForVisible(Selectors.SIDECAR_MODE_BUTTON_SELECTED('logs'))
await SidecarExpect.toolbarText({ type: toolbar.type, text: toolbar.text, exact: false })(this.app)
testLogsContent(showInLog)
} catch (err) {
return Common.oops(this, true)(err)
}
})
}
const switchContainer = (
container: string,
showInLog: string[],
notShowInLog: string[],
toolbar: { text: string; type: string }
) => {
it(`should switch to container ${container}`, async () => {
try {
await this.app.client.waitForVisible(Selectors.SIDECAR_MODE_BUTTON('container-list'))
await this.app.client.click(Selectors.SIDECAR_MODE_BUTTON('container-list'))
await this.app.client.waitForVisible(`.bx--overflow-menu-options button[data-mode="${container}"]`)
await this.app.client.click(`.bx--overflow-menu-options button[data-mode="${container}"]`)
await SidecarExpect.toolbarText({ type: toolbar.type, text: toolbar.text, exact: false })(this.app)
} catch (err) {
return Common.oops(this, true)(err)
}
})
testLogsContent(showInLog, notShowInLog)
}
const toggleStreaming = (changeToLive: boolean) => {
it('should toggle streaming', async () => {
try {
await sleep(sleepTime)
await this.app.client.waitForVisible(Selectors.SIDECAR_MODE_BUTTON('toggle-streaming'))
await this.app.client.click(Selectors.SIDECAR_MODE_BUTTON('toggle-streaming'))
if (changeToLive) {
await SidecarExpect.toolbarText({ type: 'info', text: 'Logs are live', exact: false })(this.app)
} else {
await SidecarExpect.toolbarText({ type: 'warning', text: 'Log streaming is paused', exact: false })(this.app)
}
} catch (err) {
return Common.oops(this, true)(err)
}
})
}
/* Here comes the test */
createPodWithoutWaiting(inputEncoded2, podName2)
waitForPod(podName2)
getPodViaClick(podName2)
switchToLogsTab(['No log data'], { text: 'Logs are live', type: 'info' })
createPodWithoutWaiting(inputEncoded1, podName1)
waitForPod(podName1)
getPodViaClick(podName1)
switchToLogsTab([containerName1, containerName2], { text: 'Logs are live', type: 'info' })
/** testing various combination here */
switchContainer(containerName1, [containerName1], [containerName2], { text: containerName1, type: 'info' })
switchContainer(containerName2, [containerName2], [containerName1], { text: containerName2, type: 'info' })
switchContainer(allContainers, [containerName1, containerName2], [], {
text: allContainers.toLowerCase(),
type: 'info'
})
switchContainer(containerName2, [containerName2], [containerName1], { text: containerName2, type: 'info' })
switchContainer(allContainers, [containerName1, containerName2], [], {
text: allContainers.toLowerCase(),
type: 'info'
})
switchContainer(containerName2, [containerName2], [containerName1], { text: containerName2, type: 'info' })
toggleStreaming(false) // hit pause button
toggleStreaming(true) // hit resume button
toggleStreaming(false) // hit pause button
// switch to container, streaming should be live
switchContainer(containerName1, [containerName1], [containerName2], {
text: `Logs are live streaming. Showing container ${containerName1}`,
type: 'info'
})
switchContainer(allContainers, [containerName1, containerName2], [], {
text: allContainers.toLowerCase(),
type: 'info'
})
deletePodByName(this, podName1, ns)
it('should see log streaming stopped', async () => {
try {
await sleep(sleepTime)
await SidecarExpect.toolbarText({ type: 'warning', text: 'Log streaming stopped', exact: false })(this.app)
} catch (err) {
return Common.oops(this, true)(err)
}
})
const showError = 'Log streaming stopped abnormally.'
doRetry(['not found'], {
text: showError,
type: 'error'
})
switchContainer(containerName1, ['not found'], [], {
text: showError,
type: 'error'
})
switchContainer(containerName2, ['not found'], [], {
text: showError,
type: 'error'
})
switchContainer(allContainers, ['not found'], [], {
text: showError,
type: 'error'
})
doRetry(['not found'], {
text: showError,
type: 'error'
})
/* this part isn't stable, and doesn't really test what we want, reliably: if the create is fast, then "without waiting' won't matter
createPodWithoutWaiting(inputEncoded1, podName1) // recreate this pod
getPodViaYaml(podName1) // NOTE: immediately open sidecar when pod is in creation
switchToLogsTab(['not found'], {
text: showError,
type: 'error'
})
waitForPod(podName1) // wait for pod ready
doRetry([containerName1, containerName2], {
text: 'Logs are live',
type: 'info'
})
*/
deleteNS(this, ns)
})