[Reporting] Convert test code to Typescript (#65155)

* convert tests to typescript

* comment note

* add type for api integration test

* fix import
This commit is contained in:
Tim Sullivan 2020-05-05 11:54:21 -07:00 committed by GitHub
parent 891e27eb1f
commit 76d8ffe0cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 84 additions and 24 deletions

View file

@ -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');

View file

@ -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'));

View file

@ -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 () => {

View file

@ -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, {}>;

View file

@ -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 };

View file

@ -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);
@ -38,7 +67,7 @@ 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);
@ -46,7 +75,7 @@ export function ReportingAPIProvider({ getService }) {
);
},
async postJob(apiPath) {
async postJob(apiPath: string) {
log.debug(`ReportingAPI.postJob(${apiPath})`);
const { body } = await supertest
.post(removeWhitespace(apiPath))
@ -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
@ -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);
},
};