diff options
Diffstat (limited to 'spec/javascripts')
-rw-r--r-- | spec/javascripts/monitoring/mock_data.js | 64 | ||||
-rw-r--r-- | spec/javascripts/monitoring/store/actions_spec.js | 89 | ||||
-rw-r--r-- | spec/javascripts/monitoring/store/mutations_spec.js | 39 |
3 files changed, 186 insertions, 6 deletions
diff --git a/spec/javascripts/monitoring/mock_data.js b/spec/javascripts/monitoring/mock_data.js index d9d8cb66749..9b429be69f7 100644 --- a/spec/javascripts/monitoring/mock_data.js +++ b/spec/javascripts/monitoring/mock_data.js @@ -857,3 +857,67 @@ export const environmentData = [ updated_at: '2018-07-04T18:44:54.010Z', }, ]; + +export const metricsDashboardResponse = { + dashboard: { + dashboard: 'Environment metrics', + priority: 1, + panel_groups: [ + { + group: 'System metrics (Kubernetes)', + priority: 5, + panels: [ + { + title: 'Memory Usage (Total)', + type: 'area-chart', + y_label: 'Total Memory Used', + weight: 4, + metrics: [ + { + id: 'system_metrics_kubernetes_container_memory_total', + 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', + label: 'Total', + unit: 'GB', + metric_id: 12, + }, + ], + }, + { + title: 'Core Usage (Total)', + type: 'area-chart', + y_label: 'Total Cores', + weight: 3, + metrics: [ + { + id: 'system_metrics_kubernetes_container_cores_total', + query_range: + 'avg(sum(rate(container_cpu_usage_seconds_total{container_name!="POD",pod_name=~"^%{ci_environment_slug}-(.*)",namespace="%{kube_namespace}"}[15m])) by (job)) without (job)', + label: 'Total', + unit: 'cores', + metric_id: 13, + }, + ], + }, + { + title: 'Memory Usage (Pod average)', + type: 'area-chart', + y_label: 'Memory Used per Pod', + weight: 2, + metrics: [ + { + id: 'system_metrics_kubernetes_container_memory_average', + query_range: + 'avg(sum(container_memory_usage_bytes{container_name!="POD",pod_name=~"^%{ci_environment_slug}-(.*)",namespace="%{kube_namespace}"}) by (job)) without (job) / count(avg(container_memory_usage_bytes{container_name!="POD",pod_name=~"^%{ci_environment_slug}-(.*)",namespace="%{kube_namespace}"}) without (job)) /1024/1024', + label: 'Pod average', + unit: 'MB', + metric_id: 14, + }, + ], + }, + ], + }, + ], + }, + status: 'success', +}; diff --git a/spec/javascripts/monitoring/store/actions_spec.js b/spec/javascripts/monitoring/store/actions_spec.js index a848cd24fe3..31f156b0785 100644 --- a/spec/javascripts/monitoring/store/actions_spec.js +++ b/spec/javascripts/monitoring/store/actions_spec.js @@ -3,6 +3,9 @@ import MockAdapter from 'axios-mock-adapter'; import store from '~/monitoring/stores'; import * as types from '~/monitoring/stores/mutation_types'; import { + fetchDashboard, + receiveMetricsDashboardSuccess, + receiveMetricsDashboardFailure, fetchDeploymentsData, fetchEnvironmentsData, requestMetricsData, @@ -12,7 +15,7 @@ import { import storeState from '~/monitoring/stores/state'; import testAction from 'spec/helpers/vuex_action_helper'; import { resetStore } from '../helpers'; -import { deploymentData, environmentData } from '../mock_data'; +import { deploymentData, environmentData, metricsDashboardResponse } from '../mock_data'; describe('Monitoring store actions', () => { let mock; @@ -155,4 +158,88 @@ describe('Monitoring store actions', () => { ); }); }); + + describe('fetchDashboard', () => { + let dispatch; + let state; + const response = metricsDashboardResponse; + + beforeEach(() => { + dispatch = jasmine.createSpy(); + state = storeState(); + state.dashboardEndpoint = '/dashboard'; + }); + + it('dispatches receive and success actions', done => { + const params = {}; + mock.onGet(state.dashboardEndpoint).reply(200, response); + + fetchDashboard({ state, dispatch }, params) + .then(() => { + expect(dispatch).toHaveBeenCalledWith('requestMetricsDashboard'); + expect(dispatch).toHaveBeenCalledWith('receiveMetricsDashboardSuccess', { + response, + }); + done(); + }) + .catch(done.fail); + }); + + it('dispatches failure action', done => { + const params = {}; + mock.onGet(state.dashboardEndpoint).reply(500); + + fetchDashboard({ state, dispatch }, params) + .then(() => { + expect(dispatch).toHaveBeenCalledWith( + 'receiveMetricsDashboardFailure', + new Error('Request failed with status code 500'), + ); + done(); + }) + .catch(done.fail); + }); + }); + + describe('receiveMetricsDashboardSuccess', () => { + let commit; + let dispatch; + + beforeEach(() => { + commit = jasmine.createSpy(); + dispatch = jasmine.createSpy(); + }); + + it('stores groups ', () => { + const params = {}; + const response = metricsDashboardResponse; + + receiveMetricsDashboardSuccess({ commit, dispatch }, { response, params }); + + expect(commit).toHaveBeenCalledWith( + types.RECEIVE_METRICS_DATA_SUCCESS, + metricsDashboardResponse.dashboard.panel_groups, + ); + }); + }); + + describe('receiveMetricsDashboardFailure', () => { + let commit; + + beforeEach(() => { + commit = jasmine.createSpy(); + }); + + it('commits failure action', () => { + receiveMetricsDashboardFailure({ commit }); + + expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, undefined); + }); + + it('commits failure action with error', () => { + receiveMetricsDashboardFailure({ commit }, 'uh-oh'); + + expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, 'uh-oh'); + }); + }); }); diff --git a/spec/javascripts/monitoring/store/mutations_spec.js b/spec/javascripts/monitoring/store/mutations_spec.js index 882ee1dec14..bce399ece74 100644 --- a/spec/javascripts/monitoring/store/mutations_spec.js +++ b/spec/javascripts/monitoring/store/mutations_spec.js @@ -1,7 +1,7 @@ import mutations from '~/monitoring/stores/mutations'; import * as types from '~/monitoring/stores/mutation_types'; import state from '~/monitoring/stores/state'; -import { metricsGroupsAPIResponse, deploymentData } from '../mock_data'; +import { metricsGroupsAPIResponse, deploymentData, metricsDashboardResponse } from '../mock_data'; describe('Monitoring mutations', () => { let stateCopy; @@ -11,14 +11,16 @@ describe('Monitoring mutations', () => { }); describe(types.RECEIVE_METRICS_DATA_SUCCESS, () => { + let groups; + beforeEach(() => { stateCopy.groups = []; - const groups = metricsGroupsAPIResponse.data; - - mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups); + groups = metricsGroupsAPIResponse.data; }); it('normalizes values', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups); + const expectedTimestamp = '2017-05-25T08:22:34.925Z'; const expectedValue = 0.0010794445585559514; const [timestamp, value] = stateCopy.groups[0].metrics[0].queries[0].result[0].values[0]; @@ -28,22 +30,27 @@ describe('Monitoring mutations', () => { }); it('contains two groups that contains, one of which has two queries sorted by priority', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups); + expect(stateCopy.groups).toBeDefined(); expect(stateCopy.groups.length).toEqual(2); expect(stateCopy.groups[0].metrics.length).toEqual(2); }); it('assigns queries a metric id', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups); + expect(stateCopy.groups[1].metrics[0].queries[0].metricId).toEqual('100'); }); it('removes the data if all the values from a query are not defined', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups); + expect(stateCopy.groups[1].metrics[0].queries[0].result.length).toEqual(0); }); it('assigns metric id of null if metric has no id', () => { stateCopy.groups = []; - const groups = metricsGroupsAPIResponse.data; const noId = groups.map(group => ({ ...group, ...{ @@ -63,6 +70,26 @@ describe('Monitoring mutations', () => { }); }); }); + + describe('dashboard endpoint enabled', () => { + const dashboardGroups = metricsDashboardResponse.dashboard.panel_groups; + + beforeEach(() => { + stateCopy.useDashboardEndpoint = true; + }); + + it('aliases group panels to metrics for backwards compatibility', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); + + expect(stateCopy.groups[0].metrics[0]).toBeDefined(); + }); + + it('aliases panel metrics to queries for backwards compatibility', () => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); + + expect(stateCopy.groups[0].metrics[0].queries).toBeDefined(); + }); + }); }); describe(types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS, () => { @@ -82,11 +109,13 @@ describe('Monitoring mutations', () => { metricsEndpoint: 'additional_metrics.json', environmentsEndpoint: 'environments.json', deploymentsEndpoint: 'deployments.json', + dashboardEndpoint: 'dashboard.json', }); expect(stateCopy.metricsEndpoint).toEqual('additional_metrics.json'); expect(stateCopy.environmentsEndpoint).toEqual('environments.json'); expect(stateCopy.deploymentsEndpoint).toEqual('deployments.json'); + expect(stateCopy.dashboardEndpoint).toEqual('dashboard.json'); }); }); }); |