| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- import StackdriverDataSource from '../datasource';
- import { metricDescriptors } from './testData';
- import moment from 'moment';
- import { TemplateSrvStub } from 'test/specs/helpers';
- describe('StackdriverDataSource', () => {
- const instanceSettings = {
- jsonData: {
- projectName: 'testproject',
- },
- };
- const templateSrv = new TemplateSrvStub();
- const timeSrv = {};
- 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(instanceSettings, backendSrv, templateSrv, timeSrv);
- 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 () => {
- const backendSrv = {
- datasourceRequest: async () => Promise.resolve({ status: 200, data: metricDescriptors }),
- };
- ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
- 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(instanceSettings, backendSrv, templateSrv, timeSrv);
- 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');
- });
- });
- });
- describe('when performing getProjects', () => {
- describe('and call to resource manager api succeeds', () => {
- let ds;
- let result;
- beforeEach(async () => {
- const response = {
- projects: [
- {
- projectNumber: '853996325002',
- projectId: 'test-project',
- lifecycleState: 'ACTIVE',
- name: 'Test Project',
- createTime: '2015-06-02T14:16:08.520Z',
- parent: {
- type: 'organization',
- id: '853996325002',
- },
- },
- ],
- };
- const backendSrv = {
- async datasourceRequest() {
- return Promise.resolve({ status: 200, data: response });
- },
- };
- ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
- result = await ds.getProjects();
- });
- it('should return successfully', () => {
- expect(result.length).toBe(1);
- expect(result[0].id).toBe('test-project');
- expect(result[0].name).toBe('Test Project');
- });
- });
- });
- describe('When performing query', () => {
- const options = {
- range: {
- from: moment.utc('2017-08-22T20:00:00Z'),
- to: moment.utc('2017-08-22T23:59:00Z'),
- },
- rangeRaw: {
- from: 'now-4h',
- to: 'now',
- },
- targets: [
- {
- refId: 'A',
- aggregation: {},
- },
- ],
- };
- describe('and no time series data is returned', () => {
- let ds;
- const response = {
- results: {
- A: {
- refId: 'A',
- meta: {
- rawQuery: 'arawquerystring',
- },
- series: null,
- tables: null,
- },
- },
- };
- beforeEach(() => {
- const backendSrv = {
- datasourceRequest: async () => Promise.resolve({ status: 200, data: response }),
- };
- ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
- });
- it('should return a list of datapoints', () => {
- return ds.query(options).then(results => {
- expect(results.data.length).toBe(0);
- });
- });
- });
- });
- describe('when performing getMetricTypes', () => {
- describe('and call to stackdriver api succeeds', () => {});
- let ds;
- let result;
- beforeEach(async () => {
- const backendSrv = {
- async datasourceRequest() {
- return Promise.resolve({
- data: {
- metricDescriptors: [
- {
- displayName: 'test metric name 1',
- type: 'test metric type 1',
- },
- {
- displayName: 'test metric name 2',
- type: 'test metric type 2',
- },
- ],
- },
- });
- },
- };
- ds = new StackdriverDataSource(instanceSettings, backendSrv, templateSrv, timeSrv);
- result = await ds.getMetricTypes();
- });
- it('should return successfully', () => {
- expect(result.length).toBe(2);
- expect(result[0].type).toBe('test metric type 1');
- expect(result[0].displayName).toBe('test metric name 1');
- });
- });
- describe('when interpolating a template variable for group bys', () => {
- let interpolated;
- describe('and is single value variable', () => {
- beforeEach(() => {
- templateSrv.data = {
- test: 'groupby1',
- };
- const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
- interpolated = ds.interpolateGroupBys(['[[test]]'], {});
- });
- it('should replace the variable with the value', () => {
- expect(interpolated.length).toBe(1);
- expect(interpolated[0]).toBe('groupby1');
- });
- });
- describe('and is multi value variable', () => {
- beforeEach(() => {
- templateSrv.data = {
- test: 'groupby1,groupby2',
- };
- const ds = new StackdriverDataSource(instanceSettings, {}, templateSrv, timeSrv);
- interpolated = ds.interpolateGroupBys(['[[test]]'], {});
- });
- it('should replace the variable with an array of group bys', () => {
- expect(interpolated.length).toBe(2);
- expect(interpolated[0]).toBe('groupby1');
- expect(interpolated[1]).toBe('groupby2');
- });
- });
- });
- });
|