debian-mirror-gitlab/spec/models/project_services/kubernetes_service_spec.rb

168 lines
4.7 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe KubernetesService, :use_clean_rails_memory_store_caching do
2017-08-17 22:00:37 +05:30
include KubernetesHelpers
include ReactiveCachingHelpers
2018-03-17 18:26:18 +05:30
let(:project) { create(:kubernetes_project) }
2019-09-30 21:07:59 +05:30
let(:service) { create(:kubernetes_service, project: project) }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe 'Associations' do
2017-08-17 22:00:37 +05:30
it { is_expected.to belong_to :project }
end
describe 'Validations' do
context 'when service is active' do
2017-09-10 17:25:29 +05:30
before do
subject.active = true
2019-09-04 21:01:54 +05:30
subject.skip_deprecation_validation = true
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it { is_expected.not_to validate_presence_of(:namespace) }
it { is_expected.to validate_presence_of(:api_url) }
it { is_expected.to validate_presence_of(:token) }
context 'namespace format' do
before do
subject.project = project
subject.api_url = "http://example.com"
subject.token = "test"
end
{
'foo' => true,
'1foo' => true,
'foo1' => true,
'foo-bar' => true,
'-foo' => false,
'foo-' => false,
'a' * 63 => true,
'a' * 64 => false,
'a.b' => false,
2018-03-17 18:26:18 +05:30
'a*b' => false,
'FOO' => true
2017-08-17 22:00:37 +05:30
}.each do |namespace, validity|
it "validates #{namespace} as #{validity ? 'valid' : 'invalid'}" do
subject.namespace = namespace
expect(subject.valid?).to eq(validity)
end
end
end
end
context 'when service is inactive' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
subject.project = project
2017-09-10 17:25:29 +05:30
subject.active = false
end
2017-08-17 22:00:37 +05:30
it { is_expected.not_to validate_presence_of(:api_url) }
it { is_expected.not_to validate_presence_of(:token) }
end
2018-03-17 18:26:18 +05:30
context 'with a deprecated service' do
let(:kubernetes_service) { create(:kubernetes_service) }
before do
kubernetes_service.update_attribute(:active, false)
2019-09-04 21:01:54 +05:30
kubernetes_service.skip_deprecation_validation = false
2018-11-08 19:23:39 +05:30
kubernetes_service.properties['namespace'] = "foo"
2018-03-17 18:26:18 +05:30
end
2019-07-07 11:18:12 +05:30
it 'does not update attributes' do
2018-03-17 18:26:18 +05:30
expect(kubernetes_service.save).to be_falsy
end
2019-07-07 11:18:12 +05:30
it 'includes an error with a deprecation message' do
2018-03-17 18:26:18 +05:30
kubernetes_service.valid?
2019-09-30 21:07:59 +05:30
expect(kubernetes_service.errors[:base].first).to match(/Kubernetes service integration has been disabled/)
2018-03-17 18:26:18 +05:30
end
end
context 'with an active and deprecated service' do
let(:kubernetes_service) { create(:kubernetes_service) }
before do
2019-09-04 21:01:54 +05:30
kubernetes_service.skip_deprecation_validation = false
2018-03-17 18:26:18 +05:30
kubernetes_service.active = false
2018-11-08 19:23:39 +05:30
kubernetes_service.properties['namespace'] = 'foo'
2018-03-17 18:26:18 +05:30
kubernetes_service.save
end
2019-07-07 11:18:12 +05:30
it 'deactivates the service' do
2018-03-17 18:26:18 +05:30
expect(kubernetes_service.active?).to be_falsy
end
2019-07-07 11:18:12 +05:30
it 'does not include a deprecation message as error' do
2018-03-17 18:26:18 +05:30
expect(kubernetes_service.errors.messages.count).to eq(0)
end
2019-07-07 11:18:12 +05:30
it 'updates attributes' do
2018-11-08 19:23:39 +05:30
expect(kubernetes_service.properties['namespace']).to eq("foo")
2018-03-17 18:26:18 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
describe '#initialize_properties' do
context 'without a project' do
it 'leaves the namespace unset' do
expect(described_class.new.namespace).to be_nil
end
end
end
describe '#fields' do
let(:kube_namespace) do
subject.fields.find { |h| h[:name] == 'namespace' }
end
context 'as template' do
2017-09-10 17:25:29 +05:30
before do
subject.template = true
end
2017-08-17 22:00:37 +05:30
it 'sets the namespace to the default' do
expect(kube_namespace).not_to be_nil
expect(kube_namespace[:placeholder]).to eq(subject.class::TEMPLATE_PLACEHOLDER)
end
end
context 'with associated project' do
2017-09-10 17:25:29 +05:30
before do
subject.project = project
end
2017-08-17 22:00:37 +05:30
it 'sets the namespace to the default' do
expect(kube_namespace).not_to be_nil
2017-09-10 17:25:29 +05:30
expect(kube_namespace[:placeholder]).to match(/\A#{Gitlab::PathRegex::PATH_REGEX_STR}-\d+\z/)
end
end
end
2018-03-17 18:26:18 +05:30
describe "#deprecated?" do
let(:kubernetes_service) { create(:kubernetes_service) }
2019-09-04 21:01:54 +05:30
it 'returns true' do
expect(kubernetes_service.deprecated?).to be_truthy
2018-03-17 18:26:18 +05:30
end
end
describe "#deprecation_message" do
let(:kubernetes_service) { create(:kubernetes_service) }
2019-07-07 11:18:12 +05:30
it 'indicates the service is deprecated' do
2019-09-30 21:07:59 +05:30
expect(kubernetes_service.deprecation_message).to match(/Kubernetes service integration has been disabled/)
2018-03-17 18:26:18 +05:30
end
context 'if the service is not active' do
2019-07-07 11:18:12 +05:30
it 'returns a message' do
2018-03-17 18:26:18 +05:30
kubernetes_service.update_attribute(:active, false)
2019-09-30 21:07:59 +05:30
expect(kubernetes_service.deprecation_message).to match(/Fields on this page are not used by GitLab/)
2018-03-17 18:26:18 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
end