pact_broker-client 1.38.0 → 1.40.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/README.md +18 -0
  4. data/bin/pact-broker +6 -0
  5. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +100 -0
  6. data/example/scripts/publish-pact.sh +1 -1
  7. data/lib/pact_broker/client/base_client.rb +1 -1
  8. data/lib/pact_broker/client/cli/broker.rb +25 -8
  9. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +55 -0
  10. data/lib/pact_broker/client/hal/entity.rb +16 -0
  11. data/lib/pact_broker/client/hal/http_client.rb +37 -1
  12. data/lib/pact_broker/client/hal/link.rb +12 -0
  13. data/lib/pact_broker/client/hal/links.rb +15 -0
  14. data/lib/pact_broker/client/hal_client_methods.rb +8 -0
  15. data/lib/pact_broker/client/pacts.rb +0 -1
  16. data/lib/pact_broker/client/publish_pacts.rb +90 -128
  17. data/lib/pact_broker/client/publish_pacts_the_old_way.rb +194 -0
  18. data/lib/pact_broker/client/tasks/publication_task.rb +3 -3
  19. data/lib/pact_broker/client/version.rb +1 -1
  20. data/lib/pact_broker/client/versions/record_deployment.rb +4 -4
  21. data/lib/pact_broker/client/versions/record_undeployment.rb +45 -68
  22. data/script/publish-pact.sh +32 -4
  23. data/script/record-deployment.sh +1 -3
  24. data/script/record-undeployment.sh +4 -0
  25. data/spec/fixtures/foo-bar.json +31 -0
  26. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +1 -1
  27. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +36 -7
  28. data/spec/lib/pact_broker/client/hal/http_client_spec.rb +64 -7
  29. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +3 -0
  30. data/spec/lib/pact_broker/client/{publish_pacts_spec.rb → publish_pacts_the_old_way_spec.rb} +10 -9
  31. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +18 -12
  32. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +4 -4
  33. data/spec/pacts/pact_broker_client-pact_broker.json +106 -0
  34. data/spec/service_providers/pact_broker_client_create_version_spec.rb +4 -4
  35. data/spec/service_providers/publish_pacts_spec.rb +115 -0
  36. data/spec/service_providers/record_deployment_spec.rb +5 -5
  37. data/spec/spec_helper.rb +1 -1
  38. metadata +12 -5
@@ -3,13 +3,13 @@ require 'pact_broker/client/hal/http_client'
3
3
  module PactBroker::Client
4
4
  module Hal
5
5
  describe HttpClient do
6
- before do
7
- allow(Retry).to receive(:until_truthy_or_max_times) { |&block| block.call }
8
- end
9
-
10
6
  subject { HttpClient.new(username: 'foo', password: 'bar') }
11
7
 
12
8
  describe "get" do
9
+ before do
10
+ allow(subject).to receive(:until_truthy_or_max_times) { |&block| block.call }
11
+ end
12
+
13
13
  let!(:request) do
14
14
  stub_request(:get, "http://example.org/").
15
15
  with( headers: {
@@ -41,18 +41,22 @@ module PactBroker::Client
41
41
  end
42
42
  end
43
43
 
44
-
45
44
  it "retries on failure" do
46
- expect(Retry).to receive(:until_truthy_or_max_times)
45
+ expect(subject).to receive(:until_truthy_or_max_times)
47
46
  do_get
48
47
  end
49
48
 
50
49
  it "returns a response" do
51
50
  expect(do_get.body).to eq({"some" => "json"})
52
51
  end
52
+
53
53
  end
54
54
 
55
55
  describe "post" do
56
+ before do
57
+ allow(subject).to receive(:until_truthy_or_max_times) { |&block| block.call }
58
+ end
59
+
56
60
  let!(:request) do
57
61
  stub_request(:post, "http://example.org/").
58
62
  with( headers: {
@@ -75,7 +79,7 @@ module PactBroker::Client
75
79
  end
76
80
 
77
81
  it "calls Retry.until_truthy_or_max_times" do
78
- expect(Retry).to receive(:until_truthy_or_max_times)
82
+ expect(subject).to receive(:until_truthy_or_max_times)
79
83
  do_post
80
84
  end
81
85
 
@@ -100,6 +104,59 @@ module PactBroker::Client
100
104
  end
101
105
  end
102
106
  end
107
+
108
+ describe "integration test" do
109
+ before do
110
+ allow(subject).to receive(:sleep)
111
+ end
112
+
113
+ let(:do_get) { subject.get('http://example.org') }
114
+
115
+ context "with a 50x error is returned less than the max number of tries" do
116
+ let!(:request) do
117
+ stub_request(:get, "http://example.org").
118
+ to_return({ status: 500 }, { status: 502 }, { status: 503 }, { status: 200 })
119
+ end
120
+
121
+ it "retries" do
122
+ expect(do_get.status).to eq 200
123
+ end
124
+ end
125
+
126
+ context "with a 50x error is returned more than the max number of tries" do
127
+ let!(:request) do
128
+ stub_request(:get, "http://example.org").
129
+ to_return({ status: 500 }, { status: 501 }, { status: 502 }, { status: 503 }, { status: 504 })
130
+ end
131
+
132
+ it "retries and returns the last 50x response" do
133
+ expect(do_get.status).to eq 504
134
+ end
135
+ end
136
+
137
+ context "when exceptions are raised" do
138
+ before do
139
+ allow($stderr).to receive(:puts)
140
+ end
141
+
142
+ let!(:request) do
143
+ stub_request(:get, "http://example.org")
144
+ .to_raise(Errno::ECONNREFUSED)
145
+ end
146
+
147
+ it "logs the error" do
148
+ expect($stderr).to receive(:puts).with(/Errno::ECONNREFUSED/)
149
+ begin
150
+ do_get
151
+ rescue Errno::ECONNREFUSED
152
+ end
153
+ end
154
+
155
+ it "retries and raises the last exception" do
156
+ expect { do_get }.to raise_error(Errno::ECONNREFUSED)
157
+ end
158
+ end
159
+ end
103
160
  end
104
161
  end
105
162
  end
@@ -5,6 +5,9 @@ module PactBroker
5
5
  module Pacticipants2
6
6
  describe Create do
7
7
  describe ".call" do
8
+ before do
9
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
10
+ end
8
11
  let(:pact_broker_client_options) { {} }
9
12
  let(:broker_base_url) { "http://url" }
10
13
  let(:params) { { name: 'Foo' } }
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
  require 'fakefs/safe'
3
- require 'pact_broker/client/publish_pacts'
3
+ require 'pact_broker/client/publish_pacts_the_old_way'
4
4
  require 'json'
5
5
 
6
6
  module PactBroker
7
7
  module Client
8
- describe PublishPacts do
8
+ describe PublishPactsTheOldWay do
9
9
 
10
10
  # The amount of stubbing that we have to do here indicates this class is doing
11
11
  # TOO MUCH and needs to be split up!
@@ -23,7 +23,7 @@ module PactBroker
23
23
  File.open("spec/pacts/consumer-provider.json", "w") { |file| file << pact_hash.to_json }
24
24
  File.open("spec/pacts/consumer-provider-2.json", "w") { |file| file << pact_hash.to_json }
25
25
  File.open("spec/pacts/foo-bar.json", "w") { |file| file << pact_hash_2.to_json }
26
- allow_any_instance_of(PublishPacts).to receive(:create_index_entry_point).and_return(index_entry_point)
26
+ allow_any_instance_of(PublishPactsTheOldWay).to receive(:create_index_entry_point).and_return(index_entry_point)
27
27
  end
28
28
 
29
29
  after do
@@ -63,8 +63,9 @@ module PactBroker
63
63
  let(:index_entry_point) { instance_double("PactBroker::Client::Hal::EntryPoint", :get! => index_resource )}
64
64
  let(:index_resource) { instance_double("PactBroker::Client::Hal::Entity", can?: can_create_version ) }
65
65
  let(:can_create_version) { false }
66
+ let(:options) { {} }
66
67
 
67
- subject { PublishPacts.new(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options) }
68
+ subject { PublishPactsTheOldWay.new(pact_broker_base_url, pact_file_paths, consumer_version_params, options, pact_broker_client_options) }
68
69
 
69
70
  describe "call" do
70
71
  it "creates a PactBroker Client" do
@@ -85,7 +86,7 @@ module PactBroker
85
86
  end
86
87
 
87
88
  it "returns true" do
88
- expect(subject.call).to be true
89
+ expect(subject.call.success).to eq true
89
90
  end
90
91
  end
91
92
 
@@ -140,7 +141,7 @@ module PactBroker
140
141
  end
141
142
 
142
143
  it "returns false" do
143
- expect(subject.call).to be false
144
+ expect(subject.call.success).to be false
144
145
  end
145
146
  end
146
147
 
@@ -212,7 +213,7 @@ module PactBroker
212
213
  end
213
214
 
214
215
  it "returns false" do
215
- expect(subject.call).to eq false
216
+ expect(subject.call.success).to eq false
216
217
  end
217
218
  end
218
219
  end
@@ -230,7 +231,7 @@ module PactBroker
230
231
  end
231
232
 
232
233
  it "returns false" do
233
- expect(subject.call).to eq false
234
+ expect(subject.call.success).to eq false
234
235
  end
235
236
  end
236
237
 
@@ -255,7 +256,7 @@ module PactBroker
255
256
  end
256
257
 
257
258
  it "returns true" do
258
- expect(subject.call).to eq true
259
+ expect(subject.call.success).to eq true
259
260
  end
260
261
  end
261
262
 
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'pact_broker/client/tasks/publication_task'
3
3
  require 'pact_broker/client/publish_pacts'
4
+ require 'pact_broker/client/command_result'
4
5
 
5
6
  module PactBroker::Client
6
7
  describe PublicationTask do
@@ -9,13 +10,16 @@ module PactBroker::Client
9
10
  @consumer_version = "1.2.3"
10
11
  end
11
12
 
12
- let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts", call: true)}
13
+ let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts", call: result)}
13
14
  let(:pact_file_list) { ['spec/pact/consumer-provider.json'] }
15
+ let(:success) { true }
16
+ let(:result) { instance_double(PactBroker::Client::CommandResult, success: success, message: "message")}
14
17
 
15
18
  before do
16
19
  allow(PactBroker::Client::PublishPacts).to receive(:new).and_return(publish_pacts)
17
20
  allow(FileList).to receive(:[]).with(pattern).and_return(pact_file_list)
18
21
  allow(PactBroker::Client::Git).to receive(:branch).and_return('foo')
22
+ allow($stdout).to receive(:puts)
19
23
  end
20
24
 
21
25
  let(:pattern) { "spec/pacts/*.json" }
@@ -29,15 +33,16 @@ module PactBroker::Client
29
33
 
30
34
  context "when pacts are succesfully published" do
31
35
  it "invokes PublishPacts with the default values" do
32
- expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: '1.2.3', branch: "foo", tags: [], version_required: false}, {}).and_return(publish_pacts)
33
- expect(publish_pacts).to receive(:call).and_return(true)
36
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: '1.2.3', branch: "foo", tags: [], version_required: false}, {}, {}).and_return(publish_pacts)
37
+ expect(publish_pacts).to receive(:call).and_return(result)
34
38
  Rake::Task['pact:publish'].execute
35
39
  end
36
40
  end
37
41
 
38
42
  context "when a pact fails to be published" do
43
+ let(:success) { false }
44
+
39
45
  it "raises an error" do
40
- expect(publish_pacts).to receive(:call).and_return(false)
41
46
  expect { Rake::Task['pact:publish'].execute }.to raise_error("One or more pacts failed to be published")
42
47
  end
43
48
  end
@@ -52,8 +57,8 @@ module PactBroker::Client
52
57
  end
53
58
 
54
59
  it "invokes PublishPacts with the write method set" do
55
- expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: "1.2.3", branch: "foo", tags: [], version_required: false }, {write: :merge}).and_return(publish_pacts)
56
- expect(publish_pacts).to receive(:call).and_return(true)
60
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: "1.2.3", branch: "foo", tags: [], version_required: false }, {}, {write: :merge}).and_return(publish_pacts)
61
+ expect(publish_pacts).to receive(:call).and_return(result)
57
62
  Rake::Task['pact:publish:merge'].execute
58
63
  end
59
64
  end
@@ -74,7 +79,7 @@ module PactBroker::Client
74
79
  end
75
80
 
76
81
  it "invokes PublishPacts with the git branch name as a tag" do
77
- expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(tags: ['bar', 'foo']), anything).and_return(publish_pacts)
82
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(tags: ['bar', 'foo']), anything, anything).and_return(publish_pacts)
78
83
  Rake::Task['pact:publish:git_branch'].execute
79
84
  end
80
85
  end
@@ -93,7 +98,7 @@ module PactBroker::Client
93
98
  end
94
99
 
95
100
  it "invokes PublishPacts with the branch name" do
96
- expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts)
101
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything, anything).and_return(publish_pacts)
97
102
  Rake::Task['pact:publish:git_branch_auto_detect_true'].execute
98
103
  end
99
104
  end
@@ -113,7 +118,7 @@ module PactBroker::Client
113
118
  end
114
119
 
115
120
  it "invokes PublishPacts with the specified branch name" do
116
- expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "main"), anything).and_return(publish_pacts)
121
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "main"), anything, anything).and_return(publish_pacts)
117
122
  Rake::Task['pact:publish:git_branch_auto_detect_true_with_branch'].execute
118
123
  end
119
124
  end
@@ -132,7 +137,7 @@ module PactBroker::Client
132
137
  end
133
138
 
134
139
  it "invokes PublishPacts without the branch name" do
135
- expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_not_including(branch: "foo"), anything).and_return(publish_pacts)
140
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_not_including(branch: "foo"), anything, anything).and_return(publish_pacts)
136
141
  Rake::Task['pact:publish:git_branch_auto_detect_false'].execute
137
142
  end
138
143
  end
@@ -150,7 +155,7 @@ module PactBroker::Client
150
155
  end
151
156
 
152
157
  it "invokes PublishPacts with the branch name" do
153
- expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts)
158
+ expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"),anything, anything).and_return(publish_pacts)
154
159
  Rake::Task['pact:publish:git_branch_auto_detect_default'].execute
155
160
  end
156
161
  end
@@ -180,9 +185,10 @@ module PactBroker::Client
180
185
  @pact_broker_base_url,
181
186
  pact_file_list,
182
187
  { number: "1.2.3", tags: [@tag], branch: "foo", version_required: false},
188
+ {},
183
189
  { basic_auth: @pact_broker_basic_auth, token: @pact_broker_token }
184
190
  )
185
- expect(publish_pacts).to receive(:call).and_return(true)
191
+ expect(publish_pacts).to receive(:call).and_return(result)
186
192
  Rake::Task['pact:publish:custom'].execute
187
193
  end
188
194
  end
@@ -15,14 +15,14 @@ module PactBroker
15
15
  stub_request(:get, broker_base_url).to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
16
16
  end
17
17
 
18
- let(:replaced_previous_deployed_version) { true }
18
+ let(:target) { true }
19
19
 
20
20
  let(:params) do
21
21
  {
22
22
  pacticipant_name: "Foo",
23
23
  version_number: "1",
24
24
  environment_name: "test",
25
- replaced_previous_deployed_version: replaced_previous_deployed_version,
25
+ target: target,
26
26
  output: "text"
27
27
  }
28
28
  end
@@ -57,7 +57,7 @@ module PactBroker
57
57
  end
58
58
  end
59
59
 
60
- context "when replaced_previous_deployed_version is false" do
60
+ context "when target is false" do
61
61
  before do
62
62
  allow_any_instance_of(RecordDeployment).to receive(:check_if_command_supported)
63
63
  allow_any_instance_of(RecordDeployment).to receive(:check_environment_exists)
@@ -65,7 +65,7 @@ module PactBroker
65
65
  allow_any_instance_of(RecordDeployment).to receive(:pact_broker_name).and_return("")
66
66
  end
67
67
 
68
- let(:replaced_previous_deployed_version) { false }
68
+ let(:target) { false }
69
69
 
70
70
  let(:deployed_version_resource) do
71
71
  double('PactBroker::Client::Hal::Entity', response: double('response', headers: response_headers) )
@@ -1550,6 +1550,112 @@
1550
1550
  }
1551
1551
  }
1552
1552
  },
1553
+ {
1554
+ "description": "a request for the index resource",
1555
+ "providerState": "the pb:publish-contracts relations exists in the index resource",
1556
+ "request": {
1557
+ "method": "GET",
1558
+ "path": "/",
1559
+ "headers": {
1560
+ "Accept": "application/hal+json"
1561
+ }
1562
+ },
1563
+ "response": {
1564
+ "status": 200,
1565
+ "headers": {
1566
+ "Content-Type": "application/hal+json;charset=utf-8"
1567
+ },
1568
+ "body": {
1569
+ "_links": {
1570
+ "pb:publish-contracts": {
1571
+ "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS"
1572
+ }
1573
+ }
1574
+ },
1575
+ "matchingRules": {
1576
+ "$.body._links.pb:publish-contracts.href": {
1577
+ "match": "regex",
1578
+ "regex": "http:\\/\\/.*"
1579
+ }
1580
+ }
1581
+ }
1582
+ },
1583
+ {
1584
+ "description": "a request to publish contracts",
1585
+ "request": {
1586
+ "method": "POST",
1587
+ "path": "/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS",
1588
+ "headers": {
1589
+ "Content-Type": "application/json",
1590
+ "Accept": "application/hal+json"
1591
+ },
1592
+ "body": {
1593
+ "pacticipantName": "Foo",
1594
+ "pacticipantVersionNumber": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
1595
+ "branch": "main",
1596
+ "tags": [
1597
+ "dev"
1598
+ ],
1599
+ "buildUrl": "http://build",
1600
+ "contracts": [
1601
+ {
1602
+ "consumerName": "Foo",
1603
+ "providerName": "Bar",
1604
+ "specification": "pact",
1605
+ "contentType": "application/json",
1606
+ "content": "eyJjb25zdW1lciI6eyJuYW1lIjoiRm9vIn0sInByb3ZpZGVyIjp7Im5hbWUiOiJCYXIifSwiaW50ZXJhY3Rpb25zIjpbeyJkZXNjcmlwdGlvbiI6ImFuIGV4YW1wbGUgcmVxdWVzdCIsInByb3ZpZGVyU3RhdGUiOiJhIHByb3ZpZGVyIHN0YXRlIiwicmVxdWVzdCI6eyJtZXRob2QiOiJHRVQiLCJwYXRoIjoiLyIsImhlYWRlcnMiOnt9fSwicmVzcG9uc2UiOnsic3RhdHVzIjoyMDAsImhlYWRlcnMiOnsiQ29udGVudC1UeXBlIjoiYXBwbGljYXRpb24vaGFsK2pzb24ifX19XSwibWV0YWRhdGEiOnsicGFjdFNwZWNpZmljYXRpb24iOnsidmVyc2lvbiI6IjIuMC4wIn19fQ==",
1607
+ "writeMode": "overwrite"
1608
+ }
1609
+ ]
1610
+ }
1611
+ },
1612
+ "response": {
1613
+ "status": 200,
1614
+ "headers": {
1615
+ "Content-Type": "application/hal+json;charset=utf-8"
1616
+ },
1617
+ "body": {
1618
+ "_embedded": {
1619
+ "pacticipant": {
1620
+ "name": "Foo"
1621
+ },
1622
+ "version": {
1623
+ "number": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
1624
+ "buildUrl": "http://build"
1625
+ }
1626
+ },
1627
+ "logs": [
1628
+ {
1629
+ "level": "info",
1630
+ "message": "some message"
1631
+ }
1632
+ ],
1633
+ "_links": {
1634
+ "pb:pacticipant-version-tags": [
1635
+ {
1636
+ "name": "dev"
1637
+ }
1638
+ ],
1639
+ "pb:contracts": [
1640
+ {
1641
+ "href": "http://some-pact"
1642
+ }
1643
+ ]
1644
+ }
1645
+ },
1646
+ "matchingRules": {
1647
+ "$.body.logs": {
1648
+ "min": 1
1649
+ },
1650
+ "$.body.logs[*].*": {
1651
+ "match": "type"
1652
+ },
1653
+ "$.body._links.pb:contracts[0].href": {
1654
+ "match": "type"
1655
+ }
1656
+ }
1657
+ }
1658
+ },
1553
1659
  {
1554
1660
  "description": "a request for the index resource",
1555
1661
  "providerState": "the pb:pacticipant-version and pb:environments relations exist in the index resource",