2018-07-02 06:18:23 -05:00
|
|
|
import config from 'app/core/config';
|
|
|
|
import 'app/features/plugins/datasource_srv';
|
|
|
|
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2020-03-24 10:03:53 -05:00
|
|
|
import { DataSourcePluginMeta, PluginMeta } from '@grafana/data';
|
2018-07-02 06:18:23 -05:00
|
|
|
|
2018-07-06 08:32:08 -05:00
|
|
|
// Datasource variable $datasource with current value 'BBB'
|
2019-07-18 01:03:04 -05:00
|
|
|
const templateSrv: any = {
|
2020-03-24 10:03:53 -05:00
|
|
|
getVariables: () => [
|
2018-07-06 08:32:08 -05:00
|
|
|
{
|
|
|
|
type: 'datasource',
|
|
|
|
name: 'datasource',
|
|
|
|
current: {
|
|
|
|
value: 'BBB',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
describe('datasource_srv', () => {
|
2019-12-05 03:04:03 -06:00
|
|
|
const _datasourceSrv = new DatasourceSrv({} as any, {} as any, templateSrv);
|
2018-07-13 02:09:36 -05:00
|
|
|
|
2018-12-07 07:11:40 -06:00
|
|
|
describe('when loading external datasources', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
config.datasources = {
|
|
|
|
buildInDs: {
|
2019-04-12 12:13:36 -05:00
|
|
|
id: 1,
|
2020-04-20 08:48:38 -05:00
|
|
|
uid: '1',
|
2019-04-04 11:30:15 -05:00
|
|
|
type: 'b',
|
2018-12-07 07:11:40 -06:00
|
|
|
name: 'buildIn',
|
2019-04-29 09:38:17 -05:00
|
|
|
meta: { builtIn: true } as DataSourcePluginMeta,
|
2019-04-04 11:30:15 -05:00
|
|
|
jsonData: {},
|
2018-12-07 07:11:40 -06:00
|
|
|
},
|
|
|
|
nonBuildIn: {
|
2019-04-12 12:13:36 -05:00
|
|
|
id: 2,
|
2020-04-20 08:48:38 -05:00
|
|
|
uid: '2',
|
2019-04-04 11:30:15 -05:00
|
|
|
type: 'e',
|
2018-12-07 07:11:40 -06:00
|
|
|
name: 'external1',
|
2019-04-29 09:38:17 -05:00
|
|
|
meta: { builtIn: false } as DataSourcePluginMeta,
|
2019-04-04 11:30:15 -05:00
|
|
|
jsonData: {},
|
2018-12-07 07:11:40 -06:00
|
|
|
},
|
|
|
|
nonExplore: {
|
2019-04-12 12:13:36 -05:00
|
|
|
id: 3,
|
2020-04-20 08:48:38 -05:00
|
|
|
uid: '3',
|
2019-04-04 11:30:15 -05:00
|
|
|
type: 'e2',
|
2018-12-07 07:11:40 -06:00
|
|
|
name: 'external2',
|
2019-04-04 11:30:15 -05:00
|
|
|
meta: {} as PluginMeta,
|
|
|
|
jsonData: {},
|
2018-12-07 07:11:40 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return list of explore sources', () => {
|
|
|
|
const externalSources = _datasourceSrv.getExternal();
|
|
|
|
expect(externalSources.length).toBe(2);
|
|
|
|
expect(externalSources[0].name).toBe('external1');
|
|
|
|
expect(externalSources[1].name).toBe('external2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-02 06:18:23 -05:00
|
|
|
describe('when loading metric sources', () => {
|
2019-07-18 01:03:04 -05:00
|
|
|
let metricSources: any;
|
2018-08-26 10:14:40 -05:00
|
|
|
const unsortedDatasources = {
|
2018-07-02 06:18:23 -05:00
|
|
|
mmm: {
|
|
|
|
type: 'test-db',
|
|
|
|
meta: { metrics: { m: 1 } },
|
|
|
|
},
|
|
|
|
'--Grafana--': {
|
|
|
|
type: 'grafana',
|
|
|
|
meta: { builtIn: true, metrics: { m: 1 }, id: 'grafana' },
|
|
|
|
},
|
|
|
|
'--Mixed--': {
|
|
|
|
type: 'test-db',
|
|
|
|
meta: { builtIn: true, metrics: { m: 1 }, id: 'mixed' },
|
|
|
|
},
|
|
|
|
ZZZ: {
|
|
|
|
type: 'test-db',
|
|
|
|
meta: { metrics: { m: 1 } },
|
|
|
|
},
|
|
|
|
aaa: {
|
|
|
|
type: 'test-db',
|
|
|
|
meta: { metrics: { m: 1 } },
|
|
|
|
},
|
|
|
|
BBB: {
|
|
|
|
type: 'test-db',
|
|
|
|
meta: { metrics: { m: 1 } },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
beforeEach(() => {
|
2019-04-04 11:30:15 -05:00
|
|
|
config.datasources = unsortedDatasources as any;
|
2018-07-06 08:32:08 -05:00
|
|
|
metricSources = _datasourceSrv.getMetricSources({});
|
|
|
|
config.defaultDatasource = 'BBB';
|
2018-07-02 06:18:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a list of sources sorted case insensitively with builtin sources last', () => {
|
2018-07-06 08:32:08 -05:00
|
|
|
expect(metricSources[1].name).toBe('aaa');
|
|
|
|
expect(metricSources[2].name).toBe('BBB');
|
|
|
|
expect(metricSources[3].name).toBe('mmm');
|
|
|
|
expect(metricSources[4].name).toBe('ZZZ');
|
|
|
|
expect(metricSources[5].name).toBe('--Grafana--');
|
|
|
|
expect(metricSources[6].name).toBe('--Mixed--');
|
2018-07-02 06:18:23 -05:00
|
|
|
});
|
|
|
|
|
2018-07-06 08:32:08 -05:00
|
|
|
it('should set default data source', () => {
|
|
|
|
expect(metricSources[3].name).toBe('default');
|
|
|
|
expect(metricSources[3].sort).toBe('BBB');
|
2018-07-02 06:18:23 -05:00
|
|
|
});
|
|
|
|
|
2018-07-06 08:32:08 -05:00
|
|
|
it('should set default inject the variable datasources', () => {
|
|
|
|
expect(metricSources[0].name).toBe('$datasource');
|
|
|
|
expect(metricSources[0].sort).toBe('$datasource');
|
2018-07-02 06:18:23 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|