2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2018-03-01 18:46:02 -05:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Clusters::Applications::Runner do
|
2018-03-01 18:46:02 -05:00
|
|
|
let(:ci_runner) { create(:ci_runner) }
|
|
|
|
|
|
|
|
include_examples 'cluster application core specs', :clusters_applications_runner
|
2018-10-15 05:03:15 -04:00
|
|
|
include_examples 'cluster application status specs', :clusters_applications_runner
|
2019-02-07 16:40:55 -05:00
|
|
|
include_examples 'cluster application version specs', :clusters_applications_runner
|
2018-11-26 15:02:33 -05:00
|
|
|
include_examples 'cluster application helm specs', :clusters_applications_runner
|
2019-02-06 06:05:21 -05:00
|
|
|
include_examples 'cluster application initial status specs'
|
2018-03-01 18:46:02 -05:00
|
|
|
|
|
|
|
it { is_expected.to belong_to(:runner) }
|
|
|
|
|
2019-04-12 01:42:48 -04:00
|
|
|
describe '#can_uninstall?' do
|
|
|
|
let(:gitlab_runner) { create(:clusters_applications_runner, runner: ci_runner) }
|
|
|
|
|
|
|
|
subject { gitlab_runner.can_uninstall? }
|
|
|
|
|
2019-07-16 12:11:10 -04:00
|
|
|
it { is_expected.to be_truthy }
|
2019-04-12 01:42:48 -04:00
|
|
|
end
|
|
|
|
|
2018-03-01 18:46:02 -05:00
|
|
|
describe '#install_command' do
|
|
|
|
let(:kubeclient) { double('kubernetes client') }
|
|
|
|
let(:gitlab_runner) { create(:clusters_applications_runner, runner: ci_runner) }
|
|
|
|
|
|
|
|
subject { gitlab_runner.install_command }
|
|
|
|
|
|
|
|
it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::InstallCommand) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is initialized with 4 arguments' do
|
2018-03-01 18:46:02 -05:00
|
|
|
expect(subject.name).to eq('runner')
|
|
|
|
expect(subject.chart).to eq('runner/gitlab-runner')
|
2019-04-24 11:31:37 -04:00
|
|
|
expect(subject.version).to eq(Clusters::Applications::Runner::VERSION)
|
2019-01-03 08:41:53 -05:00
|
|
|
expect(subject).to be_rbac
|
2018-03-01 18:46:02 -05:00
|
|
|
expect(subject.repository).to eq('https://charts.gitlab.io')
|
2018-08-07 08:39:38 -04:00
|
|
|
expect(subject.files).to eq(gitlab_runner.files)
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
2018-07-22 06:48:53 -04:00
|
|
|
|
2019-01-03 08:41:53 -05:00
|
|
|
context 'on a non rbac enabled cluster' do
|
2018-09-06 06:03:38 -04:00
|
|
|
before do
|
2019-01-03 08:41:53 -05:00
|
|
|
gitlab_runner.cluster.platform_kubernetes.abac!
|
2018-09-06 06:03:38 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 08:41:53 -05:00
|
|
|
it { is_expected.not_to be_rbac }
|
2018-09-06 06:03:38 -04:00
|
|
|
end
|
|
|
|
|
2018-07-22 06:48:53 -04:00
|
|
|
context 'application failed to install previously' do
|
|
|
|
let(:gitlab_runner) { create(:clusters_applications_runner, :errored, runner: ci_runner, version: '0.1.13') }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is initialized with the locked version' do
|
2019-04-24 11:31:37 -04:00
|
|
|
expect(subject.version).to eq(Clusters::Applications::Runner::VERSION)
|
2018-07-22 06:48:53 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
describe '#files' do
|
|
|
|
let(:application) { create(:clusters_applications_runner, runner: ci_runner) }
|
|
|
|
let(:values) { subject[:'values.yaml'] }
|
|
|
|
|
|
|
|
subject { application.files }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'includes runner valid values' do
|
2018-08-07 08:39:38 -04:00
|
|
|
expect(values).to include('concurrent')
|
|
|
|
expect(values).to include('checkInterval')
|
|
|
|
expect(values).to include('rbac')
|
|
|
|
expect(values).to include('runners')
|
|
|
|
expect(values).to include('privileged: true')
|
|
|
|
expect(values).to include('image: ubuntu:16.04')
|
|
|
|
expect(values).to include('resources')
|
2019-04-09 22:13:43 -04:00
|
|
|
expect(values).to match(/runnerToken: '?#{Regexp.escape(ci_runner.token)}/)
|
|
|
|
expect(values).to match(/gitlabUrl: '?#{Regexp.escape(Gitlab::Routing.url_helpers.root_url)}/)
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a runner' do
|
2018-11-26 15:02:33 -05:00
|
|
|
let(:application) { create(:clusters_applications_runner, runner: nil, cluster: cluster) }
|
2019-03-26 05:59:48 -04:00
|
|
|
let(:runner) { application.runner }
|
2018-03-01 18:46:02 -05:00
|
|
|
|
2019-03-26 05:59:48 -04:00
|
|
|
shared_examples 'runner creation' do
|
|
|
|
it 'creates a runner' do
|
|
|
|
expect { subject }.to change { Ci::Runner.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the new runner token' do
|
2019-04-09 22:13:43 -04:00
|
|
|
expect(values).to match(/runnerToken: '?#{Regexp.escape(runner.token)}/)
|
2019-03-26 05:59:48 -04:00
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
|
|
|
|
2019-03-26 05:59:48 -04:00
|
|
|
context 'project cluster' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:cluster) { create(:cluster, :with_installed_helm, projects: [project]) }
|
|
|
|
|
|
|
|
include_examples 'runner creation'
|
|
|
|
|
|
|
|
it 'creates a project runner' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(runner).to be_project_type
|
|
|
|
expect(runner.projects).to eq [project]
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
|
|
|
|
2019-03-26 05:59:48 -04:00
|
|
|
context 'group cluster' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:cluster) { create(:cluster, :with_installed_helm, cluster_type: :group_type, groups: [group]) }
|
|
|
|
|
|
|
|
include_examples 'runner creation'
|
|
|
|
|
|
|
|
it 'creates a group runner' do
|
|
|
|
subject
|
2018-03-01 18:46:02 -05:00
|
|
|
|
2019-03-26 05:59:48 -04:00
|
|
|
expect(runner).to be_group_type
|
|
|
|
expect(runner.groups).to eq [group]
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
context 'instance cluster' do
|
|
|
|
let(:cluster) { create(:cluster, :with_installed_helm, :instance) }
|
|
|
|
|
|
|
|
include_examples 'runner creation'
|
|
|
|
|
|
|
|
it 'creates an instance runner' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(runner).to be_instance_type
|
|
|
|
end
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
2018-03-06 03:26:50 -05:00
|
|
|
|
|
|
|
context 'with duplicated values on vendor/runner/values.yaml' do
|
2018-08-07 08:39:38 -04:00
|
|
|
let(:stub_values) do
|
2018-03-06 03:26:50 -05:00
|
|
|
{
|
|
|
|
"concurrent" => 4,
|
|
|
|
"checkInterval" => 3,
|
|
|
|
"rbac" => {
|
|
|
|
"create" => false
|
|
|
|
},
|
|
|
|
"clusterWideAccess" => false,
|
|
|
|
"runners" => {
|
|
|
|
"privileged" => false,
|
|
|
|
"image" => "ubuntu:16.04",
|
|
|
|
"builds" => {},
|
|
|
|
"services" => {},
|
|
|
|
"helpers" => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2018-08-07 08:39:38 -04:00
|
|
|
allow(application).to receive(:chart_values).and_return(stub_values)
|
2018-03-06 03:26:50 -05:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'overwrites values.yaml' do
|
2018-08-07 08:39:38 -04:00
|
|
|
expect(values).to match(/privileged: '?#{application.privileged}/)
|
2018-03-06 03:26:50 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|
2019-07-16 12:11:10 -04:00
|
|
|
|
|
|
|
describe '#prepare_uninstall' do
|
|
|
|
it 'pauses associated runner' do
|
|
|
|
active_runner = create(:ci_runner, contacted_at: 1.second.ago)
|
|
|
|
|
|
|
|
expect(active_runner.status).to eq(:online)
|
|
|
|
|
|
|
|
application_runner = create(:clusters_applications_runner, :scheduled, runner: active_runner)
|
|
|
|
application_runner.prepare_uninstall
|
|
|
|
|
|
|
|
expect(active_runner.status).to eq(:paused)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#make_uninstalling!' do
|
|
|
|
subject { create(:clusters_applications_runner, :scheduled, runner: ci_runner) }
|
|
|
|
|
|
|
|
it 'calls prepare_uninstall' do
|
2019-11-20 01:06:16 -05:00
|
|
|
expect_next_instance_of(described_class) do |instance|
|
|
|
|
expect(instance).to receive(:prepare_uninstall).and_call_original
|
|
|
|
end
|
2019-07-16 12:11:10 -04:00
|
|
|
|
|
|
|
subject.make_uninstalling!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#post_uninstall' do
|
|
|
|
it 'destroys its runner' do
|
|
|
|
application_runner = create(:clusters_applications_runner, :scheduled, runner: ci_runner)
|
|
|
|
|
|
|
|
expect { application_runner.post_uninstall }.to change { Ci::Runner.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
2018-03-01 18:46:02 -05:00
|
|
|
end
|