Skip to content

Commit

Permalink
feat: update record-undeployment (feature toggled off)
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 9, 2021
1 parent e718555 commit 93046d5
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 29 deletions.
34 changes: 10 additions & 24 deletions lib/pact_broker/client/deployments/record_undeployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ module PactBroker
module Client
module Deployments
class RecordUndeployment < PactBroker::Client::BaseCommand



def initialize(params, pact_broker_base_url, pact_broker_client_options)
def initialize(params, options, pact_broker_client_options)
super
@pacticipant_name = params.fetch(:pacticipant_name)
@environment_name = params.fetch(:environment_name)
Expand All @@ -21,12 +18,11 @@ def do_call
check_pacticipant_exists!
PactBroker::Client::CommandResult.new(false, deployed_version_not_found_error_message)
else
PactBroker::Client::CommandResult.new(undeployed_versions_resources.all?(:success?), result_message)
PactBroker::Client::CommandResult.new(undeployed_versions_resources.all?(&:success?), result_message)
end
end

attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
attr_reader :deployed_version_resource, :undeployment_entities
attr_reader :pacticipant_name, :environment_name, :target

def currently_deployed_versions_link
environment_resource._link("pb:currently-deployed-versions") or raise PactBroker::Client::Error.new(not_supported_message)
Expand All @@ -37,7 +33,7 @@ def currently_deployed_versions_resource
end

def undeployed_versions_resources
@undeployed_versions_resources ||= currently_deployed_versions_resource.embedded_entities("deployedVersions").collect do | entity |
@undeployed_versions_resources ||= currently_deployed_versions_resource.embedded_entities!("deployedVersions").collect do | entity |
entity._link!("self").patch(currentlyDeployed: false)
end
end
Expand Down Expand Up @@ -76,13 +72,12 @@ def result_message
end

def success_result_text_message(undeployed_versions_resource)
version_number = undeployed_versions_resource.embedded_entity{ | embedded_entity| embedded_entity['version'] }.number
message = "Recorded #{action} of #{pacticipant_name} version #{version_number} from #{environment_name} environment"
if target
message + " (target #{target})"
else
message
end
version = undeployed_versions_resource.embedded_entity{ | embedded_entity| embedded_entity && embedded_entity['version'] }
message = "Recorded #{action} of #{pacticipant_name}"
message = "#{message} version #{version.number}" if (version && version.number)
message = "#{message} from #{environment_name} environment"
message = "#{message} (target #{target})" if target
message
end

def deployed_version_not_found_error_message
Expand All @@ -96,15 +91,6 @@ def deployed_version_not_found_error_message
end
end


def deployed_version_not_found_message
if (env_names = deployed_version_links.names).any?
"#{pacticipant_name} version #{version_number} is not currently deployed to #{environment_name}. It is currently deployed to: #{env_names.join(", ")}"
else
"#{pacticipant_name} version #{version_number} is not currently deployed to any environment."
end
end

def not_supported_message
"This version of the Pact Broker does not support recording undeployments. Please upgrade to version 2.80.0 or later."
end
Expand Down
19 changes: 17 additions & 2 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module PactBroker
module Client
module Hal
class RelationNotFoundError < ::PactBroker::Client::Error; end
class EmbeddedEntityNotFoundError < ::PactBroker::Client::Error; end
class ErrorResponseReturned < ::PactBroker::Client::Error
attr_reader :entity

Expand Down Expand Up @@ -99,7 +100,17 @@ def _links!(key)

def embedded_entity
embedded_ent = yield @data["_embedded"]
Entity.new(embedded_ent["_links"]["self"]["href"], embedded_ent, @client, response)
if embedded_ent
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end

def embedded_entities!(key)
embedded_ents = (@data["_embedded"] && @data["_embedded"][key])
raise EmbeddedEntityNotFoundError.new("Could not find embedded entity with key '#{key}' in resource at #{@href}") unless embedded_ents
embedded_ents.collect do | embedded_ent |
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end

def embedded_entities(key = nil)
Expand All @@ -109,7 +120,7 @@ def embedded_entities(key = nil)
yield @data["_embedded"]
end
embedded_ents.collect do | embedded_ent |
Entity.new(embedded_ent["_links"]["self"]["href"], embedded_ent, @client, response)
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end

Expand Down Expand Up @@ -146,6 +157,10 @@ def respond_to_missing?(method_name, include_private = false)
def assert_success!(_ignored = nil)
self
end

def self_href(entity_hash)
entity_hash["_links"] && entity_hash["_links"]["self"] && entity_hash["_links"]["self"]["href"]
end
end

class ErrorEntity < Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module PactBroker::Client
with(
method: :patch,
path: '/pacticipants/Pricing%20Service',
headers: patch_request_headers,
headers: old_patch_request_headers,
body: {repository_url: repository_url} ).
will_respond_with(
status: 201,
Expand All @@ -33,7 +33,7 @@ module PactBroker::Client
with(
method: :patch,
path: '/pacticipants/Pricing%20Service',
headers: patch_request_headers,
headers: old_patch_request_headers,
body: { repository_url: repository_url }).
will_respond_with(
status: 200,
Expand Down
165 changes: 165 additions & 0 deletions spec/service_providers/record_undeployment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
require 'service_providers/pact_helper'
require 'pact_broker/client/deployments/record_undeployment'

deployment_feature_on = ENV.fetch('PACT_BROKER_FEATURES', '').include?("deployments")

RSpec.describe "recording an undeployment", pact: true, skip: !deployment_feature_on do
include_context "pact broker"
include PactBrokerPactHelperMethods

let(:pacticipant_name) { "Foo" }
let(:environment_name) { "test" }
let(:output) { "text" }
let(:target) { "customer-1" }
let(:params) do
{
pacticipant_name: pacticipant_name,
environment_name: environment_name,
target: target
}
end
let(:options) do
{
output: output,
verbose: true
}
end
let(:webmock_base_url) { "http://broker" }
let(:pact_broker_client_options) { { pact_broker_base_url: webmock_base_url } }
let(:test_environment_placeholder_path) { "/HAL-REL-PLACEHOLDER-PB-ENVIRONMENT-16926ef3-590f-4e3f-838e-719717aa88c9" }
let(:currently_deployed_versions_placeholder_path) { "/PLACEHOLDER-ENVIRONMENT-CURRENTLY-DEPLOYED-16926ef3-590f-4e3f-838e-719717aa88c9" }
let(:deployed_version_placeholder_path) { "/PLACEHOLDER-DEPLOYED-VERSION-ff3adecf-cfc5-4653-a4e3-f1861092f8e0"}

subject { PactBroker::Client::Deployments::RecordUndeployment.call(params, options, pact_broker_client_options) }

let(:index_body_hash) do
{
_links: {
:'pb:environments' => {
href: "#{webmock_base_url}/environments"
}
}
}
end

let(:environments_hash) do
{
_links: {
:'pb:environments' => [
{
name: "test",
href: pact_broker.mock_service_base_url + test_environment_placeholder_path
}
]
}
}
end

let!(:index_request) do
stub_request(:get, "http://broker").to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
end

let!(:environments_request) do
stub_request(:get, "http://broker/environments").to_return(status: 200, body: environments_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
end

def mock_test_environment
pact_broker
.given("an environment with name test and UUID 16926ef3-590f-4e3f-838e-719717aa88c9 exists")
.upon_receiving("a request for an environment")
.with(
method: "GET",
path: test_environment_placeholder_path,
headers: get_request_headers
)
.will_respond_with(
status: 200,
headers: pact_broker_response_headers,
body: {
_links: {
:'pb:currently-deployed-versions' => {
href: Pact.term( pact_broker.mock_service_base_url + currently_deployed_versions_placeholder_path, /^http.*/)
}
}
}
)
end

def mock_deployed_versions_search_results
pact_broker
.given("an version is deployed to environment with UUID 16926ef3-590f-4e3f-838e-719717aa88c9 with target customer-1")
.upon_receiving("a request to list the versions deployed to an environment for a pacticipant name and target")
.with(
method: "GET",
path: currently_deployed_versions_placeholder_path,
query: { pacticipant: pacticipant_name, target: target },
headers: get_request_headers
)
.will_respond_with(
status: 200,
headers: pact_broker_response_headers,
body: {
_embedded: {
deployedVersions: [
{
_links: {
self: {
href: Pact.term(pact_broker.mock_service_base_url + deployed_version_placeholder_path, /^http/)
}
}
}
]
}
}
)
end

def mock_mark_deployed_version_as_undeployed
pact_broker
.given("a currently deployed version exists")
.upon_receiving("a request to mark a deployed version as not currently deploye")
.with(
method: "PATCH",
path: deployed_version_placeholder_path,
body: { currentlyDeployed: false },
headers: patch_request_headers
)
.will_respond_with(
status: 200,
headers: pact_broker_response_headers,
body: deployed_version_hash
)
end

let(:deployed_version_hash) do
{
"currentlyDeployed" => false,
"_embedded" => {
"version" => {
"number" => "2"
}
}
}
end

context "when the deployment is recorded successfully" do
before do
mock_test_environment
mock_deployed_versions_search_results
mock_mark_deployed_version_as_undeployed
end

it "returns a success message" do
expect(subject.success).to be true
expect(subject.message).to include "Recorded undeployment of Foo version 2 from test environment (target customer-1) in the Pact Broker"
end

context "when the output is json" do
let(:output) { "json" }

it "returns the JSON payload" do
expect(JSON.parse(subject.message)).to eq [deployed_version_hash]
end
end
end
end
3 changes: 2 additions & 1 deletion spec/support/shared_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
let(:pact_broker_version) { Pact::Term.new(:matcher => /\d+\.\d+\.\d+/, :generate => '1.0.0') }
let(:pact_broker_response_headers) { {'Content-Type' => 'application/hal+json;charset=utf-8'} }
let(:default_request_headers) { { 'Content-Type' => 'application/json'} }
let(:patch_request_headers) { { 'Content-Type' => 'application/json'} }
let(:old_patch_request_headers) { { 'Content-Type' => 'application/json'} }
let(:patch_request_headers) { { 'Content-Type' => 'application/merge-patch+json'} }
let(:put_request_headers) { { 'Content-Type' => 'application/json', 'Accept' => 'application/hal+json'} }
let(:post_request_headers) { { 'Content-Type' => 'application/json', 'Accept' => 'application/hal+json'} }
let(:get_request_headers) { { 'Accept' => 'application/hal+json'} }
Expand Down

0 comments on commit 93046d5

Please sign in to comment.