summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/kubernetes/namespace_spec.rb
blob: e098612f6fbb1d24dd168a55d1e8ad04f00924fb (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
require 'spec_helper'

describe Gitlab::Kubernetes::Namespace do
  let(:name) { 'a_namespace' }
  let(:client) { double('kubernetes client') }
  subject { described_class.new(name, client) }

  it { expect(subject.name).to eq(name) }

  describe '#exists?' do
    context 'when namespace do not exits' do
      let(:exception) { ::Kubeclient::HttpError.new(404, "namespace #{name} not found", nil) }

      it 'returns false' do
        expect(client).to receive(:get_namespace).with(name).once.and_raise(exception)

        expect(subject.exists?).to be_falsey
      end
    end

    context 'when namespace exits' do
      let(:namespace) { ::Kubeclient::Resource.new(kind: 'Namespace', metadata: { name: name }) } # partial representation

      it 'returns true' do
        expect(client).to receive(:get_namespace).with(name).once.and_return(namespace)

        expect(subject.exists?).to be_truthy
      end
    end

    context 'when cluster cannot be reached' do
      let(:exception) { Errno::ECONNREFUSED.new }

      it 'raises exception' do
        expect(client).to receive(:get_namespace).with(name).once.and_raise(exception)

        expect { subject.exists? }.to raise_error(exception)
      end
    end
  end

  describe '#create!' do
    it 'creates a namespace' do
      matcher = have_attributes(metadata: have_attributes(name: name))
      expect(client).to receive(:create_namespace).with(matcher).once

      expect { subject.create! }.not_to raise_error
    end
  end

  describe '#ensure_exists!' do
    it 'checks for existing namespace before creating' do
      expect(subject).to receive(:exists?).once.ordered.and_return(false)
      expect(subject).to receive(:create!).once.ordered

      subject.ensure_exists!
    end

    it 'do not re-create an existing namespace' do
      expect(subject).to receive(:exists?).once.and_return(true)
      expect(subject).not_to receive(:create!)

      subject.ensure_exists!
    end
  end
end