Welcome to mirror list, hosted at ThFree Co, Russian Federation.

template_spec.rb « kubeconfig « kubernetes « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d1f1aea291e2950ce52528422dd64e973292186 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Kubernetes::Kubeconfig::Template do
  let(:template) { described_class.new }

  describe '#valid?' do
    subject { template.valid? }

    it { is_expected.to be_falsey }

    context 'with configuration added' do
      before do
        template.add_context(name: 'name', cluster: 'cluster', user: 'user')
      end

      it { is_expected.to be_truthy }
    end
  end

  describe '#to_h' do
    subject { described_class.new.to_h }

    it do
      is_expected.to eq(
        apiVersion: 'v1',
        kind: 'Config',
        clusters: [],
        users: [],
        contexts: []
      )
    end
  end

  describe '#to_yaml' do
    subject { template.to_yaml }

    it { is_expected.to eq(YAML.dump(template.to_h.deep_stringify_keys)) }
  end

  describe '#merge_yaml' do
    it 'appends to the configuration and overwrites the current context' do
      template.add_cluster(name: 'hello-cluster', url: 'hello-url')
      template.add_context(name: 'hello-context', cluster: 'hello-cluster', user: 'hello-user')
      template.add_user(name: 'hello-user', token: 'hello-token')
      ca_pem = Base64.strict_encode64('a certificate')
      template.merge_yaml(<<~YAML)
        apiVersion: v1
        kind: Config
        clusters:
          - name: 'gitlab-deploy'
            cluster:
              server: url
              certificate-authority-data: #{ca_pem.inspect}
        contexts:
          - name: gitlab-deploy
            context:
              cluster: gitlab-deploy
              namespace: namespace
              user: gitlab-deploy
        current-context: gitlab-deploy
        users:
          - name: 'gitlab-deploy'
            user: { token: token }
      YAML
      expect(template.to_h).to eq({
        apiVersion: 'v1',
        kind: 'Config',
        clusters: [
          { name: 'hello-cluster', cluster: { server: 'hello-url' } },
          { name: 'gitlab-deploy', cluster: { server: 'url', 'certificate-authority-data': ca_pem } }
        ],
        contexts: [
          { name: 'hello-context', context: { cluster: 'hello-cluster', user: 'hello-user' } },
          { name: 'gitlab-deploy', context: { cluster: 'gitlab-deploy', namespace: 'namespace', user: 'gitlab-deploy' } }
        ],
        users: [
          { name: 'hello-user', user: { token: 'hello-token' } },
          { name: 'gitlab-deploy', user: { token: 'token' } }
        ],
        'current-context': 'gitlab-deploy'
      })
    end
  end

  describe 'adding entries' do
    let(:entry) { instance_double(entry_class, to_h: attributes) }
    let(:attributes) do
      { name: 'name', other: 'other' }
    end

    subject { template.to_h }

    before do
      expect(entry_class).to receive(:new).with(attributes).and_return(entry)
    end

    describe '#add_cluster' do
      let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::Cluster }

      before do
        template.add_cluster(**attributes)
      end

      it { is_expected.to include(clusters: [attributes]) }
    end

    describe '#add_user' do
      let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::User }

      before do
        template.add_user(**attributes)
      end

      it { is_expected.to include(users: [attributes]) }
    end

    describe '#add_context' do
      let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::Context }

      before do
        template.add_context(**attributes)
      end

      it { is_expected.to include(contexts: [attributes]) }
    end
  end
end