Commit 68409687 by Erik Sundell Committed by Daniel Lee

Adds unit tests to test datasource

parent f9b75244
......@@ -2,18 +2,14 @@
export default class StackdriverDatasource {
url: string;
baseUrl: string;
cloudName: string;
constructor(instanceSettings, private backendSrv) {
this.cloudName = 'stackdriver';
this.baseUrl = `/${this.cloudName}/`;
this.baseUrl = `/stackdriver/`;
this.url = instanceSettings.url;
}
testDatasource() {
const path = `v3/projects/raintank-production/timeSeries?aggregation.crossSeriesReducer=
REDUCE_NONE&filter=metric.type%20%3D%20%22compute.googleapis.com%2Finstance%2Fcpu%2Fusage_time%
22&aggregation.perSeriesAligner=ALIGN_NONE&interval.startTime=2018-09-04T05%3A16%3A02.383Z&interval.endTime=2018-09-04T11%3A16%3A02.383Z`;
const path = `v3/projects/raintank-production/metricDescriptors`;
return this.doRequest(`${this.baseUrl}${path}`)
.then(response => {
if (response.status === 200) {
......@@ -22,22 +18,17 @@ export default class StackdriverDatasource {
message: 'Successfully queried the Azure Monitor service.',
title: 'Success',
};
} else {
throw new Error();
}
})
.catch(error => {
let message = 'Azure Monitor: ';
let message = 'Stackdriver: ';
message += error.statusText ? error.statusText + ': ' : '';
if (error.data && error.data.error && error.data.error.code) {
// 400, 401
message += error.data.error.code + '. ' + error.data.error.message;
} else if (error.data && error.data.error) {
message += error.data.error;
} else if (error.data) {
message += error.data;
} else {
message += 'Cannot connect to Azure Monitor REST API.';
message += 'Cannot connect to Stackdriver API';
}
return {
status: 'error',
......
import StackdriverDataSource from '../datasource';
import { metricDescriptors } from './testData';
describe('StackdriverDataSource', () => {
describe('when performing testDataSource', () => {
describe('and call to stackdriver api succeeds', () => {
let ds;
let result;
beforeEach(async () => {
const backendSrv = {
async datasourceRequest() {
return Promise.resolve({ status: 200 });
},
};
ds = new StackdriverDataSource({}, backendSrv);
result = await ds.testDatasource();
});
it('should return successfully', () => {
expect(result.status).toBe('success');
});
});
describe('and a list of metricDescriptors are returned', () => {
let ds;
let result;
beforeEach(async () => {
console.log('erik', metricDescriptors);
const backendSrv = {
datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
};
ds = new StackdriverDataSource({}, backendSrv);
result = await ds.testDatasource();
});
it('should return status success', () => {
expect(result.status).toBe('success');
});
});
describe('and call to stackdriver api fails with 400 error', () => {
let ds;
let result;
beforeEach(async () => {
const backendSrv = {
datasourceRequest: async () =>
Promise.reject({
statusText: 'Bad Request',
data: { error: { code: 400, message: 'Field interval.endTime had an invalid value' } },
}),
};
ds = new StackdriverDataSource({}, backendSrv);
result = await ds.testDatasource();
});
it('should return error status and a detailed error message', () => {
expect(result.status).toEqual('error');
expect(result.message).toBe('Stackdriver: Bad Request: 400. Field interval.endTime had an invalid value');
});
});
});
});
export const metricDescriptors = [
{
name: 'projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/api_request_count',
labels: [
{
key: 'state',
description: 'Request state',
},
],
metricKind: 'CUMULATIVE',
valueType: 'INT64',
unit: '1',
description: 'API request count',
displayName: 'API Request Count',
type: 'agent.googleapis.com/agent/api_request_count',
metadata: {
launchStage: 'GA',
samplePeriod: '60s',
ingestDelay: '0s',
},
},
{
name: 'projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/log_entry_count',
labels: [
{
key: 'response_code',
description: 'HTTP response code',
},
],
metricKind: 'CUMULATIVE',
valueType: 'INT64',
unit: '1',
description: 'Count of log entry writes',
displayName: 'Log Entry Count',
type: 'agent.googleapis.com/agent/log_entry_count',
metadata: {
launchStage: 'GA',
samplePeriod: '60s',
ingestDelay: '0s',
},
},
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment