-
Notifications
You must be signed in to change notification settings - Fork 8k
/
upgrade_testing.js
172 lines (137 loc) · 4.68 KB
/
upgrade_testing.js
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const omit = require('lodash/omit');
const execa = require('execa');
const reduce = require('lodash/reduce');
const find = require('lodash/find');
const pRetry = require('p-retry');
const axios = require('axios');
const getConfig = require('./upgrade_testing_config');
const CLOUD_API_KEY = process.env.CLOUD_API_KEY_SECRET;
const request = axios.create({
baseURL: 'https://staging.found.no/api/v1/',
timeout: 10000,
headers: { Authorization: `ApiKey ${CLOUD_API_KEY}` },
});
const createInstance = async () => {
console.log('CLOUD_API_KEY', CLOUD_API_KEY);
let response;
try {
response = await request.post('deployments?validate_only=false', getConfig('7.15.0'));
} catch (error) {
console.log('error', error);
throw error;
// throw new pRetry.AbortError(error);
}
if (response.status === 404) {
throw new pRetry.AbortError(response.statusText);
}
return response;
};
const isInstanceReady = async ({ deploymentId }) => {
console.log('isInstanceReady');
const clusterInfo = await request.get(`/deployments/${deploymentId}`);
// console.log('clusterInfo', clusterInfo.data);
if (!clusterInfo.data?.healthy) {
throw new Error('Instance not ready yet');
}
return clusterInfo.data;
};
const upgradeInstance = async ({ deploymentId }) => {
console.log('upgradeInstance');
const clusterInfo = await isInstanceReady({ deploymentId });
const newConfig = {
prune_orphans: false,
resources: reduce(
clusterInfo.resources,
(acc, resource, key) => {
if (!resource.length) {
acc[key] = resource;
return acc;
}
// return value
acc[key] = resource.map((resourceItem) => ({
...omit(resourceItem, ['info']),
plan: {
...resourceItem.info.plan_info.current.plan,
[key]: {
...resourceItem.info.plan_info.current.plan[key],
version: '7.16.0',
},
},
}));
return acc;
},
{}
),
};
const response = await request.put(`deployments/${deploymentId}?validate_only=false`, newConfig);
await new Promise((r) => setTimeout(r, 30000));
await pRetry(() => isInstanceReady({ deploymentId }), {
retries: 10,
minTimeout: 30 * 1000,
});
console.log('isREADY!');
return response;
};
const deleteInstance = async (deploymentId) => {
let response;
try {
response = await request.post(`deployments/${deploymentId}/_shutdown`);
} catch (error) {
console.log('error', error);
throw error;
// throw new pRetry.AbortError(error);
}
return response;
};
(async () => {
const instance = await createInstance();
console.log('instance', instance.data);
const deploymentId = instance.data.id;
const credentials = find(instance.data.resources, { kind: 'elasticsearch' }).credentials;
console.log('credentials', credentials);
const clusterInfo = await pRetry(() => isInstanceReady({ deploymentId }), {
retries: 10,
minTimeout: 30 * 1000,
});
const resources = {
elasticsearch: clusterInfo.resources.elasticsearch[0].info.metadata.endpoint,
kibana: clusterInfo.resources.kibana[0].info.metadata.endpoint,
};
const baseCommand = `CYPRESS_BASE_URL=https://${credentials.username}:${credentials.password}@${resources.kibana}:9243 CYPRESS_ELASTICSEARCH_URL=https://${credentials.username}:${credentials.password}@${resources.elasticsearch}:9243 CYPRESS_ELASTICSEARCH_USERNAME=${credentials.username} CYPRESS_ELASTICSEARCH_PASSWORD=${credentials.password} yarn --cwd x-pack/plugins/security_solution cypress:run:cloud-upgrade`;
console.log('command', baseCommand);
try {
await execa.command(
`${baseCommand} --spec cypress/cloud_upgrade_integration/trusted_apps/trusted_apps_pre.spec.ts`,
{
shell: true,
}
);
} catch (error) {
console.error('error', error);
await deleteInstance(deploymentId);
}
await upgradeInstance({ deploymentId });
await pRetry(() => isInstanceReady({ deploymentId }), {
retries: 10,
minTimeout: 30 * 1000,
});
try {
await execa.command(
`${baseCommand} --spec cypress/cloud_upgrade_integration/trusted_apps/trusted_apps_post.spec.ts`,
{
shell: true,
}
);
} catch (error) {
console.error('error', error);
await deleteInstance(deploymentId);
}
await deleteInstance(deploymentId);
})();