Skip to content

Commit 86cffd2

Browse files
AlexGPlayjcger
authored andcommitted
[Data views/Index Patterns][Scout] Fix set default data view tests (#268370)
## Summary Closes #265664 Closes #265545 Closes #265424 Closes #266542 Closes #265649 Closes #265294 It seems like many tests can run in parallel and target the same default index, at the same time ES can take some time to refresh after setting the new default. To fix this there are 2 main changes, the first one is to poll the GET endpoint until the index matches the new one that was set. The second change is to use a different space for each of the suites so that way they are not affected by other tests. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [x] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels.
1 parent c904e05 commit 86cffd2

2 files changed

Lines changed: 84 additions & 78 deletions

File tree

src/platform/plugins/shared/data_views/test/scout/api/tests/data_views/default_data_view.spec.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10+
import type { ApiClientFixture } from '@kbn/scout';
1011
import { apiTest, tags, type RoleApiCredentials } from '@kbn/scout';
1112
import { expect } from '@kbn/scout/api';
1213
import { COMMON_HEADERS, SERVICE_PATH, SERVICE_KEY } from '../../fixtures/constants';
@@ -16,17 +17,38 @@ apiTest.describe(
1617
{ tag: tags.deploymentAgnostic },
1718
() => {
1819
let adminApiCredentials: RoleApiCredentials;
20+
const testSpaceId = `default-data-view-${Date.now()}-${Math.random().toString(36).slice(2)}`;
1921

2022
const newId = () => `default-id-${Date.now()}-${Math.random()}`;
2123
const defaultPath = `${SERVICE_PATH}/default`;
2224
const serviceKeyId = `${SERVICE_KEY}_id`;
23-
24-
apiTest.beforeAll(async ({ requestAuth }) => {
25+
const scopedPath = (path: string) => `s/${testSpaceId}/${path}`;
26+
27+
function expectDefaultDataView(apiClient: ApiClientFixture, defaultId: string) {
28+
return expect
29+
.poll(async () => {
30+
const getResponse = await apiClient.get(scopedPath(defaultPath), {
31+
headers: {
32+
...COMMON_HEADERS,
33+
...adminApiCredentials.apiKeyHeader,
34+
},
35+
responseType: 'json',
36+
});
37+
return getResponse.body[serviceKeyId];
38+
})
39+
.toBe(defaultId);
40+
}
41+
42+
apiTest.beforeAll(async ({ apiServices, requestAuth }) => {
2543
adminApiCredentials = await requestAuth.getApiKey('admin');
44+
await apiServices.spaces.create({
45+
id: testSpaceId,
46+
name: testSpaceId,
47+
});
2648
});
2749

2850
apiTest.afterEach(async ({ apiClient }) => {
29-
await apiClient.post(defaultPath, {
51+
await apiClient.post(scopedPath(defaultPath), {
3052
headers: {
3153
...COMMON_HEADERS,
3254
...adminApiCredentials.apiKeyHeader,
@@ -39,10 +61,14 @@ apiTest.describe(
3961
});
4062
});
4163

64+
apiTest.afterAll(async ({ apiServices }) => {
65+
await apiServices.spaces.delete(testSpaceId);
66+
});
67+
4268
apiTest('can set default data view', async ({ apiClient }) => {
4369
const defaultId = newId();
4470

45-
const setResponse = await apiClient.post(defaultPath, {
71+
const setResponse = await apiClient.post(scopedPath(defaultPath), {
4672
headers: {
4773
...COMMON_HEADERS,
4874
...adminApiCredentials.apiKeyHeader,
@@ -57,22 +83,13 @@ apiTest.describe(
5783
expect(setResponse).toHaveStatusCode(200);
5884
expect(setResponse.body.acknowledged).toBe(true);
5985

60-
const getResponse = await apiClient.get(defaultPath, {
61-
headers: {
62-
...COMMON_HEADERS,
63-
...adminApiCredentials.apiKeyHeader,
64-
},
65-
responseType: 'json',
66-
});
67-
68-
expect(getResponse).toHaveStatusCode(200);
69-
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
86+
await expectDefaultDataView(apiClient, defaultId);
7087
});
7188

7289
apiTest('does not override existing default without force flag', async ({ apiClient }) => {
7390
const defaultId = newId();
7491

75-
await apiClient.post(defaultPath, {
92+
await apiClient.post(scopedPath(defaultPath), {
7693
headers: {
7794
...COMMON_HEADERS,
7895
...adminApiCredentials.apiKeyHeader,
@@ -84,7 +101,7 @@ apiTest.describe(
84101
},
85102
});
86103

87-
const overrideResponse = await apiClient.post(defaultPath, {
104+
const overrideResponse = await apiClient.post(scopedPath(defaultPath), {
88105
headers: {
89106
...COMMON_HEADERS,
90107
...adminApiCredentials.apiKeyHeader,
@@ -97,22 +114,13 @@ apiTest.describe(
97114

98115
expect(overrideResponse).toHaveStatusCode(200);
99116

100-
const getResponse = await apiClient.get(defaultPath, {
101-
headers: {
102-
...COMMON_HEADERS,
103-
...adminApiCredentials.apiKeyHeader,
104-
},
105-
responseType: 'json',
106-
});
107-
108-
expect(getResponse).toHaveStatusCode(200);
109-
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
117+
await expectDefaultDataView(apiClient, defaultId);
110118
});
111119

112120
apiTest('can clear default data view with force flag', async ({ apiClient }) => {
113121
const defaultId = newId();
114122

115-
await apiClient.post(defaultPath, {
123+
await apiClient.post(scopedPath(defaultPath), {
116124
headers: {
117125
...COMMON_HEADERS,
118126
...adminApiCredentials.apiKeyHeader,
@@ -124,7 +132,7 @@ apiTest.describe(
124132
},
125133
});
126134

127-
const clearResponse = await apiClient.post(defaultPath, {
135+
const clearResponse = await apiClient.post(scopedPath(defaultPath), {
128136
headers: {
129137
...COMMON_HEADERS,
130138
...adminApiCredentials.apiKeyHeader,
@@ -138,16 +146,7 @@ apiTest.describe(
138146

139147
expect(clearResponse).toHaveStatusCode(200);
140148

141-
const getResponse = await apiClient.get(defaultPath, {
142-
headers: {
143-
...COMMON_HEADERS,
144-
...adminApiCredentials.apiKeyHeader,
145-
},
146-
responseType: 'json',
147-
});
148-
149-
expect(getResponse).toHaveStatusCode(200);
150-
expect(getResponse.body[serviceKeyId]).toBe('');
149+
await expectDefaultDataView(apiClient, '');
151150
});
152151
}
153152
);

src/platform/plugins/shared/data_views/test/scout/api/tests/index_patterns/default_index_pattern.spec.ts

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10+
import type { ApiClientFixture } from '@kbn/scout';
1011
import { apiTest, tags, type RoleApiCredentials } from '@kbn/scout';
1112
import { expect } from '@kbn/scout/api';
1213
import { COMMON_HEADERS, SERVICE_PATH_LEGACY, SERVICE_KEY_LEGACY } from '../../fixtures/constants';
@@ -16,17 +17,40 @@ apiTest.describe(
1617
{ tag: tags.deploymentAgnostic },
1718
() => {
1819
let adminApiCredentials: RoleApiCredentials;
20+
const testSpaceId = `default-index-pattern-${Date.now()}-${Math.random()
21+
.toString(36)
22+
.slice(2)}`;
1923

2024
const newId = () => `default-id-${Date.now()}-${Math.random()}`;
2125
const defaultPath = `${SERVICE_PATH_LEGACY}/default`;
2226
const serviceKeyId = `${SERVICE_KEY_LEGACY}_id`;
23-
24-
apiTest.beforeAll(async ({ requestAuth }) => {
27+
const scopedPath = (path: string) => `s/${testSpaceId}/${path}`;
28+
29+
function expectDefaultIndexPattern(apiClient: ApiClientFixture, defaultId: string) {
30+
return expect
31+
.poll(async () => {
32+
const getResponse = await apiClient.get(scopedPath(defaultPath), {
33+
headers: {
34+
...COMMON_HEADERS,
35+
...adminApiCredentials.apiKeyHeader,
36+
},
37+
responseType: 'json',
38+
});
39+
return getResponse.body[serviceKeyId];
40+
})
41+
.toBe(defaultId);
42+
}
43+
44+
apiTest.beforeAll(async ({ apiServices, requestAuth }) => {
2545
adminApiCredentials = await requestAuth.getApiKey('admin');
46+
await apiServices.spaces.create({
47+
id: testSpaceId,
48+
name: testSpaceId,
49+
});
2650
});
2751

2852
apiTest.afterEach(async ({ apiClient }) => {
29-
await apiClient.post(defaultPath, {
53+
await apiClient.post(scopedPath(defaultPath), {
3054
headers: {
3155
...COMMON_HEADERS,
3256
...adminApiCredentials.apiKeyHeader,
@@ -39,10 +63,14 @@ apiTest.describe(
3963
});
4064
});
4165

66+
apiTest.afterAll(async ({ apiServices }) => {
67+
await apiServices.spaces.delete(testSpaceId);
68+
});
69+
4270
apiTest('can set default index pattern', async ({ apiClient }) => {
4371
const defaultId = newId();
4472

45-
const setResponse = await apiClient.post(defaultPath, {
73+
const setResponse = await apiClient.post(scopedPath(defaultPath), {
4674
headers: {
4775
...COMMON_HEADERS,
4876
...adminApiCredentials.apiKeyHeader,
@@ -57,22 +85,14 @@ apiTest.describe(
5785
expect(setResponse).toHaveStatusCode(200);
5886
expect(setResponse.body.acknowledged).toBe(true);
5987

60-
const getResponse = await apiClient.get(defaultPath, {
61-
headers: {
62-
...COMMON_HEADERS,
63-
...adminApiCredentials.apiKeyHeader,
64-
},
65-
responseType: 'json',
66-
});
67-
68-
expect(getResponse).toHaveStatusCode(200);
69-
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
88+
await expectDefaultIndexPattern(apiClient, defaultId);
7089
});
7190

7291
apiTest('does not override existing default without force flag', async ({ apiClient }) => {
7392
const defaultId = newId();
7493

75-
await apiClient.post(defaultPath, {
94+
// Set a default index pattern
95+
await apiClient.post(scopedPath(defaultPath), {
7696
headers: {
7797
...COMMON_HEADERS,
7898
...adminApiCredentials.apiKeyHeader,
@@ -83,8 +103,10 @@ apiTest.describe(
83103
force: true,
84104
},
85105
});
106+
await expectDefaultIndexPattern(apiClient, defaultId);
86107

87-
const overrideResponse = await apiClient.post(defaultPath, {
108+
// Try to override the default index pattern without the force flag
109+
const overrideResponse = await apiClient.post(scopedPath(defaultPath), {
88110
headers: {
89111
...COMMON_HEADERS,
90112
...adminApiCredentials.apiKeyHeader,
@@ -94,25 +116,17 @@ apiTest.describe(
94116
[serviceKeyId]: newId(),
95117
},
96118
});
97-
98119
expect(overrideResponse).toHaveStatusCode(200);
99120

100-
const getResponse = await apiClient.get(defaultPath, {
101-
headers: {
102-
...COMMON_HEADERS,
103-
...adminApiCredentials.apiKeyHeader,
104-
},
105-
responseType: 'json',
106-
});
107-
108-
expect(getResponse).toHaveStatusCode(200);
109-
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
121+
// Verify that the default index pattern is still the same
122+
await expectDefaultIndexPattern(apiClient, defaultId);
110123
});
111124

112125
apiTest('can clear default index pattern with force flag', async ({ apiClient }) => {
113126
const defaultId = newId();
114127

115-
await apiClient.post(defaultPath, {
128+
// Set a default index pattern
129+
await apiClient.post(scopedPath(defaultPath), {
116130
headers: {
117131
...COMMON_HEADERS,
118132
...adminApiCredentials.apiKeyHeader,
@@ -123,8 +137,10 @@ apiTest.describe(
123137
force: true,
124138
},
125139
});
140+
await expectDefaultIndexPattern(apiClient, defaultId);
126141

127-
const clearResponse = await apiClient.post(defaultPath, {
142+
// Clear the default index pattern
143+
const clearResponse = await apiClient.post(scopedPath(defaultPath), {
128144
headers: {
129145
...COMMON_HEADERS,
130146
...adminApiCredentials.apiKeyHeader,
@@ -135,19 +151,10 @@ apiTest.describe(
135151
force: true,
136152
},
137153
});
138-
139154
expect(clearResponse).toHaveStatusCode(200);
140155

141-
const getResponse = await apiClient.get(defaultPath, {
142-
headers: {
143-
...COMMON_HEADERS,
144-
...adminApiCredentials.apiKeyHeader,
145-
},
146-
responseType: 'json',
147-
});
148-
149-
expect(getResponse).toHaveStatusCode(200);
150-
expect(getResponse.body[serviceKeyId]).toBe('');
156+
// Verify that the default index pattern is cleared
157+
await expectDefaultIndexPattern(apiClient, '');
151158
});
152159
}
153160
);

0 commit comments

Comments
 (0)