[Telemetry] Use soClient in FTRs (#155175)

This commit is contained in:
Alejandro Fernández Haro 2023-04-18 21:38:18 +02:00 committed by GitHub
parent 17c4185fd7
commit 613619bee2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 227 deletions

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { FtrProviderContext } from '../../ftr_provider_context';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('Telemetry', () => {

View file

@ -7,17 +7,16 @@
*/
import expect from '@kbn/expect';
import { Client } from '@elastic/elasticsearch';
import { TelemetrySavedObjectAttributes } from '@kbn/telemetry-plugin/server/saved_objects';
import SuperTest from 'supertest';
import { FtrProviderContext } from '../../ftr_provider_context';
import type { KbnClient } from '@kbn/test';
import type { TelemetrySavedObjectAttributes } from '@kbn/telemetry-plugin/server/saved_objects';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default function optInTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const esArchiver = getService('esArchiver');
const esClient: Client = getService('es');
describe('/api/telemetry/v2/optIn API', () => {
let defaultAttributes: TelemetrySavedObjectAttributes;
@ -26,39 +25,39 @@ export default function optInTest({ getService }: FtrProviderContext) {
await esArchiver.emptyKibanaIndex();
const kibanaVersionAccessor = kibanaServer.version;
kibanaVersion = await kibanaVersionAccessor.get();
defaultAttributes = await getSavedObjectAttributes(esClient);
defaultAttributes = await getSavedObjectAttributes(kibanaServer);
expect(typeof kibanaVersion).to.eql('string');
expect(kibanaVersion.length).to.be.greaterThan(0);
});
afterEach(async () => {
await updateSavedObjectAttributes(esClient, defaultAttributes);
await updateSavedObjectAttributes(kibanaServer, defaultAttributes);
});
it('should support sending false with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(esClient, {
await updateSavedObjectAttributes(kibanaServer, {
allowChangingOptInStatus: true,
});
await postTelemetryV2OptIn(supertest, false, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(esClient);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(kibanaServer);
expect(enabled).to.be(false);
expect(lastVersionChecked).to.be(kibanaVersion);
});
it('should support sending true with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(esClient, {
await updateSavedObjectAttributes(kibanaServer, {
...defaultAttributes,
allowChangingOptInStatus: true,
});
await postTelemetryV2OptIn(supertest, true, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(esClient);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(kibanaServer);
expect(enabled).to.be(true);
expect(lastVersionChecked).to.be(kibanaVersion);
});
it('should not support sending false with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(esClient, {
await updateSavedObjectAttributes(kibanaServer, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
@ -66,7 +65,7 @@ export default function optInTest({ getService }: FtrProviderContext) {
});
it('should not support sending true with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(esClient, {
await updateSavedObjectAttributes(kibanaServer, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
@ -98,29 +97,31 @@ async function postTelemetryV2OptIn(
}
async function updateSavedObjectAttributes(
es: Client,
client: KbnClient,
attributes: TelemetrySavedObjectAttributes
): Promise<void> {
// Directly writing to the `.kibana` index because the SO type now is hidden, meaning it's not exposed via the SO HTTP API
await es.update({
index: '.kibana',
id: 'telemetry:telemetry',
doc: {
telemetry: attributes,
// there are many missing fields in the SO, hopefully it doesn't break Kibana
},
doc_as_upsert: true,
await client.savedObjects.create({
type: 'telemetry',
id: 'telemetry',
overwrite: true,
attributes,
});
}
async function getSavedObjectAttributes(es: Client): Promise<TelemetrySavedObjectAttributes> {
// Directly writing to the `.kibana` index because the SO type now is hidden, meaning it's not exposed via the SO HTTP API
const { _source: body } = await es.get<{ telemetry: TelemetrySavedObjectAttributes }>(
{
index: '.kibana',
id: 'telemetry:telemetry',
},
{ ignore: [404] }
);
return body?.telemetry || {};
async function getSavedObjectAttributes(
client: KbnClient
): Promise<TelemetrySavedObjectAttributes> {
try {
const body = await client.savedObjects.get<TelemetrySavedObjectAttributes>({
type: 'telemetry',
id: 'telemetry',
});
return body.attributes;
} catch (err) {
if (err.response?.status === 404) {
return {};
}
throw err;
}
}

View file

@ -7,7 +7,7 @@
*/
import { AxiosError } from 'axios';
import { FtrProviderContext } from '../../ftr_provider_context';
import type { FtrProviderContext } from '../../ftr_provider_context';
const TELEMETRY_SO_TYPE = 'telemetry';
const TELEMETRY_SO_ID = 'telemetry';

View file

@ -7,21 +7,15 @@
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default function optInTest({ getService }: FtrProviderContext) {
const client = getService('es');
const client = getService('kibanaServer');
const supertest = getService('supertest');
describe('/api/telemetry/v2/last_reported API Telemetry lastReported', () => {
before(async () => {
await client.delete(
{
index: '.kibana',
id: 'telemetry:telemetry',
},
{ ignore: [404] }
);
await client.savedObjects.delete({ type: 'telemetry', id: 'telemetry' });
});
it('GET should return undefined when there is no stored telemetry.lastReported value', async () => {
@ -31,22 +25,25 @@ export default function optInTest({ getService }: FtrProviderContext) {
it('PUT should update telemetry.lastReported to now', async () => {
await supertest.put('/api/telemetry/v2/last_reported').set('kbn-xsrf', 'xxx').expect(200);
const { _source } = await client.get<{ telemetry: { lastReported: number } }>({
index: '.kibana',
id: 'telemetry:telemetry',
const {
attributes: { lastReported },
} = await client.savedObjects.get<{ lastReported: number }>({
type: 'telemetry',
id: 'telemetry',
});
expect(_source?.telemetry.lastReported).to.be.a('number');
expect(lastReported).to.be.a('number');
});
it('GET should return the previously stored lastReported value', async () => {
const { _source } = await client.get<{ telemetry: { lastReported: number } }>({
index: '.kibana',
id: 'telemetry:telemetry',
const {
attributes: { lastReported },
} = await client.savedObjects.get<{ lastReported: number }>({
type: 'telemetry',
id: 'telemetry',
});
expect(_source?.telemetry.lastReported).to.be.a('number');
const lastReported = _source?.telemetry.lastReported;
expect(lastReported).to.be.a('number');
await supertest
.get('/api/telemetry/v2/last_reported')

View file

@ -7,30 +7,26 @@
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default function optInTest({ getService }: FtrProviderContext) {
const client = getService('es');
const client = getService('kibanaServer');
const supertest = getService('supertest');
describe('/api/telemetry/v2/userHasSeenNotice API Telemetry User has seen OptIn Notice', () => {
it('should update telemetry setting field via PUT', async () => {
await client.delete(
{
index: '.kibana',
id: 'telemetry:telemetry',
},
{ ignore: [404] }
);
await client.savedObjects.delete({ type: 'telemetry', id: 'telemetry' });
await supertest.put('/api/telemetry/v2/userHasSeenNotice').set('kbn-xsrf', 'xxx').expect(200);
const { _source } = await client.get<{ telemetry: { userHasSeenNotice: boolean } }>({
index: '.kibana',
id: 'telemetry:telemetry',
const {
attributes: { userHasSeenNotice },
} = await client.savedObjects.get<{ userHasSeenNotice: boolean }>({
type: 'telemetry',
id: 'telemetry',
});
expect(_source?.telemetry.userHasSeenNotice).to.be(true);
expect(userHasSeenNotice).to.be(true);
});
});
}

View file

@ -11,7 +11,5 @@ export default function ({ loadTestFile }: FtrProviderContext) {
describe('Telemetry', () => {
loadTestFile(require.resolve('./telemetry'));
loadTestFile(require.resolve('./telemetry_local'));
loadTestFile(require.resolve('./opt_in'));
loadTestFile(require.resolve('./telemetry_optin_notice_seen'));
});
}

View file

@ -1,125 +0,0 @@
/*
* 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 expect from '@kbn/expect';
import { Client } from '@elastic/elasticsearch';
import { TelemetrySavedObjectAttributes } from '@kbn/telemetry-plugin/server/saved_objects';
import SuperTest from 'supertest';
import { FtrProviderContext } from '../../ftr_provider_context';
export default function optInTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const esArchiver = getService('esArchiver');
const esClient: Client = getService('es');
describe('/api/telemetry/v2/optIn API', () => {
let defaultAttributes: TelemetrySavedObjectAttributes;
let kibanaVersion: string;
before(async () => {
await esArchiver.emptyKibanaIndex();
const kibanaVersionAccessor = kibanaServer.version;
kibanaVersion = await kibanaVersionAccessor.get();
defaultAttributes = await getSavedObjectAttributes(esClient);
expect(typeof kibanaVersion).to.eql('string');
expect(kibanaVersion.length).to.be.greaterThan(0);
});
afterEach(async () => {
await updateSavedObjectAttributes(esClient, defaultAttributes);
});
it('should support sending false with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(esClient, {
allowChangingOptInStatus: true,
});
await postTelemetryV2OptIn(supertest, false, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(esClient);
expect(enabled).to.be(false);
expect(lastVersionChecked).to.be(kibanaVersion);
});
it('should support sending true with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(esClient, {
...defaultAttributes,
allowChangingOptInStatus: true,
});
await postTelemetryV2OptIn(supertest, true, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(esClient);
expect(enabled).to.be(true);
expect(lastVersionChecked).to.be(kibanaVersion);
});
it('should not support sending false with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(esClient, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
await postTelemetryV2OptIn(supertest, false, 400);
});
it('should not support sending true with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(esClient, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
await postTelemetryV2OptIn(supertest, true, 400);
});
it('should not support sending null', async () => {
await postTelemetryV2OptIn(supertest, null, 400);
});
it('should not support sending junk', async () => {
await postTelemetryV2OptIn(supertest, 42, 400);
});
});
}
async function postTelemetryV2OptIn(
supertest: SuperTest.SuperTest<SuperTest.Test>,
value: unknown,
statusCode: number
): Promise<any> {
const { body } = await supertest
.post('/api/telemetry/v2/optIn')
.set('kbn-xsrf', 'xxx')
.send({ enabled: value })
.expect(statusCode);
return body;
}
async function updateSavedObjectAttributes(
es: Client,
attributes: TelemetrySavedObjectAttributes
): Promise<void> {
// Directly writing to the `.kibana` index because the SO type now is hidden, meaning it's not exposed via the SO HTTP API
await es.update({
index: '.kibana',
id: 'telemetry:telemetry',
doc: {
telemetry: attributes,
// there are many missing fields in the SO, hopefully it doesn't break Kibana
},
doc_as_upsert: true,
});
}
async function getSavedObjectAttributes(es: Client): Promise<TelemetrySavedObjectAttributes> {
// Directly writing to the `.kibana` index because the SO type now is hidden, meaning it's not exposed via the SO HTTP API
const { _source: body } = await es.get<{ telemetry: TelemetrySavedObjectAttributes }>(
{
index: '.kibana',
id: 'telemetry:telemetry',
},
{ ignore: [404] }
);
return body?.telemetry || {};
}

View file

@ -1,35 +0,0 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
export default function optInTest({ getService }: FtrProviderContext) {
const client = getService('es');
const supertest = getService('supertest');
describe('/api/telemetry/v2/userHasSeenNotice API Telemetry User has seen OptIn Notice', () => {
it('should update telemetry setting field via PUT', async () => {
await client.delete(
{
index: '.kibana',
id: 'telemetry:telemetry',
},
{ ignore: [404] }
);
await supertest.put('/api/telemetry/v2/userHasSeenNotice').set('kbn-xsrf', 'xxx').expect(200);
const { _source } = await client.get<{ telemetry: { userHasSeenNotice: boolean } }>({
index: '.kibana',
id: 'telemetry:telemetry',
});
expect(_source?.telemetry.userHasSeenNotice).to.be(true);
});
});
}