pact_broker-client 1.44.0 → 1.45.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ module PactBroker
8
8
  module Client
9
9
  module Hal
10
10
  class RelationNotFoundError < ::PactBroker::Client::Error; end
11
+ class EmbeddedEntityNotFoundError < ::PactBroker::Client::Error; end
11
12
  class ErrorResponseReturned < ::PactBroker::Client::Error
12
13
  attr_reader :entity
13
14
 
@@ -99,7 +100,17 @@ module PactBroker
99
100
 
100
101
  def embedded_entity
101
102
  embedded_ent = yield @data["_embedded"]
102
- Entity.new(embedded_ent["_links"]["self"]["href"], embedded_ent, @client, response)
103
+ if embedded_ent
104
+ Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
105
+ end
106
+ end
107
+
108
+ def embedded_entities!(key)
109
+ embedded_ents = (@data["_embedded"] && @data["_embedded"][key])
110
+ raise EmbeddedEntityNotFoundError.new("Could not find embedded entity with key '#{key}' in resource at #{@href}") unless embedded_ents
111
+ embedded_ents.collect do | embedded_ent |
112
+ Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
113
+ end
103
114
  end
104
115
 
105
116
  def embedded_entities(key = nil)
@@ -109,7 +120,7 @@ module PactBroker
109
120
  yield @data["_embedded"]
110
121
  end
111
122
  embedded_ents.collect do | embedded_ent |
112
- Entity.new(embedded_ent["_links"]["self"]["href"], embedded_ent, @client, response)
123
+ Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
113
124
  end
114
125
  end
115
126
 
@@ -146,6 +157,10 @@ module PactBroker
146
157
  def assert_success!(_ignored = nil)
147
158
  self
148
159
  end
160
+
161
+ def self_href(entity_hash)
162
+ entity_hash["_links"] && entity_hash["_links"]["self"] && entity_hash["_links"]["self"]["href"]
163
+ end
149
164
  end
150
165
 
151
166
  class ErrorEntity < Entity
@@ -30,7 +30,7 @@ module PactBroker
30
30
  if json_output?
31
31
  response_entity.response.raw_body
32
32
  else
33
- green(message = "Pacticipant \"#{params[:name]}\" #{action}")
33
+ green(message = "Pacticipant \"#{params[:name]}\" #{action} in #{pact_broker_name}")
34
34
  end
35
35
  end
36
36
 
@@ -1,5 +1,5 @@
1
1
  module PactBroker
2
2
  module Client
3
- VERSION = '1.44.0'
3
+ VERSION = '1.45.0'
4
4
  end
5
5
  end
@@ -5,6 +5,14 @@ bundle exec bin/pact-broker create-version-tag --pacticipant Foo --version 2 --t
5
5
  bundle exec bin/pact-broker describe-version --pacticipant Foo --version 2
6
6
  bundle exec bin/pact-broker create-environment --name test
7
7
  bundle exec bin/pact-broker can-i-deploy --pacticipant Foo --version 2 --to-environment test
8
- bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target foo
9
- bundle exec bin/pact-broker record-undeployment --pacticipant Foo --version 2 --environment test --target foo
10
- bundle exec bin/pact-broker record-release --pacticipant Foo --version 2 --environment test
8
+
9
+ bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test
10
+ bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target customer-1
11
+ bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target customer-1
12
+
13
+ bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test
14
+ bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test
15
+ bundle exec bin/pact-broker record-undeployment --pacticipant Foo --environment test --target customer-1
16
+
17
+ bundle exec bin/pact-broker record-release --pacticipant Foo --version 2 --environment test
18
+ bundle exec bin/pact-broker record-support-ended --pacticipant Foo --version 2 --environment test
@@ -0,0 +1,7 @@
1
+ [dry-run] Computer says no ¯\_(ツ)_/¯ (but you're ignoring this by enabling dry run)
2
+ [dry-run] 
3
+ [dry-run] text matrix
4
+ [dry-run] 
5
+ [dry-run] some reason
6
+ [dry-run] 
7
+ [dry-run] Dry run enabled - ignoring any failures
@@ -0,0 +1,7 @@
1
+ [dry-run] Computer says yes \o/ (and maybe you don't need to enable dry run)
2
+ [dry-run] 
3
+ [dry-run] text matrix
4
+ [dry-run] 
5
+ [dry-run] some reason
6
+ [dry-run] 
7
+ [dry-run] Dry run enabled - ignoring any failures
@@ -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\n"
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}, { 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)
@@ -0,0 +1,208 @@
1
+ require 'pact_broker/client/deployments/record_support_ended'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Deployments
6
+ describe RecordSupportEnded do
7
+ let(:params) do
8
+ {
9
+ pacticipant_name: "Foo",
10
+ version_number: version_number,
11
+ environment_name: "test"
12
+ }
13
+ end
14
+ let(:version_number) { "2" }
15
+ let(:output) { "text" }
16
+ let(:options) { { output: output, verbose: true } }
17
+ let(:pact_broker_base_url) { "http://broker" }
18
+ let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } }
19
+
20
+ let(:index_body_hash) do
21
+ {
22
+ _links: {
23
+ :'pb:environments' => {
24
+ href: environments_url
25
+ },
26
+ :'pb:pacticipant' => {
27
+ href: pacticipant_url
28
+ }
29
+ }
30
+ }
31
+ end
32
+
33
+ let(:environments_hash) do
34
+ {
35
+ _links: {
36
+ :'pb:environments' => [
37
+ {
38
+ name: "test",
39
+ href: test_environment_url
40
+ }
41
+ ]
42
+ }
43
+ }
44
+ end
45
+
46
+ let(:environment_hash) do
47
+ {
48
+ _links: {
49
+ :'pb:currently-supported-versions' => {
50
+ href: currently_supported_versions_url
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ let(:supported_versions_hash) do
57
+ {
58
+ _embedded: {
59
+ releasedVersions: [
60
+ {
61
+ number: "customer-1",
62
+ _links: {
63
+ self: {
64
+ href: supported_version_url_1
65
+ }
66
+ }
67
+ },
68
+ {
69
+ number: returned_target_2,
70
+ _links: {
71
+ self: {
72
+ href: supported_version_url_2
73
+ }
74
+ }
75
+ }
76
+ ]
77
+ }
78
+ }
79
+ end
80
+
81
+ let(:returned_target_2) { nil }
82
+ let(:supported_version_hash) do
83
+ {
84
+ _embedded: {
85
+ version: {
86
+ number: "2"
87
+ }
88
+ }
89
+ }
90
+ end
91
+
92
+ let(:environments_url) { "#{webmock_base_url}/environments" }
93
+ let(:test_environment_url) { "#{webmock_base_url}/environments/1234" }
94
+ let(:currently_supported_versions_url) { "#{webmock_base_url}/currently-deployed-versions" }
95
+ let(:supported_version_url_1) { "#{webmock_base_url}/deployed-version-1" }
96
+ let(:supported_version_url_2) { "#{webmock_base_url}/deployed-version-2" }
97
+ let(:pacticipant_url) { "#{webmock_base_url}/pacticipant" }
98
+
99
+ let(:webmock_base_url) { "http://broker" }
100
+
101
+ let!(:index_request) do
102
+ stub_request(:get, "http://broker").to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
103
+ end
104
+
105
+ let!(:environments_request) do
106
+ stub_request(:get, "http://broker/environments").to_return(status: 200, body: environments_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
107
+ end
108
+
109
+ let!(:environment_request) do
110
+ stub_request(:get, test_environment_url).to_return(status: 200, body: environment_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
111
+ end
112
+
113
+ let!(:supported_versions_request) do
114
+ stub_request(:get, currently_supported_versions_url + "?pacticipant=Foo&version=2").to_return(status: 200, body: supported_versions_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
115
+ end
116
+
117
+ let!(:supported_version_patch_request_1) do
118
+ stub_request(:patch, supported_version_url_1).with(body: { currentlySupported: false}.to_json).to_return(status: 200, body: supported_version_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
119
+ end
120
+
121
+ let!(:supported_version_patch_request_2) do
122
+ stub_request(:patch, supported_version_url_2).with(body: { currentlySupported: false}.to_json).to_return(status: 200, body: supported_version_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
123
+ end
124
+
125
+ let!(:pacticipant_request) do
126
+ stub_request(:get, pacticipant_url).to_return(status: pacticipant_request_status, body: {}.to_json, headers: { "Content-Type" => "application/hal+json" })
127
+ end
128
+
129
+ let(:pacticipant_request_status) { 200 }
130
+
131
+ subject { RecordSupportEnded.call(params, options, pact_broker_client_options) }
132
+
133
+ its(:success) { is_expected.to eq true }
134
+ its(:message) { is_expected.to include "Recorded support ended for Foo version 2 in test environment in the Pact Broker" }
135
+
136
+ context "when there is no pb:environments relation in the index" do
137
+ let(:index_body_hash) do
138
+ {
139
+ _links: {}
140
+ }
141
+ end
142
+
143
+ its(:success) { is_expected.to be false }
144
+ its(:message) { is_expected.to include "support" }
145
+ end
146
+
147
+ context "when output is json" do
148
+ let(:output) { "json" }
149
+ its(:message) { is_expected.to eq [supported_version_hash, supported_version_hash].to_json }
150
+ end
151
+
152
+ context "when there is an error returned from one of the supported version updates (there should only ever be one supported version, but testing just for the sake of it)" do
153
+ let!(:supported_version_patch_request_2) do
154
+ stub_request(:patch, supported_version_url_2).to_return(status: 400, body: { errors: { foo: ["some error"]}}.to_json, headers: { "Content-Type" => "application/hal+json" })
155
+ end
156
+
157
+ let(:returned_target_2) { "customer-1" }
158
+
159
+ its(:success) { is_expected.to be false }
160
+ its(:message) { is_expected.to include "Recorded" }
161
+ its(:message) { is_expected.to include "some error" }
162
+ end
163
+
164
+ context "when there is no currently-deployed-versions relation in the environment resource" do
165
+ let(:environment_hash) do
166
+ {
167
+ _links: {}
168
+ }
169
+ end
170
+
171
+ its(:success) { is_expected.to be false }
172
+ its(:message) { is_expected.to include "support" }
173
+ end
174
+
175
+ context "when there is no matching supported versions" do
176
+ let(:supported_versions_hash) do
177
+ {
178
+ _embedded: {
179
+ releasedVersions: []
180
+ }
181
+ }
182
+ end
183
+
184
+ let(:expected_message) { "Foo version 2 is not currently released in test environment. Cannot record support ended." }
185
+
186
+ its(:success) { is_expected.to be false }
187
+ its(:message) { is_expected.to include expected_message }
188
+
189
+ context "when there are no supported versions for the pacticipant" do
190
+ context "when the pacticipant does not exist" do
191
+ let(:pacticipant_request_status) { 404 }
192
+
193
+ its(:success) { is_expected.to be false }
194
+ its(:message) { is_expected.to include "No pacticipant with name 'Foo' found" }
195
+ end
196
+ end
197
+
198
+ context "when output is json" do
199
+ let(:output) { "json" }
200
+
201
+ its(:message) { is_expected.to eq({ error: { message: expected_message } }.to_json) }
202
+ end
203
+ end
204
+
205
+ end
206
+ end
207
+ end
208
+ end