Skip to content

Commit c218f31

Browse files
committed
[profiling] fix failing profiling tests due to [fleet-agent-policies/policy-elastic-agent-on-cloud] not found (#268428)
## Summary Closes #268400 Closes #268399 Profiling's `setupResources` POST takes the cloud branch (Scout's stateful Kibana runs with `xpack.cloud.id` set), so it tries to create the `profiler_collector` and `profiler_symbolizer` Fleet package policies on the `policy-elastic-agent-on-cloud` agent policy, which only `has_no_setup.spec.ts` provisions via `profilingHelper.installPolicies()`. When the UX Scout tests ran first, the worker order/state shifted just enough that `has_setup_apm_not_installed.spec.ts`'s `beforeAll` reached `setupResources` before `has_no_setup.spec.ts` had created the agent policy, so Fleet's `packagePolicyClient.create` blew up with `Saved object [fleet-agent-policies/policy-elastic-agent-on-cloud] not found`, which the route surfaced as the opaque 500. The fix makes each spec self-provision the agent policy instead of relying on test ordering. Tested locally: ``` # start servers node scripts/scout start-server --arch stateful --domain classic # run ux tests first npx playwright test --project local --grep @local-stateful-classic --config x-pack/solutions/observability/plugins/ux/test/scout/ui/playwright.config.ts # run profiling tests npx playwright test --project local --grep @local-stateful-classic --config x-pack/solutions/observability/plugins/profiling/test/scout/api/playwright.config.ts ``` Before the fix the following error occured: ``` proc [kibana] [2026-05-08T14:05:35.889+02:00][ERROR][plugins.profiling] Error: Saved object [fleet-agent-policies/policy-elastic-agent-on-cloud] not found proc [kibana] at SavedObjectsErrorHelpers.createGenericNotFoundError (saved_objects_error_helpers.ts:284:28) proc [kibana] at agent_policy.ts:752:40 proc [kibana] at processTicksAndRejections (node:internal/process/task_queues:104:5) proc [kibana] at AgentPolicyService.get (agent_policy.ts:747:27) proc [kibana] at PackagePolicyClientImpl.create (package_policy.ts:547:27) proc [kibana] at createCollectorPackagePolicy (fleet_policies.ts:89:3) proc [kibana] at async Promise.all (index 0) proc [kibana] at setupCloud (setup_cloud.ts:36:3) proc [kibana] at route.ts:139:11 proc [kibana] at handle (route.ts:161:26) proc [kibana] at handler (route.ts:78:14) proc [kibana] at Router.handle (router.ts:209:30) proc [kibana] at exports.Manager.execute (/Users/dmle/elastic/github/kibana/node_modules/@hapi/hapi/lib/toolkit.js:60:28) proc [kibana] at Object.internals.handler (/Users/dmle/elastic/github/kibana/node_modules/@hapi/hapi/lib/handler.js:46:20) proc [kibana] at exports.execute (/Users/dmle/elastic/github/kibana/node_modules/@hapi/hapi/lib/handler.js:31:20) proc [kibana] at Request._lifecycle (/Users/dmle/elastic/github/kibana/node_modules/@hapi/hapi/lib/request.js:384:32) proc [kibana] at Request._execute (/Users/dmle/elastic/github/kibana/node_modules/@hapi/hapi/lib/request.js:294:9) {"service":{"version":"9.5.0","type":"kibana","state":"available","node":{"roles":["background_tasks","ui"]},"id":"_jN54-qzRc2aypW_5PVHdw"}} ``` (cherry picked from commit 40f2b94)
1 parent c1592a5 commit c218f31

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

x-pack/solutions/observability/packages/kbn-scout-oblt/src/playwright/fixtures/worker/profiling/profiling_setup_fixture.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ export const profilingSetupFixture = base.extend<{}, { profilingSetup: Profiling
4747
'content-type': 'application/json',
4848
'kbn-xsrf': 'reporting',
4949
},
50+
// The route is not idempotent on retry; we want a fast, clear failure so the
51+
// underlying error (logged in Kibana by handleRouteHandlerError) is the first
52+
// thing the developer sees instead of a series of 500s.
53+
retries: 0,
5054
});
5155
log.info('Profiling resources set up successfully');
52-
} catch (error) {
53-
log.error(`Error setting up profiling resources: ${error}`);
56+
} catch (error: any) {
57+
const status = error?.response?.status ?? error?.originalError?.response?.status;
58+
const body = error?.response?.data ?? error?.originalError?.response?.data;
59+
log.error(
60+
`Error setting up profiling resources POST /api/profiling/setup/es_resources: status=${status} body=${JSON.stringify(
61+
body
62+
)}`
63+
);
5464
throw error;
5565
}
5666
};
@@ -97,20 +107,25 @@ export const profilingSetupFixture = base.extend<{}, { profilingSetup: Profiling
97107
const cleanup = async (): Promise<void> => {
98108
log.info(`Unloading Profiling data`);
99109

100-
const indices = await esClient.cat.indices({ format: 'json' });
110+
const ignoreNotFound = (error: any) => {
111+
if (error?.meta?.statusCode === 404) return undefined;
112+
throw error;
113+
};
101114

102-
const profilingIndices = indices
103-
.filter((index) => index.index !== undefined)
115+
const profilingIndices = (await esClient.cat.indices({ format: 'json' }))
104116
.map((index) => index.index)
105-
.filter((index) => {
106-
return index!.startsWith('profiling') || index!.startsWith('.profiling');
107-
}) as string[];
117+
.filter(
118+
(name): name is string =>
119+
!!name && (name.startsWith('profiling') || name.startsWith('.profiling'))
120+
);
108121

109122
await Promise.all([
110-
...profilingIndices.map((index) => esClient.indices.delete({ index })),
111-
esClient.indices.deleteDataStream({
112-
name: 'profiling-events*',
113-
}),
123+
...profilingIndices.map((index) =>
124+
esClient.indices.delete({ index, ignore_unavailable: true }).catch(ignoreNotFound)
125+
),
126+
// 'profiling-events*' may not exist on a fresh cluster; tolerate the 404 so we
127+
// do not abort the whole Promise.all and leave preceding cleanup work half-done.
128+
esClient.indices.deleteDataStream({ name: 'profiling-events*' }).catch(ignoreNotFound),
114129
]);
115130
log.info('Unloaded Profiling data');
116131
};

x-pack/solutions/observability/plugins/profiling/test/scout/api/tests/has_setup_apm_not_installed.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ apiTest.describe(
1717
() => {
1818
let viewerApiCreditials: RoleApiCredentials;
1919
let adminApiCreditials: RoleApiCredentials;
20-
apiTest.beforeAll(async ({ profilingSetup, requestAuth }) => {
20+
apiTest.beforeAll(async ({ profilingHelper, profilingSetup, requestAuth }) => {
21+
// Ensure the agent policy that the cloud setup attaches the profiler_collector and
22+
// profiler_symbolizer package policies to exists. Without this, setupResources()
23+
// returns 500 when this spec runs ahead of (or independently of) has_no_setup.spec.
24+
await profilingHelper.installPolicies();
25+
2126
if (!(await profilingSetup.checkStatus()).has_setup) {
2227
await profilingSetup.setupResources();
2328
}

x-pack/solutions/observability/plugins/profiling/test/scout/api/tests/has_setup_with_data.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ import { esArchiversPath, esResourcesEndpoint } from '../../common/fixtures/cons
1414
apiTest.describe('Profiling is setup and data is loaded', { tag: tags.stateful.classic }, () => {
1515
let viewerApiCreditials: RoleApiCredentials;
1616
let adminApiCreditials: RoleApiCredentials;
17-
apiTest.beforeAll(async ({ requestAuth, profilingSetup }) => {
17+
apiTest.beforeAll(async ({ requestAuth, profilingHelper, profilingSetup }) => {
18+
// Make this spec self-sufficient instead of relying on has_no_setup.spec running first:
19+
// ensure the cloud agent policy exists and profiling resources are set up before
20+
// attempting to load data.
21+
await profilingHelper.installPolicies();
22+
23+
if (!(await profilingSetup.checkStatus()).has_setup) {
24+
await profilingSetup.setupResources();
25+
}
26+
1827
await profilingSetup.loadData(esArchiversPath);
1928
viewerApiCreditials = await requestAuth.getApiKey('viewer');
2029
adminApiCreditials = await requestAuth.getApiKey('admin');

0 commit comments

Comments
 (0)