Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Jan 13, 2022
1 parent f687bd6 commit bcf2825
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 48 deletions.
43 changes: 14 additions & 29 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,9 @@ describe('getExistingIndexAliases', () => {

describe('setIndexAliasToHidden', () => {
test('should call cluster with given index name and aliases', async () => {
await clusterClientAdapter.setIndexAliasToHidden('foo-bar-000001', {
aliases: {
'foo-bar': {
is_write_index: true,
},
},
});
await clusterClientAdapter.setIndexAliasToHidden('foo-bar', [
{ alias: 'foo-bar', indexName: 'foo-bar-000001', is_write_index: true },
]);
expect(clusterClient.indices.updateAliases).toHaveBeenCalledWith({
body: {
actions: [
Expand All @@ -477,18 +473,11 @@ describe('setIndexAliasToHidden', () => {
});
});

test('should update multiple aliases at once and preserve existing alias settings', async () => {
await clusterClientAdapter.setIndexAliasToHidden('foo-bar-000001', {
aliases: {
'foo-bar': {
is_write_index: true,
},
'foo-b': {
index_routing: 'index',
routing: 'route',
},
},
});
test('should update multiple indices for an alias at once and preserve existing alias settings', async () => {
await clusterClientAdapter.setIndexAliasToHidden('foo-bar', [
{ alias: 'foo-bar', indexName: 'foo-bar-000001', is_write_index: true },
{ alias: 'foo-bar', indexName: 'foo-bar-000002', index_routing: 'index', routing: 'route' },
]);
expect(clusterClient.indices.updateAliases).toHaveBeenCalledWith({
body: {
actions: [
Expand All @@ -502,8 +491,8 @@ describe('setIndexAliasToHidden', () => {
},
{
add: {
index: 'foo-bar-000001',
alias: 'foo-b',
index: 'foo-bar-000002',
alias: 'foo-bar',
is_hidden: true,
index_routing: 'index',
routing: 'route',
Expand All @@ -517,15 +506,11 @@ describe('setIndexAliasToHidden', () => {
test('should throw error when call cluster throws an error', async () => {
clusterClient.indices.updateAliases.mockRejectedValue(new Error('Fail'));
await expect(
clusterClientAdapter.setIndexAliasToHidden('foo-bar-000001', {
aliases: {
'foo-bar': {
is_write_index: true,
},
},
})
clusterClientAdapter.setIndexAliasToHidden('foo-bar', [
{ alias: 'foo-bar', indexName: 'foo-bar-000001', is_write_index: true },
])
).rejects.toThrowErrorMatchingInlineSnapshot(
`"error setting existing index aliases for index foo-bar-000001 to is_hidden: Fail"`
`"error setting existing index aliases for alias foo-bar to is_hidden: Fail"`
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/event_log/server/es/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getIlmPolicy() {
rollover: {
max_size: '50GB',
max_age: '30d',
max_docs: 1, // you know, for testing
// max_docs: 1, // you know, for testing
},
},
},
Expand Down
100 changes: 85 additions & 15 deletions x-pack/plugins/event_log/server/es/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { contextMock } from './context.mock';
import { initializeEs } from './init';
import { initializeEs, parseIndexAliases } from './init';

describe('initializeEs', () => {
let esContext = contextMock.create();
Expand Down Expand Up @@ -267,11 +267,10 @@ describe('initializeEs', () => {
});

await initializeEs(esContext);
expect(esContext.esAdapter.getExistingIndexAliases).toHaveBeenCalled();
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith(
'foo-bar-000001',
testAliases
);
expect(esContext.esAdapter.getExistingIndexAliases).toHaveBeenCalledTimes(1);
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith('foo-bar', [
{ alias: 'foo-bar', indexName: 'foo-bar-000001', is_write_index: true },
]);
});

test(`should not update existing index aliases if any exist and are already hidden`, async () => {
Expand Down Expand Up @@ -310,6 +309,9 @@ describe('initializeEs', () => {
'foo-bar': {
is_write_index: true,
},
'bar-foo': {
is_write_index: true,
},
},
};
esContext.esAdapter.getExistingIndexAliases.mockResolvedValue({
Expand All @@ -321,17 +323,18 @@ describe('initializeEs', () => {

await initializeEs(esContext);
expect(esContext.esAdapter.getExistingIndexAliases).toHaveBeenCalled();
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith(
'foo-bar-000001',
testAliases
);
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith(
'foo-bar-000002',
testAliases
);
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledTimes(2);
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith('foo-bar', [
{ alias: 'foo-bar', indexName: 'foo-bar-000001', is_write_index: true },
{ alias: 'foo-bar', indexName: 'foo-bar-000002', is_write_index: true },
]);
expect(esContext.esAdapter.setIndexAliasToHidden).toHaveBeenCalledWith('bar-foo', [
{ alias: 'bar-foo', indexName: 'foo-bar-000001', is_write_index: true },
{ alias: 'bar-foo', indexName: 'foo-bar-000002', is_write_index: true },
]);
expect(esContext.logger.error).toHaveBeenCalledTimes(1);
expect(esContext.logger.error).toHaveBeenCalledWith(
`error setting existing \"foo-bar-000001\" index aliases - Fail`
`error setting existing \"foo-bar\" index aliases - Fail`
);
expect(esContext.esAdapter.doesIlmPolicyExist).toHaveBeenCalled();
});
Expand Down Expand Up @@ -384,3 +387,70 @@ describe('initializeEs', () => {
expect(esContext.esAdapter.createIndex).not.toHaveBeenCalled();
});
});

describe('parseIndexAliases', () => {
test('should parse IndicesGetAliasResponse into desired format', () => {
const indexGetAliasResponse = {
'.kibana-event-log-7.15.2-000003': {
aliases: {
'.kibana-event-log-7.15.2': {
is_write_index: true,
},
another_alias: {
is_write_index: true,
},
},
},
'.kibana-event-log-7.15.2-000002': {
aliases: {
'.kibana-event-log-7.15.2': {
is_write_index: false,
},
},
},
'.kibana-event-log-7.15.2-000001': {
aliases: {
'.kibana-event-log-7.15.2': {
is_write_index: false,
},
},
},
'.kibana-event-log-8.0.0-000001': {
aliases: {
'.kibana-event-log-8.0.0': {
is_write_index: true,
is_hidden: true,
},
},
},
};
expect(parseIndexAliases(indexGetAliasResponse)).toEqual([
{
alias: '.kibana-event-log-7.15.2',
indexName: '.kibana-event-log-7.15.2-000003',
is_write_index: true,
},
{
alias: 'another_alias',
indexName: '.kibana-event-log-7.15.2-000003',
is_write_index: true,
},
{
alias: '.kibana-event-log-7.15.2',
indexName: '.kibana-event-log-7.15.2-000002',
is_write_index: false,
},
{
alias: '.kibana-event-log-7.15.2',
indexName: '.kibana-event-log-7.15.2-000001',
is_write_index: false,
},
{
alias: '.kibana-event-log-8.0.0',
indexName: '.kibana-event-log-8.0.0-000001',
is_hidden: true,
is_write_index: true,
},
]);
});
});
4 changes: 1 addition & 3 deletions x-pack/plugins/event_log/server/es/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ParsedIndexAlias extends estypes.IndicesAliasDefinition {
is_hidden?: boolean;
}

function parseIndexAliases(aliasInfo: estypes.IndicesGetAliasResponse): ParsedIndexAlias[] {
export function parseIndexAliases(aliasInfo: estypes.IndicesGetAliasResponse): ParsedIndexAlias[] {
return Object.keys(aliasInfo).flatMap((indexName: string) =>
Object.keys(aliasInfo[indexName].aliases).map((alias: string) => ({
...aliasInfo[indexName].aliases[alias],
Expand Down Expand Up @@ -110,7 +110,6 @@ class EsInitializationSteps {
// should not block the rest of initialization, log the error and move on
this.esContext.logger.error(`error getting existing indices - ${err.message}`);
}

await asyncForEach(Object.keys(indices), async (indexName: string) => {
try {
const hidden: string | boolean | undefined = indices[indexName]?.settings?.index?.hidden;
Expand Down Expand Up @@ -154,7 +153,6 @@ class EsInitializationSteps {
try {
const aliasData = indexAliasData[aliasName];
const isNotHidden = aliasData.some((data) => data.is_hidden !== true);

if (isNotHidden) {
this.esContext.logger.debug(`setting existing "${aliasName}" index alias to hidden.`);
await this.esContext.esAdapter.setIndexAliasToHidden(
Expand Down

0 comments on commit bcf2825

Please sign in to comment.