Skip to content

Commit

Permalink
feat(cli): specify pact urls as the arguments to pact-provider-verifi…
Browse files Browse the repository at this point in the history
…er instead of using --pact-urls option
  • Loading branch information
bethesque committed Oct 1, 2017
1 parent 7677987 commit df78617
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 60 deletions.
4 changes: 2 additions & 2 deletions bin/pact-provider-verifier
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env ruby
require 'pact/provider_verifier/cli'
Pact::ProviderVerifier::CLI.start
require 'pact/provider_verifier/cli/verify'
Pact::ProviderVerifier::CLI::Verify.start
46 changes: 23 additions & 23 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ class App

PROXY_PACT_HELPER = File.expand_path(File.join(File.dirname(__FILE__), "pact_helper.rb"))

def initialize options = {}
def initialize pact_urls, options = {}
@pact_urls = pact_urls
@options = options
end

def self.call options
new(options).call
def self.call pact_urls, options
new(pact_urls, options).call
end

def call
Expand All @@ -31,6 +32,10 @@ def call
exit exit_statuses.count{ | status | status != 0 }
end

private

attr_reader :pact_urls, :options

def setup
print_deprecation_note
set_environment_variables
Expand All @@ -39,18 +44,18 @@ def setup
end

def set_environment_variables
ENV['PROVIDER_STATES_SETUP_URL'] = @options.provider_states_setup_url
ENV['VERBOSE_LOGGING'] = @options.verbose if @options.verbose
ENV['CUSTOM_PROVIDER_HEADER'] = @options.custom_provider_header if @options.custom_provider_header
ENV['PROVIDER_STATES_SETUP_URL'] = options.provider_states_setup_url
ENV['VERBOSE_LOGGING'] = options.verbose if options.verbose
ENV['CUSTOM_PROVIDER_HEADER'] = options.custom_provider_header if options.custom_provider_header
end

def configure_service_provider
# Have to declare these locally as the class scope gets lost within the block
rack_reverse_proxy = configure_reverse_proxy
rack_reverse_proxy = configure_custom_header_middlware(rack_reverse_proxy)

provider_application_version = @options.provider_app_version
publish_results = @options.publish_verification_results
provider_application_version = options.provider_app_version
publish_results = options.publish_verification_results

Pact.service_provider "Running Provider Application" do
app do
Expand All @@ -66,7 +71,7 @@ def configure_service_provider
end

def configure_reverse_proxy
provider_base_url = @options.provider_base_url
provider_base_url = options.provider_base_url
Rack::ReverseProxy.new do
reverse_proxy_options(
verify_mode: OpenSSL::SSL::VERIFY_NONE,
Expand All @@ -78,32 +83,27 @@ def configure_reverse_proxy
end

def configure_custom_header_middlware rack_reverse_proxy
if @options.custom_provider_header
if options.custom_provider_header
Pact::ProviderVerifier::AddHeaderMiddlware.new(rack_reverse_proxy, parse_header)
else
rack_reverse_proxy
end
end

def pact_urls
@options.pact_urls.split(',')
end

def verify_pact pact_url
begin
options = {
verify_options = {
:pact_helper => PROXY_PACT_HELPER,
:pact_uri => pact_url,
:backtrace => false,
:pact_broker_username => @options.broker_username,
:pact_broker_password => @options.broker_password
:pact_broker_username => options.broker_username,
:pact_broker_password => options.broker_password
}
options[:description] = ENV['PACT_DESCRIPTION'] if ENV['PACT_DESCRIPTION']
options[:provider_state] = ENV['PACT_PROVIDER_STATE'] if ENV['PACT_PROVIDER_STATE']
verify_options[:description] = ENV['PACT_DESCRIPTION'] if ENV['PACT_DESCRIPTION']
verify_options[:provider_state] = ENV['PACT_PROVIDER_STATE'] if ENV['PACT_PROVIDER_STATE']

Cli::RunPactVerification.call(options)
Cli::RunPactVerification.call(verify_options)
rescue SystemExit => e
puts ""
e.status
end
end
Expand All @@ -113,12 +113,12 @@ def require_pact_project_pact_helper
end

def parse_header
header_name, header_value = @options.custom_provider_header.split(":", 2).collect(&:strip)
header_name, header_value = options.custom_provider_header.split(":", 2).collect(&:strip)
{header_name => header_value}
end

def print_deprecation_note
if @options.provider_states_url
if options.provider_states_url
$stderr.puts "WARN: The --provider-states-url option is deprecated and the URL endpoint can be removed from the application"
end
end
Expand Down
27 changes: 0 additions & 27 deletions lib/pact/provider_verifier/cli.rb

This file was deleted.

71 changes: 71 additions & 0 deletions lib/pact/provider_verifier/cli/custom_thor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'thor'

module Pact
module ProviderVerifier
module CLI
##
# Custom Thor task allows the following:
#
# `script arg1 arg2` to be interpreted as `script <default_task> arg1 arg2`
# `--option 1 --option 2` to be interpreted as `--option 1 2` (the standard Thor format for multiple value options)
# `script --help` to display the help for the default task instead of the command list
#
class CustomThor < ::Thor

no_commands do
def self.start given_args = ARGV, config = {}
super(massage_args(given_args))
end

def help *args
if args.empty?
super(self.class.default_task)
else
super
end
end

def self.massage_args argv
prepend_default_task_name(turn_muliple_tag_options_into_array(argv))
end

def self.prepend_default_task_name argv
if known_first_arguments.include?(argv[0])
argv
else
[default_command] + argv
end
end

# other task names, help, and the help shortcuts
def self.known_first_arguments
@known_first_arguments ||= tasks.keys + ::Thor::HELP_MAPPINGS + ['help']
end

def self.turn_muliple_tag_options_into_array argv
new_argv = []
opt_name = nil
argv.each_with_index do | arg, i |
if arg.start_with?('-')
opt_name = arg
existing = new_argv.find { | a | a.first == opt_name }
if !existing
new_argv << [arg]
end
else
if opt_name
existing = new_argv.find { | a | a.first == opt_name }
existing << arg
opt_name = nil
else
new_argv << [arg]
end
end
end
new_argv.flatten
end
end
end
end
end
end
50 changes: 50 additions & 0 deletions lib/pact/provider_verifier/cli/verify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'thor'
require 'socket'
require 'pact/provider_verifier/app'
require 'pact/provider_verifier/cli/custom_thor'

module Pact
module ProviderVerifier
module CLI
class Verify < CustomThor
desc 'PACT_URL ...', "Verify pact(s) against a provider. Supports local and networked (http-based) files."
method_option :provider_base_url, aliases: "-h", desc: "Provider host URL", :required => true
method_option :provider_states_setup_url, aliases: "-c", desc: "Base URL to setup the provider states at", :required => false
method_option :provider_app_version, aliases: "-a", desc: "Provider application version, required when publishing verification results", :required => false
method_option :publish_verification_results, aliases: "-r", desc: "Publish verification results to the broker", required: false
method_option :broker_username, aliases: "-n", desc: "Pact Broker basic auth username", :required => false
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password", :required => false
method_option :custom_provider_header, desc: "Header to add to provider state set up and pact requests. eg 'Authorization: Basic cGFjdDpwYWN0'", :required => false
method_option :provider_states_url, aliases: "-s", desc: "DEPRECATED", :required => false
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false
method_option :pact_urls, aliases: "-u", desc: "DEPRECATED. Please provide as space separated arguments.", :required => false

def verify(*pact_urls)
print_deprecation_warnings
Pact::ProviderVerifier::App.call(merged_urls(pact_urls), options)
end

default_task :verify

desc 'version', 'Show the pact-provider-verifier gem version'
def version
require 'pact/provider_verifier/version'
puts Pact::ProviderVerifier::VERSION
end

no_commands do
def merged_urls pact_urls_from_args
from_opts = options.pact_urls ? options.pact_urls.split(',') : []
from_opts + pact_urls_from_args
end

def print_deprecation_warnings
if options.pact_urls
$stderr.puts "WARN: The --pact-urls option is deprecated. Please pass in a space separated list of URLs as the first arguments to the pact-provider-verifier command."
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/integration_check_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sleep 2
end

subject { `bundle exec bin/pact-provider-verifier --provider-base-url http://localhost:4569 --pact-urls ./spec/support/echo-host.json` }
subject { `bundle exec bin/pact-provider-verifier ./spec/support/echo-host.json --provider-base-url http://localhost:4569` }

it "sets the correct Host header" do
expect(subject).to include "1 interaction, 0 failures"
Expand Down
10 changes: 5 additions & 5 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

context "when the verification passes" do

subject { `bundle exec bin/pact-provider-verifier -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/me-they.json --provider_states_setup_url http://localhost:4567/provider-state -v` }
subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state -v` }

it "exits with a 0 exit code" do
subject
Expand All @@ -20,7 +20,7 @@

context "with two passing interactions" do

subject { `bundle exec bin/pact-provider-verifier -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/me-they.json,./test/another-they.json --provider_states_setup_url http://localhost:4567/provider-state -v` }
subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json ./test/another-they.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state -v` }
it "exits with a 0 exit code" do
expect($?).to eq 0
end
Expand All @@ -32,7 +32,7 @@

context "when the verification fails" do

subject { `bundle exec bin/pact-provider-verifier -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/fail.json --provider_states_setup_url http://localhost:4567/provider-state -v` }
subject { `bundle exec bin/pact-provider-verifier ./test/fail.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state -v` }

it "exits with a non 0 exit code" do
subject
Expand All @@ -46,7 +46,7 @@

context "when there is an error setting up the state" do

subject { `bundle exec bin/pact-provider-verifier -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/me-they.json --provider_states_setup_url http://localhost:4567/wrong -v` }
subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/wrong -v` }

it "exits with a non 0 exit code" do
subject
Expand All @@ -60,7 +60,7 @@

context "running verification with filtered interactions" do

subject { `PACT_DESCRIPTION="Provider state success" PACT_PROVIDER_STATE="There is a greeting" bundle exec bin/pact-provider-verifier -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/me-they.json --provider_states_setup_url http://localhost:4567/provider-state -v` }
subject { `PACT_DESCRIPTION="Provider state success" PACT_PROVIDER_STATE="There is a greeting" bundle exec bin/pact-provider-verifier ./test/me-they.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state -v` }

it "exits with a 0 exit code" do
subject
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_with_custom_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

context "with --custom-provider-header specified" do

subject { `bundle exec bin/pact-provider-verifier --custom-provider-header "Authorization: Basic cGFjdDpwYWN0" -a 1.0.100 --provider-base-url http://localhost:4570 --pact-urls ./test/me-they.json --provider_states_setup_url http://localhost:4570/provider-state -v` }
subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json --custom-provider-header "Authorization: Basic cGFjdDpwYWN0" -a 1.0.100 --provider-base-url http://localhost:4570 --provider_states_setup_url http://localhost:4570/provider-state -v` }

it "exits with a 0 exit code" do
subject
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_with_ssl_no_verify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
run_provider_with_self_signed_cert @port
end
sleep 2
subject = `bundle exec bin/pact-provider-verifier -a 1.0.0 --provider-base-url https://localhost:#{@port} --pact-urls ./test/me-they.json --provider_states_setup_url https://localhost:#{@port}/provider-state -v`
subject = `bundle exec bin/pact-provider-verifier ./test/me-they.json -a 1.0.0 --provider-base-url https://localhost:#{@port} --provider_states_setup_url https://localhost:#{@port}/provider-state -v`
expect(subject).to include "2 interactions, 0 failures"
ensure
Process.kill('KILL', @ssl_server_pid)
Expand Down
Loading

0 comments on commit df78617

Please sign in to comment.