123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- import { from } from 'rxjs';
- import { DataQueryRequest, DataQueryResponseData, toDataFrame } from '@grafana/data';
- import createMockDatasource from './__mocks__/datasource';
- import { invalidSubscriptionError } from './__mocks__/errors';
- import { AzureMonitorQuery, AzureQueryType } from './types';
- import { VariableSupport } from './variables';
- jest.mock('@grafana/runtime', () => ({
- ...(jest.requireActual('@grafana/runtime') as unknown as object),
- getTemplateSrv: () => ({
- replace: (val: string) => {
- return val;
- },
- }),
- }));
- describe('VariableSupport', () => {
- describe('querying for grafana template variable fns', () => {
- it('can fetch subscriptions', (done) => {
- const fakeSubscriptions = ['subscriptionId'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- getSubscriptions: jest.fn().mockResolvedValueOnce(fakeSubscriptions),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'SubscriptionsQuery',
- rawQuery: 'Subscriptions()',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(fakeSubscriptions);
- done();
- });
- });
- it('can fetch resourceGroups with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- getResourceGroups: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'ResourceGroupsQuery',
- rawQuery: 'ResourceGroups()',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch resourceGroups with a subscriptionId arg', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- getResourceGroups: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'ResourceGroupsQuery',
- rawQuery: 'ResourceGroups(sub)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch metricDefinitions with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- getMetricDefinitions: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricDefinitionsQuery',
- rawQuery: 'Namespaces(resourceGroup)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch metricDefinitions with a subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- getMetricDefinitions: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricDefinitionsQuery',
- rawQuery: 'Namespaces(resourceGroup, subscriptionId)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch resourceNames with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- getResourceNames: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'ResourceNamesQuery',
- rawQuery: 'ResourceNames(resourceGroup, metricDefinition)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch resourceNames with a subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- getResourceNames: jest.fn().mockResolvedValueOnce(expectedResults),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'ResourceNamesQuery',
- rawQuery: 'ResourceNames(subscriptionId, resourceGroup, metricDefinition)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch a metricNamespace with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- azureMonitorDatasource: {
- getMetricNamespaces: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricNamespaceQuery',
- rawQuery: 'metricNamespace(resourceGroup, metricDefinition, resourceName)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch a metricNamespace with a subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureMonitorDatasource: {
- getMetricNamespaces: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricNamespaceQuery',
- rawQuery: 'metricNamespace(subscriptionId, resourceGroup, metricDefinition, resourceName)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch metricNames with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- azureMonitorDatasource: {
- getMetricNames: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricNamesQuery',
- rawQuery: 'metricNames(resourceGroup, metricDefinition, resourceName, metricNamespace)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch metricNames with a subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureMonitorDatasource: {
- getMetricNames: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricNamesQuery',
- rawQuery: 'metricNames(subscription, resourceGroup, metricDefinition, resourceName, metricNamespace)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch workspaces with a default subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- getWorkspaces: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'WorkspacesQuery',
- rawQuery: 'workspaces()',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can fetch workspaces with a subscriptionId', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- getWorkspaces: jest.fn().mockResolvedValueOnce(expectedResults),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'WorkspacesQuery',
- rawQuery: 'workspaces(subscriptionId)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('can handle legacy string queries', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureMonitorDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- getMetricDefinitions: jest.fn((sub: string, rg: string) => {
- if (sub === 'subscriptionId' && rg === 'resourceGroup') {
- return Promise.resolve(expectedResults);
- }
- return Promise.resolve([`getMetricDefinitions unexpected input: ${sub}, ${rg}`]);
- }),
- })
- );
- const mockRequest = {
- targets: ['Namespaces(subscriptionId, resourceGroup)' as unknown as AzureMonitorQuery],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('returns an empty array for unknown queries', (done) => {
- const variableSupport = new VariableSupport(createMockDatasource());
- const mockRequest = {
- targets: [
- {
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- rawQuery: 'nonsense',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data).toEqual([]);
- done();
- });
- });
- it('should return None when there is no data', (done) => {
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- azureMonitorDatasource: {
- getMetricNames: jest.fn().mockResolvedValueOnce([]),
- },
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'MetricNamesQuery',
- rawQuery: 'metricNames(resourceGroup, metricDefinition, resourceName, metricNamespace)',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data.length).toBe(0);
- done();
- });
- });
- });
- it('passes on the query to the main datasource for all non-grafana template variable fns', (done) => {
- const expectedResults = ['test'];
- const variableSupport = new VariableSupport(
- createMockDatasource({
- query: () =>
- from(
- Promise.resolve({
- data: [toDataFrame(expectedResults)],
- })
- ),
- })
- );
- const mockRequest = {
- targets: [
- {
- queryType: AzureQueryType.LogAnalytics,
- azureLogAnalytics: {
- query: 'some log thing',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.data[0].source).toEqual(expectedResults);
- done();
- });
- });
- it('should handle http error', (done) => {
- const error = invalidSubscriptionError();
- const variableSupport = new VariableSupport(
- createMockDatasource({
- azureLogAnalyticsDatasource: {
- defaultSubscriptionId: 'defaultSubscriptionId',
- },
- getResourceGroups: jest.fn().mockRejectedValue(error),
- })
- );
- const mockRequest = {
- targets: [
- {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: {
- kind: 'ResourceGroupsQuery',
- rawQuery: 'ResourceGroups()',
- },
- } as AzureMonitorQuery,
- ],
- } as DataQueryRequest<AzureMonitorQuery>;
- const observables = variableSupport.query(mockRequest);
- observables.subscribe((result: DataQueryResponseData) => {
- expect(result.error?.message).toBe(error.data.error.message);
- done();
- });
- });
- });
|