Skip to content

Commit

Permalink
[Reporting] Convert test code to Typescript (#65155)
Browse files Browse the repository at this point in the history
* convert tests to typescript

* comment note

* add type for api integration test

* fix import
  • Loading branch information
tsullivan authored May 5, 2020
1 parent 891e27e commit 76d8ffe
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { OSS_KIBANA_ARCHIVE_PATH, OSS_DATA_ARCHIVE_PATH } from './constants';
import { OSS_DATA_ARCHIVE_PATH, OSS_KIBANA_ARCHIVE_PATH } from './constants';
import { FtrProviderContext } from '../ftr_provider_context';

export default function({ loadTestFile, getService }) {
// eslint-disable-next-line import/no-default-export
export default function({ loadTestFile, getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

export default function({ loadTestFile }) {
import { FtrProviderContext } from '../../ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function({ loadTestFile }: FtrProviderContext) {
describe('CSV', function() {
this.tags('ciGroup2');
loadTestFile(require.resolve('./csv_saved_search'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../ftr_provider_context';
import { ReportingUsageStats } from '../services/reporting_api';
import * as GenerationUrls from './generation_urls';

export default function({ getService }) {
interface UsageStats {
reporting: ReportingUsageStats;
}

// eslint-disable-next-line import/no-default-export
export default function({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const reportingAPI = getService('reportingAPI');
const usageAPI = getService('usageAPI');
const usageAPI = getService('usageAPI' as any); // NOTE Usage API service is not Typescript

describe('reporting usage', () => {
before(() => reportingAPI.deleteAllReportingIndexes());
afterEach(() => reportingAPI.deleteAllReportingIndexes());

describe('initial state', () => {
let usage;
let usage: UsageStats;

before(async () => {
usage = await usageAPI.getUsageStats();
usage = (await usageAPI.getUsageStats()) as UsageStats;
});

it('shows reporting as available and enabled', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { ReportingAPIProvider } from './reporting_api';
import { GenericFtrProviderContext } from '@kbn/test/types/ftr';

import { services } from './services';

export type FtrProviderContext = GenericFtrProviderContext<typeof services, {}>;
15 changes: 15 additions & 0 deletions x-pack/test/reporting/services/index.ts
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;
* you may not use this file except in compliance with the Elastic License.
*/

import { ReportingAPIProvider } from './reporting_api';
import { services as xpackServices } from '../../functional/services';

export const services = {
...xpackServices,
reportingAPI: ReportingAPIProvider,
};

export { ReportingAPIProvider };
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,57 @@
*/

import expect from '@kbn/expect';
// @ts-ignore no module definition
import { indexTimestamp } from '../../../legacy/plugins/reporting/server/lib/esqueue/helpers/index_timestamp';
import { FtrProviderContext } from '../ftr_provider_context';

function removeWhitespace(str) {
interface PDFAppCounts {
app: {
[appName: string]: number;
};
layout: {
[layoutType: string]: number;
};
}

export interface ReportingUsageStats {
available: boolean;
enabled: boolean;
total: number;
last_7_days: {
total: number;
printable_pdf: PDFAppCounts;
[jobType: string]: any;
};
printable_pdf: PDFAppCounts;
status: any;
[jobType: string]: any;
}

interface UsageStats {
reporting: ReportingUsageStats;
}

function removeWhitespace(str: string) {
return str.replace(/\s/g, '');
}

export function ReportingAPIProvider({ getService }) {
export function ReportingAPIProvider({ getService }: FtrProviderContext) {
const log = getService('log');
const supertest = getService('supertest');
const esSupertest = getService('esSupertest');

return {
async waitForJobToFinish(downloadReportPath) {
async waitForJobToFinish(downloadReportPath: string) {
log.debug(`Waiting for job to finish: ${downloadReportPath}`);
const JOB_IS_PENDING_CODE = 503;

const statusCode = await new Promise(resolve => {
const intervalId = setInterval(async () => {
const response = await supertest
const response = (await supertest
.get(downloadReportPath)
.responseType('blob')
.set('kbn-xsrf', 'xxx');
.set('kbn-xsrf', 'xxx')) as any;
log.debug(`Report at path ${downloadReportPath} returned code ${response.statusCode}`);
if (response.statusCode !== JOB_IS_PENDING_CODE) {
clearInterval(intervalId);
Expand All @@ -38,15 +67,15 @@ export function ReportingAPIProvider({ getService }) {
expect(statusCode).to.be(200);
},

async expectAllJobsToFinishSuccessfully(jobPaths) {
async expectAllJobsToFinishSuccessfully(jobPaths: string[]) {
await Promise.all(
jobPaths.map(async path => {
await this.waitForJobToFinish(path);
})
);
},

async postJob(apiPath) {
async postJob(apiPath: string) {
log.debug(`ReportingAPI.postJob(${apiPath})`);
const { body } = await supertest
.post(removeWhitespace(apiPath))
Expand All @@ -59,7 +88,7 @@ export function ReportingAPIProvider({ getService }) {
*
* @return {Promise<Function>} A function to call to clean up the index alias that was added.
*/
async coerceReportsIntoExistingIndex(indexName) {
async coerceReportsIntoExistingIndex(indexName: string) {
log.debug(`ReportingAPI.coerceReportsIntoExistingIndex(${indexName})`);

// Adding an index alias coerces the report to be generated on an existing index which means any new
Expand Down Expand Up @@ -96,35 +125,35 @@ export function ReportingAPIProvider({ getService }) {
await esSupertest.delete('/.reporting*').expect(200);
},

expectRecentPdfAppStats(stats, app, count) {
expectRecentPdfAppStats(stats: UsageStats, app: string, count: number) {
expect(stats.reporting.last_7_days.printable_pdf.app[app]).to.be(count);
},

expectAllTimePdfAppStats(stats, app, count) {
expectAllTimePdfAppStats(stats: UsageStats, app: string, count: number) {
expect(stats.reporting.printable_pdf.app[app]).to.be(count);
},

expectRecentPdfLayoutStats(stats, layout, count) {
expectRecentPdfLayoutStats(stats: UsageStats, layout: string, count: number) {
expect(stats.reporting.last_7_days.printable_pdf.layout[layout]).to.be(count);
},

expectAllTimePdfLayoutStats(stats, layout, count) {
expectAllTimePdfLayoutStats(stats: UsageStats, layout: string, count: number) {
expect(stats.reporting.printable_pdf.layout[layout]).to.be(count);
},

expectRecentJobTypeTotalStats(stats, jobType, count) {
expectRecentJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) {
expect(stats.reporting.last_7_days[jobType].total).to.be(count);
},

expectAllTimeJobTypeTotalStats(stats, jobType, count) {
expectAllTimeJobTypeTotalStats(stats: UsageStats, jobType: string, count: number) {
expect(stats.reporting[jobType].total).to.be(count);
},

getCompletedReportCount(stats) {
getCompletedReportCount(stats: UsageStats) {
return stats.reporting.status.completed;
},

expectCompletedReportCount(stats, count) {
expectCompletedReportCount(stats: UsageStats, count: number) {
expect(this.getCompletedReportCount(stats)).to.be(count);
},
};
Expand Down

0 comments on commit 76d8ffe

Please sign in to comment.