Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCOUT][POC] Api Tests with Jest #210435

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/kbn-scout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ export type {
ScoutServerConfig,
ScoutTestConfig,
} from './src/types';
export {
kbnSuperTestWithAuthClient,
getEsArchiver,
getEsClient,
getKbnClient,
getLogger,
} from './src/common';
43 changes: 39 additions & 4 deletions packages/kbn-scout/src/common/services/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,63 @@
*/

import { createEsClientForTesting, KbnClient } from '@kbn/test';
import { ScoutLogger } from './logger';
import supertest, { type AgentOptions } from 'supertest';
import { kbnTestConfig } from '@kbn/test';
import * as Url from 'url';
import { ScoutTestConfig, EsClient } from '../../types';

import { ScoutLogger } from './logger';
interface ClientOptions {
serviceName: string;
url: string;
username: string;
password: string;
log: ScoutLogger;
log?: ScoutLogger;
}

function createClientUrlWithAuth({ serviceName, url, username, password, log }: ClientOptions) {
const clientUrl = new URL(url);
clientUrl.username = username;
clientUrl.password = password;

log.serviceLoaded(`${serviceName}Client`);
if (log) log.serviceLoaded(`${serviceName}Client`);

return clientUrl.toString();
}

let esClientInstance: EsClient | null = null;
let kbnClientInstance: KbnClient | null = null;
let kbnSuperTestWithAuthClientInstance: supertest.Agent | null = null;

export const kbnSuperTestWithAuthClient = () => {
if (!kbnSuperTestWithAuthClientInstance) {
const kibanaUrl = createClientUrlWithAuth({
serviceName: 'kbn',
url: Url.format({
protocol: kbnTestConfig.getUrlParts().protocol,
hostname: kbnTestConfig.getUrlParts().hostname,
port: kbnTestConfig.getUrlParts().port,
}),
username: 'elastic',
password: 'changeme',
});

const options: AgentOptions = {};
// if (kibanaServerConfig.certificateAuthorities) {
// options.ca = kibanaServerConfig.certificateAuthorities;
// options.rejectUnauthorized = false;
// }

// const serverArgs = config.get('kbnTestServer.serverArgs', []) as string[];
// const http2Enabled = serverArgs.includes('--server.protocol=http2');
// if (http2Enabled) {
// options.http2 = true;
// }

kbnSuperTestWithAuthClientInstance = supertest(kibanaUrl, options);
}

return kbnSuperTestWithAuthClientInstance;
};

export function getEsClient(config: ScoutTestConfig, log: ScoutLogger) {
if (!esClientInstance) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-scout/src/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { getEsClient, getKbnClient } from './clients';
export { getEsClient, getKbnClient, kbnSuperTestWithAuthClient } from './clients';
export { createScoutConfig } from './config';
export { getEsArchiver } from './es_archiver';
export { createKbnUrl } from './kibana_url';
Expand Down
11 changes: 11 additions & 0 deletions packages/kbn-scout/src/jest_api_tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/


// TODO: Add things like supertest here?
2 changes: 1 addition & 1 deletion packages/kbn-scout/src/types/test_config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ScoutTestConfig {
projectType?: ServerlessProjectType;
isCloud: boolean;
license: string;
cloudUsersFilePath: string;
cloudUsersFilePath?: string;
hosts: {
kibana: string;
elasticsearch: string;
Expand Down
178 changes: 178 additions & 0 deletions x-pack/platform/plugins/shared/maps/api_tests/maps_telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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 {
ELASTIC_HTTP_VERSION_HEADER,
X_ELASTIC_INTERNAL_ORIGIN_REQUEST,
} from '@kbn/core-http-common';
import * as Url from 'url';
import { describe, expect, it, beforeAll } from '@jest/globals';
import {
kbnSuperTestWithAuthClient as supertest,
getEsArchiver,
getEsClient,
getKbnClient,
ScoutTestConfig,
getLogger,
} from '@kbn/scout';
import { kbnTestConfig, esTestConfig } from '@kbn/test';

describe('maps_telemetry', () => {
const log = getLogger();
const testConfig: ScoutTestConfig = {
serverless: false,
isCloud: false,
license: 'trial',
hosts: {
kibana: Url.format({
protocol: kbnTestConfig.getUrlParts().protocol,
hostname: kbnTestConfig.getUrlParts().hostname,
port: kbnTestConfig.getUrlParts().port,
}),
elasticsearch: Url.format({
protocol: esTestConfig.getUrlParts().protocol,
hostname: esTestConfig.getUrlParts().hostname,
port: esTestConfig.getUrlParts().port,
}),
},
auth: {
username: 'elastic',
password: 'changeme',
},
};
const kbnClient = getKbnClient(testConfig, log);
const esClient = getEsClient(testConfig, log);
const esArchiver = getEsArchiver(esClient, kbnClient, log);

beforeAll(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/logstash_functional');
await kbnClient.importExport.load('x-pack/test/functional/fixtures/kbn_archiver/maps.json');
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/maps/data');
});
afterAll(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/logstash_functional');
await esArchiver.unload('x-pack/test/functional/es_archives/maps/data');
await kbnClient.importExport.unload('x-pack/test/functional/fixtures/kbn_archiver/maps.json');
});

it('should return the correct telemetry values for map saved objects', async () => {
expect(1).toBeTruthy();
const {
body: [{ stats: apiResponse }],
} = await supertest()
.post(`/internal/telemetry/clusters/_stats`)
.set(ELASTIC_HTTP_VERSION_HEADER, '2')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.set('kbn-xsrf', 'xxxx')
.send({
unencrypted: true,
refreshCache: true,
})
.expect(200);

// const geoPointFieldStats = apiResponse.cluster_stats.indices.mappings.field_types.find(
// (fieldStat: estypes.ClusterStatsFieldTypes) => {
// return fieldStat.name === 'geo_point';
// }
// );
// expect(geoPointFieldStats.count).to.be(55);
// expect(geoPointFieldStats.index_count).to.be(12);

// const geoShapeFieldStats = apiResponse.cluster_stats.indices.mappings.field_types.find(
// (fieldStat: estypes.ClusterStatsFieldTypes) => {
// return fieldStat.name === 'geo_shape';
// }
// );
// expect(geoShapeFieldStats.count).to.be(3);
// expect(geoShapeFieldStats.index_count).to.be(3);

// const mapUsage = apiResponse.stack_stats.kibana.plugins.maps;
// delete mapUsage.timeCaptured;

// expect(mapUsage).eql({
// mapsTotalCount: 28,
// basemaps: {},
// joins: { term: { min: 1, max: 1, total: 4, avg: 0.14285714285714285 } },
// layerTypes: {
// es_docs: { min: 1, max: 3, total: 20, avg: 0.7142857142857143 },
// es_agg_grids: { min: 1, max: 1, total: 6, avg: 0.21428571428571427 },
// es_point_to_point: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// es_top_hits: { min: 1, max: 1, total: 2, avg: 0.07142857142857142 },
// es_agg_heatmap: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// esql: { min: 1, max: 1, total: 2, avg: 0.07142857142857142 },
// kbn_tms_raster: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// ems_basemap: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// ems_region: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// },
// resolutions: {
// coarse: { min: 1, max: 1, total: 4, avg: 0.14285714285714285 },
// super_fine: { min: 1, max: 1, total: 3, avg: 0.10714285714285714 },
// },
// scalingOptions: {
// limit: { min: 1, max: 3, total: 15, avg: 0.5357142857142857 },
// clusters: { min: 1, max: 1, total: 1, avg: 0.03571428571428571 },
// mvt: { min: 1, max: 1, total: 4, avg: 0.14285714285714285 },
// },
// attributesPerMap: {
// customIconsCount: {
// avg: 0,
// max: 0,
// min: 0,
// },
// dataSourcesCount: {
// avg: 1.2142857142857142,
// max: 7,
// min: 1,
// },
// emsVectorLayersCount: {
// idThatDoesNotExitForEMSFileSource: {
// avg: 0.03571428571428571,
// max: 1,
// min: 1,
// },
// },
// layerTypesCount: {
// BLENDED_VECTOR: {
// avg: 0.03571428571428571,
// max: 1,
// min: 1,
// },
// EMS_VECTOR_TILE: {
// avg: 0.03571428571428571,
// max: 1,
// min: 1,
// },
// GEOJSON_VECTOR: {
// avg: 0.8571428571428571,
// max: 6,
// min: 1,
// },
// HEATMAP: {
// avg: 0.03571428571428571,
// max: 1,
// min: 1,
// },
// MVT_VECTOR: {
// avg: 0.25,
// max: 1,
// min: 1,
// },
// RASTER_TILE: {
// avg: 0.03571428571428571,
// max: 1,
// min: 1,
// },
// },
// layersCount: {
// avg: 1.25,
// max: 8,
// min: 1,
// },
// },
// });
});
});
15 changes: 15 additions & 0 deletions x-pack/platform/plugins/shared/maps/jest.integration.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test/jest_integration_node',
rootDir: '../../../../..',
roots: ['<rootDir>/x-pack/platform/plugins/shared/maps'],
testMatch: ['**/api_tests**/*.test.{js,mjs,ts,tsx}'],
};