diff options
Diffstat (limited to 'spec/frontend/monitoring/store/utils_spec.js')
-rw-r--r-- | spec/frontend/monitoring/store/utils_spec.js | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/spec/frontend/monitoring/store/utils_spec.js b/spec/frontend/monitoring/store/utils_spec.js index fe5754e1216..3a70bda51da 100644 --- a/spec/frontend/monitoring/store/utils_spec.js +++ b/spec/frontend/monitoring/store/utils_spec.js @@ -5,6 +5,9 @@ import { parseAnnotationsResponse, removeLeadingSlash, mapToDashboardViewModel, + normalizeQueryResult, + convertToGrafanaTimeRange, + addDashboardMetaDataToLink, } from '~/monitoring/stores/utils'; import { annotationsData } from '../mock_data'; import { NOT_IN_DB_PREFIX } from '~/monitoring/constants'; @@ -16,6 +19,8 @@ describe('mapToDashboardViewModel', () => { expect(mapToDashboardViewModel({})).toEqual({ dashboard: '', panelGroups: [], + links: [], + variables: {}, }); }); @@ -44,6 +49,8 @@ describe('mapToDashboardViewModel', () => { expect(mapToDashboardViewModel(response)).toEqual({ dashboard: 'Dashboard Name', + links: [], + variables: {}, panelGroups: [ { group: 'Group 1', @@ -63,6 +70,7 @@ describe('mapToDashboardViewModel', () => { format: 'engineering', precision: 2, }, + links: [], metrics: [], }, ], @@ -75,6 +83,8 @@ describe('mapToDashboardViewModel', () => { it('key', () => { const response = { dashboard: 'Dashboard Name', + links: [], + variables: {}, panel_groups: [ { group: 'Group A', @@ -147,6 +157,7 @@ describe('mapToDashboardViewModel', () => { format: SUPPORTED_FORMATS.engineering, precision: 2, }, + links: [], metrics: [], }); }); @@ -170,6 +181,7 @@ describe('mapToDashboardViewModel', () => { format: SUPPORTED_FORMATS.engineering, precision: 2, }, + links: [], metrics: [], }); }); @@ -238,6 +250,77 @@ describe('mapToDashboardViewModel', () => { expect(getMappedPanel().maxValue).toBe(100); }); + + describe('panel with links', () => { + const title = 'Example'; + const url = 'https://example.com'; + + it('maps an empty link collection', () => { + setupWithPanel({ + links: undefined, + }); + + expect(getMappedPanel().links).toEqual([]); + }); + + it('maps a link', () => { + setupWithPanel({ links: [{ title, url }] }); + + expect(getMappedPanel().links).toEqual([{ title, url }]); + }); + + it('maps a link without a title', () => { + setupWithPanel({ + links: [{ url }], + }); + + expect(getMappedPanel().links).toEqual([{ title: url, url }]); + }); + + it('maps a link without a url', () => { + setupWithPanel({ + links: [{ title }], + }); + + expect(getMappedPanel().links).toEqual([{ title, url: '#' }]); + }); + + it('maps a link without a url or title', () => { + setupWithPanel({ + links: [{}], + }); + + expect(getMappedPanel().links).toEqual([{ title: 'null', url: '#' }]); + }); + + it('maps a link with an unsafe url safely', () => { + // eslint-disable-next-line no-script-url + const unsafeUrl = 'javascript:alert("XSS")'; + + setupWithPanel({ + links: [ + { + title, + url: unsafeUrl, + }, + ], + }); + + expect(getMappedPanel().links).toEqual([{ title, url: '#' }]); + }); + + it('maps multple links', () => { + setupWithPanel({ + links: [{ title, url }, { url }, { title }], + }); + + expect(getMappedPanel().links).toEqual([ + { title, url }, + { title: url, url }, + { title, url: '#' }, + ]); + }); + }); }); describe('metrics mapping', () => { @@ -317,6 +400,28 @@ describe('mapToDashboardViewModel', () => { }); }); +describe('normalizeQueryResult', () => { + const testData = { + metric: { + __name__: 'up', + job: 'prometheus', + instance: 'localhost:9090', + }, + values: [[1435781430.781, '1'], [1435781445.781, '1'], [1435781460.781, '1']], + }; + + it('processes a simple matrix result', () => { + expect(normalizeQueryResult(testData)).toEqual({ + metric: { __name__: 'up', job: 'prometheus', instance: 'localhost:9090' }, + values: [ + ['2015-07-01T20:10:30.781Z', 1], + ['2015-07-01T20:10:45.781Z', 1], + ['2015-07-01T20:11:00.781Z', 1], + ], + }); + }); +}); + describe('uniqMetricsId', () => { [ { input: { id: 1 }, expected: `${NOT_IN_DB_PREFIX}_1` }, @@ -419,3 +524,86 @@ describe('removeLeadingSlash', () => { }); }); }); + +describe('user-defined links utils', () => { + const mockRelativeTimeRange = { + metricsDashboard: { + duration: { + seconds: 86400, + }, + }, + grafana: { + from: 'now-86400s', + to: 'now', + }, + }; + const mockAbsoluteTimeRange = { + metricsDashboard: { + start: '2020-06-08T16:13:01.995Z', + end: '2020-06-08T21:12:32.243Z', + }, + grafana: { + from: 1591632781995, + to: 1591650752243, + }, + }; + describe('convertToGrafanaTimeRange', () => { + it('converts relative timezone to grafana timezone', () => { + expect(convertToGrafanaTimeRange(mockRelativeTimeRange.metricsDashboard)).toEqual( + mockRelativeTimeRange.grafana, + ); + }); + + it('converts absolute timezone to grafana timezone', () => { + expect(convertToGrafanaTimeRange(mockAbsoluteTimeRange.metricsDashboard)).toEqual( + mockAbsoluteTimeRange.grafana, + ); + }); + }); + + describe('addDashboardMetaDataToLink', () => { + const link = { title: 'title', url: 'https://gitlab.com' }; + const grafanaLink = { ...link, type: 'grafana' }; + + it('adds relative time range to link w/o type for metrics dashboards', () => { + const adder = addDashboardMetaDataToLink({ + timeRange: mockRelativeTimeRange.metricsDashboard, + }); + expect(adder(link)).toMatchObject({ + title: 'title', + url: 'https://gitlab.com?duration_seconds=86400', + }); + }); + + it('adds relative time range to Grafana type links', () => { + const adder = addDashboardMetaDataToLink({ + timeRange: mockRelativeTimeRange.metricsDashboard, + }); + expect(adder(grafanaLink)).toMatchObject({ + title: 'title', + url: 'https://gitlab.com?from=now-86400s&to=now', + }); + }); + + it('adds absolute time range to link w/o type for metrics dashboard', () => { + const adder = addDashboardMetaDataToLink({ + timeRange: mockAbsoluteTimeRange.metricsDashboard, + }); + expect(adder(link)).toMatchObject({ + title: 'title', + url: + 'https://gitlab.com?start=2020-06-08T16%3A13%3A01.995Z&end=2020-06-08T21%3A12%3A32.243Z', + }); + }); + + it('adds absolute time range to Grafana type links', () => { + const adder = addDashboardMetaDataToLink({ + timeRange: mockAbsoluteTimeRange.metricsDashboard, + }); + expect(adder(grafanaLink)).toMatchObject({ + title: 'title', + url: 'https://gitlab.com?from=1591632781995&to=1591650752243', + }); + }); + }); +}); |