install_command_spec.rb 8.1 KB
Newer Older
1 2 3
require 'rails_helper'

describe Gitlab::Kubernetes::Helm::InstallCommand do
4 5
  let(:files) { { 'ca.pem': 'some file content' } }
  let(:repository) { 'https://repository.example.com' }
6
  let(:rbac) { false }
7
  let(:version) { '1.2.3' }
8 9
  let(:preinstall) { nil }
  let(:postinstall) { nil }
10 11 12 13 14

  let(:install_command) do
    described_class.new(
      name: 'app-name',
      chart: 'chart-name',
15
      rbac: rbac,
16
      files: files,
17
      version: version,
18 19 20
      repository: repository,
      preinstall: preinstall,
      postinstall: postinstall
21 22
    )
  end
23

24
  subject { install_command }
25

26 27 28
  it_behaves_like 'helm commands' do
    let(:commands) do
      <<~EOS
29
      helm init --upgrade
30
      for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
31
      helm repo add app-name https://repository.example.com
32
      helm repo update
33 34 35 36 37 38 39 40 41 42 43 44 45
      #{helm_install_comand}
      EOS
    end

    let(:helm_install_comand) do
      <<~EOS.squish
      helm install chart-name
        --name app-name
        --tls
        --tls-ca-cert /data/helm/app-name/config/ca.pem
        --tls-cert /data/helm/app-name/config/cert.pem
        --tls-key /data/helm/app-name/config/key.pem
        --version 1.2.3
A
Amit Rathi 已提交
46
        --set rbac.create\\=false,rbac.enabled\\=false
47
        --namespace gitlab-managed-apps
48
        -f /data/helm/app-name/config/values.yaml
49
      EOS
50 51 52
    end
  end

53 54 55 56 57 58
  context 'when rbac is true' do
    let(:rbac) { true }

    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
59
        helm init --upgrade
60
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
61
        helm repo add app-name https://repository.example.com
62
        helm repo update
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.squish
        helm install chart-name
          --name app-name
          --tls
          --tls-ca-cert /data/helm/app-name/config/ca.pem
          --tls-cert /data/helm/app-name/config/cert.pem
          --tls-key /data/helm/app-name/config/key.pem
          --version 1.2.3
          --set rbac.create\\=true,rbac.enabled\\=true
          --namespace gitlab-managed-apps
78
          -f /data/helm/app-name/config/values.yaml
79 80 81 82 83
        EOS
      end
    end
  end

84 85
  context 'when there is no repository' do
    let(:repository) { nil }
86 87 88 89

    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
90
        helm init --upgrade
91
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
92 93 94 95 96 97 98 99 100 101 102 103 104
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.squish
        helm install chart-name
          --name app-name
          --tls
          --tls-ca-cert /data/helm/app-name/config/ca.pem
          --tls-cert /data/helm/app-name/config/cert.pem
          --tls-key /data/helm/app-name/config/key.pem
          --version 1.2.3
A
Amit Rathi 已提交
105
          --set rbac.create\\=false,rbac.enabled\\=false
106
          --namespace gitlab-managed-apps
107
          -f /data/helm/app-name/config/values.yaml
108 109
        EOS
      end
110
    end
111
  end
112

113 114 115 116 117 118
  context 'when there is a pre-install script' do
    let(:preinstall) { ['/bin/date', '/bin/true'] }

    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
119
        helm init --upgrade
120
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
121
        helm repo add app-name https://repository.example.com
122
        helm repo update
123 124 125 126 127 128 129 130
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.strip
        /bin/date
        /bin/true
A
Amit Rathi 已提交
131
        helm install chart-name --name app-name --tls --tls-ca-cert /data/helm/app-name/config/ca.pem --tls-cert /data/helm/app-name/config/cert.pem --tls-key /data/helm/app-name/config/key.pem --version 1.2.3 --set rbac.create\\=false,rbac.enabled\\=false --namespace gitlab-managed-apps -f /data/helm/app-name/config/values.yaml
132 133 134 135 136 137 138 139 140 141 142
        EOS
      end
    end
  end

  context 'when there is a post-install script' do
    let(:postinstall) { ['/bin/date', "/bin/false\n"] }

    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
143
        helm init --upgrade
144
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
145
        helm repo add app-name https://repository.example.com
146
        helm repo update
147 148 149 150 151 152
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.strip
A
Amit Rathi 已提交
153
        helm install chart-name --name app-name --tls --tls-ca-cert /data/helm/app-name/config/ca.pem --tls-cert /data/helm/app-name/config/cert.pem --tls-key /data/helm/app-name/config/key.pem --version 1.2.3 --set rbac.create\\=false,rbac.enabled\\=false --namespace gitlab-managed-apps -f /data/helm/app-name/config/values.yaml
154 155 156 157 158 159 160
        /bin/date
        /bin/false
        EOS
      end
    end
  end

161 162
  context 'when there is no ca.pem file' do
    let(:files) { { 'file.txt': 'some content' } }
163 164 165 166

    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
167
        helm init --upgrade
168
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
169
        helm repo add app-name https://repository.example.com
170
        helm repo update
171 172 173 174 175 176 177 178 179
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.squish
        helm install chart-name
           --name app-name
           --version 1.2.3
A
Amit Rathi 已提交
180
           --set rbac.create\\=false,rbac.enabled\\=false
181
           --namespace gitlab-managed-apps
182
           -f /data/helm/app-name/config/values.yaml
183 184
        EOS
      end
185
    end
186 187
  end

188 189
  context 'when there is no version' do
    let(:version) { nil }
190

191 192 193
    it_behaves_like 'helm commands' do
      let(:commands) do
        <<~EOS
194
        helm init --upgrade
195
        for i in $(seq 1 30); do helm version && break; sleep 1s; echo "Retrying ($i)..."; done
196
        helm repo add app-name https://repository.example.com
197
        helm repo update
198 199 200 201 202 203 204 205 206 207 208 209 210
        #{helm_install_command}
        EOS
      end

      let(:helm_install_command) do
        <<~EOS.squish
        helm install chart-name
          --name app-name
          --tls
          --tls-ca-cert /data/helm/app-name/config/ca.pem
          --tls-cert /data/helm/app-name/config/cert.pem
          --tls-key /data/helm/app-name/config/key.pem
          --namespace gitlab-managed-apps
211
          -f /data/helm/app-name/config/values.yaml
212
        EOS
213 214
      end
    end
215
  end
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  describe '#rbac?' do
    subject { install_command.rbac? }

    context 'rbac is enabled' do
      let(:rbac) { true }

      it { is_expected.to be_truthy }
    end

    context 'rbac is not enabled' do
      let(:rbac) { false }

      it { is_expected.to be_falsey }
    end
  end

  describe '#pod_resource' do
    subject { install_command.pod_resource }

    context 'rbac is enabled' do
      let(:rbac) { true }

      it 'generates a pod that uses the tiller serviceAccountName' do
        expect(subject.spec.serviceAccountName).to eq('tiller')
      end
    end

    context 'rbac is not enabled' do
      let(:rbac) { false }

      it 'generates a pod that uses the default serviceAccountName' do
        expect(subject.spec.serviceAcccountName).to be_nil
      end
    end
  end

253 254 255
  describe '#config_map_resource' do
    let(:metadata) do
      {
256 257 258
        name: "values-content-configuration-app-name",
        namespace: 'gitlab-managed-apps',
        labels: { name: "values-content-configuration-app-name" }
259
      }
260
    end
261

262
    let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: files) }
263

264
    subject { install_command.config_map_resource }
265

266 267
    it 'returns a KubeClient resource with config map content for the application' do
      is_expected.to eq(resource)
268
    end
269
  end
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

  describe '#service_account_resource' do
    subject { install_command.service_account_resource }

    it 'returns nothing' do
      is_expected.to be_nil
    end
  end

  describe '#cluster_role_binding_resource' do
    subject { install_command.cluster_role_binding_resource }

    it 'returns nothing' do
      is_expected.to be_nil
    end
  end
286
end