debian-mirror-gitlab/spec/javascripts/monitoring/utils_spec.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-09-30 21:07:59 +05:30
import { getTimeDiff, graphDataValidatorForValues } from '~/monitoring/utils';
2019-07-07 11:18:12 +05:30
import { timeWindows } from '~/monitoring/constants';
2019-09-30 21:07:59 +05:30
import { graphDataPrometheusQuery, graphDataPrometheusQueryRange } from './mock_data';
2019-07-07 11:18:12 +05:30
describe('getTimeDiff', () => {
2019-10-12 21:52:04 +05:30
function secondsBetween({ start, end }) {
return (new Date(end) - new Date(start)) / 1000;
}
function minutesBetween(timeRange) {
return secondsBetween(timeRange) / 60;
}
function hoursBetween(timeRange) {
return minutesBetween(timeRange) / 60;
}
2019-07-07 11:18:12 +05:30
it('defaults to an 8 hour (28800s) difference', () => {
const params = getTimeDiff();
2019-10-12 21:52:04 +05:30
expect(hoursBetween(params)).toEqual(8);
2019-07-07 11:18:12 +05:30
});
it('accepts time window as an argument', () => {
2019-10-12 21:52:04 +05:30
const params = getTimeDiff('thirtyMinutes');
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
expect(minutesBetween(params)).toEqual(30);
2019-07-07 11:18:12 +05:30
});
it('returns a value for every defined time window', () => {
const nonDefaultWindows = Object.keys(timeWindows).filter(window => window !== 'eightHours');
2019-10-12 21:52:04 +05:30
nonDefaultWindows.forEach(timeWindow => {
const params = getTimeDiff(timeWindow);
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
// Ensure we're not returning the default
expect(hoursBetween(params)).not.toEqual(8);
2019-07-07 11:18:12 +05:30
});
});
});
2019-09-30 21:07:59 +05:30
describe('graphDataValidatorForValues', () => {
/*
* When dealing with a metric using the query format, e.g.
* query: 'max(go_memstats_alloc_bytes{job="prometheus"}) by (job) /1024/1024'
* the validator will look for the `value` key instead of `values`
*/
it('validates data with the query format', () => {
const validGraphData = graphDataValidatorForValues(true, graphDataPrometheusQuery);
expect(validGraphData).toBe(true);
});
/*
* When dealing with a metric using the query?range format, e.g.
* query_range: 'avg(sum(container_memory_usage_bytes{container_name!="POD",pod_name=~"^%{ci_environment_slug}-(.*)",namespace="%{kube_namespace}"}) by (job)) without (job) /1024/1024/1024',
* the validator will look for the `values` key instead of `value`
*/
it('validates data with the query_range format', () => {
const validGraphData = graphDataValidatorForValues(false, graphDataPrometheusQueryRange);
expect(validGraphData).toBe(true);
});
});