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

fix ui counters flaky test #91372

Merged
merged 2 commits into from
Feb 15, 2021
Merged
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
71 changes: 44 additions & 27 deletions test/api_integration/apis/ui_counters/ui_counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import expect from '@kbn/expect';
import { ReportManager, METRIC_TYPE, UiCounterMetricType } from '@kbn/analytics';
import moment from 'moment';
import { FtrProviderContext } from '../../ftr_provider_context';
import { SavedObject } from '../../../../src/core/server';
import { UICounterSavedObjectAttributes } from '../../../../src/plugins/kibana_usage_collection/server/collectors/ui_counters/ui_counter_saved_object_type';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const es = getService('es');

const createUiCounterEvent = (eventName: string, type: UiCounterMetricType, count = 1) => ({
eventName,
Expand All @@ -23,12 +24,21 @@ export default function ({ getService }: FtrProviderContext) {
count,
});

// FLAKY: https://github.com/elastic/kibana/issues/85086
describe.skip('UI Counters API', () => {
before(async () => {
await esArchiver.emptyKibanaIndex();
});
const getCounterById = (
savedObjects: Array<SavedObject<UICounterSavedObjectAttributes>>,
targetId: string
): SavedObject<UICounterSavedObjectAttributes> => {
const savedObject = savedObjects.find(({ id }: { id: string }) => id === targetId);
if (!savedObject) {
throw new Error(`Unable to find savedObject id ${targetId}`);
}

return savedObject;
};

describe('UI Counters API', () => {
const dayDate = moment().format('DDMMYYYY');
before(async () => await esArchiver.emptyKibanaIndex());

it('stores ui counter events in savedObjects', async () => {
const reportManager = new ReportManager();
Expand All @@ -44,12 +54,18 @@ export default function ({ getService }: FtrProviderContext) {
.send({ report })
.expect(200);

const { body: response } = await es.search({ index: '.kibana', q: 'type:ui-counter' });
const {
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=ui-counter')
.set('kbn-xsrf', 'kibana')
.expect(200);

const ids = response.hits.hits.map(({ _id }: { _id: string }) => _id);
expect(ids.includes(`ui-counter:myApp:${dayDate}:${METRIC_TYPE.COUNT}:my_event`)).to.eql(
true
const countTypeEvent = getCounterById(
savedObjects,
`myApp:${dayDate}:${METRIC_TYPE.COUNT}:my_event`
);
expect(countTypeEvent.attributes.count).to.eql(1);
});

it('supports multiple events', async () => {
Expand All @@ -70,28 +86,29 @@ export default function ({ getService }: FtrProviderContext) {
.expect(200);

const {
body: {
hits: { hits },
},
} = await es.search({ index: '.kibana', q: 'type:ui-counter' });

const countTypeEvent = hits.find(
(hit: { _id: string }) =>
hit._id === `ui-counter:myApp:${dayDate}:${METRIC_TYPE.COUNT}:${uniqueEventName}`
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=ui-counter&fields=count')
.set('kbn-xsrf', 'kibana')
.expect(200);

const countTypeEvent = getCounterById(
savedObjects,
`myApp:${dayDate}:${METRIC_TYPE.COUNT}:${uniqueEventName}`
);
expect(countTypeEvent._source['ui-counter'].count).to.eql(1);
expect(countTypeEvent.attributes.count).to.eql(1);

const clickTypeEvent = hits.find(
(hit: { _id: string }) =>
hit._id === `ui-counter:myApp:${dayDate}:${METRIC_TYPE.CLICK}:${uniqueEventName}`
const clickTypeEvent = getCounterById(
savedObjects,
`myApp:${dayDate}:${METRIC_TYPE.CLICK}:${uniqueEventName}`
);
expect(clickTypeEvent._source['ui-counter'].count).to.eql(2);
expect(clickTypeEvent.attributes.count).to.eql(2);

const secondEvent = hits.find(
(hit: { _id: string }) =>
hit._id === `ui-counter:myApp:${dayDate}:${METRIC_TYPE.COUNT}:${uniqueEventName}_2`
const secondEvent = getCounterById(
savedObjects,
`myApp:${dayDate}:${METRIC_TYPE.COUNT}:${uniqueEventName}_2`
);
expect(secondEvent._source['ui-counter'].count).to.eql(1);
expect(secondEvent.attributes.count).to.eql(1);
});
});
}