pact_broker-client 1.43.0 → 1.47.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +35 -0
- data/CHANGELOG.md +41 -0
- data/README.md +54 -54
- data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +127 -94
- data/doc/pacts/markdown/Pact Broker Client - Pactflow.md +94 -0
- data/doc/pacts/markdown/README.md +1 -0
- data/lib/pact_broker/client/base_command.rb +3 -0
- data/lib/pact_broker/client/can_i_deploy.rb +40 -5
- data/lib/pact_broker/client/cli/broker.rb +8 -190
- data/lib/pact_broker/client/cli/custom_thor.rb +0 -16
- data/lib/pact_broker/client/cli/deployment_commands.rb +94 -0
- data/lib/pact_broker/client/cli/matrix_commands.rb +93 -0
- data/lib/pact_broker/client/cli/pacticipant_commands.rb +9 -0
- data/lib/pact_broker/client/cli/webhook_commands.rb +122 -0
- data/lib/pact_broker/client/deployments.rb +4 -0
- data/lib/pact_broker/client/deployments/record_deployment.rb +38 -0
- data/lib/pact_broker/client/deployments/record_release.rb +99 -0
- data/lib/pact_broker/client/deployments/record_support_ended.rb +103 -0
- data/lib/pact_broker/client/deployments/record_undeployment.rb +127 -0
- data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
- data/lib/pact_broker/client/environments.rb +6 -3
- data/lib/pact_broker/client/environments/describe_environment.rb +3 -13
- data/lib/pact_broker/client/hal/entity.rb +22 -4
- data/lib/pact_broker/client/hal/http_client.rb +3 -2
- data/lib/pact_broker/client/pacticipants.rb +3 -3
- data/lib/pact_broker/client/pacticipants/create.rb +2 -2
- data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
- data/lib/pact_broker/client/verification_required.rb +32 -0
- data/lib/pact_broker/client/version.rb +1 -1
- data/lib/pact_broker/client/versions.rb +4 -1
- data/lib/pact_broker/client/versions/describe.rb +3 -1
- data/lib/pact_broker/client/versions/formatter.rb +3 -1
- data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
- data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
- data/lib/pact_broker/client/webhooks/create.rb +14 -8
- data/script/record-deployment.sh +1 -1
- data/script/record-deployments-and-releases.sh +16 -0
- data/script/record-undeployment.sh +1 -1
- data/script/webhook-commands.sh +12 -0
- data/spec/fixtures/approvals/can_i_deploy_failure_dry_run.approved.txt +7 -0
- data/spec/fixtures/approvals/can_i_deploy_success_dry_run.approved.txt +7 -0
- data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
- data/spec/integration/describe_environment_spec.rb +31 -0
- data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +62 -2
- data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +15 -2
- data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +4 -2
- data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
- data/spec/lib/pact_broker/client/deployments/record_support_ended_spec.rb +208 -0
- data/spec/lib/pact_broker/client/deployments/record_undeployment_spec.rb +219 -0
- data/spec/pacts/pact_broker_client-pact_broker.json +140 -112
- data/spec/pacts/pact_broker_client-pactflow.json +118 -0
- data/spec/service_providers/pact_broker_client_register_repository_spec.rb +2 -2
- data/spec/service_providers/pact_helper.rb +15 -10
- data/spec/service_providers/pactflow_webhooks_create_spec.rb +86 -0
- data/spec/service_providers/publish_pacts_spec.rb +3 -1
- data/spec/service_providers/record_deployment_spec.rb +4 -28
- data/spec/service_providers/record_release_spec.rb +130 -0
- data/spec/service_providers/record_undeployment_spec.rb +164 -0
- data/spec/service_providers/webhooks_create_spec.rb +1 -1
- data/spec/spec_helper.rb +14 -2
- data/spec/support/shared_context.rb +2 -1
- data/tasks/pact.rake +21 -1
- metadata +39 -7
- data/lib/pact_broker/client/versions/record_deployment.rb +0 -85
- data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
- data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -75
@@ -0,0 +1,32 @@
|
|
1
|
+
require "pact_broker/client/can_i_deploy"
|
2
|
+
|
3
|
+
module PactBroker
|
4
|
+
module Client
|
5
|
+
class VerificationRequired < PactBroker::Client::CanIDeploy
|
6
|
+
def call
|
7
|
+
create_result(fetch_matrix_with_retries)
|
8
|
+
rescue StandardError => e
|
9
|
+
message = "Error determining if a verification already existed (#{e.message}) - verification should run just in case"
|
10
|
+
if options[:verbose]
|
11
|
+
message = "#{message}\n#{e.class} - #{e.backtrace.join("\n")}"
|
12
|
+
end
|
13
|
+
Result.new(true, message)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def create_result(matrix)
|
19
|
+
matrix_and_notices = format_matrix(matrix) + "\n\n" + remove_warnings(Term::ANSIColor.uncolor(notice_or_reason(matrix, :white)))
|
20
|
+
if matrix.any_unknown?
|
21
|
+
Result.new(true, matrix_and_notices + "\n\nVerification is required.")
|
22
|
+
else
|
23
|
+
Result.new(false, matrix_and_notices + "\n\nNo verification is required.")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_warnings(lines)
|
28
|
+
lines.split("\n").select{ | line | !line.include?("WARN:") }.join("\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# Need Versions class to extend BaseClient until we can remove the old Versions code
|
2
|
+
require 'pact_broker/client/base_client'
|
1
3
|
require 'pact_broker/client/pact_broker_client'
|
2
4
|
require 'pact_broker/client/versions/formatter'
|
3
5
|
|
4
6
|
module PactBroker
|
5
7
|
module Client
|
6
|
-
class Versions
|
8
|
+
class Versions < BaseClient
|
7
9
|
class Describe
|
8
10
|
|
9
11
|
class Result
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# Need Versions class to extend BaseClient until we can remove the old Versions code
|
2
|
+
require 'pact_broker/client/base_client'
|
1
3
|
require 'pact_broker/client/versions/json_formatter'
|
2
4
|
require 'pact_broker/client/versions/text_formatter'
|
3
5
|
|
4
6
|
module PactBroker
|
5
7
|
module Client
|
6
|
-
class Versions
|
8
|
+
class Versions < BaseClient
|
7
9
|
class Formatter
|
8
10
|
def self.call(matrix_lines, format)
|
9
11
|
formatter = case format
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# Need Versions class to extend BaseClient until we can remove the old Versions code
|
2
|
+
require 'pact_broker/client/base_client'
|
1
3
|
require 'table_print'
|
2
4
|
|
3
5
|
module PactBroker
|
4
6
|
module Client
|
5
|
-
class Versions
|
7
|
+
class Versions < BaseClient
|
6
8
|
class JsonFormatter
|
7
|
-
def self.call(
|
8
|
-
JSON.pretty_generate(
|
9
|
+
def self.call(version)
|
10
|
+
JSON.pretty_generate(version)
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# Need Versions class to extend BaseClient until we can remove the old Versions code
|
2
|
+
require 'pact_broker/client/base_client'
|
1
3
|
require 'table_print'
|
2
4
|
|
3
5
|
module PactBroker
|
4
6
|
module Client
|
5
|
-
class Versions
|
7
|
+
class Versions < BaseClient
|
6
8
|
class TextFormatter
|
7
9
|
|
8
10
|
Line = Struct.new(:number, :tags)
|
@@ -3,6 +3,7 @@ require 'pact_broker/client/hal'
|
|
3
3
|
require 'ostruct'
|
4
4
|
require 'json'
|
5
5
|
require 'pact_broker/client/command_result'
|
6
|
+
require "pact_broker/client/backports"
|
6
7
|
|
7
8
|
module PactBroker
|
8
9
|
module Client
|
@@ -61,16 +62,17 @@ module PactBroker
|
|
61
62
|
|
62
63
|
def request_body
|
63
64
|
webhook_request_body = JSON.parse(params.body) rescue params.body
|
65
|
+
request_params = {
|
66
|
+
url: params.url,
|
67
|
+
method: params.http_method,
|
68
|
+
headers: params.headers,
|
69
|
+
body: webhook_request_body,
|
70
|
+
username: params.username,
|
71
|
+
password: params.password
|
72
|
+
}.compact
|
64
73
|
{
|
65
74
|
events: params.events.collect{ | event | { "name" => event }},
|
66
|
-
request:
|
67
|
-
url: params.url,
|
68
|
-
method: params.http_method,
|
69
|
-
headers: params.headers,
|
70
|
-
body: webhook_request_body,
|
71
|
-
username: params.username,
|
72
|
-
password: params.password
|
73
|
-
}
|
75
|
+
request: request_params
|
74
76
|
}.tap { |req| req[:description] = params.description if params.description }
|
75
77
|
end
|
76
78
|
|
@@ -85,6 +87,10 @@ module PactBroker
|
|
85
87
|
body[:provider] = { name: params.provider }
|
86
88
|
end
|
87
89
|
|
90
|
+
if params.team_uuid
|
91
|
+
body[:teamUuid] = params.team_uuid
|
92
|
+
end
|
93
|
+
|
88
94
|
body
|
89
95
|
end
|
90
96
|
|
data/script/record-deployment.sh
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
bundle exec bin/pact-broker record-deployment \
|
2
2
|
--pacticipant foo-consumer --version 1 --environment prod --broker-base-url http://localhost:9292
|
@@ -0,0 +1,16 @@
|
|
1
|
+
bundle exec bin/pact-broker create-or-update-pacticipant --name Foo
|
2
|
+
bundle exec bin/pact-broker create-version-tag --pacticipant Foo --version 2 --tag main --auto-create-version
|
3
|
+
bundle exec bin/pact-broker describe-version --pacticipant Foo --version 2
|
4
|
+
bundle exec bin/pact-broker create-environment --name test
|
5
|
+
bundle exec bin/pact-broker can-i-deploy --pacticipant Foo --version 2 --to-environment test
|
6
|
+
|
7
|
+
bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test
|
8
|
+
bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target customer-1
|
9
|
+
bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target customer-1
|
10
|
+
|
11
|
+
bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test
|
12
|
+
bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test
|
13
|
+
bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test --target customer-1
|
14
|
+
|
15
|
+
bundle exec bin/pact-broker record-release --pacticipant Foo --version 2 --environment test
|
16
|
+
bundle exec bin/pact-broker record-support-ended --pacticipant Foo --version 2 --environment test
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
|
3
|
+
bundle exec bin/pact-broker create-webhook \
|
4
|
+
'https://api.github.com/repos/foo/bar/statuses/${pactbroker.consumerVersionNumber}' \
|
5
|
+
-X POST \
|
6
|
+
-H "Content-Type: application/json" \
|
7
|
+
-d '{ "state": "${pactbroker.githubVerificationStatus}", "description": "Pact Verification Tests ${pactbroker.providerVersionTags}", "context": "${pactbroker.providerName}", "target_url": "${pactbroker.verificationResultUrl}" }' \
|
8
|
+
--user username:password \
|
9
|
+
--description "Publish pact verification status to Github" \
|
10
|
+
--contract-published \
|
11
|
+
--provider-verification-published \
|
12
|
+
--team-uuid 4ac05ed8-9e3b-4159-96c0-ad19e3b93658
|
@@ -0,0 +1,7 @@
|
|
1
|
+
[33m[dry-run] [0mComputer says no ¯\_(ツ)_/¯ (but you're ignoring this by enabling dry run)
|
2
|
+
[33m[dry-run] [0m
|
3
|
+
[33m[dry-run] [0mtext matrix
|
4
|
+
[33m[dry-run] [0m
|
5
|
+
[33m[dry-run] [0msome reason
|
6
|
+
[33m[dry-run] [0m
|
7
|
+
[33m[dry-run] [0m[32mDry run enabled - ignoring any failures[0m
|
@@ -0,0 +1,7 @@
|
|
1
|
+
[33m[dry-run] [0mComputer says yes \o/ (and maybe you don't need to enable dry run)
|
2
|
+
[33m[dry-run] [0m
|
3
|
+
[33m[dry-run] [0mtext matrix
|
4
|
+
[33m[dry-run] [0m
|
5
|
+
[33m[dry-run] [0msome reason
|
6
|
+
[33m[dry-run] [0m
|
7
|
+
[33m[dry-run] [0m[32mDry run enabled - ignoring any failures[0m
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'pact_broker/client/cli/broker'
|
2
|
+
|
3
|
+
RSpec.describe "describe-environment" do
|
4
|
+
let(:index_body_hash) do
|
5
|
+
{
|
6
|
+
_links: {
|
7
|
+
"pb:pacticipant" => {
|
8
|
+
href: "http://broker/pacticipants/{pacticipant}"
|
9
|
+
}
|
10
|
+
}
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:pacticipant_body_hash) { JSON.parse(File.read("./spec/support/pacticipant_get.json")) }
|
15
|
+
|
16
|
+
let!(:index_request) do
|
17
|
+
stub_request(:get, "http://broker").to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
|
18
|
+
end
|
19
|
+
|
20
|
+
let!(:pacticipant_request) do
|
21
|
+
stub_request(:get, "http://broker/pacticipants/Foo").to_return(status: 200, body: pacticipant_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:parameters) { %w{describe-pacticipant --name Foo --broker-base-url http://broker} }
|
25
|
+
|
26
|
+
subject { capture(:stdout) { PactBroker::Client::CLI::Broker.start(parameters) } }
|
27
|
+
|
28
|
+
it "prints the pacticipant properties" do
|
29
|
+
Approvals.verify(subject, :name => "describe_pacticipant", format: :txt)
|
30
|
+
end
|
31
|
+
end
|
@@ -8,6 +8,7 @@ module PactBroker
|
|
8
8
|
let(:version_selectors) { [{ pacticipant: "Foo", version: "1" }] }
|
9
9
|
let(:matrix_options) { { } }
|
10
10
|
let(:pact_broker_client_options) { { foo: 'bar' } }
|
11
|
+
let(:dry_run) { false }
|
11
12
|
let(:matrix_client) { instance_double('PactBroker::Client::Matrix') }
|
12
13
|
let(:matrix) do
|
13
14
|
instance_double('Matrix::Resource',
|
@@ -23,7 +24,7 @@ module PactBroker
|
|
23
24
|
let(:any_unknown) { unknown_count > 0 }
|
24
25
|
let(:supports_unknown_count) { true }
|
25
26
|
let(:retry_while_unknown) { 0 }
|
26
|
-
let(:options) { { output: 'text', retry_while_unknown: retry_while_unknown, retry_interval: 5 } }
|
27
|
+
let(:options) { { output: 'text', retry_while_unknown: retry_while_unknown, retry_interval: 5, dry_run: dry_run } }
|
27
28
|
let(:notices) { nil }
|
28
29
|
let(:supports_ignore) { true }
|
29
30
|
let(:deployable) { true }
|
@@ -69,6 +70,14 @@ module PactBroker
|
|
69
70
|
expect(subject.message).to include "some notice"
|
70
71
|
end
|
71
72
|
end
|
73
|
+
|
74
|
+
context "when dry_run is enabled" do
|
75
|
+
let(:dry_run) { true }
|
76
|
+
|
77
|
+
it "prefixes each line with [dry-run]" do
|
78
|
+
Approvals.verify(subject.message, :name => "can_i_deploy_success_dry_run", format: :txt)
|
79
|
+
end
|
80
|
+
end
|
72
81
|
end
|
73
82
|
|
74
83
|
context "when the versions are not deployable" do
|
@@ -94,6 +103,18 @@ module PactBroker
|
|
94
103
|
expect(subject.message).to include "some notice"
|
95
104
|
end
|
96
105
|
end
|
106
|
+
|
107
|
+
context "when dry_run is enabled" do
|
108
|
+
let(:dry_run) { true }
|
109
|
+
|
110
|
+
it "returns a success response" do
|
111
|
+
expect(subject.success).to be true
|
112
|
+
end
|
113
|
+
|
114
|
+
it "prefixes each line with [dry-run]" do
|
115
|
+
Approvals.verify(subject.message, :name => "can_i_deploy_failure_dry_run", format: :txt)
|
116
|
+
end
|
117
|
+
end
|
97
118
|
end
|
98
119
|
|
99
120
|
context "when retry_while_unknown is greater than 0" do
|
@@ -135,6 +156,19 @@ module PactBroker
|
|
135
156
|
it "returns a failure message" do
|
136
157
|
expect(subject.message).to match /does not provide a count/
|
137
158
|
end
|
159
|
+
|
160
|
+
context "when dry_run is enabled" do
|
161
|
+
let(:dry_run) { true }
|
162
|
+
|
163
|
+
it "returns a success response" do
|
164
|
+
expect(subject.success).to be true
|
165
|
+
end
|
166
|
+
|
167
|
+
it "returns a failure message" do
|
168
|
+
expect(subject.message).to include "[dry-run]"
|
169
|
+
expect(subject.message).to match /does not provide a count/
|
170
|
+
end
|
171
|
+
end
|
138
172
|
end
|
139
173
|
end
|
140
174
|
|
@@ -169,6 +203,19 @@ module PactBroker
|
|
169
203
|
it "returns a failure message" do
|
170
204
|
expect(subject.message).to include "error text"
|
171
205
|
end
|
206
|
+
|
207
|
+
context "when dry_run is enabled" do
|
208
|
+
let(:dry_run) { true }
|
209
|
+
|
210
|
+
it "returns a success response" do
|
211
|
+
expect(subject.success).to be true
|
212
|
+
end
|
213
|
+
|
214
|
+
it "returns a failure message" do
|
215
|
+
expect(subject.message).to include "[dry-run]"
|
216
|
+
expect(subject.message).to match /error text/
|
217
|
+
end
|
218
|
+
end
|
172
219
|
end
|
173
220
|
|
174
221
|
context "when a StandardError is raised" do
|
@@ -183,7 +230,20 @@ module PactBroker
|
|
183
230
|
end
|
184
231
|
|
185
232
|
it "returns a failure message and backtrace" do
|
186
|
-
expect(subject.message).to include "Error retrieving matrix. StandardError - error text
|
233
|
+
expect(subject.message).to include "Error retrieving matrix. StandardError - error text"
|
234
|
+
end
|
235
|
+
|
236
|
+
context "when dry_run is enabled" do
|
237
|
+
let(:dry_run) { true }
|
238
|
+
|
239
|
+
it "returns a success response" do
|
240
|
+
expect(subject.success).to be true
|
241
|
+
end
|
242
|
+
|
243
|
+
it "returns a failure message" do
|
244
|
+
expect(subject.message).to include "[dry-run]"
|
245
|
+
expect(subject.message).to match /error text/
|
246
|
+
end
|
187
247
|
end
|
188
248
|
end
|
189
249
|
end
|
@@ -25,7 +25,8 @@ module PactBroker
|
|
25
25
|
verbose: 'verbose',
|
26
26
|
retry_while_unknown: 1,
|
27
27
|
retry_interval: 2,
|
28
|
-
limit: 1000
|
28
|
+
limit: 1000,
|
29
|
+
dry_run: false
|
29
30
|
}
|
30
31
|
end
|
31
32
|
|
@@ -37,7 +38,7 @@ module PactBroker
|
|
37
38
|
end
|
38
39
|
|
39
40
|
it "invokes the CanIDeploy service" do
|
40
|
-
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, { to_tag: nil, to_environment: nil, limit: 1000, ignore_selectors: []}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, { pact_broker_base_url: 'http://pact-broker', verbose: 'verbose' })
|
41
|
+
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, { to_tag: nil, to_environment: nil, limit: 1000, ignore_selectors: []}, {output: 'table', retry_while_unknown: 1, retry_interval: 2, dry_run: false, verbose: "verbose"}, { pact_broker_base_url: 'http://pact-broker', verbose: 'verbose' })
|
41
42
|
invoke_can_i_deploy
|
42
43
|
end
|
43
44
|
|
@@ -94,6 +95,18 @@ module PactBroker
|
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
98
|
+
context "when PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true" do
|
99
|
+
before do
|
100
|
+
allow(ENV).to receive(:[]).and_call_original
|
101
|
+
allow(ENV).to receive(:[]).with("PACT_BROKER_CAN_I_DEPLOY_DRY_RUN").and_return("true")
|
102
|
+
end
|
103
|
+
|
104
|
+
it "invokes the CanIDeploy service with dry_run set to true" do
|
105
|
+
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, hash_including(dry_run: true), anything)
|
106
|
+
invoke_can_i_deploy
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
97
110
|
context "when successful" do
|
98
111
|
it "prints the message to stdout" do
|
99
112
|
expect($stdout).to receive(:puts).with(message)
|
@@ -33,7 +33,8 @@ module PactBroker
|
|
33
33
|
broker_username: "username",
|
34
34
|
broker_password: "password",
|
35
35
|
contract_content_changed: true,
|
36
|
-
verbose: true
|
36
|
+
verbose: true,
|
37
|
+
team_uuid: "1234"
|
37
38
|
}
|
38
39
|
end
|
39
40
|
|
@@ -49,7 +50,8 @@ module PactBroker
|
|
49
50
|
body: "data",
|
50
51
|
consumer: "consumer",
|
51
52
|
provider: "provider",
|
52
|
-
events: ["contract_content_changed"]
|
53
|
+
events: ["contract_content_changed"],
|
54
|
+
team_uuid: "1234"
|
53
55
|
}.tap { |it| Pact::Fixture.add_fixture(:create_webhook_params, it) }
|
54
56
|
end
|
55
57
|
|
@@ -0,0 +1,204 @@
|
|
1
|
+
require 'pact_broker/client/deployments/record_deployment'
|
2
|
+
|
3
|
+
module PactBroker
|
4
|
+
module Client
|
5
|
+
module Deployments
|
6
|
+
describe RecordDeployment do
|
7
|
+
describe ".call" do
|
8
|
+
let(:broker_base_url) { "http://broker" }
|
9
|
+
let(:index_body_hash) do
|
10
|
+
{
|
11
|
+
_links: {
|
12
|
+
"pb:environments" => { href: "#{broker_base_url}/environments" },
|
13
|
+
"pb:pacticipant-version" => {
|
14
|
+
href: "#{broker_base_url}/pacticipants/{pacticipant}/versions/{version}"
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
let(:version_body_hash) do
|
20
|
+
{
|
21
|
+
"_links" => {
|
22
|
+
"pb:record-deployment" => [
|
23
|
+
{
|
24
|
+
name: "test",
|
25
|
+
href: "#{broker_base_url}/record-deployment"
|
26
|
+
}
|
27
|
+
]
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
let(:environments_body_hash) do
|
32
|
+
{
|
33
|
+
"_links" => {
|
34
|
+
"pb:environments" => [
|
35
|
+
{
|
36
|
+
"name" => "test",
|
37
|
+
"href" => ""
|
38
|
+
}
|
39
|
+
]
|
40
|
+
}
|
41
|
+
|
42
|
+
}
|
43
|
+
end
|
44
|
+
let(:record_deployment_body_hash) do
|
45
|
+
{ "some" => "response" }
|
46
|
+
end
|
47
|
+
let!(:index_request) do
|
48
|
+
stub_request(:get, broker_base_url).to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
|
49
|
+
end
|
50
|
+
let!(:version_request) do
|
51
|
+
stub_request(:get, broker_base_url + "/pacticipants/Foo/versions/1").to_return(status: 200, body: version_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
|
52
|
+
end
|
53
|
+
|
54
|
+
let!(:environments_request) do
|
55
|
+
stub_request(:get, "http://broker/environments").
|
56
|
+
with(headers: { 'Accept'=>'application/hal+json' }).
|
57
|
+
to_return(status: 200, body: environments_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
|
58
|
+
end
|
59
|
+
let!(:record_deployment_request) do
|
60
|
+
stub_request(:post, "http://broker/record-deployment").
|
61
|
+
to_return(status: 200, body: record_deployment_body_hash.to_json , headers: {})
|
62
|
+
end
|
63
|
+
|
64
|
+
let(:target) { "blue" }
|
65
|
+
|
66
|
+
let(:params) do
|
67
|
+
{
|
68
|
+
pacticipant_name: "Foo",
|
69
|
+
version_number: "1",
|
70
|
+
environment_name: "test",
|
71
|
+
target: target
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
let(:options) do
|
76
|
+
{
|
77
|
+
output: output,
|
78
|
+
verbose: true
|
79
|
+
}
|
80
|
+
end
|
81
|
+
let(:output) { "text" }
|
82
|
+
let(:pact_broker_client_options) { { pact_broker_base_url: broker_base_url} }
|
83
|
+
|
84
|
+
subject { RecordDeployment.call(params, options, pact_broker_client_options) }
|
85
|
+
|
86
|
+
context "when the pb:environments relation does not exist" do
|
87
|
+
let(:index_body_hash) do
|
88
|
+
{
|
89
|
+
"_links" => {}
|
90
|
+
}
|
91
|
+
end
|
92
|
+
it "returns an error response" do
|
93
|
+
expect(subject.success).to be false
|
94
|
+
expect(subject.message).to include "does not support"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when the specified version does not exist" do
|
99
|
+
let!(:version_request) do
|
100
|
+
stub_request(:get, broker_base_url + "/pacticipants/Foo/versions/1").to_return(status: 404)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns an error response" do
|
104
|
+
expect(subject.success).to be false
|
105
|
+
expect(subject.message).to include "Foo version 1 not found"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "when the pacticipant version does not support recording deployments" do
|
110
|
+
let(:version_body_hash) do
|
111
|
+
{
|
112
|
+
"_links" => {}
|
113
|
+
}
|
114
|
+
end
|
115
|
+
it "returns an error response" do
|
116
|
+
expect(subject.success).to be false
|
117
|
+
expect(subject.message).to include "does not support"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "when the specified environment is not available for recording a deployment" do
|
122
|
+
let(:version_body_hash) do
|
123
|
+
{
|
124
|
+
"_links" => {
|
125
|
+
"pb:record-deployment" => [
|
126
|
+
{
|
127
|
+
"name" => "prod",
|
128
|
+
"href" => ""
|
129
|
+
}
|
130
|
+
]
|
131
|
+
}
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
context "when the environment does not exist" do
|
136
|
+
let(:environments_body_hash) do
|
137
|
+
{
|
138
|
+
"_links" => {
|
139
|
+
"pb:environments" => [
|
140
|
+
{
|
141
|
+
"name" => "prod",
|
142
|
+
"href" => ""
|
143
|
+
},{
|
144
|
+
"name" => "uat",
|
145
|
+
"href" => ""
|
146
|
+
}
|
147
|
+
]
|
148
|
+
}
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
it "returns an error response" do
|
153
|
+
expect(subject.success).to be false
|
154
|
+
expect(subject.message).to include "No environment found with name 'test'. Available options: prod"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "when the environment does exist" do
|
159
|
+
it "returns an error response" do
|
160
|
+
expect(subject.success).to be false
|
161
|
+
expect(subject.message).to include "Environment 'test' is not an available option for recording a deployment of Foo. Available options: prod"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "when the output is json" do
|
167
|
+
let(:output) { "json" }
|
168
|
+
|
169
|
+
it "returns the JSON payload" do
|
170
|
+
expect(JSON.parse(subject.message)).to eq record_deployment_body_hash
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "when the response headers contain Pactflow" do
|
175
|
+
before do
|
176
|
+
allow_any_instance_of(RecordDeployment).to receive(:check_if_command_supported)
|
177
|
+
allow_any_instance_of(RecordDeployment).to receive(:check_environment_exists)
|
178
|
+
allow_any_instance_of(RecordDeployment).to receive(:record_action)
|
179
|
+
allow_any_instance_of(RecordDeployment).to receive(:index_resource).and_return(index_resource)
|
180
|
+
end
|
181
|
+
|
182
|
+
let(:response_headers) { { "X-Pactflow-Sha" => "abc" } }
|
183
|
+
|
184
|
+
let(:index_resource) do
|
185
|
+
double('PactBroker::Client::Hal::Entity', response: double('response', headers: response_headers) )
|
186
|
+
end
|
187
|
+
|
188
|
+
it "indicates the API was Pactflow" do
|
189
|
+
expect(subject.message).to include "Recorded deployment of Foo version 1 to test environment (target blue) in Pactflow"
|
190
|
+
end
|
191
|
+
|
192
|
+
context "when target is nil" do
|
193
|
+
let(:target) { nil }
|
194
|
+
|
195
|
+
it "does not include the target in the result message" do
|
196
|
+
expect(subject.message).to include "Recorded deployment of Foo version 1 to test environment in"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|