Skip to content

Commit

Permalink
Add API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry350 committed Aug 7, 2024
1 parent 114f3aa commit b1adcd2
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 24 deletions.
1 change: 1 addition & 0 deletions .buildkite/ftr_oblt_stateful_configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defaultQueue: 'n2-4-spot'
enabled:
- x-pack/test/alerting_api_integration/observability/config.ts
- x-pack/test/api_integration/apis/logs_ui/config.ts
- x-pack/test/api_integration/apis/logs_shared/config.ts
- x-pack/test/api_integration/apis/metrics_ui/config.ts
- x-pack/test/api_integration/apis/osquery/config.ts
- x-pack/test/api_integration/apis/synthetics/config.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { defaultLogViewId } from '../../../common/log_views';
import { MIGRATE_LOG_VIEW_SETTINGS_URL } from '../../../common/http_api/deprecations';
import { logSourcesKibanaAdvancedSettingRT } from '../../../common';
import { LogsSharedBackendLibs } from '../../lib/logs_shared_types';
Expand All @@ -21,34 +22,41 @@ export const initMigrateLogViewSettingsRoute = ({
framework.router.put(
{ path: MIGRATE_LOG_VIEW_SETTINGS_URL, validate: false },
async (context, request, response) => {
const [_, pluginStartDeps, pluginStart] = await getStartServices();
try {
const [_, pluginStartDeps, pluginStart] = await getStartServices();

const logSourcesService =
await pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getScopedLogSourcesService(
request
);
const logViewsClient = pluginStart.logViews.getScopedClient(request);
const logSourcesService =
await pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getScopedLogSourcesService(
request
);
const logViewsClient = pluginStart.logViews.getScopedClient(request);

const logView = await logViewsClient.getLogView('default');
const logView = await logViewsClient.getLogView('default');

if (!logView || logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) {
throw new Error(
"Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting."
);
}
if (!logView || logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) {
return response.customError({
body: new Error(
"Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting."
),
statusCode: 400,
});
}

const indices = (
await logViewsClient.getResolvedLogView({
type: 'log-view-reference',
logViewId: 'default',
})
).indices;
const indices = (
await logViewsClient.getResolvedLogView({
type: 'log-view-reference',
logViewId: defaultLogViewId,
})
).indices;

await logSourcesService.setLogSources([{ indexPattern: indices }]);
await logViewsClient.putLogView('default', {
logIndices: { type: 'kibana_advanced_setting' },
});
return response.ok();
await logSourcesService.setLogSources([{ indexPattern: indices }]);
await logViewsClient.putLogView(defaultLogViewId, {
logIndices: { type: 'kibana_advanced_setting' },
});
return response.ok();
} catch (error) {
throw error;
}
}
);
};
17 changes: 17 additions & 0 deletions x-pack/test/api_integration/apis/logs_shared/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { FtrConfigProviderContext } from '@kbn/test';

export default async function ({ readConfigFile }: FtrConfigProviderContext) {
const baseIntegrationTestsConfig = await readConfigFile(require.resolve('../../config.ts'));

return {
...baseIntegrationTestsConfig.getAll(),
testFiles: [require.resolve('.')],
};
}
14 changes: 14 additions & 0 deletions x-pack/test/api_integration/apis/logs_shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Logs shared routes', () => {
loadTestFile(require.resolve('./migrate_log_view_settings'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 expect from '@kbn/expect';
import { LogViewAttributes } from '@kbn/logs-shared-plugin/common/log_views';
import { infraSourceConfigurationSavedObjectName } from '@kbn/infra-plugin/server/lib/sources';
import { logViewSavedObjectName } from '@kbn/logs-shared-plugin/server';
import { defaultLogViewId } from '@kbn/logs-shared-plugin/common/log_views';
import { MIGRATE_LOG_VIEW_SETTINGS_URL } from '@kbn/logs-shared-plugin/common/http_api/deprecations';
import { OBSERVABILITY_LOGS_DATA_ACCESS_LOG_SOURCES_ID } from '@kbn/management-settings-ids';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const logViewsService = getService('infraLogViews');
const kibanaServer = getService('kibanaServer');
const retry = getService('retry');

const INDICES = 'logs-*,something-else-*,test-*';

describe('Log view settings migration', () => {
describe('Migration API', () => {
before(async () => {
await kibanaServer.savedObjects.clean({
types: [infraSourceConfigurationSavedObjectName, logViewSavedObjectName],
});
});

afterEach(async () => {
await kibanaServer.savedObjects.clean({
types: [infraSourceConfigurationSavedObjectName, logViewSavedObjectName],
});
});

it('performs a migration when the log view is not using the Kibana advanced setting', async () => {
const logViewAttributes: Partial<LogViewAttributes> = {
name: 'Test Log View 1',
description: 'Test Description 1',
logIndices: { type: 'index_name', indexName: INDICES },
logColumns: [],
};

await logViewsService.putLogView(defaultLogViewId, {
attributes: logViewAttributes,
});

await supertest
.put(MIGRATE_LOG_VIEW_SETTINGS_URL)
.set({
'kbn-xsrf': 'some-xsrf-token',
})
.send()
.expect(200);

await retry.try(async () => {
const migratedLogView = await logViewsService.getLogView(defaultLogViewId);
expect(migratedLogView.data.attributes.logIndices.type).to.eql('kibana_advanced_setting');
const uiSetting = await kibanaServer.uiSettings.get(
OBSERVABILITY_LOGS_DATA_ACCESS_LOG_SOURCES_ID
);
expect(uiSetting).to.eql([INDICES]);
});
});

it('should error when the log view is already using the Kibana advanced setting', async () => {
const logViewAttributes: Partial<LogViewAttributes> = {
name: 'Test Log View 1',
description: 'Test Description 1',
logIndices: { type: 'kibana_advanced_setting' },
logColumns: [],
};

await logViewsService.putLogView(defaultLogViewId, {
attributes: logViewAttributes,
});

const response = await supertest
.put(MIGRATE_LOG_VIEW_SETTINGS_URL)
.set({
'kbn-xsrf': 'some-xsrf-token',
})
.send()
.expect(400);

expect(response.body.message).to.eql(
"Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting."
);
});
});
});
}
3 changes: 2 additions & 1 deletion x-pack/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"@kbn/osquery-plugin",
"@kbn/entities-schema",
"@kbn/actions-simulators-plugin",
"@kbn/cases-api-integration-test-plugin"
"@kbn/cases-api-integration-test-plugin",
"@kbn/management-settings-ids"
]
}

0 comments on commit b1adcd2

Please sign in to comment.