Class: Gitlab::QA::Component::Specs

Inherits:
Scenario::Template show all
Defined in:
lib/gitlab/qa/component/specs.rb

Overview

This class represents GitLab QA specs image that is implemented in the ‘qa/` directory located in GitLab CE / EE repositories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Scenario::Template

perform

Constructor Details

#initializeSpecs

Returns a new instance of Specs.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/qa/component/specs.rb', line 18

def initialize
  @docker = Docker::Engine.new(stream_output: true) # stream test output directly instead of through logger
  @env = {}
  @volumes = {}
  @additional_hosts = []
  @volumes = { '/var/run/docker.sock' => '/var/run/docker.sock' }

  include_optional_volumes(
    Runtime::Env.qa_rspec_report_path => 'rspec',
    Runtime::Env.qa_knapsack_report_path => 'knapsack'
  )
end

Instance Attribute Details

#additional_hostsObject

Returns the value of attribute additional_hosts.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def additional_hosts
  @additional_hosts
end

#argsObject

Returns the value of attribute args.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def args
  @args
end

#envObject

Returns the value of attribute env.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def env
  @env
end

#hostnameObject

Returns the value of attribute hostname.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def hostname
  @hostname
end

#networkObject

Returns the value of attribute network.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def network
  @network
end

#releaseObject

Returns the value of attribute release.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def release
  @release
end

#runner_networkObject

Returns the value of attribute runner_network.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def runner_network
  @runner_network
end

#suiteObject

Returns the value of attribute suite.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def suite
  @suite
end

#volumesObject

Returns the value of attribute volumes.



16
17
18
# File 'lib/gitlab/qa/component/specs.rb', line 16

def volumes
  @volumes
end

Instance Method Details

#internal_performObject

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gitlab/qa/component/specs.rb', line 44

def internal_perform
  return Runtime::Logger.info("Skipping tests.") if skip_tests?

  raise ArgumentError unless [suite, release].all?

  docker_pull_qa_image_if_needed

  Runtime::Logger.info("Running test suite `#{suite}` for #{release.project_name}")

  name = "#{release.project_name}-qa-#{SecureRandom.hex(4)}"

  feature_flag_sets = []

  # When `args` includes:
  #   `[..., "--disable-feature", "a", "--enable-feature", "b", "--set-feature-flags", "c=enable", ...]`
  # `feature_flag_sets` will be set to:
  #   `[["--disable-feature", "a"], ["--enable-feature", "b"], ["--set-feature-flags", "c=enable"]]`
  # This will result in tests running three times, once with each feature flag option.
  while (index = args&.index { |x| x =~ /--.*-feature/ })
    feature_flag_sets << args.slice!(index, 2)
  end

  # When `args` do not have any feature flag options, we add [] so that test is run exactly once.
  feature_flag_sets << [] unless feature_flag_sets.any?

  feature_flag_sets.each do |feature_flag_set|
    @docker.run(
      image: qa_image,
      args: [suite, *args_with_flags(args, feature_flag_set)],
      mask_secrets: Runtime::Env.variables_to_mask
    ) do |command|
      command << "-t --rm --net=#{network || 'bridge'}"

      unless hostname.nil?
        command << "--hostname #{hostname}"
        command.env('QA_HOSTNAME', hostname)
      end

      if Runtime::Env.docker_add_hosts.present? || additional_hosts.present?
        hosts = Runtime::Env.docker_add_hosts.concat(additional_hosts).map { |host| "--add-host=#{host} " }.join
        command << hosts # override /etc/hosts in docker container when test runs
      end

      Runtime::Env.variables.merge(env).each do |key, value|
        command.env(key, value)
      end

      command.volume(
        File.join(Runtime::Env.host_artifacts_dir, name),
        File.join(Docker::Volumes::QA_CONTAINER_WORKDIR, 'tmp')
      )

      volumes.to_h.each { |to, from| command.volume(to, from) }

      command.name(name)
    end
  end
end

#performObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/qa/component/specs.rb', line 31

def perform
  if Runtime::Env.use_selenoid?
    Component::Selenoid.perform do |selenoid|
      selenoid.network = network
      selenoid.instance do
        internal_perform
      end
    end
  else
    internal_perform
  end
end