summaryrefslogtreecommitdiff
path: root/spec/lib/quality/kubernetes_client_spec.rb
blob: 5bac102ac41dea8c3eebee83706b8e1ab947038e (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Quality::KubernetesClient do
  let(:namespace) { 'review-apps-ee' }
  let(:release_name) { 'my-release' }

  subject { described_class.new(namespace: namespace) }

  describe '#cleanup' do
    it 'raises an error if the Kubernetes command fails' do
      expect(Gitlab::Popen).to receive(:popen_with_detail)
        .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=true -l release=\"#{release_name}\""])
        .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))

      expect { subject.cleanup(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
    end

    it 'calls kubectl with the correct arguments' do
      expect(Gitlab::Popen).to receive(:popen_with_detail)
        .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=true -l release=\"#{release_name}\""])
        .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))

      # We're not verifying the output here, just silencing it
      expect { subject.cleanup(release_name: release_name) }.to output.to_stdout
    end

    context 'with multiple releases' do
      let(:release_name) { ['my-release', 'my-release-2'] }

      it 'raises an error if the Kubernetes command fails' do
        expect(Gitlab::Popen).to receive(:popen_with_detail)
                                   .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=true -l 'release in (#{release_name.join(', ')})'"])
                                   .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))

        expect { subject.cleanup(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
      end

      it 'calls kubectl with the correct arguments' do
        expect(Gitlab::Popen).to receive(:popen_with_detail)
                                   .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=true -l 'release in (#{release_name.join(', ')})'"])
                                   .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))

        # We're not verifying the output here, just silencing it
        expect { subject.cleanup(release_name: release_name) }.to output.to_stdout
      end
    end

    context 'with `wait: false`' do
      it 'raises an error if the Kubernetes command fails' do
        expect(Gitlab::Popen).to receive(:popen_with_detail)
                                   .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=false -l release=\"#{release_name}\""])
                                   .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))

        expect { subject.cleanup(release_name: release_name, wait: false) }.to raise_error(described_class::CommandFailedError)
      end

      it 'calls kubectl with the correct arguments' do
        expect(Gitlab::Popen).to receive(:popen_with_detail)
                                   .with([%(kubectl --namespace "#{namespace}" delete ) \
          'ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa ' \
          "--now --ignore-not-found --include-uninitialized --wait=false -l release=\"#{release_name}\""])
                                   .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))

        # We're not verifying the output here, just silencing it
        expect { subject.cleanup(release_name: release_name, wait: false) }.to output.to_stdout
      end
    end
  end
end