Skip to content

Commit fd17b7a

Browse files
authored
[Scout] update fleet tests and fleet api service (elastic#250451)
## Summary This PR makes a few changes in Scout `FleetApiService`: adds: - `package_policies.create: (body: PackagePolicyCreateBody, queryParams?: Record<string, string>)` updates: - `agent_policies.create` with a new argument `(options: AgentPolicyCreateOptions)` - `agent_policies.update` with a new argument `(options: AgentPolicyUpdateOptions)` - fixes `package_policies.get` implementation to accept `queryParams ` argument. It also update fleet, apm and profiling scout tests according the updates. Global loading indicator check was removed from Fleet UI tests, it is deprecated and often leads to flakiness. closes elastic#250273
1 parent 3e1b364 commit fd17b7a

10 files changed

Lines changed: 143 additions & 101 deletions

File tree

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/apis/fleet/index.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import type { KbnClient, ScoutLogger } from '../../../../../../common';
1111
import { measurePerformanceAsync } from '../../../../../../common';
1212
import type {
13-
AgentPolicyCreateBody,
13+
AgentPolicyCreateOptions,
14+
AgentPolicyUpdateOptions,
1415
BulkGetBody,
1516
FleetOutputBody,
1617
FleetServerHostCreateBody,
17-
AgentPolicyUpdateBody,
18+
PackagePolicyCreateBody,
1819
} from './types';
1920

2021
export interface FleetApiService {
@@ -28,24 +29,14 @@ export interface FleetApiService {
2829
package_policies: {
2930
get: (queryParams?: Record<string, any>) => Promise<any>;
3031
getById: (id: string) => Promise<any>;
32+
create: (body: PackagePolicyCreateBody, queryParams?: Record<string, string>) => Promise<any>;
3133
delete: (id: string) => Promise<any>;
3234
bulkDelete: (ids: [string]) => Promise<any>;
3335
};
3436
agent_policies: {
3537
get: (queryParams?: Record<string, any>) => Promise<any>;
36-
create: (
37-
policyName: string,
38-
policyNamespace: string,
39-
sysMonitoring?: boolean,
40-
params?: AgentPolicyCreateBody
41-
) => Promise<any>;
42-
update: (
43-
policyName: string,
44-
policyNamespace: string,
45-
agentPolicyId: string,
46-
params?: AgentPolicyUpdateBody,
47-
queryParams?: Record<string, string>
48-
) => Promise<any>;
38+
create: (options: AgentPolicyCreateOptions) => Promise<any>;
39+
update: (options: AgentPolicyUpdateOptions) => Promise<any>;
4940
bulkGet: (
5041
bulkGetIds: string[],
5142
params?: BulkGetBody,
@@ -132,14 +123,15 @@ export const getFleetApiHelper = (log: ScoutLogger, kbnClient: KbnClient): Fleet
132123
},
133124
},
134125
package_policies: {
135-
get: async () => {
126+
get: async (queryParams?: Record<string, any>) => {
136127
return await measurePerformanceAsync(log, `fleetApi.package_policies.get`, async () => {
137128
return await kbnClient.request({
138129
method: 'GET',
139130
path: '/api/fleet/package_policies',
140131
headers: {
141132
'Content-Type': 'application/json',
142133
},
134+
query: queryParams,
143135
});
144136
});
145137
},
@@ -155,6 +147,23 @@ export const getFleetApiHelper = (log: ScoutLogger, kbnClient: KbnClient): Fleet
155147
}
156148
);
157149
},
150+
create: async (body: PackagePolicyCreateBody, queryParams?: Record<string, string>) => {
151+
return await measurePerformanceAsync(
152+
log,
153+
`fleetApi.package_policies.create [${body.name}]`,
154+
async () => {
155+
return await kbnClient.request({
156+
method: 'POST',
157+
path: `/api/fleet/package_policies`,
158+
headers: {
159+
'Content-Type': 'application/json',
160+
},
161+
query: queryParams,
162+
body,
163+
});
164+
}
165+
);
166+
},
158167
delete: async (id: string) => {
159168
return await measurePerformanceAsync(
160169
log,
@@ -193,12 +202,12 @@ export const getFleetApiHelper = (log: ScoutLogger, kbnClient: KbnClient): Fleet
193202
});
194203
});
195204
},
196-
create: async (
197-
policyName: string,
198-
policyNamespace: string,
199-
sysMonitoring?: boolean,
200-
params?: AgentPolicyCreateBody
201-
) => {
205+
create: async ({
206+
policyName,
207+
policyNamespace,
208+
sysMonitoring,
209+
params,
210+
}: AgentPolicyCreateOptions) => {
202211
return await measurePerformanceAsync(
203212
log,
204213
`fleetApi.agent_policies.create [${policyName}]`,
@@ -222,13 +231,13 @@ export const getFleetApiHelper = (log: ScoutLogger, kbnClient: KbnClient): Fleet
222231
);
223232
},
224233

225-
update: async (
226-
policyName: string,
227-
policyNamespace: string,
228-
agentPolicyId: string,
229-
params?: AgentPolicyUpdateBody,
230-
queryParams?: Record<string, string>
231-
) => {
234+
update: async ({
235+
policyName,
236+
policyNamespace,
237+
agentPolicyId,
238+
params,
239+
queryParams,
240+
}: AgentPolicyUpdateOptions) => {
232241
return await measurePerformanceAsync(
233242
log,
234243
`fleetApi.agent_policies.update [${agentPolicyId}]`,

src/platform/packages/shared/kbn-scout/src/playwright/fixtures/scope/worker/apis/fleet/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ export interface AgentPolicyCreateBody {
4848
unenroll_timeout?: number;
4949
}
5050

51+
export interface AgentPolicyCreateOptions {
52+
policyName: string;
53+
policyNamespace: string;
54+
sysMonitoring?: boolean;
55+
params?: AgentPolicyCreateBody;
56+
}
57+
58+
export interface PackagePolicyCreateBody {
59+
policy_ids: string[];
60+
package: {
61+
name: string;
62+
version?: string;
63+
};
64+
name: string;
65+
description?: string;
66+
namespace: string;
67+
inputs: Record<string, any>;
68+
}
69+
5170
export interface FleetOutputBody {
5271
allow_edit?: string[];
5372
ca_sha256?: string;
@@ -166,3 +185,11 @@ export interface AgentPolicyUpdateBody {
166185
supports_agentless?: boolean;
167186
unenroll_timeout?: number;
168187
}
188+
189+
export interface AgentPolicyUpdateOptions {
190+
policyName: string;
191+
policyNamespace: string;
192+
agentPolicyId: string;
193+
params?: AgentPolicyUpdateBody;
194+
queryParams?: Record<string, string>;
195+
}

src/platform/packages/shared/kbn-scout/test/scout/api/tests/api_services/fleet.spec.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,14 @@ apiTest.describe('Fleet Agent Policies Management', { tag: ['@svlSecurity', '@es
7979
const paramsPolicyNamespace = 'default';
8080
const paramsPolicyName = `${policyName}-params`;
8181

82-
const response = await apiServices.fleet.agent_policies.create(
83-
paramsPolicyName,
84-
paramsPolicyNamespace,
85-
undefined,
86-
{
82+
const response = await apiServices.fleet.agent_policies.create({
83+
policyName: paramsPolicyName,
84+
policyNamespace: paramsPolicyNamespace,
85+
params: {
8786
description: 'Test policy with parameters',
8887
monitoring_enabled: ['logs', 'metrics'],
89-
}
90-
);
88+
},
89+
});
9190

9291
expect(response).toHaveStatusCode(200);
9392
expect(response.data.item.name).toBe(paramsPolicyName);
@@ -100,22 +99,22 @@ apiTest.describe('Fleet Agent Policies Management', { tag: ['@svlSecurity', '@es
10099
const policyNamespace = 'default';
101100

102101
// First create a policy
103-
const createResponse = await apiServices.fleet.agent_policies.create(
102+
const createResponse = await apiServices.fleet.agent_policies.create({
104103
policyName,
105-
policyNamespace
106-
);
104+
policyNamespace,
105+
});
107106
policyId = createResponse.data.item.id;
108107

109108
// Then update it
110109
const updatedName = `${policyName}-updated`;
111-
const updateResponse = await apiServices.fleet.agent_policies.update(
112-
updatedName,
110+
const updateResponse = await apiServices.fleet.agent_policies.update({
111+
policyName: updatedName,
113112
policyNamespace,
114-
policyId,
115-
{
113+
agentPolicyId: policyId,
114+
params: {
116115
description: 'Updated policy description',
117-
}
118-
);
116+
},
117+
});
119118

120119
expect(updateResponse).toHaveStatusCode(200);
121120
expect(updateResponse.data.item.name).toBe(updatedName);
@@ -126,8 +125,14 @@ apiTest.describe('Fleet Agent Policies Management', { tag: ['@svlSecurity', '@es
126125
const policy1Name = `bulk-test-1-${Date.now()}`;
127126
const policy2Name = `bulk-test-2-${Date.now()}`;
128127

129-
const policy1Response = await apiServices.fleet.agent_policies.create(policy1Name, 'default');
130-
const policy2Response = await apiServices.fleet.agent_policies.create(policy2Name, 'default');
128+
const policy1Response = await apiServices.fleet.agent_policies.create({
129+
policyName: policy1Name,
130+
policyNamespace: 'default',
131+
});
132+
const policy2Response = await apiServices.fleet.agent_policies.create({
133+
policyName: policy2Name,
134+
policyNamespace: 'default',
135+
});
131136

132137
const policyIds = [policy1Response.data.item.id, policy2Response.data.item.id];
133138
// Bulk get the policies
@@ -143,7 +148,10 @@ apiTest.describe('Fleet Agent Policies Management', { tag: ['@svlSecurity', '@es
143148

144149
apiTest('should delete an agent policy', async ({ apiServices }) => {
145150
// First create a policy
146-
const createResponse = await apiServices.fleet.agent_policies.create(policyName, 'default');
151+
const createResponse = await apiServices.fleet.agent_policies.create({
152+
policyName,
153+
policyNamespace: 'default',
154+
});
147155
const agentPolicyId = createResponse.data.item.id;
148156

149157
// Then delete it
@@ -154,7 +162,10 @@ apiTest.describe('Fleet Agent Policies Management', { tag: ['@svlSecurity', '@es
154162

155163
apiTest('should delete an agent policy with force flag', async ({ apiServices }) => {
156164
// First create a policy
157-
const createResponse = await apiServices.fleet.agent_policies.create(policyName, 'default');
165+
const createResponse = await apiServices.fleet.agent_policies.create({
166+
policyName,
167+
policyNamespace: 'default',
168+
});
158169
const agentPolicyId = createResponse.data.item.id;
159170

160171
// Then delete it with force
@@ -252,7 +263,8 @@ apiTest.describe('Fleet Server Hosts Management', { tag: ['@svlSecurity', '@ess'
252263
apiTest('should get fleet server hosts', async ({ apiServices }) => {
253264
// Note: The get method doesn't return a value in current implementation
254265
// This test verifies it doesn't throw an error
255-
await apiServices.fleet.server_hosts.get();
266+
const resp = await apiServices.fleet.server_hosts.get();
267+
expect(resp.status).toBe(200);
256268
});
257269

258270
apiTest('should create a fleet server host with parameters', async ({ apiServices }) => {

x-pack/platform/plugins/shared/fleet/test/scout/ui/fixtures/page_objects/copy_integration_page.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export class CopyIntegrationPage {
1515
}
1616

1717
async waitForPageToLoad() {
18-
await this.page.waitForLoadingIndicatorHidden();
18+
await this.page.testSubj.waitForSelector('createPackagePolicy_page', {
19+
state: 'visible',
20+
timeout: 20_000,
21+
});
1922
}
2023

2124
getPackagePolicyNameInput() {

x-pack/platform/plugins/shared/fleet/test/scout/ui/fixtures/page_objects/create_integration_landing_page.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export class CreateIntegrationLandingPage {
2323
}
2424

2525
async waitForPageToLoad() {
26-
await this.page.waitForLoadingIndicatorHidden();
26+
await this.page
27+
.getByTestId('kbnAppWrapper visibleChrome')
28+
.getByTestId('kbnRedirectAppLink')
29+
.waitFor({ state: 'visible', timeout: 20_000 });
2730
}
2831

2932
getLicensePaywallCard() {

x-pack/platform/plugins/shared/fleet/test/scout/ui/fixtures/page_objects/fleet_home.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export class FleetHomePage {
1818
}
1919

2020
async waitForPageToLoad() {
21-
await this.page.waitForLoadingIndicatorHidden();
22-
await this.page.testSubj.waitForSelector(FLEET_AGENTS_TAB_SELECTOR, { state: 'visible' });
21+
await this.page.testSubj.waitForSelector(FLEET_AGENTS_TAB_SELECTOR, {
22+
state: 'visible',
23+
timeout: 20_000,
24+
});
2325
await this.page.testSubj.waitForSelector(FLEET_SETUP_LOADING_SELECTOR, { state: 'hidden' });
2426
}
2527

x-pack/platform/plugins/shared/fleet/test/scout/ui/fixtures/page_objects/integration_home.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export class IntegrationHomePage {
1616
}
1717

1818
async waitForPageToLoad() {
19-
await this.page.waitForLoadingIndicatorHidden();
20-
await this.page.testSubj.waitForSelector('epmList.integrationCards', { state: 'visible' });
19+
await this.page.testSubj.waitForSelector('epmList.integrationCards', {
20+
state: 'visible',
21+
timeout: 20_000,
22+
});
2123
}
2224

2325
getIntegrationCard(integration: string) {

x-pack/platform/plugins/shared/fleet/test/scout/ui/tests/copy_integration.spec.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,18 @@ test.describe('Copy integration', { tag: ['@ess'] }, () => {
1515
let agentPolicyId: string;
1616
let packagePolicyId: string;
1717

18-
test.beforeAll(async ({ kbnClient }) => {
19-
const agentPolicyResponse = await kbnClient.request<{ item: { id: string } }>({
20-
method: 'POST',
21-
path: '/api/fleet/agent_policies',
22-
body: {
23-
name: testAgentPolicyName,
24-
namespace: 'default',
18+
test.beforeAll(async ({ apiServices }) => {
19+
const agentPolicyResponse = await apiServices.fleet.agent_policies.create({
20+
policyName: testAgentPolicyName,
21+
policyNamespace: 'default',
22+
params: {
2523
monitoring_enabled: ['logs', 'metrics'],
2624
},
2725
});
2826
agentPolicyId = agentPolicyResponse.data.item.id;
2927

30-
const packagePolicyResponse = await kbnClient.request<{ item: { id: string } }>({
31-
method: 'POST',
32-
path: '/api/fleet/package_policies',
33-
query: {
34-
format: 'simplified',
35-
},
36-
body: {
28+
const packagePolicyResponse = await apiServices.fleet.package_policies.create(
29+
{
3730
policy_ids: [agentPolicyId],
3831
package: {
3932
name: 'nginx',
@@ -84,11 +77,14 @@ test.describe('Copy integration', { tag: ['@ess'] }, () => {
8477
},
8578
},
8679
},
87-
});
80+
{
81+
format: 'simplified',
82+
}
83+
);
8884
packagePolicyId = packagePolicyResponse.data.item.id;
8985
});
9086

91-
test.afterAll(async ({ kbnClient }) => {
87+
test.afterAll(async ({ apiServices, kbnClient }) => {
9288
if (packagePolicyId) {
9389
await kbnClient.request({
9490
method: 'POST',
@@ -99,14 +95,8 @@ test.describe('Copy integration', { tag: ['@ess'] }, () => {
9995
});
10096
}
10197

102-
const packagePoliciesResponse = await kbnClient.request<{
103-
items: Array<{ id: string; name: string }>;
104-
}>({
105-
method: 'GET',
106-
path: '/api/fleet/package_policies',
107-
query: {
108-
kuery: `ingest-package-policies.name:${packagePolicyName}*`,
109-
},
98+
const packagePoliciesResponse = await apiServices.fleet.package_policies.get({
99+
kuery: `ingest-package-policies.name:${packagePolicyName}*`,
110100
});
111101
for (const policy of packagePoliciesResponse.data.items) {
112102
if (policy.name.startsWith(packagePolicyName)) {
@@ -121,13 +111,7 @@ test.describe('Copy integration', { tag: ['@ess'] }, () => {
121111
}
122112

123113
if (agentPolicyId) {
124-
await kbnClient.request({
125-
method: 'POST',
126-
path: '/api/fleet/agent_policies/delete',
127-
body: {
128-
agentPolicyId,
129-
},
130-
});
114+
await apiServices.fleet.agent_policies.delete(agentPolicyId);
131115
}
132116
});
133117

0 commit comments

Comments
 (0)