diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/__mocks__/query.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/__mocks__/query.ts index 2b499896b5e..9da3bf31761 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/__mocks__/query.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/__mocks__/query.ts @@ -1,18 +1,30 @@ import { AzureMonitorQuery, AzureQueryType } from '../types'; -export default function createMockQuery(): AzureMonitorQuery { +export default function createMockQuery(overrides?: Partial): AzureMonitorQuery { return { + queryType: AzureQueryType.AzureMonitor, + refId: 'A', + subscription: '99999999-cccc-bbbb-aaaa-9106972f9572', + subscriptions: ['99999999-cccc-bbbb-aaaa-9106972f9572'], + datasource: { + type: 'grafana-azure-monitor-datasource', + uid: 'AAAAA11111BBBBB22222CCCC', + }, + ...overrides, + azureLogAnalytics: { query: '//change this example to create your own time series query\n //the table to query (e.g. Usage, Heartbeat, Perf)\n| where $__timeFilter(TimeGenerated) //this is a macro used to show the full chart’s time range, choose the datetime column here\n| summarize count() by , bin(TimeGenerated, $__interval) //change “group by column” to a column in your table, such as “Computer”. The $__interval macro is used to auto-select the time grain. Can also use 1h, 5m etc.\n| order by TimeGenerated asc', resultFormat: 'time_series', workspace: 'e3fe4fde-ad5e-4d60-9974-e2f3562ffdf2', resource: 'test-resource', + ...overrides?.azureLogAnalytics, }, azureResourceGraph: { query: 'Resources | summarize count()', resultFormat: 'table', + ...overrides?.azureResourceGraph, }, azureMonitor: { @@ -32,15 +44,7 @@ export default function createMockQuery(): AzureMonitorQuery { alias: '', // timeGrains: [], top: '10', - }, - - queryType: AzureQueryType.AzureMonitor, - refId: 'A', - subscription: '99999999-cccc-bbbb-aaaa-9106972f9572', - subscriptions: ['99999999-cccc-bbbb-aaaa-9106972f9572'], - datasource: { - type: 'grafana-azure-monitor-datasource', - uid: 'AAAAA11111BBBBB22222CCCC', + ...overrides?.azureMonitor, }, }; } diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.test.ts new file mode 100644 index 00000000000..f0677e6a1a8 --- /dev/null +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.test.ts @@ -0,0 +1,37 @@ +import { createMockInstanceSetttings } from './__mocks__/instanceSettings'; +import createMockQuery from './__mocks__/query'; +import Datasource from './datasource'; + +describe('Azure Monitor Datasource', () => { + describe('interpolateVariablesInQueries()', () => { + it('should interpolate variables in the queries', () => { + const ds = new Datasource(createMockInstanceSetttings()); + const queries = [createMockQuery({ azureMonitor: { resourceGroup: '$resourceGroup' } })]; + + const interpolatedQueries = ds.interpolateVariablesInQueries(queries, { + resourceGroup: { text: 'the-resource-group', value: 'the-resource-group' }, + }); + + expect(interpolatedQueries).toContainEqual( + expect.objectContaining({ + azureMonitor: expect.objectContaining({ resourceGroup: 'the-resource-group' }), + }) + ); + }); + + it('should include a datasource ref when interpolating queries', () => { + const ds = new Datasource(createMockInstanceSetttings()); + const query = createMockQuery(); + delete query.datasource; + const queries = [query]; + + const interpolatedQueries = ds.interpolateVariablesInQueries(queries, {}); + + expect(interpolatedQueries).toContainEqual( + expect.objectContaining({ + datasource: expect.objectContaining({ type: 'azuremonitor', uid: 'abc' }), + }) + ); + }); + }); +}); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts index 59ebd9d1bc0..f5adb0410cd 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts @@ -203,7 +203,10 @@ export default class Datasource extends DataSourceApi