summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/store/variable_mapping_spec.js
blob: de124b0313c6a0c0092feeeb8154eed7ddc6b080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
  parseTemplatingVariables,
  mergeURLVariables,
  optionsFromSeriesData,
} from '~/monitoring/stores/variable_mapping';
import {
  templatingVariablesExamples,
  storeTextVariables,
  storeCustomVariables,
  storeMetricLabelValuesVariables,
} from '../mock_data';
import * as urlUtils from '~/lib/utils/url_utility';

describe('Monitoring variable mapping', () => {
  describe('parseTemplatingVariables', () => {
    it.each`
      case                                 | input
      ${'For undefined templating object'} | ${undefined}
      ${'For empty templating object'}     | ${{}}
    `('$case, returns an empty array', ({ input }) => {
      expect(parseTemplatingVariables(input)).toEqual([]);
    });

    it.each`
      case                                                        | input                                            | output
      ${'Returns parsed object for text variables'}               | ${templatingVariablesExamples.text}              | ${storeTextVariables}
      ${'Returns parsed object for custom variables'}             | ${templatingVariablesExamples.custom}            | ${storeCustomVariables}
      ${'Returns parsed object for metric label value variables'} | ${templatingVariablesExamples.metricLabelValues} | ${storeMetricLabelValuesVariables}
    `('$case, returns an empty array', ({ input, output }) => {
      expect(parseTemplatingVariables(input)).toEqual(output);
    });
  });

  describe('mergeURLVariables', () => {
    beforeEach(() => {
      jest.spyOn(urlUtils, 'queryToObject');
    });

    afterEach(() => {
      urlUtils.queryToObject.mockRestore();
    });

    it('returns empty object if variables are not defined in yml or URL', () => {
      urlUtils.queryToObject.mockReturnValueOnce({});

      expect(mergeURLVariables([])).toEqual([]);
    });

    it('returns empty object if variables are defined in URL but not in yml', () => {
      urlUtils.queryToObject.mockReturnValueOnce({
        'var-env': 'one',
        'var-instance': 'localhost',
      });

      expect(mergeURLVariables([])).toEqual([]);
    });

    it('returns yml variables if variables defined in yml but not in the URL', () => {
      urlUtils.queryToObject.mockReturnValueOnce({});

      const variables = [
        {
          name: 'env',
          value: 'one',
        },
        {
          name: 'instance',
          value: 'localhost',
        },
      ];

      expect(mergeURLVariables(variables)).toEqual(variables);
    });

    it('returns yml variables if variables defined in URL do not match with yml variables', () => {
      const urlParams = {
        'var-env': 'one',
        'var-instance': 'localhost',
      };
      const variables = [
        {
          name: 'env',
          value: 'one',
        },
        {
          name: 'service',
          value: 'database',
        },
      ];
      urlUtils.queryToObject.mockReturnValueOnce(urlParams);

      expect(mergeURLVariables(variables)).toEqual(variables);
    });

    it('returns merged yml and URL variables if there is some match', () => {
      const urlParams = {
        'var-env': 'one',
        'var-instance': 'localhost:8080',
      };
      const variables = [
        {
          name: 'instance',
          value: 'localhost',
        },
        {
          name: 'service',
          value: 'database',
        },
      ];

      urlUtils.queryToObject.mockReturnValueOnce(urlParams);

      expect(mergeURLVariables(variables)).toEqual([
        {
          name: 'instance',
          value: 'localhost:8080',
        },
        {
          name: 'service',
          value: 'database',
        },
      ]);
    });
  });

  describe('optionsFromSeriesData', () => {
    it('fetches the label values from missing data', () => {
      expect(optionsFromSeriesData({ label: 'job' })).toEqual([]);
    });

    it('fetches the label values from a simple series', () => {
      const data = [
        {
          __name__: 'up',
          job: 'job1',
        },
        {
          __name__: 'up',
          job: 'job2',
        },
      ];

      expect(optionsFromSeriesData({ label: 'job', data })).toEqual([
        { text: 'job1', value: 'job1' },
        { text: 'job2', value: 'job2' },
      ]);
    });

    it('fetches the label values from multiple series', () => {
      const data = [
        {
          __name__: 'up',
          job: 'job1',
          instance: 'host1',
        },
        {
          __name__: 'up',
          job: 'job2',
          instance: 'host1',
        },
        {
          __name__: 'up',
          job: 'job1',
          instance: 'host2',
        },
        {
          __name__: 'up',
          job: 'job2',
          instance: 'host2',
        },
      ];

      expect(optionsFromSeriesData({ label: '__name__', data })).toEqual([
        { text: 'up', value: 'up' },
      ]);

      expect(optionsFromSeriesData({ label: 'job', data })).toEqual([
        { text: 'job1', value: 'job1' },
        { text: 'job2', value: 'job2' },
      ]);

      expect(optionsFromSeriesData({ label: 'instance', data })).toEqual([
        { text: 'host1', value: 'host1' },
        { text: 'host2', value: 'host2' },
      ]);
    });

    it('fetches the label values from a series with missing values', () => {
      const data = [
        {
          __name__: 'up',
          job: 'job1',
        },
        {
          __name__: 'up',
          job: 'job2',
        },
        {
          __name__: 'up',
        },
      ];

      expect(optionsFromSeriesData({ label: 'job', data })).toEqual([
        { text: 'job1', value: 'job1' },
        { text: 'job2', value: 'job2' },
      ]);
    });
  });
});