Commit 5606ac4c authored by Andrejs Cunskis's avatar Andrejs Cunskis

Merge branch 'acunskis-contracts' into 'master'

QA: Contracts test setup improvements

See merge request gitlab-org/gitlab!82490
parents 378c54a3 d024806e
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'rake'
host = ARGV.shift
ENV['CONTRACT_HOST'] ||= host
list = []
loop do
keyword = ARGV.shift
case keyword
when '--mr'
ENV['CONTRACT_MR'] ||= ARGV.shift
list.push 'test:merge_request'
else
break
end
end
app = Rake.application
Dir.chdir('contracts/provider') do
app.init
app.add_import 'Rakefile'
app.load_rakefile
list.each do |element|
app[element].invoke
end
end
# frozen_string_literal: true
require 'pact/tasks/verification_task'
Pact::VerificationTask.new(:metadata) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json', pact_helper: './spec/metadata_helper.rb'
end
Pact::VerificationTask.new(:discussions) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json', pact_helper: './spec/discussions_helper.rb'
end
Pact::VerificationTask.new(:diffs) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json', pact_helper: './spec/diffs_helper.rb'
end
task 'test:merge_request' => ['pact:verify:metadata', 'pact:verify:discussions', 'pact:verify:diffs']
# frozen_string_literal: true
require 'faraday'
module Provider
module Environments
class Base
attr_writer :base_url, :merge_request
module Environments
class Base
attr_writer :base_url, :merge_request
def call(env)
@payload
end
def call(env)
@payload
end
def http(endpoint)
Faraday.default_adapter = :net_http
response = Faraday.get(@base_url + endpoint)
@payload = [response.status, response.headers, [response.body]]
self
end
def http(endpoint)
Faraday.default_adapter = :net_http
response = Faraday.get(@base_url + endpoint)
@payload = [response.status, response.headers, [response.body]]
self
end
def merge_request(endpoint)
if endpoint.include? '.json'
http(@merge_request + endpoint)
def merge_request(endpoint)
http(@merge_request + endpoint) if endpoint.include? '.json'
end
end
end
......
# frozen_string_literal: true
require_relative './base'
module Environments
class Local < Base
def initialize
@base_url = ENV['CONTRACT_HOST']
@merge_request = ENV['CONTRACT_MR']
module Provider
module Environments
class Local < Base
def initialize
@base_url = ENV['CONTRACT_HOST']
@merge_request = ENV['CONTRACT_MR']
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module DiffsHelper
local = Environments::Local.new
module Provider
module DiffsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Diffs Endpoint" do
app { local.merge_request('/diffs_batch.json?page=0') }
Pact.service_provider "Merge Request Diffs Endpoint" do
app { local.merge_request('/diffs_batch.json?page=0') }
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json'
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json'
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module DiscussionsHelper
local = Environments::Local.new
module Provider
module DiscussionsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Discussions Endpoint" do
app { local.merge_request('/discussions.json') }
Pact.service_provider "Merge Request Discussions Endpoint" do
app { local.merge_request('/discussions.json') }
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json'
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json'
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module MetadataHelper
local = Environments::Local.new
module Provider
module MetadataHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Metadata Endpoint" do
app { local.merge_request('/diffs_metadata.json') }
Pact.service_provider "Merge Request Metadata Endpoint" do
app { local.merge_request('/diffs_metadata.json') }
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json'
honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json'
end
end
end
end
# frozen_string_literal: true
module SpecHelper
unless ENV['CONTRACT_HOST']
raise(ArgumentError, 'Contract tests require CONTRACT_HOST environment variable to be set!')
end
require_relative '../../../config/bundler_setup'
Bundler.require(:default)
root = File.expand_path('../', __dir__)
loader = Zeitwerk::Loader.new
loader.push_dir(root)
loader.ignore("#{root}/consumer")
loader.ignore("#{root}/contracts")
loader.collapse("#{root}/provider/spec")
loader.setup
end
# frozen_string_literal: true
require 'pact/tasks/verification_task'
contracts = File.expand_path('../contracts', __dir__)
provider = File.expand_path('provider', contracts)
# rubocop:disable Rails/RakeEnvironment
namespace :contracts do
namespace :mr do
Pact::VerificationTask.new(:metadata) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_metadata_endpoint.json",
pact_helper: "#{provider}/spec/metadata_helper.rb"
)
end
Pact::VerificationTask.new(:discussions) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_discussions_endpoint.json",
pact_helper: "#{provider}/spec/discussions_helper.rb"
)
end
Pact::VerificationTask.new(:diffs) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_diffs_endpoint.json",
pact_helper: "#{provider}/spec/diffs_helper.rb"
)
end
desc 'Run all merge request contract tests'
task 'test:merge_request', :contract_mr do |_t, arg|
raise(ArgumentError, 'Merge request contract tests require contract_mr to be set') unless arg[:contract_mr]
ENV['CONTRACT_MR'] = arg[:contract_mr]
errors = %w[metadata discussions diffs].each_with_object([]) do |task, err|
Rake::Task["contracts:mr:pact:verify:#{task}"].execute
rescue StandardError, SystemExit
err << "contracts:mr:pact:verify:#{task}"
end
raise StandardError, "Errors in tasks #{errors.join(', ')}" unless errors.empty?
end
end
end
# rubocop:enable Rails/RakeEnvironment
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