mirror of https://github.com/grafana/grafana
Loki: Update mocks and move into __mocks__ (#79993)
Loki: Update mocks and move into __mocks__pull/80092/head
parent
a8fb01a502
commit
c48e2c7d0d
@ -0,0 +1,59 @@ |
|||||||
|
import { DataSourceInstanceSettings, DataSourceSettings, PluginType } from '@grafana/data'; |
||||||
|
import { TemplateSrv } from '@grafana/runtime'; |
||||||
|
|
||||||
|
import { LokiDatasource } from '../datasource'; |
||||||
|
import { LokiOptions } from '../types'; |
||||||
|
|
||||||
|
export function createDefaultConfigOptions() { |
||||||
|
return { |
||||||
|
jsonData: { maxLines: '531' }, |
||||||
|
secureJsonFields: {}, |
||||||
|
} as DataSourceSettings<LokiOptions>; |
||||||
|
} |
||||||
|
|
||||||
|
const defaultTemplateSrvMock = { |
||||||
|
replace: (input: string) => input, |
||||||
|
getVariables: () => [], |
||||||
|
}; |
||||||
|
|
||||||
|
export function createLokiDatasource( |
||||||
|
templateSrvMock: Partial<TemplateSrv> = defaultTemplateSrvMock, |
||||||
|
settings: Partial<DataSourceInstanceSettings<LokiOptions>> = {} |
||||||
|
): LokiDatasource { |
||||||
|
const customSettings: DataSourceInstanceSettings<LokiOptions> = { |
||||||
|
url: 'myloggingurl', |
||||||
|
id: 0, |
||||||
|
uid: '', |
||||||
|
type: '', |
||||||
|
name: '', |
||||||
|
meta: { |
||||||
|
id: 'id', |
||||||
|
name: 'name', |
||||||
|
type: PluginType.datasource, |
||||||
|
module: '', |
||||||
|
baseUrl: '', |
||||||
|
info: { |
||||||
|
author: { |
||||||
|
name: 'Test', |
||||||
|
}, |
||||||
|
description: '', |
||||||
|
links: [], |
||||||
|
logos: { |
||||||
|
large: '', |
||||||
|
small: '', |
||||||
|
}, |
||||||
|
screenshots: [], |
||||||
|
updated: '', |
||||||
|
version: '', |
||||||
|
}, |
||||||
|
}, |
||||||
|
readOnly: false, |
||||||
|
jsonData: { |
||||||
|
maxLines: '20', |
||||||
|
}, |
||||||
|
access: 'direct', |
||||||
|
...settings, |
||||||
|
}; |
||||||
|
|
||||||
|
return new LokiDatasource(customSettings, templateSrvMock as TemplateSrv); |
||||||
|
} |
@ -1,113 +1,4 @@ |
|||||||
import { |
import { DataFrame, DataFrameType, FieldType } from '@grafana/data'; |
||||||
DataFrame, |
|
||||||
DataFrameType, |
|
||||||
DataSourceInstanceSettings, |
|
||||||
DataSourceSettings, |
|
||||||
FieldType, |
|
||||||
PluginType, |
|
||||||
toUtc, |
|
||||||
} from '@grafana/data'; |
|
||||||
import { TemplateSrv } from '@grafana/runtime'; |
|
||||||
|
|
||||||
import { getMockDataSource } from '../../../features/datasources/__mocks__'; |
|
||||||
|
|
||||||
import { LokiDatasource } from './datasource'; |
|
||||||
import { LokiOptions } from './types'; |
|
||||||
|
|
||||||
export function createDefaultConfigOptions(): DataSourceSettings<LokiOptions> { |
|
||||||
return getMockDataSource<LokiOptions>({ |
|
||||||
jsonData: { maxLines: '531' }, |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
const rawRange = { |
|
||||||
from: toUtc('2018-04-25 10:00'), |
|
||||||
to: toUtc('2018-04-25 11:00'), |
|
||||||
}; |
|
||||||
|
|
||||||
const defaultTimeSrvMock = { |
|
||||||
timeRange: jest.fn().mockReturnValue({ |
|
||||||
from: rawRange.from, |
|
||||||
to: rawRange.to, |
|
||||||
raw: rawRange, |
|
||||||
}), |
|
||||||
}; |
|
||||||
|
|
||||||
const defaultTemplateSrvMock = { |
|
||||||
replace: (input: string) => input, |
|
||||||
getVariables: () => [], |
|
||||||
}; |
|
||||||
|
|
||||||
export function createLokiDatasource( |
|
||||||
templateSrvMock: Partial<TemplateSrv> = defaultTemplateSrvMock, |
|
||||||
settings: Partial<DataSourceInstanceSettings<LokiOptions>> = {}, |
|
||||||
timeSrvStub = defaultTimeSrvMock |
|
||||||
): LokiDatasource { |
|
||||||
const customSettings: DataSourceInstanceSettings<LokiOptions> = { |
|
||||||
url: 'myloggingurl', |
|
||||||
id: 0, |
|
||||||
uid: '', |
|
||||||
type: '', |
|
||||||
name: '', |
|
||||||
meta: { |
|
||||||
id: 'id', |
|
||||||
name: 'name', |
|
||||||
type: PluginType.datasource, |
|
||||||
module: '', |
|
||||||
baseUrl: '', |
|
||||||
info: { |
|
||||||
author: { |
|
||||||
name: 'Test', |
|
||||||
}, |
|
||||||
description: '', |
|
||||||
links: [], |
|
||||||
logos: { |
|
||||||
large: '', |
|
||||||
small: '', |
|
||||||
}, |
|
||||||
screenshots: [], |
|
||||||
updated: '', |
|
||||||
version: '', |
|
||||||
}, |
|
||||||
}, |
|
||||||
readOnly: false, |
|
||||||
jsonData: { |
|
||||||
maxLines: '20', |
|
||||||
}, |
|
||||||
access: 'direct', |
|
||||||
...settings, |
|
||||||
}; |
|
||||||
|
|
||||||
// @ts-expect-error
|
|
||||||
return new LokiDatasource(customSettings, templateSrvMock, timeSrvStub); |
|
||||||
} |
|
||||||
|
|
||||||
export function createMetadataRequest( |
|
||||||
labelsAndValues: Record<string, string[]>, |
|
||||||
series?: Record<string, Array<Record<string, string>>> |
|
||||||
) { |
|
||||||
// added % to allow urlencoded labelKeys. Note, that this is not confirm with Loki, as loki does not allow specialcharacters in labelKeys, but needed for tests.
|
|
||||||
const lokiLabelsAndValuesEndpointRegex = /^label\/([%\w]*)\/values/; |
|
||||||
const lokiSeriesEndpointRegex = /^series/; |
|
||||||
const lokiLabelsEndpoint = 'labels'; |
|
||||||
const labels = Object.keys(labelsAndValues); |
|
||||||
|
|
||||||
return async function metadataRequestMock(url: string, params?: Record<string, string | number>) { |
|
||||||
if (url === lokiLabelsEndpoint) { |
|
||||||
return labels; |
|
||||||
} else { |
|
||||||
const labelsMatch = url.match(lokiLabelsAndValuesEndpointRegex); |
|
||||||
const seriesMatch = url.match(lokiSeriesEndpointRegex); |
|
||||||
if (labelsMatch) { |
|
||||||
return labelsAndValues[labelsMatch[1]] || []; |
|
||||||
} else if (seriesMatch && series && params) { |
|
||||||
return series[params['match[]']] || []; |
|
||||||
} else { |
|
||||||
throw new Error(`Unexpected url error, ${url}`); |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
export function getMockFrames() { |
export function getMockFrames() { |
||||||
const logFrameA: DataFrame = { |
const logFrameA: DataFrame = { |
@ -0,0 +1,26 @@ |
|||||||
|
export function createMetadataRequest( |
||||||
|
labelsAndValues: Record<string, string[]>, |
||||||
|
series?: Record<string, Array<Record<string, string>>> |
||||||
|
) { |
||||||
|
// added % to allow urlencoded labelKeys. Note, that this is not confirm with Loki, as loki does not allow specialcharacters in labelKeys, but needed for tests.
|
||||||
|
const lokiLabelsAndValuesEndpointRegex = /^label\/([%\w]*)\/values/; |
||||||
|
const lokiSeriesEndpointRegex = /^series/; |
||||||
|
const lokiLabelsEndpoint = 'labels'; |
||||||
|
const labels = Object.keys(labelsAndValues); |
||||||
|
|
||||||
|
return async function metadataRequestMock(url: string, params?: Record<string, string | number>) { |
||||||
|
if (url === lokiLabelsEndpoint) { |
||||||
|
return labels; |
||||||
|
} else { |
||||||
|
const labelsMatch = url.match(lokiLabelsAndValuesEndpointRegex); |
||||||
|
const seriesMatch = url.match(lokiSeriesEndpointRegex); |
||||||
|
if (labelsMatch) { |
||||||
|
return labelsAndValues[labelsMatch[1]] || []; |
||||||
|
} else if (seriesMatch && series && params) { |
||||||
|
return series[params['match[]']] || []; |
||||||
|
} else { |
||||||
|
throw new Error(`Unexpected url error, ${url}`); |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
Loading…
Reference in new issue