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

[Reporting] Convert test code to Typescript #65155

Merged
merged 5 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
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
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 '../../../common/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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this service is mistakenly using /api/stats?extended and returning the data in a different format where all the fields are snake_cased.


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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to use the ReportingUsageType interface that is exposed in the Reporting plugin code. However, the structure returned by the service API endpoint is has some post-processing that converts all the field names to snake_case. It's a separate issue that requires cleanup.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed an issue: #65338

}

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