diff --git a/x-pack/plugins/profiling/common/setup.test.ts b/x-pack/plugins/profiling/common/setup.test.ts new file mode 100644 index 0000000000000..91893b160cd67 --- /dev/null +++ b/x-pack/plugins/profiling/common/setup.test.ts @@ -0,0 +1,155 @@ +/* + * 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 { + areResourcesSetup, + createDefaultSetupState, + mergePartialSetupStates, + PartialSetupState, +} from './setup'; + +function createCloudState(available: boolean): PartialSetupState { + return { + cloud: { + available, + }, + }; +} + +function createDataState(available: boolean): PartialSetupState { + return { + data: { + available, + }, + }; +} + +function createPackageState(installed: boolean): PartialSetupState { + return { + packages: { + installed, + }, + }; +} + +function createPermissionState(configured: boolean): PartialSetupState { + return { + permissions: { + configured, + }, + }; +} + +function createApmPolicyState(installed: boolean): PartialSetupState { + return { + policies: { + apm: { + installed, + }, + }, + }; +} + +function createCollectorPolicyState(installed: boolean): PartialSetupState { + return { + policies: { + collector: { + installed, + }, + }, + }; +} + +function createSymbolizerPolicyState(installed: boolean): PartialSetupState { + return { + policies: { + symbolizer: { + installed, + }, + }, + }; +} + +function createResourceState({ + enabled, + created, +}: { + enabled: boolean; + created: boolean; +}): PartialSetupState { + return { + resource_management: { + enabled, + }, + resources: { + created, + }, + }; +} + +function createSettingsState(configured: boolean): PartialSetupState { + return { + settings: { + configured, + }, + }; +} + +describe('Merging partial state operations', () => { + const defaultSetupState = createDefaultSetupState(); + + test('partial states with missing key', () => { + const mergedState = mergePartialSetupStates(defaultSetupState, [ + createCloudState(true), + createDataState(true), + ]); + + expect(mergedState.cloud.available).toEqual(true); + expect(mergedState.cloud.required).toEqual(true); + expect(mergedState.data.available).toEqual(true); + }); + + test('deeply nested partial states with overlap', () => { + const mergedState = mergePartialSetupStates(defaultSetupState, [ + createApmPolicyState(true), + createCollectorPolicyState(true), + createSymbolizerPolicyState(true), + ]); + + expect(mergedState.policies.apm.installed).toEqual(true); + expect(mergedState.policies.collector.installed).toEqual(true); + expect(mergedState.policies.symbolizer.installed).toEqual(true); + }); + + test('check resource status with failed partial states', () => { + const mergedState = mergePartialSetupStates(defaultSetupState, [ + createPackageState(false), + createApmPolicyState(true), + createCollectorPolicyState(true), + createSymbolizerPolicyState(true), + createPermissionState(false), + createResourceState({ enabled: true, created: true }), + createSettingsState(true), + ]); + + expect(areResourcesSetup(mergedState)).toEqual(false); + }); + + test('check resource status with all successful partial states', () => { + const mergedState = mergePartialSetupStates(defaultSetupState, [ + createPackageState(true), + createApmPolicyState(true), + createCollectorPolicyState(true), + createSymbolizerPolicyState(true), + createPermissionState(true), + createResourceState({ enabled: true, created: true }), + createSettingsState(true), + ]); + + expect(areResourcesSetup(mergedState)).toEqual(true); + }); +}); diff --git a/x-pack/plugins/profiling/common/setup.ts b/x-pack/plugins/profiling/common/setup.ts index 4a05162ab537e..0b1dca55b0d5e 100644 --- a/x-pack/plugins/profiling/common/setup.ts +++ b/x-pack/plugins/profiling/common/setup.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { merge } from 'lodash'; import { RecursivePartial } from '@kbn/apm-plugin/typings/common'; export interface SetupState { @@ -97,7 +98,7 @@ export function areResourcesSetup(state: SetupState): boolean { } function mergeRecursivePartial(base: T, partial: RecursivePartial): T { - return { ...base, ...partial }; + return merge(base, partial); } export function mergePartialSetupStates( diff --git a/x-pack/plugins/profiling/server/lib/setup/security_role.ts b/x-pack/plugins/profiling/server/lib/setup/security_role.ts index a6f65e438ce60..62a4d2d6d284f 100644 --- a/x-pack/plugins/profiling/server/lib/setup/security_role.ts +++ b/x-pack/plugins/profiling/server/lib/setup/security_role.ts @@ -16,7 +16,7 @@ export async function validateSecurityRole({ const esClient = client.getEsClient(); const roles = await esClient.security.getRole(); return { - settings: { + permissions: { configured: PROFILING_READER_ROLE_NAME in roles, }, };