mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Postgres: Fix test datasource always returns success (#43762)
This commit is contained in:
parent
20574c130a
commit
85246d7179
66
public/app/plugins/datasource/postgres/datasource.test.ts
Normal file
66
public/app/plugins/datasource/postgres/datasource.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { backendSrv } from 'app/core/services/backend_srv';
|
||||||
|
import { of, throwError } from 'rxjs';
|
||||||
|
import { createFetchResponse } from 'test/helpers/createFetchResponse';
|
||||||
|
import { PostgresDatasource } from './datasource';
|
||||||
|
|
||||||
|
jest.mock('@grafana/runtime', () => ({
|
||||||
|
...(jest.requireActual('@grafana/runtime') as any),
|
||||||
|
getBackendSrv: () => backendSrv,
|
||||||
|
getTemplateSrv: () => ({
|
||||||
|
replace: (val: string): string => {
|
||||||
|
return val;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('Postgres datasource', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when performing testDatasource call', () => {
|
||||||
|
it('should return the error from the server', async () => {
|
||||||
|
setupFetchMock(
|
||||||
|
undefined,
|
||||||
|
throwError(() => ({
|
||||||
|
status: 400,
|
||||||
|
statusText: 'Bad Request',
|
||||||
|
data: {
|
||||||
|
results: {
|
||||||
|
meta: {
|
||||||
|
error: 'db query error: pq: password authentication failed for user "postgres"',
|
||||||
|
frames: [
|
||||||
|
{
|
||||||
|
schema: {
|
||||||
|
refId: 'meta',
|
||||||
|
meta: {
|
||||||
|
executedQueryString: 'SELECT 1',
|
||||||
|
},
|
||||||
|
fields: [],
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
values: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
const ds = new PostgresDatasource({ name: '', id: 0, jsonData: {} } as any);
|
||||||
|
const result = await ds.testDatasource();
|
||||||
|
expect(result.status).toEqual('error');
|
||||||
|
expect(result.message).toEqual('db query error: pq: password authentication failed for user "postgres"');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function setupFetchMock(response: any, mock?: any) {
|
||||||
|
const defaultMock = () => mock ?? of(createFetchResponse(response));
|
||||||
|
|
||||||
|
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
||||||
|
fetchMock.mockImplementation(defaultMock);
|
||||||
|
return fetchMock;
|
||||||
|
}
|
@ -167,16 +167,37 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _metaRequest(rawSql: string) {
|
||||||
|
const refId = 'meta';
|
||||||
|
const range = this.timeSrv.timeRange();
|
||||||
|
const query = {
|
||||||
|
refId: refId,
|
||||||
|
datasource: this.getRef(),
|
||||||
|
rawSql,
|
||||||
|
format: 'table',
|
||||||
|
};
|
||||||
|
return getBackendSrv().fetch<BackendDataSourceResponse>({
|
||||||
|
url: '/api/ds/query',
|
||||||
|
method: 'POST',
|
||||||
|
data: {
|
||||||
|
from: range.from.valueOf().toString(),
|
||||||
|
to: range.to.valueOf().toString(),
|
||||||
|
queries: [query],
|
||||||
|
},
|
||||||
|
requestId: refId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getVersion(): Promise<any> {
|
getVersion(): Promise<any> {
|
||||||
return this.metricFindQuery("SELECT current_setting('server_version_num')::int/100", {});
|
return lastValueFrom(this._metaRequest("SELECT current_setting('server_version_num')::int/100"));
|
||||||
}
|
}
|
||||||
|
|
||||||
getTimescaleDBVersion(): Promise<any> {
|
getTimescaleDBVersion(): Promise<any> {
|
||||||
return this.metricFindQuery("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'", {});
|
return lastValueFrom(this._metaRequest("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
testDatasource(): Promise<any> {
|
testDatasource(): Promise<any> {
|
||||||
return this.metricFindQuery('SELECT 1', {})
|
return lastValueFrom(this._metaRequest('SELECT 1'))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return { status: 'success', message: 'Database Connection OK' };
|
return { status: 'success', message: 'Database Connection OK' };
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user