From 8da634f7804aeb0629350296ec030333d98d20bf Mon Sep 17 00:00:00 2001 From: Olof Bourghardt Date: Fri, 27 Aug 2021 17:06:22 +0200 Subject: [PATCH] Loki: add tests for addAdHocFilters (#38594) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Loki: add tests for addAdHocFilters * Change description of test Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * Replace ADD_FILTER with "=" operator in test description Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * Loki: refafctor test for addAdHocFilter * Loki: refactor tests for better readability * Remove comments Co-authored-by: Piotr Jamróz Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Co-authored-by: Piotr Jamróz --- .../datasource/loki/datasource.test.ts | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 9e9aea37ba9..6672f6d167e 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -84,6 +84,13 @@ const testMetricsResponse: FetchResponse = { config: ({} as unknown) as BackendSrvRequest, }; +interface AdHocFilter { + condition: string; + key: string; + operator: string; + value: string; +} + describe('LokiDatasource', () => { const fetchMock = jest.spyOn(backendSrv, 'fetch'); @@ -782,6 +789,80 @@ describe('LokiDatasource', () => { }); }); + describe('addAdHocFilters', () => { + let ds: LokiDatasource; + let adHocFilters: AdHocFilter[]; + describe('when called with "=" operator', () => { + beforeEach(() => { + adHocFilters = [ + { + condition: '', + key: 'job', + operator: '=', + value: 'grafana', + }, + ]; + const templateSrvMock = ({ + getAdhocFilters: (): AdHocFilter[] => adHocFilters, + replace: (a: string) => a, + } as unknown) as TemplateSrv; + ds = createLokiDSForTests(templateSrvMock); + }); + describe('and query has no parser', () => { + it('then the correct label should be added for logs query', () => { + assertAdHocFilters('{bar="baz"}', '{bar="baz",job="grafana"}', ds); + }); + + it('then the correct label should be added for metrics query', () => { + assertAdHocFilters('rate({bar="baz"}[5m])', 'rate({bar="baz",job="grafana"}[5m])', ds); + }); + }); + describe('and query has parser', () => { + it('then the correct label should be added for logs query', () => { + assertAdHocFilters('{bar="baz"} | logfmt', '{bar="baz"} | logfmt | job="grafana"', ds); + }); + it('then the correct label should be added for metrics query', () => { + assertAdHocFilters('rate({bar="baz"} | logfmt [5m])', 'rate({bar="baz",job="grafana"} | logfmt [5m])', ds); + }); + }); + }); + + describe('when called with "!=" operator', () => { + beforeEach(() => { + adHocFilters = [ + { + condition: '', + key: 'job', + operator: '!=', + value: 'grafana', + }, + ]; + const templateSrvMock = ({ + getAdhocFilters: (): AdHocFilter[] => adHocFilters, + replace: (a: string) => a, + } as unknown) as TemplateSrv; + ds = createLokiDSForTests(templateSrvMock); + }); + describe('and query has no parser', () => { + it('then the correct label should be added for logs query', () => { + assertAdHocFilters('{bar="baz"}', '{bar="baz",job!="grafana"}', ds); + }); + + it('then the correct label should be added for metrics query', () => { + assertAdHocFilters('rate({bar="baz"}[5m])', 'rate({bar="baz",job!="grafana"}[5m])', ds); + }); + }); + describe('and query has parser', () => { + it('then the correct label should be added for logs query', () => { + assertAdHocFilters('{bar="baz"} | logfmt', '{bar="baz"} | logfmt | job!="grafana"', ds); + }); + it('then the correct label should be added for metrics query', () => { + assertAdHocFilters('rate({bar="baz"} | logfmt [5m])', 'rate({bar="baz",job!="grafana"} | logfmt [5m])', ds); + }); + }); + }); + }); + describe('adjustInterval', () => { const dynamicInterval = 15; const range = 1642; @@ -803,6 +884,13 @@ describe('LokiDatasource', () => { }); }); +function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) { + const lokiQuery: LokiQuery = { refId: 'A', expr: query }; + const result = ds.addAdHocFilters(lokiQuery.expr); + + expect(result).toEqual(expectedResults); +} + function createLokiDSForTests( templateSrvMock = ({ getAdhocFilters: (): any[] => [],