2020-10-29 11:05:36 -05:00
|
|
|
import { DataQueryResponse, dateTime, DefaultTimeRange } from '@grafana/data';
|
2020-05-06 10:54:24 -05:00
|
|
|
import { setBackendSrv } from '@grafana/runtime';
|
2020-10-29 11:05:36 -05:00
|
|
|
import { TemplateSrv } from '../../../features/templating/template_srv';
|
|
|
|
import { CloudWatchDatasource } from './datasource';
|
2020-05-06 10:54:24 -05:00
|
|
|
|
|
|
|
describe('datasource', () => {
|
2020-05-11 05:53:42 -05:00
|
|
|
describe('query', () => {
|
|
|
|
it('should return error if log query and log groups is not specified', async () => {
|
|
|
|
const { datasource } = setup();
|
2020-07-09 07:11:13 -05:00
|
|
|
const response: DataQueryResponse = (await datasource
|
|
|
|
.query({
|
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
queryMode: 'Logs' as 'Logs',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} as any)
|
|
|
|
.toPromise()) as any;
|
2020-06-18 04:35:11 -05:00
|
|
|
expect(response.error?.message).toBe('Log group is required');
|
2020-05-11 05:53:42 -05:00
|
|
|
});
|
2020-05-06 10:54:24 -05:00
|
|
|
|
2020-05-11 05:53:42 -05:00
|
|
|
it('should return empty response if queries are hidden', async () => {
|
|
|
|
const { datasource } = setup();
|
2020-07-09 07:11:13 -05:00
|
|
|
const response: DataQueryResponse = (await datasource
|
|
|
|
.query({
|
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
queryMode: 'Logs' as 'Logs',
|
|
|
|
hide: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} as any)
|
|
|
|
.toPromise()) as any;
|
2020-05-11 05:53:42 -05:00
|
|
|
expect(response.data).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-29 11:05:36 -05:00
|
|
|
describe('performTimeSeriesQuery', () => {
|
|
|
|
it('should return the same length of data as result', async () => {
|
|
|
|
const { datasource } = setup();
|
|
|
|
const awsRequestMock = jest.spyOn(datasource, 'awsRequest');
|
|
|
|
const buildCloudwatchConsoleUrlMock = jest.spyOn(datasource, 'buildCloudwatchConsoleUrl');
|
|
|
|
buildCloudwatchConsoleUrlMock.mockImplementation(() => '');
|
|
|
|
awsRequestMock.mockImplementation(async () => {
|
|
|
|
return {
|
|
|
|
results: {
|
|
|
|
a: { refId: 'a', series: [{ name: 'cpu', points: [1, 1] }], meta: { gmdMeta: '' } },
|
|
|
|
b: { refId: 'b', series: [{ name: 'memory', points: [2, 2] }], meta: { gmdMeta: '' } },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const response: DataQueryResponse = await datasource.performTimeSeriesQuery(
|
|
|
|
{
|
|
|
|
queries: [
|
|
|
|
{ datasourceId: 1, refId: 'a' },
|
|
|
|
{ datasourceId: 1, refId: 'b' },
|
|
|
|
],
|
|
|
|
} as any,
|
|
|
|
{ from: dateTime(), to: dateTime() } as any
|
|
|
|
);
|
|
|
|
expect(response.data.length).toEqual(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-11 05:53:42 -05:00
|
|
|
describe('describeLogGroup', () => {
|
|
|
|
it('replaces region correctly in the query', async () => {
|
|
|
|
const { datasource, datasourceRequestMock } = setup();
|
2020-05-06 10:54:24 -05:00
|
|
|
await datasource.describeLogGroups({ region: 'default' });
|
|
|
|
expect(datasourceRequestMock.mock.calls[0][0].data.queries[0].region).toBe('us-west-1');
|
|
|
|
|
|
|
|
await datasource.describeLogGroups({ region: 'eu-east' });
|
|
|
|
expect(datasourceRequestMock.mock.calls[1][0].data.queries[0].region).toBe('eu-east');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-11 05:53:42 -05:00
|
|
|
|
|
|
|
function setup() {
|
|
|
|
const datasource = new CloudWatchDatasource({ jsonData: { defaultRegion: 'us-west-1' } } as any, new TemplateSrv(), {
|
|
|
|
timeRange() {
|
|
|
|
return DefaultTimeRange;
|
|
|
|
},
|
|
|
|
} as any);
|
|
|
|
const datasourceRequestMock = jest.fn();
|
|
|
|
datasourceRequestMock.mockResolvedValue({ data: [] });
|
|
|
|
setBackendSrv({ datasourceRequest: datasourceRequestMock } as any);
|
|
|
|
|
|
|
|
return { datasource, datasourceRequestMock };
|
|
|
|
}
|