Skip to content

Commit 16cbec7

Browse files
[Telemetry] oss api tests (#64602)
* Adds telemetry API tests for oss * Modifies test expectations to match that within oss Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 94a1e16 commit 16cbec7

5 files changed

Lines changed: 342 additions & 0 deletions

File tree

test/api_integration/apis/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ export default function({ loadTestFile }) {
3333
loadTestFile(require.resolve('./status'));
3434
loadTestFile(require.resolve('./stats'));
3535
loadTestFile(require.resolve('./ui_metric'));
36+
loadTestFile(require.resolve('./telemetry'));
3637
});
3738
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export default function({ loadTestFile }) {
21+
describe('Telemetry', () => {
22+
loadTestFile(require.resolve('./telemetry_local'));
23+
loadTestFile(require.resolve('./opt_in'));
24+
loadTestFile(require.resolve('./telemetry_optin_notice_seen'));
25+
});
26+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import expect from '@kbn/expect';
21+
22+
import { TelemetrySavedObjectAttributes } from 'src/plugins/telemetry/server/telemetry_repository';
23+
import { FtrProviderContext } from '../../ftr_provider_context';
24+
25+
export default function optInTest({ getService }: FtrProviderContext) {
26+
const supertest = getService('supertest');
27+
const kibanaServer = getService('kibanaServer');
28+
describe('/api/telemetry/v2/optIn API', () => {
29+
let defaultAttributes: TelemetrySavedObjectAttributes;
30+
let kibanaVersion: any;
31+
before(async () => {
32+
const kibanaVersionAccessor = kibanaServer.version;
33+
kibanaVersion = await kibanaVersionAccessor.get();
34+
defaultAttributes =
35+
(await getSavedObjectAttributes(supertest).catch(err => {
36+
if (err.message === 'expected 200 "OK", got 404 "Not Found"') {
37+
return null;
38+
}
39+
throw err;
40+
})) || {};
41+
42+
expect(typeof kibanaVersion).to.eql('string');
43+
expect(kibanaVersion.length).to.be.greaterThan(0);
44+
});
45+
46+
afterEach(async () => {
47+
await updateSavedObjectAttributes(supertest, defaultAttributes);
48+
});
49+
50+
it('should support sending false with allowChangingOptInStatus true', async () => {
51+
await updateSavedObjectAttributes(supertest, {
52+
...defaultAttributes,
53+
allowChangingOptInStatus: true,
54+
});
55+
await postTelemetryV2Optin(supertest, false, 200);
56+
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest);
57+
expect(enabled).to.be(false);
58+
expect(lastVersionChecked).to.be(kibanaVersion);
59+
});
60+
61+
it('should support sending true with allowChangingOptInStatus true', async () => {
62+
await updateSavedObjectAttributes(supertest, {
63+
...defaultAttributes,
64+
allowChangingOptInStatus: true,
65+
});
66+
await postTelemetryV2Optin(supertest, true, 200);
67+
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest);
68+
expect(enabled).to.be(true);
69+
expect(lastVersionChecked).to.be(kibanaVersion);
70+
});
71+
72+
it('should not support sending false with allowChangingOptInStatus false', async () => {
73+
await updateSavedObjectAttributes(supertest, {
74+
...defaultAttributes,
75+
allowChangingOptInStatus: false,
76+
});
77+
await postTelemetryV2Optin(supertest, false, 400);
78+
});
79+
80+
it('should not support sending true with allowChangingOptInStatus false', async () => {
81+
await updateSavedObjectAttributes(supertest, {
82+
...defaultAttributes,
83+
allowChangingOptInStatus: false,
84+
});
85+
await postTelemetryV2Optin(supertest, true, 400);
86+
});
87+
88+
it('should not support sending null', async () => {
89+
await postTelemetryV2Optin(supertest, null, 400);
90+
});
91+
92+
it('should not support sending junk', async () => {
93+
await postTelemetryV2Optin(supertest, 42, 400);
94+
});
95+
});
96+
}
97+
98+
async function postTelemetryV2Optin(supertest: any, value: any, statusCode: number): Promise<any> {
99+
const { body } = await supertest
100+
.post('/api/telemetry/v2/optIn')
101+
.set('kbn-xsrf', 'xxx')
102+
.send({ enabled: value })
103+
.expect(statusCode);
104+
105+
return body;
106+
}
107+
108+
async function updateSavedObjectAttributes(
109+
supertest: any,
110+
attributes: TelemetrySavedObjectAttributes
111+
): Promise<any> {
112+
return await supertest
113+
.post('/api/saved_objects/telemetry/telemetry')
114+
.query({ overwrite: true })
115+
.set('kbn-xsrf', 'xxx')
116+
.send({ attributes })
117+
.expect(200);
118+
}
119+
120+
async function getSavedObjectAttributes(supertest: any): Promise<TelemetrySavedObjectAttributes> {
121+
const { body } = await supertest.get('/api/saved_objects/telemetry/telemetry').expect(200);
122+
return body.attributes;
123+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import expect from '@kbn/expect';
21+
import _ from 'lodash';
22+
23+
/*
24+
* Create a single-level array with strings for all the paths to values in the
25+
* source object, up to 3 deep. Going deeper than 3 causes a bit too much churn
26+
* in the tests.
27+
*/
28+
function flatKeys(source) {
29+
const recursivelyFlatKeys = (obj, path = [], depth = 0) => {
30+
return depth < 3 && _.isObject(obj)
31+
? _.map(obj, (v, k) => recursivelyFlatKeys(v, [...path, k], depth + 1))
32+
: path.join('.');
33+
};
34+
35+
return _.uniq(_.flattenDeep(recursivelyFlatKeys(source))).sort((a, b) => a.localeCompare(b));
36+
}
37+
38+
export default function({ getService }) {
39+
const supertest = getService('supertest');
40+
41+
describe('/api/telemetry/v2/clusters/_stats', () => {
42+
it('should pull local stats and validate data types', async () => {
43+
const timeRange = {
44+
min: '2018-07-23T22:07:00Z',
45+
max: '2018-07-23T22:13:00Z',
46+
};
47+
48+
const { body } = await supertest
49+
.post('/api/telemetry/v2/clusters/_stats')
50+
.set('kbn-xsrf', 'xxx')
51+
.send({ timeRange, unencrypted: true })
52+
.expect(200);
53+
54+
expect(body.length).to.be(1);
55+
const stats = body[0];
56+
expect(stats.collection).to.be('local');
57+
expect(stats.stack_stats.kibana.count).to.be.a('number');
58+
expect(stats.stack_stats.kibana.indices).to.be.a('number');
59+
expect(stats.stack_stats.kibana.os.platforms[0].platform).to.be.a('string');
60+
expect(stats.stack_stats.kibana.os.platforms[0].count).to.be(1);
61+
expect(stats.stack_stats.kibana.os.platformReleases[0].platformRelease).to.be.a('string');
62+
expect(stats.stack_stats.kibana.os.platformReleases[0].count).to.be(1);
63+
expect(stats.stack_stats.kibana.plugins.telemetry.opt_in_status).to.be(false);
64+
expect(stats.stack_stats.kibana.plugins.telemetry.usage_fetcher).to.be.a('string');
65+
expect(stats.stack_stats.kibana.plugins.stack_management).to.be.an('object');
66+
expect(stats.stack_stats.kibana.plugins.ui_metric).to.be.an('object');
67+
expect(stats.stack_stats.kibana.plugins.application_usage).to.be.an('object');
68+
expect(stats.stack_stats.kibana.plugins.kql.defaultQueryLanguage).to.be.a('string');
69+
expect(stats.stack_stats.kibana.plugins['tsvb-validation']).to.be.an('object');
70+
expect(stats.stack_stats.kibana.plugins.localization).to.be.an('object');
71+
expect(stats.stack_stats.kibana.plugins.csp.strict).to.be(true);
72+
expect(stats.stack_stats.kibana.plugins.csp.warnLegacyBrowsers).to.be(true);
73+
expect(stats.stack_stats.kibana.plugins.csp.rulesChangedFromDefault).to.be(false);
74+
});
75+
76+
it('should pull local stats and validate fields', async () => {
77+
const timeRange = {
78+
min: '2018-07-23T22:07:00Z',
79+
max: '2018-07-23T22:13:00Z',
80+
};
81+
82+
const { body } = await supertest
83+
.post('/api/telemetry/v2/clusters/_stats')
84+
.set('kbn-xsrf', 'xxx')
85+
.send({ timeRange, unencrypted: true })
86+
.expect(200);
87+
88+
const stats = body[0];
89+
90+
const actual = flatKeys(stats);
91+
expect(actual).to.be.an('array');
92+
const expected = [
93+
'cluster_name',
94+
'cluster_stats.cluster_uuid',
95+
'cluster_stats.indices.analysis',
96+
'cluster_stats.indices.completion',
97+
'cluster_stats.indices.count',
98+
'cluster_stats.indices.docs',
99+
'cluster_stats.indices.fielddata',
100+
'cluster_stats.indices.mappings',
101+
'cluster_stats.indices.query_cache',
102+
'cluster_stats.indices.segments',
103+
'cluster_stats.indices.shards',
104+
'cluster_stats.indices.store',
105+
'cluster_stats.nodes.count',
106+
'cluster_stats.nodes.discovery_types',
107+
'cluster_stats.nodes.fs',
108+
'cluster_stats.nodes.ingest',
109+
'cluster_stats.nodes.jvm',
110+
'cluster_stats.nodes.network_types',
111+
'cluster_stats.nodes.os',
112+
'cluster_stats.nodes.packaging_types',
113+
'cluster_stats.nodes.plugins',
114+
'cluster_stats.nodes.process',
115+
'cluster_stats.nodes.versions',
116+
'cluster_stats.status',
117+
'cluster_stats.timestamp',
118+
'cluster_uuid',
119+
'collection',
120+
'collectionSource',
121+
'stack_stats.kibana.count',
122+
'stack_stats.kibana.indices',
123+
'stack_stats.kibana.os',
124+
'stack_stats.kibana.plugins',
125+
'stack_stats.kibana.versions',
126+
'timestamp',
127+
'version',
128+
];
129+
130+
expect(expected.every(m => actual.includes(m))).to.be.ok();
131+
});
132+
});
133+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import expect from '@kbn/expect';
21+
import { Client, DeleteDocumentParams, GetParams, GetResponse } from 'elasticsearch';
22+
import { TelemetrySavedObjectAttributes } from 'src/plugins/telemetry/server/telemetry_repository';
23+
import { FtrProviderContext } from '../../ftr_provider_context';
24+
25+
export default function optInTest({ getService }: FtrProviderContext) {
26+
const client: Client = getService('legacyEs');
27+
const supertest = getService('supertest');
28+
29+
describe('/api/telemetry/v2/userHasSeenNotice API Telemetry User has seen OptIn Notice', () => {
30+
it('should update telemetry setting field via PUT', async () => {
31+
try {
32+
await client.delete({
33+
index: '.kibana',
34+
id: 'telemetry:telemetry',
35+
} as DeleteDocumentParams);
36+
} catch (err) {
37+
if (err.statusCode !== 404) {
38+
throw err;
39+
}
40+
}
41+
42+
await supertest
43+
.put('/api/telemetry/v2/userHasSeenNotice')
44+
.set('kbn-xsrf', 'xxx')
45+
.expect(200);
46+
47+
const {
48+
_source: { telemetry },
49+
}: GetResponse<{
50+
telemetry: TelemetrySavedObjectAttributes;
51+
}> = await client.get({
52+
index: '.kibana',
53+
id: 'telemetry:telemetry',
54+
} as GetParams);
55+
56+
expect(telemetry.userHasSeenNotice).to.be(true);
57+
});
58+
});
59+
}

0 commit comments

Comments
 (0)