Skip to content

Commit

Permalink
Adjusted tests to include the artifacts index
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Feb 25, 2021
1 parent c8c8101 commit 045e150
Showing 1 changed file with 18 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,41 @@ import ESFleetPoliciesLeaderIndex from './elasticsearch/fleet_policies_leader.js
import ESFleetServersIndex from './elasticsearch/fleet_servers.json';
import ESFleetEnrollmentApiKeysIndex from './elasticsearch/fleet_enrollment_api_keys.json';
import EsFleetActionsIndex from './elasticsearch/fleet_actions.json';
import EsFleetArtifactsIndex from './elasticsearch/fleet_artifacts.json';
import { FLEET_SERVER_INDICES } from '../../../common';

const FLEET_INDEXES_MIGRATION_HASH = {
const FLEET_INDEXES_MIGRATION_HASH: Record<typeof FLEET_SERVER_INDICES[number], string> = {
'.fleet-actions': hash(EsFleetActionsIndex),
'.fleet-agents': hash(ESFleetAgentIndex),
'.fleet-artifacts': hash(EsFleetArtifactsIndex),
'.fleet-enrollment-apy-keys': hash(ESFleetEnrollmentApiKeysIndex),
'.fleet-policies': hash(ESFleetPoliciesIndex),
'.fleet-policies-leader': hash(ESFleetPoliciesLeaderIndex),
'.fleet-servers': hash(ESFleetServersIndex),
};

const getIndexList = (returnAliases: boolean = false): string[] => {
const response = [...FLEET_SERVER_INDICES];

if (returnAliases) {
return response.sort();
}

return response.map((index) => `${index}_1`).sort();
};

describe('setupFleetServerIndexes ', () => {
it('should create all the indices and aliases if nothings exists', async () => {
const esMock = elasticsearchServiceMock.createInternalClient();
await setupFleetServerIndexes(esMock);

const indexesCreated = esMock.indices.create.mock.calls.map((call) => call[0].index).sort();
expect(indexesCreated).toEqual([
'.fleet-actions_1',
'.fleet-agents_1',
'.fleet-enrollment-api-keys_1',
'.fleet-policies-leader_1',
'.fleet-policies_1',
'.fleet-servers_1',
]);
expect(indexesCreated).toEqual(getIndexList());
const aliasesCreated = esMock.indices.updateAliases.mock.calls
.map((call) => (call[0].body as any)?.actions[0].add.alias)
.sort();

expect(aliasesCreated).toEqual([
'.fleet-actions',
'.fleet-agents',
'.fleet-enrollment-api-keys',
'.fleet-policies',
'.fleet-policies-leader',
'.fleet-servers',
]);
expect(aliasesCreated).toEqual(getIndexList(true));
});

it('should not create any indices and create aliases if indices exists but not the aliases', async () => {
Expand All @@ -63,7 +62,6 @@ describe('setupFleetServerIndexes ', () => {
[params.index]: {
mappings: {
_meta: {
// @ts-expect-error
migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')],
},
},
Expand All @@ -79,14 +77,7 @@ describe('setupFleetServerIndexes ', () => {
.map((call) => (call[0].body as any)?.actions[0].add.alias)
.sort();

expect(aliasesCreated).toEqual([
'.fleet-actions',
'.fleet-agents',
'.fleet-enrollment-api-keys',
'.fleet-policies',
'.fleet-policies-leader',
'.fleet-servers',
]);
expect(aliasesCreated).toEqual(getIndexList(true));
});

it('should put new indices mapping if the mapping has been updated ', async () => {
Expand Down Expand Up @@ -115,14 +106,7 @@ describe('setupFleetServerIndexes ', () => {
.map((call) => call[0].index)
.sort();

expect(indexesMappingUpdated).toEqual([
'.fleet-actions_1',
'.fleet-agents_1',
'.fleet-enrollment-api-keys_1',
'.fleet-policies-leader_1',
'.fleet-policies_1',
'.fleet-servers_1',
]);
expect(indexesMappingUpdated).toEqual(getIndexList());
});

it('should not create any indices or aliases if indices and aliases already exists', async () => {
Expand All @@ -137,7 +121,6 @@ describe('setupFleetServerIndexes ', () => {
[params.index]: {
mappings: {
_meta: {
// @ts-expect-error
migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')],
},
},
Expand Down

0 comments on commit 045e150

Please sign in to comment.