Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'qa/qa/specs/helpers')
-rw-r--r--qa/qa/specs/helpers/context_selector.rb86
-rw-r--r--qa/qa/specs/helpers/quarantine.rb12
-rw-r--r--qa/qa/specs/helpers/rspec.rb30
3 files changed, 120 insertions, 8 deletions
diff --git a/qa/qa/specs/helpers/context_selector.rb b/qa/qa/specs/helpers/context_selector.rb
new file mode 100644
index 00000000000..4313f7c34dd
--- /dev/null
+++ b/qa/qa/specs/helpers/context_selector.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+require 'rspec/core'
+
+module QA
+ module Specs
+ module Helpers
+ module ContextSelector
+ extend self
+
+ def configure_rspec
+ ::RSpec.configure do |config|
+ config.before do |example|
+ if example.metadata.key?(:only)
+ skip('Test is not compatible with this environment or pipeline') unless ContextSelector.context_matches?(example.metadata[:only])
+ elsif example.metadata.key?(:exclude)
+ skip('Test is excluded in this job') if ContextSelector.exclude?(example.metadata[:exclude])
+ end
+ end
+ end
+ end
+
+ def exclude?(*options)
+ return false unless Runtime::Env.ci_job_name.present?
+
+ context_matches?(*options)
+ end
+
+ def context_matches?(*options)
+ return false unless Runtime::Scenario.attributes[:gitlab_address]
+
+ opts = {}
+ opts[:domain] = '.+'
+ opts[:tld] = '.com'
+
+ uri = URI(Runtime::Scenario.gitlab_address)
+
+ options.each do |option|
+ opts[:domain] = 'gitlab' if option == :production
+
+ next unless option.is_a?(Hash)
+
+ if option[:pipeline].present? && Runtime::Env.ci_project_name.present?
+ return pipeline_matches?(option[:pipeline])
+
+ elsif option[:job].present?
+ return job_matches?(option[:job])
+
+ elsif option[:subdomain].present?
+ opts.merge!(option)
+
+ opts[:subdomain] = case option[:subdomain]
+ when Array
+ "(#{option[:subdomain].join("|")})."
+ when Regexp
+ option[:subdomain]
+ else
+ "(#{option[:subdomain]})."
+ end
+ end
+ end
+
+ uri.host.match?(/^#{opts[:subdomain]}#{opts[:domain]}#{opts[:tld]}$/)
+ end
+
+ alias_method :dot_com?, :context_matches?
+
+ def job_matches?(job_patterns)
+ Array(job_patterns).any? do |job|
+ pattern = job.is_a?(Regexp) ? job : Regexp.new(job)
+ pattern = Regexp.new(pattern.source, pattern.options | Regexp::IGNORECASE)
+ pattern =~ Runtime::Env.ci_job_name
+ end
+ end
+
+ def pipeline_matches?(pipeline_to_run_in)
+ Array(pipeline_to_run_in).any? { |pipeline| pipeline.to_s.casecmp?(pipeline_from_project_name(Runtime::Env.ci_project_name)) }
+ end
+
+ def pipeline_from_project_name(project_name)
+ project_name.to_s.start_with?('gitlab-qa') ? Runtime::Env.default_branch : project_name
+ end
+ end
+ end
+ end
+end
diff --git a/qa/qa/specs/helpers/quarantine.rb b/qa/qa/specs/helpers/quarantine.rb
index d365819057e..15b4ed8336b 100644
--- a/qa/qa/specs/helpers/quarantine.rb
+++ b/qa/qa/specs/helpers/quarantine.rb
@@ -6,22 +6,18 @@ module QA
module Specs
module Helpers
module Quarantine
- include RSpec::Core::Pending
+ include ::RSpec::Core::Pending
extend self
def configure_rspec
- RSpec.configure do |config|
+ ::RSpec.configure do |config|
config.before(:context, :quarantine) do
Quarantine.skip_or_run_quarantined_contexts(config.inclusion_filter.rules, self.class)
end
config.before do |example|
Quarantine.skip_or_run_quarantined_tests_or_contexts(config.inclusion_filter.rules, example)
-
- if example.metadata.key?(:only)
- skip('Test is not compatible with this environment or pipeline') unless Runtime::Env.context_matches?(example.metadata[:only])
- end
end
end
end
@@ -52,10 +48,10 @@ module QA
if example.metadata.key?(:quarantine)
quarantine_tag = example.metadata[:quarantine]
- if quarantine_tag&.is_a?(Hash) && quarantine_tag&.key?(:only)
+ if quarantine_tag.is_a?(Hash) && quarantine_tag&.key?(:only)
# If the :quarantine hash contains :only, we respect that.
# For instance `quarantine: { only: { subdomain: :staging } }` will only quarantine the test when it runs against staging.
- return unless Runtime::Env.context_matches?(quarantine_tag[:only])
+ return unless ContextSelector.context_matches?(quarantine_tag[:only])
end
skip(quarantine_message(quarantine_tag))
diff --git a/qa/qa/specs/helpers/rspec.rb b/qa/qa/specs/helpers/rspec.rb
new file mode 100644
index 00000000000..f49e556b0d9
--- /dev/null
+++ b/qa/qa/specs/helpers/rspec.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+require 'rspec/core'
+
+module QA
+ module Specs
+ module Helpers
+ module RSpec
+ # We need a reporter for internal tests that's different from the reporter for
+ # external tests otherwise the results will be mixed up. We don't care about
+ # most reporting, but we do want to know if a test fails
+ class RaiseOnFailuresReporter < ::RSpec::Core::NullReporter
+ def self.example_failed(example)
+ raise example.exception
+ end
+ end
+
+ # We use an example group wrapper to prevent the state of internal tests
+ # expanding into the global state
+ # See: https://github.com/rspec/rspec-core/issues/2603
+ def describe_successfully(*args, &describe_body)
+ example_group = RSpec.describe(*args, &describe_body)
+ ran_successfully = example_group.run RaiseOnFailuresReporter
+ expect(ran_successfully).to eq true
+ example_group
+ end
+ end
+ end
+ end
+end