File: k8s_integration_helper_spec.js

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (120 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download
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
import {
  humanizeClusterErrors,
  createK8sAccessConfiguration,
  fluxSyncStatus,
  updateFluxRequested,
} from '~/environments/helpers/k8s_integration_helper';
import {
  CLUSTER_AGENT_ERROR_MESSAGES,
  STATUS_TRUE,
  STATUS_FALSE,
  STATUS_UNKNOWN,
  REASON_PROGRESSING,
} from '~/environments/constants';

jest.mock('~/lib/utils/csrf', () => ({ headers: { token: 'mock-csrf-token' } }));

describe('k8s_integration_helper', () => {
  describe('humanizeClusterErrors', () => {
    it.each(['unauthorized', 'forbidden', 'not found', 'other'])(
      'returns correct object of statuses when error reason is %s',
      (reason) => {
        expect(humanizeClusterErrors(reason)).toEqual(CLUSTER_AGENT_ERROR_MESSAGES[reason]);
      },
    );
  });

  describe('createK8sAccessConfiguration', () => {
    const kasTunnelUrl = '//kas-tunnel-url';
    const gitlabAgentId = 1;

    const subject = createK8sAccessConfiguration({ kasTunnelUrl, gitlabAgentId });

    it('receives kasTunnelUrl and sets it as a basePath', () => {
      expect(subject).toMatchObject({
        basePath: kasTunnelUrl,
      });
    });

    it('receives gitlabAgentId and sets it as part of headers', () => {
      expect(subject.headers).toMatchObject({
        'GitLab-Agent-Id': gitlabAgentId,
      });
    });

    it('provides csrf headers into headers', () => {
      expect(subject.headers).toMatchObject({
        token: 'mock-csrf-token',
      });
    });

    it('provides proper content type to the headers', () => {
      expect(subject.headers).toMatchObject({
        'Content-Type': 'application/json',
        Accept: 'application/json',
      });
    });

    it('includes credentials', () => {
      expect(subject).toMatchObject({
        credentials: 'include',
      });
    });
  });

  describe('fluxSyncStatus', () => {
    const message = 'message from Flux';
    let fluxConditions;

    it.each`
      status            | type             | reason                | statusText                    | statusMessage
      ${STATUS_TRUE}    | ${'Stalled'}     | ${''}                 | ${'stalled'}                  | ${{ message }}
      ${STATUS_TRUE}    | ${'Reconciling'} | ${''}                 | ${'reconciling'}              | ${''}
      ${STATUS_UNKNOWN} | ${'Ready'}       | ${REASON_PROGRESSING} | ${'reconcilingWithBadConfig'} | ${{ message }}
      ${STATUS_TRUE}    | ${'Ready'}       | ${''}                 | ${'reconciled'}               | ${''}
      ${STATUS_FALSE}   | ${'Ready'}       | ${''}                 | ${'failed'}                   | ${{ message }}
      ${STATUS_UNKNOWN} | ${'Ready'}       | ${''}                 | ${'unknown'}                  | ${''}
    `(
      'renders sync status as $statusText when status is $status, type is $type, and reason is $reason',
      ({ status, type, reason, statusText, statusMessage }) => {
        fluxConditions = [
          {
            status,
            type,
            reason,
            message,
          },
        ];

        expect(fluxSyncStatus(fluxConditions)).toMatchObject({
          status: statusText,
          ...statusMessage,
        });
      },
    );
  });

  describe('updateFluxRequested', () => {
    const defaultPath = '/metadata/annotations/reconcile.fluxcd.io~1requestedAt';
    const defaultValue = new Date().toISOString();
    const customPath = '/custom/path';
    const customValue = true;

    it.each([
      ['with default values', undefined, undefined],
      ['with custom path', customPath, undefined],
      ['with custom value', undefined, customValue],
      ['with custom path and value', customPath, customValue],
    ])('%s', (description, path, value) => {
      expect(updateFluxRequested({ path, value })).toEqual(
        JSON.stringify([
          {
            op: 'replace',
            path: path || defaultPath,
            value: value || defaultValue,
          },
        ]),
      );
    });
  });
});