Skip to content

Commit

Permalink
feat: all logging to indicate which pacts are being fetched from the …
Browse files Browse the repository at this point in the history
…broker
  • Loading branch information
bethesque committed Jun 24, 2018
1 parent 436f3f2 commit 06fa615
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
44 changes: 30 additions & 14 deletions lib/pact/pact_broker/fetch_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
module Pact
module PactBroker
class FetchPacts
attr_reader :provider, :tags, :broker_base_url, :basic_auth_options, :http_client, :index_entity
attr_reader :provider, :tags, :broker_base_url, :http_client_options, :http_client, :index_entity

ALL_PROVIDER_TAG_RELATION = 'pb:provider-pacts-with-tag'.freeze
LATEST_PROVIDER_TAG_RELATION = 'pb:latest-provider-pacts-with-tag'.freeze
LATEST_PROVIDER_RELATION = 'pb:latest-provider-pacts'.freeze
PACTS = 'pacts'.freeze
HREF = 'href'.freeze

def initialize(provider, tags, broker_base_url, basic_auth_options)
def initialize(provider, tags, broker_base_url, http_client_options)
@provider = provider
@tags = (tags || []).collect do |tag|
if tag.is_a?(String)
Expand All @@ -22,27 +22,31 @@ def initialize(provider, tags, broker_base_url, basic_auth_options)
tag
end
end
@basic_auth_options = basic_auth_options
@http_client_options = http_client_options
@broker_base_url = broker_base_url
@http_client = Pact::Hal::HttpClient.new(basic_auth_options)
@http_client = Pact::Hal::HttpClient.new(http_client_options)
end

def self.call(provider, tags, broker_base_url, basic_auth_options)
new(provider, tags, broker_base_url, basic_auth_options).call
def self.call(provider, tags, broker_base_url, http_client_options)
new(provider, tags, broker_base_url, http_client_options).call
end

def call
get_index
if tag_exist
get_tagged_pacts_for_provider
log_message
if get_index.success?
if any_tags?
get_tagged_pacts_for_provider
else
get_latest_pacts_for_provider
end
else
get_latest_pacts_for_provider
raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}")
end
end

private

def tag_exist
def any_tags?
tags && tags.any?
end

Expand All @@ -66,8 +70,7 @@ def get_link(tag)
end

def get_index
response = http_client.get(broker_base_url)
@index_entity = Pact::Hal::Entity.new(response.body, http_client)
@index_entity = Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get
end

def get_latest_pacts_for_provider
Expand All @@ -77,8 +80,21 @@ def get_latest_pacts_for_provider

def get_pact_urls(link_by_provider)
link_by_provider.fetch(PACTS).collect do |pact|
Pact::Provider::PactURI.new(pact[HREF], basic_auth_options)
Pact::Provider::PactURI.new(pact[HREF], http_client_options)
end
end

def log_message
message = "INFO: Fetching pacts for #{provider} from #{broker_base_url}"
if tags.any?
desc = tags.collect do |tag|
all_or_latest = tag[:all] ? "all" : "latest"
name = tag[:fallback] ? "#{tag[:name]} (or #{tag[:fallback]} if not found)" : tag[:name]
"#{all_or_latest} #{name}"
end.join(", ")
message << " for tags: #{desc}"
end
Pact.configuration.output_stream.puts message
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact/hal/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Hal
end

let(:response) do
instance_double('Pact::Hal::HttpClient::Response', success?: success, body: response_body)
instance_double('Pact::Hal::HttpClient::Response', success?: success, body: response_body, raw_body: response_body.to_json)
end

let(:success) { true }
Expand Down
61 changes: 61 additions & 0 deletions spec/lib/pact/pact_broker/fetch_pacts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'pact/pact_broker/fetch_pacts'

module Pact
module PactBroker
describe FetchPacts do

describe "call" do
let(:provider) { "Foo"}
let(:tags) { ["master", "prod"] }
let(:broker_base_url) { "http://broker.org" }
let(:http_client_options) { {} }

before do
stub_request(:get, "http://broker.org/").to_return(status: 500, body: "foo", headers: {})
end

subject { FetchPacts.call(provider, tags, broker_base_url, http_client_options)}

let(:subject_with_rescue) do
begin
subject
rescue Pact::Error
# can't be bothered stubbing out everything to make the rest of the code execute nicely
# when all we care about is the message
end
end

context "when there is an error retrieving the index resource" do
it "raises a Pact::Error" do
expect { subject }.to raise_error Pact::Error, /500.*foo/
end
end

context "for the latest tag" do
it "logs a message" do
expect(Pact.configuration.output_stream).to receive(:puts).with("INFO: Fetching pacts for Foo from http://broker.org for tags: latest master, latest prod")
subject_with_rescue
end
end

context "with a fallback tag" do
let(:tags) { [{ name: "branch", fallback: "master" }] }

it "logs a message" do
expect(Pact.configuration.output_stream).to receive(:puts).with("INFO: Fetching pacts for Foo from http://broker.org for tags: latest branch (or master if not found)")
subject_with_rescue
end
end

context "when all: true" do
let(:tags) { [{ name: "prod", all: true }] }

it "logs a message" do
expect(Pact.configuration.output_stream).to receive(:puts).with("INFO: Fetching pacts for Foo from http://broker.org for tags: all prod")
subject_with_rescue
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,14 @@ module Configuration
subject do
ServiceProviderDSL.build 'some-provider' do
app {}
honours_pacts_from_pact_broker options do
honours_pacts_from_pact_broker do
end
end
end

it 'adds a verification to the Pact.provider_world' do
allow(Pact::PactBroker::FetchPacts).to receive(:call).and_return(['pact-urls'])

it 'builds a PactVerificationFromBroker' do
expect(PactVerificationFromBroker).to receive(:build).with('some-provider')
subject

expect(Pact.provider_world.pact_verifications.first)
.to eq(Pact::Provider::PactVerification.new(nil, 'pact-urls', nil))
end
end
end
Expand Down

0 comments on commit 06fa615

Please sign in to comment.