forked from TencentBlueKing/bk-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operations.java
371 lines (334 loc) · 14.3 KB
/
Operations.java
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
package com.tencent.bk.job.api.util;
import com.tencent.bk.job.api.constant.HighRiskGrammarActionEnum;
import com.tencent.bk.job.api.constant.ResourceScopeTypeEnum;
import com.tencent.bk.job.api.constant.ScriptTypeEnum;
import com.tencent.bk.job.api.model.EsbResp;
import com.tencent.bk.job.api.props.TestProps;
import com.tencent.bk.job.api.v3.constants.APIV3Urls;
import com.tencent.bk.job.api.v3.model.EsbAccountV3BasicDTO;
import com.tencent.bk.job.api.v3.model.EsbCronInfoV3DTO;
import com.tencent.bk.job.api.v3.model.EsbDangerousRuleV3DTO;
import com.tencent.bk.job.api.v3.model.EsbFileSourceV3DTO;
import com.tencent.bk.job.api.v3.model.EsbJobExecuteV3DTO;
import com.tencent.bk.job.api.v3.model.EsbPageDataV3;
import com.tencent.bk.job.api.v3.model.EsbPlanBasicInfoV3DTO;
import com.tencent.bk.job.api.v3.model.EsbScriptVersionDetailV3DTO;
import com.tencent.bk.job.api.v3.model.EsbServerV3DTO;
import com.tencent.bk.job.api.v3.model.HostDTO;
import com.tencent.bk.job.api.v3.model.request.EsbCreateDangerousRuleV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbCreatePublicScriptV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbCreateScriptV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbDeleteCronV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbDeletePublicScriptV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbDeletePublicScriptVersionV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbDeleteScriptV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbDeleteScriptVersionV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbExecuteJobV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbFastExecuteScriptV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbFastTransferFileV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbGetPlanListV3Request;
import com.tencent.bk.job.api.v3.model.request.EsbManageDangerousRuleV3Req;
import com.tencent.bk.job.api.v3.model.request.EsbSaveCronV3Request;
import io.restassured.common.mapper.TypeRef;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.tencent.bk.job.api.constant.Constant.SHELL_SCRIPT_CONTENT_BASE64;
import static io.restassured.RestAssured.given;
/**
* 常用操作
*/
public class Operations {
public static EsbScriptVersionDetailV3DTO createScript() {
EsbCreateScriptV3Request req = new EsbCreateScriptV3Request();
req.setContent(SHELL_SCRIPT_CONTENT_BASE64);
req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ));
req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue());
req.setDescription(TestValueGenerator.generateUniqueStrValue("shell_script_desc", 50));
req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50));
req.setType(ScriptTypeEnum.SHELL.getValue());
req.setVersion("v1");
req.setVersionDesc("v1_desc");
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.CREATE_SCRIPT)
.then()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<EsbResp<EsbScriptVersionDetailV3DTO>>() {
})
.getData();
}
public static EsbScriptVersionDetailV3DTO createPublicScript() {
EsbCreatePublicScriptV3Req req = new EsbCreatePublicScriptV3Req();
req.setContent(SHELL_SCRIPT_CONTENT_BASE64);
req.setDescription(TestValueGenerator.generateUniqueStrValue("shell_script_desc", 50));
req.setName(TestValueGenerator.generateUniqueStrValue("shell_script", 50));
req.setType(ScriptTypeEnum.SHELL.getValue());
req.setVersion("v1");
req.setVersionDesc("v1_desc");
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.CREATE_PUBLIC_SCRIPT)
.then()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<EsbResp<EsbScriptVersionDetailV3DTO>>() {
})
.getData();
}
public static void deleteScript(EsbDeleteScriptV3Req req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_SCRIPT)
.then()
.statusCode(200);
}
public static void deleteScriptVersion(EsbDeleteScriptVersionV3Req req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_SCRIPT_VERSION)
.then()
.statusCode(200);
}
public static void deletePublicScript(EsbDeletePublicScriptV3Req req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_PUBLIC_SCRIPT)
.then()
.statusCode(200);
}
public static void deletePublicScriptVersion(EsbDeletePublicScriptVersionV3Req req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_PUBLIC_SCRIPT_VERSION)
.then()
.statusCode(200);
}
public static void deleteDangerousRule(EsbManageDangerousRuleV3Req req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_DANGEROUS_RULE)
.then()
.statusCode(200);
}
public static EsbDangerousRuleV3DTO createDangerousRule() {
EsbCreateDangerousRuleV3Req req = new EsbCreateDangerousRuleV3Req();
req.setExpression("rm ");
req.setDescription(TestValueGenerator.generateUniqueStrValue("dangerous_rule_desc", 50));
req.setScriptTypeList(Arrays.asList(ScriptTypeEnum.SHELL.getValue().byteValue()));
req.setAction(HighRiskGrammarActionEnum.SCAN.getCode());
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.CREATE_DANGEROUS_RULE)
.then()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<EsbResp<EsbDangerousRuleV3DTO>>() {
})
.getData();
}
public static EsbJobExecuteV3DTO fastExecuteScriptTask() {
EsbFastExecuteScriptV3Request req = new EsbFastExecuteScriptV3Request();
req.setBizId(TestProps.DEFAULT_BIZ);
req.setContent("IyEvYmluL2Jhc2gKZWNobyAnMTIzJwo=");
req.setScriptLanguage(1);
req.setAccountAlias(TestProps.DEFAULT_OS_ACCOUNT_ALIAS);
req.setName(buildJobName());
EsbServerV3DTO targetServer = new EsbServerV3DTO();
List<HostDTO> ips = new ArrayList<>();
ips.add(TestProps.HOST_1_DEFAULT_BIZ);
ips.add(TestProps.HOST_2_DEFAULT_BIZ);
targetServer.setIps(ips);
req.setTargetServer(targetServer);
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.FAST_EXECUTE_SCRIPT)
.then()
.extract()
.as(new TypeRef<EsbResp<EsbJobExecuteV3DTO>>() {
})
.getData();
}
public static EsbJobExecuteV3DTO fastTransferFileTask() {
EsbFastTransferFileV3Request req = new EsbFastTransferFileV3Request();
req.setBizId(TestProps.DEFAULT_BIZ);
req.setAccountAlias(TestProps.DEFAULT_OS_ACCOUNT_ALIAS);
req.setTargetPath("/tmp/");
req.setFileSources(buildCommonFileSource());
req.setTargetServer(buildCommonTargetServer());
req.setName(buildJobName());
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.FAST_TRANSFER_FILE)
.then()
.extract()
.as(new TypeRef<EsbResp<EsbJobExecuteV3DTO>>() {
})
.getData();
}
private static List<EsbFileSourceV3DTO> buildCommonFileSource() {
List<EsbFileSourceV3DTO> fileSources = new ArrayList<>();
EsbFileSourceV3DTO fileSource = new EsbFileSourceV3DTO();
fileSource.setAccount(buildCommonFileSourceAccount());
List<String> files = new ArrayList<>();
files.add("/tmp/1.log");
fileSource.setFiles(files);
fileSource.setServer(buildCommonSourceServer());
fileSources.add(fileSource);
return fileSources;
}
private static EsbAccountV3BasicDTO buildCommonFileSourceAccount() {
EsbAccountV3BasicDTO account = new EsbAccountV3BasicDTO();
account.setAlias(TestProps.DEFAULT_OS_ACCOUNT_ALIAS);
return account;
}
private static EsbServerV3DTO buildCommonSourceServer() {
EsbServerV3DTO sourceServer = new EsbServerV3DTO();
List<HostDTO> sourceIps = new ArrayList<>();
sourceIps.add(TestProps.HOST_1_DEFAULT_BIZ);
sourceServer.setIps(sourceIps);
return sourceServer;
}
private static EsbServerV3DTO buildCommonTargetServer() {
EsbServerV3DTO targetServer = new EsbServerV3DTO();
List<HostDTO> sourceIps = new ArrayList<>();
sourceIps.add(TestProps.HOST_2_DEFAULT_BIZ);
targetServer.setIps(sourceIps);
return targetServer;
}
public static EsbJobExecuteV3DTO fastExecuteFailScriptTask() {
EsbFastExecuteScriptV3Request req = new EsbFastExecuteScriptV3Request();
req.setBizId(TestProps.DEFAULT_BIZ);
// #!/bin/bash
// exit 1
req.setContent("IyEvYmluL2Jhc2gKZXhpdCAxCg==");
req.setScriptLanguage(1);
req.setAccountAlias(TestProps.DEFAULT_OS_ACCOUNT_ALIAS);
req.setName(buildJobName());
EsbServerV3DTO targetServer = new EsbServerV3DTO();
List<HostDTO> ips = new ArrayList<>();
ips.add(TestProps.HOST_1_DEFAULT_BIZ);
targetServer.setIps(ips);
req.setTargetServer(targetServer);
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.FAST_EXECUTE_SCRIPT)
.then()
.extract()
.as(new TypeRef<EsbResp<EsbJobExecuteV3DTO>>() {
})
.getData();
}
public static EsbJobExecuteV3DTO fastExecuteLongTimeScriptTask() {
EsbFastExecuteScriptV3Request req = new EsbFastExecuteScriptV3Request();
req.setBizId(TestProps.DEFAULT_BIZ);
// #!/bin/bash
// sleep 100
req.setContent("IyEvYmluL2Jhc2gKc2xlZXAgMTAwCg==");
req.setScriptLanguage(1);
req.setAccountAlias(TestProps.DEFAULT_OS_ACCOUNT_ALIAS);
req.setName("api-test-long-time-task." + DateUtils.formatLocalDateTime(LocalDateTime.now(),
"yyyyMMddhhmmssSSS"));
EsbServerV3DTO targetServer = new EsbServerV3DTO();
List<HostDTO> ips = new ArrayList<>();
ips.add(TestProps.HOST_1_DEFAULT_BIZ);
targetServer.setIps(ips);
req.setTargetServer(targetServer);
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.FAST_EXECUTE_SCRIPT)
.then()
.extract()
.as(new TypeRef<EsbResp<EsbJobExecuteV3DTO>>() {
})
.getData();
}
public static EsbJobExecuteV3DTO executePlanTask() {
EsbExecuteJobV3Request request = new EsbExecuteJobV3Request();
request.setAppId(TestProps.DEFAULT_BIZ);
// request.setTaskId(TASK_PLAN_ID);
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(request))
.post(APIV3Urls.EXECUTE_JOB_PLAN)
.then()
.extract()
.as(new TypeRef<EsbResp<EsbJobExecuteV3DTO>>() {
})
.getData();
}
public static EsbCronInfoV3DTO createCron() {
Long planId = getTaskPlanId();
if (planId == null) {
return null;
}
EsbSaveCronV3Request req = new EsbSaveCronV3Request();
req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ));
req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue());
req.setName(TestValueGenerator.generateUniqueStrValue("cron_task", 50));
req.setCronExpression("* * * * *");
req.setPlanId(planId);
return given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.SAVE_CRON)
.then()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<EsbResp<EsbCronInfoV3DTO>>() {
})
.getData();
}
public static void deleteCron(EsbDeleteCronV3Request req) {
given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.DELETE_CRON)
.then()
.statusCode(200);
}
public static Long getTaskPlanId() {
EsbGetPlanListV3Request req = new EsbGetPlanListV3Request();
req.setScopeId(String.valueOf(TestProps.DEFAULT_BIZ));
req.setScopeType(ResourceScopeTypeEnum.BIZ.getValue());
req.setLength(1);
EsbPageDataV3<EsbPlanBasicInfoV3DTO> data = given()
.spec(ApiUtil.requestSpec(TestProps.DEFAULT_TEST_USER))
.body(JsonUtil.toJson(req))
.post(APIV3Urls.GET_JOB_PLAN_LIST)
.then()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<EsbResp<EsbPageDataV3<EsbPlanBasicInfoV3DTO>>>() {
})
.getData();
List<EsbPlanBasicInfoV3DTO> planList = data.getData();
if (planList != null && planList.size() > 0) {
EsbPlanBasicInfoV3DTO planBasicInfoV3DTO = planList.get(0);
return planBasicInfoV3DTO.getId();
}
return null;
}
private static String buildJobName() {
return "api.test." + DateUtils.formatLocalDateTime(LocalDateTime.now(), "yyyyMMddhhmmssSSS");
}
}