2019-11-25 22:06:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-07 07:50:39 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Kubernetes::Namespace do
|
|
|
|
let(:name) { 'a_namespace' }
|
|
|
|
let(:client) { double('kubernetes client') }
|
2020-01-17 01:08:39 -05:00
|
|
|
let(:labels) { nil }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2020-01-17 01:08:39 -05:00
|
|
|
subject { described_class.new(name, client, labels: labels) }
|
2017-11-07 07:50:39 -05:00
|
|
|
|
|
|
|
it { expect(subject.name).to eq(name) }
|
|
|
|
|
|
|
|
describe '#exists?' do
|
|
|
|
context 'when namespace do not exits' do
|
2018-11-13 07:46:01 -05:00
|
|
|
let(:exception) { ::Kubeclient::ResourceNotFoundError.new(404, "namespace #{name} not found", nil) }
|
2017-11-07 07:50:39 -05:00
|
|
|
|
|
|
|
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
|
2020-01-17 01:08:39 -05:00
|
|
|
|
|
|
|
context 'with labels' do
|
|
|
|
let(:labels) { { foo: :bar } }
|
|
|
|
|
|
|
|
it 'creates a namespace with labels' do
|
|
|
|
matcher = have_attributes(metadata: have_attributes(name: name, labels: have_attributes(foo: :bar)))
|
|
|
|
expect(client).to receive(:create_namespace).with(matcher).once
|
|
|
|
|
|
|
|
expect { subject.create! }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2017-11-07 07:50:39 -05:00
|
|
|
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
|
2019-04-10 02:38:27 -04:00
|
|
|
|
|
|
|
context 'when client errors' do
|
|
|
|
let(:exception) { Kubeclient::HttpError.new(500, 'system failure', nil) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(client).to receive(:get_namespace).with(name).once.and_raise(exception)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises the exception' do
|
|
|
|
expect { subject.ensure_exists! }.to raise_error(exception)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the error' do
|
|
|
|
expect(subject.send(:logger)).to receive(:error).with(
|
|
|
|
hash_including(
|
2020-02-19 01:09:21 -05:00
|
|
|
exception: {
|
|
|
|
class: 'Kubeclient::HttpError',
|
|
|
|
message: 'system failure'
|
|
|
|
},
|
2019-04-10 02:38:27 -04:00
|
|
|
status_code: 500,
|
|
|
|
namespace: 'a_namespace',
|
|
|
|
class_name: 'Gitlab::Kubernetes::Namespace',
|
2020-02-19 01:09:21 -05:00
|
|
|
event: :failed_to_create_namespace
|
2019-04-10 02:38:27 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { subject.ensure_exists! }.to raise_error(exception)
|
|
|
|
end
|
|
|
|
end
|
2017-11-07 07:50:39 -05:00
|
|
|
end
|
|
|
|
end
|