Commit 11ca79e3 authored by Mark Lapierre's avatar Mark Lapierre

Merge branch 'ml-qa-refactor-docker-services' into 'master'

Extract shared docker code into a base class

See merge request gitlab-org/gitlab!18306
parents e4392000 62d65a9d
......@@ -396,8 +396,6 @@ module QA
autoload :Shellout, 'qa/service/shellout'
autoload :KubernetesCluster, 'qa/service/kubernetes_cluster'
autoload :Omnibus, 'qa/service/omnibus'
autoload :Runner, 'qa/service/runner'
autoload :LDAP, 'qa/service/ldap'
module ClusterProvider
autoload :Base, 'qa/service/cluster_provider/base'
......@@ -405,6 +403,12 @@ module QA
autoload :Minikube, 'qa/service/cluster_provider/minikube'
autoload :K3d, 'qa/service/cluster_provider/k3d'
end
module DockerRun
autoload :Base, 'qa/service/docker_run/base'
autoload :LDAP, 'qa/service/docker_run/ldap'
autoload :GitlabRunner, 'qa/service/docker_run/gitlab_runner'
end
end
##
......
......@@ -29,7 +29,7 @@ module QA
end
def fabricate_via_api!
Service::Runner.new(name).tap do |runner|
Service::DockerRun::GitlabRunner.new(name).tap do |runner|
runner.pull
runner.token = @token ||= project.runners_token
runner.address = Runtime::Scenario.gitlab_address
......@@ -46,7 +46,7 @@ module QA
super
Service::Runner.new(name).remove!
Service::DockerRun::GitlabRunner.new(name).remove!
end
def api_delete_path
......
# frozen_string_literal: true
module QA
module Service
module DockerRun
class Base
include Service::Shellout
def initialize
@network = Runtime::Scenario.attributes[:network] || 'test'
end
def network
shell "docker network inspect #{@network}"
rescue CommandError
'bridge'
else
@network
end
def pull
shell "docker pull #{@image}"
end
def host_name
"#{@name}.#{network}"
end
def register!
raise NotImplementedError
end
def remove!
shell "docker rm -f #{@name}" if running?
end
def running?
`docker ps -f name=#{@name}`.include?(@name)
end
end
end
end
end
# frozen_string_literal: true
require 'securerandom'
module QA
module Service
module DockerRun
class GitlabRunner < Base
attr_accessor :token, :address, :tags, :image, :run_untagged
attr_writer :config
def initialize(name)
@image = 'gitlab/gitlab-runner:alpine'
@name = name || "qa-runner-#{SecureRandom.hex(4)}"
@tags = %w[qa test]
@run_untagged = false
super()
end
def config
@config ||= <<~END
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
END
end
def register!
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}
#{@image} -c "#{register_command}"
CMD
end
private
def register_command
<<~CMD
printf '#{config.chomp.gsub(/\n/, "\\n").gsub('"', '\"')}' > /etc/gitlab-runner/config.toml &&
gitlab-runner register --run-untagged=#{@run_untagged} &&
gitlab-runner run
CMD
end
end
end
end
end
# frozen_string_literal: true
module QA
module Service
module DockerRun
class LDAP < Base
def initialize(volume)
@image = 'osixia/openldap:latest'
@name = 'ldap-server'
@volume = volume
super()
end
def register!
shell <<~CMD.tr("\n", ' ')
docker run -d --rm
--network #{network}
--hostname #{host_name}
--name #{@name}
-p 389:389
--volume #{volume_or_fixture(@volume)}:/container/service/slapd/assets/config/bootstrap/ldif/custom
#{@image} --copy-service
CMD
end
def volume_or_fixture(volume_name)
if volume_exists?(volume_name)
volume_name
else
File.expand_path("../fixtures/ldap/#{volume_name}", __dir__)
end
end
def volume_exists?(volume_name)
`docker volume ls -q -f name=#{volume_name}`.include?(volume_name)
end
end
end
end
end
# frozen_string_literal: true
module QA
module Service
class LDAP
include Service::Shellout
def initialize(volume)
@image = 'osixia/openldap:latest'
@name = 'ldap-server'
@network = Runtime::Scenario.attributes[:network] || 'test'
@volume = volume
end
def network
shell "docker network inspect #{@network}"
rescue CommandError
'bridge'
else
@network
end
def pull
shell "docker pull #{@image}"
end
def host_name
"#{@name}.#{network}"
end
def register!
shell <<~CMD.tr("\n", ' ')
docker run -d --rm
--network #{network}
--hostname #{host_name}
--name #{@name}
-p 389:389
--volume #{volume_or_fixture(@volume)}:/container/service/slapd/assets/config/bootstrap/ldif/custom
#{@image} --copy-service
CMD
end
def remove!
shell "docker rm -f #{@name}" if running?
end
def running?
`docker ps -f name=#{@name}`.include?(@name)
end
def volume_or_fixture(volume_name)
if volume_exists?(volume_name)
volume_name
else
File.expand_path("../fixtures/ldap/#{volume_name}", __dir__)
end
end
def volume_exists?(volume_name)
`docker volume ls -q -f name=#{volume_name}`.include?(volume_name)
end
end
end
end
# frozen_string_literal: true
require 'securerandom'
module QA
module Service
class Runner
include Service::Shellout
attr_accessor :token, :address, :tags, :image, :run_untagged
attr_writer :config
def initialize(name)
@image = 'gitlab/gitlab-runner:alpine'
@name = name || "qa-runner-#{SecureRandom.hex(4)}"
@network = Runtime::Scenario.attributes[:network] || 'test'
@tags = %w[qa test]
@run_untagged = false
end
def config
@config ||= <<~END
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
END
end
def network
shell "docker network inspect #{@network}"
rescue CommandError
'bridge'
else
@network
end
def pull
shell "docker pull #{@image}"
end
def register!
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}
#{@image} -c "#{register_command}"
CMD
end
def remove!
shell "docker rm -f #{@name}"
end
private
def register_command
<<~CMD
printf '#{config.chomp.gsub(/\n/, "\\n").gsub('"', '\"')}' > /etc/gitlab-runner/config.toml &&
gitlab-runner register --run-untagged=#{@run_untagged} &&
gitlab-runner run
CMD
end
end
end
end
......@@ -6,7 +6,7 @@ module QA
let(:executor) { "qa-runner-#{Time.now.to_i}" }
after do
Service::Runner.new(executor).remove!
Service::DockerRun::GitlabRunner.new(executor).remove!
end
it 'users creates a pipeline which gets processed' do
......
......@@ -6,7 +6,7 @@ module QA
let(:executor) { "qa-runner-#{Time.now.to_i}" }
after do
Service::Runner.new(executor).remove!
Service::DockerRun::GitlabRunner.new(executor).remove!
end
it 'user registers a new specific runner' do
......
......@@ -26,7 +26,7 @@ module QA
end
after do
Service::Runner.new(@runner_name).remove!
Service::DockerRun::GitlabRunner.new(@runner_name).remove!
end
keys = [
......
......@@ -39,14 +39,14 @@ module QA
end
def run_ldap_service_with_user_as(user_status)
Service::LDAP.new(user_status).tap do |runner|
Service::DockerRun::LDAP.new(user_status).tap do |runner|
runner.pull
runner.register!
end
end
def remove_ldap_service_with_user_as(user_status)
Service::LDAP.new(user_status).remove!
Service::DockerRun::LDAP.new(user_status).remove!
end
def login_with_ldap_admin_user
......
......@@ -49,7 +49,7 @@ module QA
after do
# Remove the runner even if the test fails
Service::Runner.new(@runner.name).remove! if @runner
Service::DockerRun::GitlabRunner.new(@runner.name).remove! if @runner
end
it 'user starts the web terminal' do
......
......@@ -44,7 +44,7 @@ module QA
end
after(:context) do
Service::Runner.new(@executor).remove!
Service::DockerRun::GitlabRunner.new(@executor).remove!
end
it 'creates a pipeline with merged results' do
......
......@@ -12,7 +12,7 @@ module QA
let(:dast_vuln_count) { 4 }
after do
Service::Runner.new(@executor).remove!
Service::DockerRun::GitlabRunner.new(@executor).remove!
end
before do
......
......@@ -17,7 +17,7 @@ module QA
describe 'Security Reports' do
after do
Service::Runner.new(@executor).remove!
Service::DockerRun::GitlabRunner.new(@executor).remove!
end
before do
......
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