-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
es_version_precheck.test.ts
169 lines (143 loc) · 5.45 KB
/
es_version_precheck.test.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
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { SemVer } from 'semver';
import { IScopedClusterClient, kibanaResponseFactory } from '@kbn/core/server';
import { coreMock } from '@kbn/core/server/mocks';
import { licensingMock } from '@kbn/licensing-plugin/server/mocks';
import { getMockVersionInfo } from './__fixtures__/version';
import {
esVersionCheck,
getAllNodeVersions,
verifyAllMatchKibanaVersion,
} from './es_version_precheck';
import { versionService } from './version';
const { currentMajor, currentVersion } = getMockVersionInfo();
// Re-implement the mock that was imported directly from `x-pack/mocks`
function createCoreRequestHandlerContextMock() {
return {
core: coreMock.createRequestHandlerContext(),
licensing: licensingMock.createRequestHandlerContext(),
};
}
const xpackMocks = {
createRequestHandlerContext: createCoreRequestHandlerContextMock,
};
describe('getAllNodeVersions', () => {
it('returns a list of unique node versions', async () => {
const adminClient = {
asInternalUser: {
nodes: {
info: jest.fn().mockResolvedValue({
nodes: {
node1: { version: '7.0.0' },
node2: { version: '7.0.0' },
node3: { version: '6.0.0' },
},
}),
},
},
} as unknown as IScopedClusterClient;
await expect(getAllNodeVersions(adminClient)).resolves.toEqual([
new SemVer('6.0.0'),
new SemVer('7.0.0'),
]);
});
});
describe('verifyAllMatchKibanaVersion', () => {
it('detects higher version nodes', () => {
const result = verifyAllMatchKibanaVersion([new SemVer('99999.0.0')], currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(true);
});
it('detects lower version nodes', () => {
const result = verifyAllMatchKibanaVersion([new SemVer('0.0.0')], currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(true);
});
it('detects if all are on same major correctly', () => {
const versions = [
currentVersion,
currentVersion.inc('minor'),
currentVersion.inc('minor').inc('minor'),
];
const result = verifyAllMatchKibanaVersion(versions, currentMajor);
expect(result.allNodesMatch).toBe(true);
expect(result.allNodesUpgraded).toBe(false);
});
it('detects partial matches', () => {
const versions = [
new SemVer('0.0.0'),
currentVersion.inc('minor'),
currentVersion.inc('minor').inc('minor'),
];
const result = verifyAllMatchKibanaVersion(versions, currentMajor);
expect(result.allNodesMatch).toBe(false);
expect(result.allNodesUpgraded).toBe(false);
});
});
describe('EsVersionPrecheck', () => {
beforeEach(() => {
versionService.setup('8.0.0');
});
it('returns a 403 when callCluster fails with a 403', async () => {
const ctx = xpackMocks.createRequestHandlerContext();
ctx.core.elasticsearch.client.asInternalUser.nodes.info.mockRejectedValue({ statusCode: 403 });
const result = await esVersionCheck(
coreMock.createCustomRequestHandlerContext(ctx),
kibanaResponseFactory
);
expect(result).toHaveProperty('status', 403);
});
it('returns a 426 message w/ allNodesUpgraded = false when nodes are not on same version', async () => {
const ctx = xpackMocks.createRequestHandlerContext();
ctx.core.elasticsearch.client.asInternalUser.nodes.info.mockResponse({
nodes: {
// @ts-expect-error incomplete type
node1: { version: currentVersion.raw },
// @ts-expect-error incomplete type
node2: { version: new SemVer(currentVersion.raw).inc('major').raw },
},
});
const result = await esVersionCheck(
coreMock.createCustomRequestHandlerContext(ctx),
kibanaResponseFactory
);
expect(result).toHaveProperty('status', 426);
expect(result).toHaveProperty('payload.attributes.allNodesUpgraded', false);
});
it('returns a 426 message w/ allNodesUpgraded = true when nodes are on next version', async () => {
const ctx = xpackMocks.createRequestHandlerContext();
ctx.core.elasticsearch.client.asInternalUser.nodes.info.mockResponse({
nodes: {
// @ts-expect-error incomplete type
node1: { version: new SemVer(currentVersion.raw).inc('major').raw },
// @ts-expect-error incomplete type
node2: { version: new SemVer(currentVersion.raw).inc('major').raw },
},
});
const result = await esVersionCheck(
coreMock.createCustomRequestHandlerContext(ctx),
kibanaResponseFactory
);
expect(result).toHaveProperty('status', 426);
expect(result).toHaveProperty('payload.attributes.allNodesUpgraded', true);
});
it('returns undefined when nodes are on same version', async () => {
const ctx = xpackMocks.createRequestHandlerContext();
ctx.core.elasticsearch.client.asInternalUser.nodes.info.mockResponse({
nodes: {
// @ts-expect-error incomplete type
node1: { version: currentVersion.raw },
// @ts-expect-error incomplete type
node2: { version: currentVersion.raw },
},
});
await expect(
esVersionCheck(coreMock.createCustomRequestHandlerContext(ctx), kibanaResponseFactory)
).resolves.toBe(undefined);
});
});