class Gitlab::QA::Component::Base

Attributes

docker[R]
environment[RW]
exec_commands[RW]
name[W]
network[RW]
reconfigure[R]
runner_network[RW]
volumes[RW]
wait_until_ready[R]

Public Class Methods

new() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 11
def initialize
  @docker = Docker::Engine.new
  @environment = {}
  @volumes = {}
  @network_aliases = []

  self.exec_commands = []
end

Public Instance Methods

add_network_alias(name) click to toggle source
# File lib/gitlab/qa/component/base.rb, line 20
def add_network_alias(name)
  @network_aliases.push(name)
end
hostname() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 28
def hostname
  "#{name}.#{network}"
end
image() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 32
def image
  return self.class.const_get('DOCKER_IMAGE') if self.class.const_defined?('DOCKER_IMAGE')

  raise NotImplementedError, "#{self.class.name} must specify a docker image as DOCKER_IMAGE"
end
instance(skip_teardown: false) { |self| ... } click to toggle source
# File lib/gitlab/qa/component/base.rb, line 44
def instance(skip_teardown: false)
  instance_no_teardown do
    yield self if block_given?
  end
ensure
  teardown unless skip_teardown
end
Also aliased as: launch_and_teardown_instance
launch_and_teardown_instance(skip_teardown: false)
Alias for: instance
name() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 24
def name
  raise NotImplementedError, "#{self.class.name} must specify a default name"
end
prepare() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 54
def prepare
  prepare_docker_image
  prepare_docker_container
  prepare_network
end
prepare_docker_container() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 74
def prepare_docker_container
  return unless docker.container_exists?(name)

  docker.remove(name)
end
prepare_docker_image() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 60
def prepare_docker_image
  pull
end
prepare_network() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 64
def prepare_network
  if runner_network && !docker.network_exists?(runner_network) # rubocop:disable Style/IfUnlessModifier
    docker.network_create("--driver=bridge --internal #{runner_network}")
  end

  return if docker.network_exists?(network)

  docker.network_create(network)
end
process_exec_commands() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 135
def process_exec_commands
  exec_commands.each { |command| docker.exec(name, command) }
end
pull() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 129
def pull
  return if Runtime::Env.skip_pull?

  docker.pull(image: image, tag: tag)
end
restart() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 103
def restart
  assert_name!

  docker.restart(name)
end
start() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 80
def start # rubocop:disable Metrics/AbcSize
  docker.run(image: image, tag: tag) do |command|
    command << "-d"
    command << "--name #{name}"
    command << "--net #{network}"
    command << "--hostname #{hostname}"

    @volumes.to_h.each do |to, from|
      command.volume(to, from, 'Z')
    end

    command.volume(File.join(Runtime::Env.host_artifacts_dir, name, 'logs'), '/var/log/gitlab', 'Z')

    @environment.to_h.each do |key, value|
      command.env(key, value)
    end

    @network_aliases.to_a.each do |network_alias|
      command << "--network-alias #{network_alias}"
    end
  end
end
tag() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 38
def tag
  return self.class.const_get('DOCKER_IMAGE_TAG') if self.class.const_defined?('DOCKER_IMAGE_TAG')

  raise NotImplementedError, "#{self.class.name} must specify a docker image tag as DOCKER_IMAGE_TAG"
end
teardown() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 109
def teardown
  unless teardown?
    puts "The orchestrated docker containers have not been removed."
    docker.ps

    return
  end

  teardown!
end
teardown!() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 120
def teardown!
  assert_name!

  return unless docker.running?(name)

  docker.stop(name)
  docker.remove(name)
end

Private Instance Methods

assert_name!() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 143
def assert_name!
  raise 'Invalid instance name!' unless name
end
instance_no_teardown() { |self| ... } click to toggle source
# File lib/gitlab/qa/component/base.rb, line 147
def instance_no_teardown
  prepare
  start
  reconfigure
  wait_until_ready
  process_exec_commands

  yield self if block_given?
end
teardown?() click to toggle source
# File lib/gitlab/qa/component/base.rb, line 157
def teardown?
  !Runtime::Scenario.attributes.include?(:teardown) || Runtime::Scenario.teardown
end