Skip to content

Commit

Permalink
[Profiling] Fix incorrect status of single-click installation (#159168)
Browse files Browse the repository at this point in the history
## Summary

This PR addresses two bugs discovered after
#157949:

* when setting up security roles, the incorrect status was set
* when merging the statuses of all steps, the merged status could lose
some statuses
  • Loading branch information
jbcrail authored Jun 7, 2023
1 parent e52e889 commit 5e907ed
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
155 changes: 155 additions & 0 deletions x-pack/plugins/profiling/common/setup.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 2 additions & 1 deletion x-pack/plugins/profiling/common/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { merge } from 'lodash';
import { RecursivePartial } from '@kbn/apm-plugin/typings/common';

export interface SetupState {
Expand Down Expand Up @@ -97,7 +98,7 @@ export function areResourcesSetup(state: SetupState): boolean {
}

function mergeRecursivePartial<T>(base: T, partial: RecursivePartial<T>): T {
return { ...base, ...partial };
return merge(base, partial);
}

export function mergePartialSetupStates(
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/profiling/server/lib/setup/security_role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand Down

0 comments on commit 5e907ed

Please sign in to comment.