Commit fc7edab8 by Timothy Palpant Committed by GitHub

Plugins: Implement testDatasource for Jaeger (#28916)

* Implement testDatasource for Jaeger

* Unit tests

* Run prettier
parent 9e5f0a48
...@@ -53,6 +53,103 @@ describe('JaegerDatasource', () => { ...@@ -53,6 +53,103 @@ describe('JaegerDatasource', () => {
}); });
}); });
describe('when performing testDataSource', () => {
describe('and call succeeds', () => {
it('should return successfully', async () => {
const backendSrvMock = makeTestDatasourceMock(
Promise.resolve({
statusText: 'OK',
status: 200,
data: {
data: ['service1'],
},
})
);
await withMockedBackendSrv(backendSrvMock, async () => {
const ds = new JaegerDatasource(defaultSettings);
const response = await ds.testDatasource();
expect(response.status).toEqual('success');
expect(response.message).toBe('Data source connected and services found.');
});
});
});
describe('and call succeeds, but returns no services', () => {
it('should display an error', async () => {
const backendSrvMock = makeTestDatasourceMock(
Promise.resolve({
statusText: 'OK',
status: 200,
})
);
await withMockedBackendSrv(backendSrvMock, async () => {
const ds = new JaegerDatasource(defaultSettings);
const response = await ds.testDatasource();
expect(response.status).toEqual('error');
expect(response.message).toBe(
'Data source connected, but no services received. Verify that Jaeger is configured properly.'
);
});
});
});
describe('and call returns error with message', () => {
it('should return the formatted error', async () => {
const backendSrvMock = {
datasourceRequest(options: BackendSrvRequest): Promise<any> {
return Promise.reject({
statusText: 'Not found',
status: 404,
data: {
message: '404 page not found',
},
});
},
} as BackendSrv;
await withMockedBackendSrv(backendSrvMock, async () => {
const ds = new JaegerDatasource(defaultSettings);
const response = await ds.testDatasource();
expect(response.status).toEqual('error');
expect(response.message).toBe('Jaeger: Not found. 404. 404 page not found');
});
});
});
describe('and call returns error without message', () => {
it('should return JSON error', async () => {
const backendSrvMock = {
datasourceRequest(options: BackendSrvRequest): Promise<any> {
return Promise.reject({
statusText: 'Bad gateway',
status: 502,
data: {
errors: ['Could not connect to Jaeger backend'],
},
});
},
} as BackendSrv;
await withMockedBackendSrv(backendSrvMock, async () => {
const ds = new JaegerDatasource(defaultSettings);
const response = await ds.testDatasource();
expect(response.status).toEqual('error');
expect(response.message).toBe('Jaeger: Bad gateway. 502. {"errors":["Could not connect to Jaeger backend"]}');
});
});
});
});
function makeTestDatasourceMock(result: Promise<any>) {
return {
datasourceRequest(options: BackendSrvRequest): Promise<any> {
return result;
},
} as BackendSrv;
}
function makeBackendSrvMock(traceId: string) { function makeBackendSrvMock(traceId: string) {
return { return {
datasourceRequest(options: BackendSrvRequest): Promise<any> { datasourceRequest(options: BackendSrvRequest): Promise<any> {
......
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
} from '@grafana/data'; } from '@grafana/data';
import { getBackendSrv, BackendSrvRequest } from '@grafana/runtime'; import { getBackendSrv, BackendSrvRequest } from '@grafana/runtime';
import { Observable, from, of } from 'rxjs'; import { Observable, from, of } from 'rxjs';
import { map } from 'rxjs/operators'; import { catchError, map } from 'rxjs/operators';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { serializeParams } from 'app/core/utils/fetch'; import { serializeParams } from 'app/core/utils/fetch';
...@@ -77,7 +77,41 @@ export class JaegerDatasource extends DataSourceApi<JaegerQuery> { ...@@ -77,7 +77,41 @@ export class JaegerDatasource extends DataSourceApi<JaegerQuery> {
} }
async testDatasource(): Promise<any> { async testDatasource(): Promise<any> {
return true; return this._request('/api/services')
.pipe(
map(res => {
const values: any[] = res?.data?.data || [];
const testResult =
values.length > 0
? { status: 'success', message: 'Data source connected and services found.' }
: {
status: 'error',
message:
'Data source connected, but no services received. Verify that Jaeger is configured properly.',
};
return testResult;
}),
catchError((err: any) => {
let message = 'Jaeger: ';
if (err.statusText) {
message += err.statusText;
} else {
message += 'Cannot connect to Jaeger';
}
if (err.status) {
message += `. ${err.status}`;
}
if (err.data && err.data.message) {
message += `. ${err.data.message}`;
} else if (err.data) {
message += `. ${JSON.stringify(err.data)}`;
}
return of({ status: 'error', message: message });
})
)
.toPromise();
} }
getTimeRange(): { start: number; end: number } { getTimeRange(): { start: number; end: number } {
......
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