Commit bac764f9 authored by Walmyr Lima e Silva Filho's avatar Walmyr Lima e Silva Filho

Merge branch 'qa-add-docker-executor' into 'master'

Extend QA Runner service to support executors

See merge request gitlab-org/gitlab!31157
parents 8b2f1d2d 8a31776e
......@@ -171,7 +171,12 @@ module QA
end
def runners(tag_list: nil)
response = get Runtime::API::Request.new(api_client, "#{api_runners_path}?tag_list=#{tag_list.compact.join(',')}").url
response = if tag_list
get Runtime::API::Request.new(api_client, "#{api_runners_path}?tag_list=#{tag_list.compact.join(',')}").url
else
get Runtime::API::Request.new(api_client, "#{api_runners_path}").url
end
parse_body(response)
end
......
......@@ -5,8 +5,8 @@ require 'securerandom'
module QA
module Resource
class Runner < Base
attr_writer :name, :tags, :image
attr_accessor :config, :token
attr_writer :name, :tags, :image, :executor, :executor_image
attr_accessor :config, :token, :run_untagged
attribute :id
attribute :project do
......@@ -20,35 +20,42 @@ module QA
@name || "qa-runner-#{SecureRandom.hex(4)}"
end
def tags
@tags || %w[qa e2e]
end
def image
@image || 'gitlab/gitlab-runner:alpine'
end
def executor
@executor || :shell
end
def executor_image
@executor_image || 'registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-qa-alpine-ruby-2.6'
end
def fabricate_via_api!
Service::DockerRun::GitlabRunner.new(name).tap do |runner|
runner.pull
runner.token = @token ||= project.runners_token
runner.address = Runtime::Scenario.gitlab_address
runner.tags = tags
runner.tags = @tags if @tags
runner.image = image
runner.config = config if config
runner.executor = executor
runner.executor_image = executor_image if executor == :docker
runner.run_untagged = run_untagged if run_untagged
runner.register!
end
end
def remove_via_api!
runners = project.runners(tag_list: tags)
runners = project.runners(tag_list: @tags)
unless runners && !runners.empty?
raise "Project #{project.path_with_namespace} has no runners with tags #{tags}."
raise "Project #{project.path_with_namespace} has no runners#{" with tags #{@tags}." if @tags&.any?}"
end
this_runner = runners.find { |runner| runner[:description] == name }
unless this_runner
raise "Project #{project.path_with_namespace} does not have a runner with a description matching #{name} and tags #{tags}. Runners available: #{runners}"
raise "Project #{project.path_with_namespace} does not have a runner with a description matching #{name} #{"or tags #{@tags}" if @tags&.any?}. Runners available: #{runners}"
end
@id = this_runner[:id]
......
......@@ -6,14 +6,21 @@ module QA
module Service
module DockerRun
class GitlabRunner < Base
attr_accessor :token, :address, :tags, :image, :run_untagged
attr_writer :config
attr_reader :tags
attr_accessor :token, :address, :image, :run_untagged
attr_writer :config, :executor, :executor_image
CONFLICTING_VARIABLES_MESSAGE = <<~MSG
There are conflicting options preventing the runner from starting.
%s cannot be specified if %s is %s
MSG
def initialize(name)
@image = 'gitlab/gitlab-runner:alpine'
@name = name || "qa-runner-#{SecureRandom.hex(4)}"
@tags = %w[qa test]
@run_untagged = false
@run_untagged = true
@executor = :shell
@executor_image = 'registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-qa-alpine-ruby-2.6'
super()
end
......@@ -32,23 +39,49 @@ module QA
shell <<~CMD.tr("\n", ' ')
docker run -d --rm --entrypoint=/bin/sh
--network #{network} --name #{@name}
-p 8093:8093
-e CI_SERVER_URL=#{@address}
-e REGISTER_NON_INTERACTIVE=true
-e REGISTRATION_TOKEN=#{@token}
-e RUNNER_EXECUTOR=shell
-e RUNNER_TAG_LIST=#{@tags.join(',')}
-e RUNNER_NAME=#{@name}
#{'-v /var/run/docker.sock:/var/run/docker.sock' if @executor == :docker}
--privileged
#{@image} -c "#{register_command}"
CMD
end
def tags=(tags)
@tags = tags
@run_untagged = false
end
private
def register_command
<<~CMD
args = []
args << '--non-interactive'
args << "--name #{@name}"
args << "--url #{@address}"
args << "--registration-token #{@token}"
args << if run_untagged
raise CONFLICTING_VARIABLES_MESSAGE % [:tags=, :run_untagged, run_untagged] if @tags&.any?
'--run-untagged=true'
else
raise 'You must specify tags to run!' unless @tags&.any?
"--tag-list #{@tags.join(',')}"
end
args << "--executor #{@executor}"
if @executor == :docker
args << "--docker-image #{@executor_image}"
args << '--docker-tlsverify=false'
args << '--docker-privileged=true'
args << "--docker-network-mode=#{network}"
end
<<~CMD.strip
printf '#{config.chomp.gsub(/\n/, "\\n").gsub('"', '\"')}' > /etc/gitlab-runner/config.toml &&
gitlab-runner register --run-untagged=#{@run_untagged} &&
gitlab-runner register \
#{args.join(' ')} &&
gitlab-runner run
CMD
end
......
......@@ -8,6 +8,7 @@ module QA
let(:runner) do
Resource::Runner.fabricate_via_api! do |runner|
runner.name = executor
runner.run_untagged = true
end
end
......@@ -17,9 +18,6 @@ module QA
mr.file_name = '.gitlab-ci.yml'
mr.file_content = <<~EOF
test:
tags:
- qa
- e2e
script:
- echo '(66.67%) covered'
EOF
......
# frozen_string_literal: true
module QA
describe Service::DockerRun::GitlabRunner do
let(:runner_name) { 'test-runner' }
let(:address) { 'gitlab.test' }
let(:token) { 'abc123' }
let(:tags) { %w[qa test] }
subject do
described_class.new(runner_name).tap do |runner|
runner.address = address
runner.token = token
end
end
it 'defaults to run untagged' do
expect(subject.run_untagged).to be(true)
end
describe '#register!' do
let(:register) { subject.send(:register!) }
before do
allow(subject).to receive(:shell)
end
context 'defaults' do
before do
register
end
it 'runs non-interactively' do
expect(subject).to have_received(:shell).with(/ --non-interactive /)
end
it 'sets pertinent information' do
expect(subject).to have_received(:shell).with(/--name #{runner_name} /)
expect(subject).to have_received(:shell).with(/--url #{subject.address} /)
expect(subject).to have_received(:shell).with(/--registration-token #{subject.token} /)
end
it 'runs untagged' do
expect(subject).to have_received(:shell).with(/--run-untagged=true /)
end
it 'has no tags' do
expect(subject.tags).to be_falsey
end
it 'runs daemonized' do
expect(subject).to have_received(:shell).with(/ -d /)
end
it 'cleans itself up' do
expect(subject).to have_received(:shell).with(/ --rm /)
end
end
context 'running untagged' do
before do
register
end
it 'passes --run-untagged=true' do
expect(subject).to have_received(:shell).with(/--run-untagged=true /)
end
it 'does not pass tag list' do
expect(subject).not_to have_received(:shell).with(/--tag-list/)
end
end
context 'running tagged' do
context 'with only tags set' do
before do
subject.tags = tags
register
end
it 'does not pass --run-untagged' do
expect(subject).not_to have_received(:shell).with(/--run-untagged=true/)
end
it 'passes the tags with comma-separation' do
expect(subject).to have_received(:shell).with(/--tag-list #{tags.join(',')} /)
end
end
context 'with specifying only run_untagged' do
before do
subject.run_untagged = false
end
it 'raises an error if tags are not specified' do
expect { register }.to raise_error(/must specify tags/i)
end
end
context 'when specifying contradicting variables' do
before do
subject.tags = tags
subject.run_untagged = true
end
it 'raises an error' do
expect { register }.to raise_error(/conflicting options/i)
end
end
end
context 'executors' do
it 'defaults to the shell executor' do
register
expect(subject).to have_received(:shell).with(/--executor shell /)
end
context 'docker' do
before do
subject.executor = :docker
register
end
it 'specifies the docker executor' do
expect(subject).to have_received(:shell).with(/--executor docker /)
end
it 'mounts the docker socket to the host runner' do
expect(subject).to have_received(:shell).with(/-v \/var\/run\/docker.sock:\/var\/run\/docker.sock /)
end
it 'runs in privileged mode' do
expect(subject).to have_received(:shell).with(/--privileged /)
end
it 'has a default image' do
expect(subject).to have_received(:shell).with(/--docker-image \b.+\b /)
end
it 'does not verify TLS' do
expect(subject).to have_received(:shell).with(/--docker-tlsverify=false /)
end
it 'passes privileged mode' do
expect(subject).to have_received(:shell).with(/--docker-privileged=true /)
end
it 'passes the host network' do
expect(subject).to have_received(:shell).with(/--docker-network-mode=#{subject.network}/)
end
end
end
end
describe '#tags=' do
before do
subject.tags = tags
end
it 'sets the tags' do
expect(subject.tags).to eq(tags)
end
it 'sets run_untagged' do
expect(subject.run_untagged).to be(false)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment