-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestWorkspaceUtil.ts
403 lines (330 loc) · 16 KB
/
TestWorkspaceUtil.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import 'reflect-metadata';
import { che } from '@eclipse-che/api';
import { TestConstants } from '../../TestConstants';
import { injectable, inject } from 'inversify';
import { DriverHelper } from '../DriverHelper';
import { WorkspaceStatus } from './WorkspaceStatus';
import { error } from 'selenium-webdriver';
import { CheApiRequestHandler } from '../requestHandlers/CheApiRequestHandler';
import { CLASSES } from '../../inversify.types';
import { Logger } from '../Logger';
import axios from 'axios';
import { ITestWorkspaceUtil } from './ITestWorkspaceUtil';
import { ApiUrlResolver } from './ApiUrlResolver';
@injectable()
export class TestWorkspaceUtil implements ITestWorkspaceUtil {
readonly attempts: number = TestConstants.TS_SELENIUM_WORKSPACE_STATUS_ATTEMPTS;
readonly polling: number = TestConstants.TS_SELENIUM_WORKSPACE_STATUS_POLLING;
constructor(
@inject(CLASSES.DriverHelper) private readonly driverHelper: DriverHelper,
@inject(CLASSES.CheApiRequestHandler) private readonly processRequestHandler: CheApiRequestHandler,
@inject(CLASSES.ApiUrlResolver) private readonly apiUrlResolver: ApiUrlResolver
) { }
public async waitWorkspaceStatus(workspaceName: string, expectedWorkspaceStatus: WorkspaceStatus) {
Logger.debug('TestWorkspaceUtil.waitWorkspaceStatus');
let workspaceStatus: string = '';
let expectedStatus: boolean = false;
for (let i = 0; i < this.attempts; i++) {
const response = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspaceApiUrl(workspaceName));
if (response.status !== 200) {
throw new Error(`Can not get status of a workspace. Code: ${response.status} Data: ${response.data}`);
}
workspaceStatus = await response.data.status.phase;
if (workspaceStatus === expectedWorkspaceStatus) {
expectedStatus = true;
break;
}
await this.driverHelper.wait(this.polling);
}
if (!expectedStatus) {
let waitTime = this.attempts * this.polling;
throw new error.TimeoutError(`The workspace was not stopped in ${waitTime} ms. Currnet status is: ${workspaceStatus}`);
}
}
public async stopWorkspaceByName(workspaceName: string) {
Logger.debug('TestWorkspaceUtil.stopWorkspaceByName');
const stopWorkspaceApiUrl: string = await this.apiUrlResolver.getWorkspaceApiUrl(workspaceName);
let stopWorkspaceResponse;
try {
stopWorkspaceResponse = await this.processRequestHandler.patch(stopWorkspaceApiUrl, [{'op': 'replace', 'path': '/spec/started', 'value': false}]);
} catch (err) {
console.log(`Stop workspace call failed. URL used: ${stopWorkspaceApiUrl}`);
throw err;
}
if (stopWorkspaceResponse.status !== 200) {
throw new Error(`Cannot stop workspace. Code: ${stopWorkspaceResponse.status} Data: ${stopWorkspaceResponse.data}`);
}
await this.waitWorkspaceStatus(workspaceName, WorkspaceStatus.STOPPED);
}
// delete a worksapce without stopping phase (similar with force deleting)
public async deleteWorkspaceByName(workspaceName: string) {
Logger.debug(`TestWorkspaceUtil.deleteWorkspaceByName ${workspaceName}` );
const deleteWorkspaceApiUrl: string = await this.apiUrlResolver.getWorkspaceApiUrl(workspaceName);
let deleteWorkspaceResponse;
let deleteWorkspaceStatus: boolean = false;
try {
deleteWorkspaceResponse = await this.processRequestHandler.delete(deleteWorkspaceApiUrl);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
Logger.error(`The workspace :${workspaceName} not found`);
throw error;
}
Logger.error(`Delete workspace call failed. URL used: ${deleteWorkspaceStatus}`);
throw error;
}
if (deleteWorkspaceResponse.status !== 204) {
throw new Error(`Can not delete workspace. Code: ${deleteWorkspaceResponse.status} Data: ${deleteWorkspaceResponse.data}`);
}
for (let i = 0; i < this.attempts; i++) {
try {
deleteWorkspaceResponse = await this.processRequestHandler.get(deleteWorkspaceApiUrl);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
deleteWorkspaceStatus = true;
break;
}
}
}
if (!deleteWorkspaceStatus) {
let waitTime = this.attempts * this.polling;
throw new error.TimeoutError(`The workspace was not stopped in ${waitTime} ms.`);
}
}
// stop workspace before deleting with checking stopping phase
public async stopAndDeleteWorkspaceByName(workspaceName: string) {
Logger.debug('TestWorkspaceUtil.stopAndDeleteWorkspaceByName');
await this.stopWorkspaceByName(workspaceName);
await this.deleteWorkspaceByName(workspaceName);
}
// stop all run workspaces in the namespace
public async stopAllRunningWorkspaces(namespace: string) {
Logger.debug('TestWorkspaceUtil.stopAllRunProjects');
let response = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspacesApiUrl());
for (let i = 0; i < response.data.items.length; i++) {
Logger.info('The project is being stopped: ' + response.data.items[i].metadata.name);
await this.stopWorkspaceByName(response.data.items[i].metadata.name);
}
}
// stop all run workspaces, check statused and remove the workspaces
public async stopAndDeleteAllRunningWorkspaces(namespace: string) {
Logger.debug('TestWorkspaceUtil.stopAndDeleteAllRunProjects');
let response = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspacesApiUrl());
await this.stopAllRunningWorkspaces(namespace);
for (let i = 0; i < response.data.items.length; i++) {
Logger.info('The project is being deleted: ' + response.data.items[i].metadata.name);
await this.deleteWorkspaceByName(response.data.items[i].metadata.name);
}
}
// stop all run workspaces without stopping and waiting for of 'Stopped' phase
// similar with 'force' deleting
public async deleteAllWorkspaces(namespace: string) {
Logger.debug('TestWorkspaceUtil.deleteAllRunProjects');
let response = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspacesApiUrl());
for (let i = 0; i < response.data.items.length; i++) {
Logger.info('The project is being deleted .......: ' + response.data.items[i].metadata.name);
await this.deleteWorkspaceByName(response.data.items[i].metadata.name);
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async waitPluginAdding(namespace: string, workspaceName: string, pluginName: string) {
Logger.debug('TestWorkspaceUtil.waitPluginAdding');
const workspaceStatusApiUrl: string = `${await this.apiUrlResolver.getWorkspacesApiUrl()}/${namespace}:${workspaceName}`;
const attempts: number = TestConstants.TS_SELENIUM_PLUGIN_PRECENCE_ATTEMPTS;
const polling: number = TestConstants.TS_SELENIUM_DEFAULT_POLLING;
for (let i = 0; i < attempts; i++) {
const response = await this.processRequestHandler.get(workspaceStatusApiUrl);
if (response.status !== 200) {
await this.driverHelper.wait(polling);
continue;
}
const machines: string = JSON.stringify(response.data.runtime.machines);
const isPluginPresent: boolean = machines.search(pluginName) > 0;
if (isPluginPresent) {
break;
}
if (i === attempts - 1) {
throw new error.TimeoutError(`Exceeded maximum tries attempts, the '${pluginName}' plugin is not present in the workspace runtime.`);
}
await this.driverHelper.wait(polling);
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async getListOfWorkspaceId(): Promise<string[]> {
Logger.debug('TestWorkspaceUtil.getListOfWorkspaceId');
const getAllWorkspacesResponse = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspacesApiUrl());
interface IMyObj {
id: string;
status: string;
}
let stringified = JSON.stringify(getAllWorkspacesResponse.data);
let arrayOfWorkspaces = <IMyObj[]>JSON.parse(stringified);
let wsList: Array<string> = [];
for (let entry of arrayOfWorkspaces) {
wsList.push(entry.id);
}
return wsList;
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async getIdOfRunningWorkspace(wsName: string): Promise<string> {
Logger.debug('TestWorkspaceUtil.getIdOfRunningWorkspace');
const getWorkspacesByNameResponse = await this.processRequestHandler.get(`${await this.apiUrlResolver.getWorkspacesApiUrl()}/:${wsName}`);
return getWorkspacesByNameResponse.data.id;
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async getIdOfRunningWorkspaces(): Promise<Array<string>> {
Logger.debug('TestWorkspaceUtil.getIdOfRunningWorkspaces');
try {
const getAllWorkspacesResponse = await this.processRequestHandler.get(await this.apiUrlResolver.getWorkspacesApiUrl());
interface IMyObj {
id: string;
status: string;
}
let stringified = JSON.stringify(getAllWorkspacesResponse.data);
let arrayOfWorkspaces = <IMyObj[]>JSON.parse(stringified);
let idOfRunningWorkspace: Array<string> = new Array();
for (let entry of arrayOfWorkspaces) {
if (entry.status === 'RUNNING') {
idOfRunningWorkspace.push(entry.id);
}
}
return idOfRunningWorkspace;
} catch (err) {
console.log(`Getting id of running workspaces failed. URL used: ${await this.apiUrlResolver.getWorkspacesApiUrl()}`);
throw err;
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async removeWorkspaceById(id: string) {
Logger.debug('TestWorkspaceUtil.removeWorkspaceById');
const workspaceIdUrl: string = `${await this.apiUrlResolver.getWorkspacesApiUrl()}/${id}`;
try {
const deleteWorkspaceResponse = await this.processRequestHandler.delete(workspaceIdUrl);
if (deleteWorkspaceResponse.status !== 204) {
throw new Error(`Can not remove workspace. Code: ${deleteWorkspaceResponse.status} Data: ${deleteWorkspaceResponse.data}`);
}
} catch (err) {
console.log(`Removing of workspace failed.`);
throw err;
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async stopWorkspaceById(id: string) {
Logger.debug('TestWorkspaceUtil.stopWorkspaceById');
const stopWorkspaceApiUrl: string = `${await this.apiUrlResolver.getWorkspacesApiUrl()}/${id}`;
let stopWorkspaceResponse;
try {
stopWorkspaceResponse = await this.processRequestHandler.delete(`${stopWorkspaceApiUrl}`);
} catch (err) {
console.log(`Stop workspace call failed. URL used: ${stopWorkspaceApiUrl}`);
throw err;
}
if (stopWorkspaceResponse.status !== 200) {
throw new Error(`Can not stop workspace. Code: ${stopWorkspaceResponse.status} Data: ${stopWorkspaceResponse.data}`);
}
let stopped: boolean = false;
let wsStatus = await this.processRequestHandler.get(stopWorkspaceApiUrl);
for (let i = 0; i < TestConstants.TS_SELENIUM_PLUGIN_PRECENCE_ATTEMPTS; i++) {
wsStatus = await this.processRequestHandler.get(stopWorkspaceApiUrl);
if (wsStatus.data.status === WorkspaceStatus.STOPPED) {
stopped = true;
break;
}
await this.driverHelper.wait(TestConstants.TS_SELENIUM_DEFAULT_POLLING);
}
if (!stopped) {
let waitTime = TestConstants.TS_SELENIUM_PLUGIN_PRECENCE_ATTEMPTS * TestConstants.TS_SELENIUM_DEFAULT_POLLING;
throw new error.TimeoutError(`The workspace was not stopped in ${waitTime} ms. Currnet status is: ${wsStatus.data.status}`);
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async cleanUpAllWorkspaces() {
Logger.debug('TestWorkspaceUtil.cleanUpAllWorkspaces');
let listOfRunningWorkspaces: Array<string> = await this.getIdOfRunningWorkspaces();
for (const entry of listOfRunningWorkspaces) {
await this.stopWorkspaceById(entry);
}
let listAllWorkspaces: Array<string> = await this.getListOfWorkspaceId();
for (const entry of listAllWorkspaces) {
await this.removeWorkspaceById(entry);
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
public async cleanUpRunningWorkspace(workspaceName: string) {
if (workspaceName === undefined || workspaceName.length === 0) {
Logger.warn(`Could nod delete workspace because workspaceName is undefined or empty`);
return;
}
Logger.debug(`TestWorkspaceUtil.cleanUpRunningWorkspace ${workspaceName}`);
const workspaceID: string = await this.getIdOfRunningWorkspace(workspaceName);
if (workspaceID === undefined || workspaceID.length === 0) {
Logger.error(`Could nod delete workspace with name ${workspaceName} because workspaceID is undefined or empty`);
return;
}
Logger.trace(`TestWorkspaceUtil.cleanUpRunningWorkspace Stopping workspace:${workspaceName} with ID:${workspaceID}`);
await this.stopWorkspaceById(workspaceID);
Logger.trace(`TestWorkspaceUtil.cleanUpRunningWorkspace Deleting workspace ${workspaceName}`);
await this.removeWorkspaceById(workspaceID);
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
async createWsFromDevFile(customTemplate: che.workspace.devfile.Devfile) {
Logger.debug('TestWorkspaceUtil.createWsFromDevFile');
try {
await this.processRequestHandler.post(await this.apiUrlResolver.getWorkspacesApiUrl() + '/devfile', customTemplate);
} catch (error) {
console.error(error);
throw error;
}
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
async getBaseDevfile(): Promise<che.workspace.devfile.Devfile> {
Logger.debug('TestWorkspaceUtil.getBaseDevfile');
const baseDevfile: che.workspace.devfile.Devfile = {
apiVersion: '1.0.0',
metadata: {
name: 'test-workspace'
}
};
return baseDevfile;
}
/**
* @deprecated Method deprecated. Works with CHE server only
*/
async startWorkspace(workspaceId: string) {
Logger.debug('TestWorkspaceUtil.startWorkspace');
try {
await this.processRequestHandler.post(`${await this.apiUrlResolver.getWorkspacesApiUrl()}/${workspaceId}/runtime`);
} catch (error) {
console.error(error);
throw error;
}
}
}