kibana/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.test.ts
2020-05-22 09:08:58 +02:00

75 lines
2.3 KiB
TypeScript

/*
* 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 sinon from 'sinon';
import {
getClusterUuids,
fetchClusterUuids,
handleClusterUuidsResponse,
} from './get_cluster_uuids';
describe('get_cluster_uuids', () => {
const callCluster = sinon.stub();
const response = {
aggregations: {
cluster_uuids: {
buckets: [{ key: 'abc' }, { key: 'xyz' }, { key: '123' }],
},
},
};
const expectedUuids = response.aggregations.cluster_uuids.buckets
.map((bucket) => bucket.key)
.map((expectedUuid) => ({ clusterUuid: expectedUuid }));
const start = new Date().toISOString();
const end = new Date().toISOString();
describe('getClusterUuids', () => {
it('returns cluster UUIDs', async () => {
callCluster.withArgs('search').returns(Promise.resolve(response));
expect(
await getClusterUuids({ callCluster, start, end, usageCollection: {} as any }, {
maxBucketSize: 1,
} as any)
).toStrictEqual(expectedUuids);
});
});
describe('fetchClusterUuids', () => {
it('searches for clusters', async () => {
callCluster.returns(Promise.resolve(response));
expect(
await fetchClusterUuids({ callCluster, start, end, usageCollection: {} as any }, {
maxBucketSize: 1,
} as any)
).toStrictEqual(response);
});
});
describe('handleClusterUuidsResponse', () => {
// filterPath makes it easy to ignore anything unexpected because it will come back empty
it('handles unexpected response', () => {
const clusterUuids = handleClusterUuidsResponse({});
expect(clusterUuids.length).toStrictEqual(0);
});
it('handles valid response', () => {
const clusterUuids = handleClusterUuidsResponse(response);
expect(clusterUuids).toStrictEqual(expectedUuids);
});
it('handles no buckets response', () => {
const clusterUuids = handleClusterUuidsResponse({
aggregations: {
cluster_uuids: {
buckets: [],
},
},
});
expect(clusterUuids.length).toStrictEqual(0);
});
});
});