Class: PactBroker::Test::HttpTestDataBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/test/http_test_data_builder.rb

Defined Under Namespace

Classes: LogRequestLength, SetRequestId

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pact_broker_base_url, auth = {}) ⇒ HttpTestDataBuilder

Returns a new instance of HttpTestDataBuilder.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 45

def initialize(pact_broker_base_url, auth = {})
  @correlation_id = SecureRandom.hex(10)

  stream = ENV["DEBUG"] == "true" ? $stdout : StringIO.new
  logger = ::Logger.new(stream)

  @client = Faraday.new(url: pact_broker_base_url) do |faraday|
    faraday.use LogRequestLength, logger
    faraday.use SetRequestId, @correlation_id, logger
    faraday.request :json
    faraday.response :json, :content_type => /\bjson$/
    if ENV["DEBUG"] == "true"
      faraday.response :logger, logger, headers: false, body: true do | l |
        l.filter(/(Authorization: ).*/,'\1[REMOVED]')
      end
    end
    faraday.basic_auth(auth[:username], auth[:password]) if auth[:username]
    faraday.headers["Authorization"] = "Bearer #{auth[:token]}" if auth[:token]
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def client
  @client
end

#last_consumer_nameObject (readonly)

Returns the value of attribute last_consumer_name.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_consumer_name
  @last_consumer_name
end

#last_consumer_version_numberObject (readonly)

Returns the value of attribute last_consumer_version_number.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_consumer_version_number
  @last_consumer_version_number
end

#last_provider_nameObject (readonly)

Returns the value of attribute last_provider_name.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_provider_name
  @last_provider_name
end

#last_provider_version_branchObject (readonly)

Returns the value of attribute last_provider_version_branch.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_provider_version_branch
  @last_provider_version_branch
end

#last_provider_version_numberObject (readonly)

Returns the value of attribute last_provider_version_number.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_provider_version_number
  @last_provider_version_number
end

#last_provider_version_tagObject (readonly)

Returns the value of attribute last_provider_version_tag.



43
44
45
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def last_provider_version_tag
  @last_provider_version_tag
end

Instance Method Details

#can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 355

def can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil)
  can_i_deploy_response = client.get("can-i-deploy", { pacticipant: pacticipant, version: version, to: to, environment: to_environment}.compact ).tap { |response| check_for_error(response) }
  can = !!(can_i_deploy_response.body["summary"] || {})["deployable"]
  puts "can-i-deploy #{pacticipant} version #{version} to #{to || to_environment}: #{can ? 'yes' : 'no'}"
  summary = can_i_deploy_response.body["summary"]
  verification_result_urls = (can_i_deploy_response.body["matrix"] || []).collect do | row |
    row.dig("verificationResult", "_links", "self", "href")
  end.compact
  summary.merge!("verification_result_urls" => verification_result_urls)
  puts summary.to_yaml
  separate
  self
end

#can_i_merge(pacticipant:, version:) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 369

def can_i_merge(pacticipant:, version:)
  can_i_merge_response = client.get("matrix", { q: [pacticipant: pacticipant, version: version], latestby: "cvp", mainBranch: true, latest: true }.compact ).tap { |response| check_for_error(response) }
  can = !!(can_i_merge_response.body["summary"] || {})["deployable"]
  puts "can-i-merge #{pacticipant} version #{version}: #{can ? 'yes' : 'no'}"
  summary = can_i_merge_response.body["summary"]
  verification_result_urls = (can_i_merge_response.body["matrix"] || []).collect do | row |
    row.dig("verificationResult", "_links", "self", "href")
  end.compact
  summary.merge!("verification_result_urls" => verification_result_urls)
  puts summary.to_yaml
  separate
  self
end

#comment(string) ⇒ Object



81
82
83
84
85
86
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 81

def comment string
  puts "**********************************************************"
  puts string
  puts "**********************************************************\n\n"
  self
end

#create_environment(name:, production: false) ⇒ Object



144
145
146
147
148
149
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 144

def create_environment(name:, production: false)
  puts "Creating environment #{name}"
  client.post("/environments", { name: name, displayName: name, production: production }).tap { |response| check_for_error(response) }
  separate
  self
end

#create_global_webhook_for_anything_published(uuid: nil, url: "https://postman-echo.com/post") ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 332

def create_global_webhook_for_anything_published(uuid: nil, url: "https://postman-echo.com/post")
  puts "Creating global webhook for contract changed event with uuid #{uuid}"
  uuid ||= SecureRandom.uuid

  path = "webhooks/#{uuid}"
  client.put(path, webhook_body_with_all_parameters(url).to_json).tap { |response| check_for_error(response) }
  separate
  self
end

#create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post", body: nil) ⇒ Object

rubocop: enable Metrics/MethodLength



324
325
326
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 324

def create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post", body: nil)
  create_global_webhook_for_event(uuid: uuid, url: url, body: body, event_name: "contract_content_changed")
end

#create_global_webhook_for_contract_requiring_verification_published(uuid: nil, url: "https://postman-echo.com/post", body: nil) ⇒ Object



328
329
330
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 328

def create_global_webhook_for_contract_requiring_verification_published(uuid: nil, url: "https://postman-echo.com/post", body: nil)
  create_global_webhook_for_event(uuid: uuid, url: url, body: body, event_name: "contract_requiring_verification_published")
end

#create_global_webhook_for_event(**kwargs) ⇒ Object



282
283
284
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 282

def create_global_webhook_for_event(**kwargs)
  create_webhook_for_event(**kwargs)
end

#create_label(name, label) ⇒ Object



158
159
160
161
162
163
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 158

def create_label(name, label)
  puts "Creating label '#{label}' for #{name}"
  client.put("pacticipants/#{encode(name)}/labels/#{encode(label)}", {}).tap { |response| check_for_error(response) }
  separate
  self
end

#create_pacticipant(name, main_branch: nil) ⇒ Object



151
152
153
154
155
156
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 151

def create_pacticipant(name, main_branch: nil)
  puts "Creating pacticipant with name #{name}"
  client.post("pacticipants", { name: name, mainBranch: main_branch }).tap { |response| check_for_error(response) }
  separate
  self
end

#create_tag(pacticipant:, version:, tag:) ⇒ Object



95
96
97
98
99
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 95

def create_tag(pacticipant:, version:, tag:)
  puts "Creating tag '#{tag}' for #{pacticipant} version #{version}"
  client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}/tags/#{encode(tag)}", {}).tap { |response| check_for_error(response) }
  self
end

#create_tagged_pacticipant_version(pacticipant:, version:, tag:) ⇒ Object



88
89
90
91
92
93
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 88

def create_tagged_pacticipant_version(pacticipant:, version:, tag:)
  [*tag].each do | t |
    create_tag(pacticipant: pacticipant, version: version, tag: t)
  end
  self
end

#create_version(pacticipant:, version:, branch: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 101

def create_version(pacticipant:, version:, branch: nil)
  if branch
    puts "Adding #{pacticipant} version #{version} to branch #{branch}"
    puts ""
    client.put("pacticipants/#{encode(pacticipant)}/branches/#{encode(branch)}/versions/#{encode(version)}", {}).tap { |response| check_for_error(response) }
  else
    client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) }
  end
  self
end

#create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", body: nil, provider: nil, consumer: nil, event_name:) ⇒ Object

rubocop: disable Metrics/MethodLength



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 287

def create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", body: nil, provider: nil, consumer: nil, event_name:)
  require "securerandom"
  webhook_prefix = "global " if provider.nil? && consumer.nil?
  puts "Creating #{webhook_prefix}webhook for contract changed event with uuid #{uuid}"
  uuid ||= SecureRandom.uuid
  default_body = {
    "pactUrl" => "${pactbroker.pactUrl}",
    "eventName" => "${pactbroker.eventName}",
    "consumerName" => "${pactbroker.consumerName}",
    "consumerVersionNumber" => "${pactbroker.consumerVersionNumber}",
    "consumerVersionTags" => "${pactbroker.consumerVersionTags}",
    "consumerVersionBranch" => "${pactbroker.consumerVersionBranch}",
    "providerName" => "${pactbroker.providerName}",
    "providerVersionNumber" => "${pactbroker.providerVersionNumber}",
    "providerVersionBranch" => "${pactbroker.providerVersionBranch}",
    "providerVersionDescriptions" => "${pactbroker.providerVersionDescriptions}",
  }
  request_body = {
    "consumer" => consumer,
    "provider" => provider,
    "description" => webhook_description(consumer, provider),
    "events" => Array(event_name).map { |name| {"name" => name} },
    "request" => {
      "method" => "POST",
      "url" => url,
      "body" => body || default_body,
      "username" => "user",
      "password" => "pass"
    }
  }.compact
  path = "webhooks/#{uuid}"
  client.put(path, request_body.to_json).tap { |response| check_for_error(response) }
  separate
  self
end

#delete_integration(consumer:, provider:) ⇒ Object



383
384
385
386
387
388
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 383

def delete_integration(consumer:, provider:)
  puts "Deleting all data for the integration between #{consumer} and #{provider}"
  client.delete("integrations/provider/#{encode(provider)}/consumer/#{encode(consumer)}").tap { |response| check_for_error(response) }
  separate
  self
end

#delete_pacticipant(name) ⇒ Object



390
391
392
393
394
395
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 390

def delete_pacticipant(name)
  puts "Deleting pacticipant #{name}"
  @publish_pact_response = client.delete("pacticipants/#{encode(name)}").tap { |response| check_for_error(response) }
  separate
  self
end

#delete_webhook(uuid:) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 342

def delete_webhook(uuid:)
  puts "Deleting webhook with uuid #{uuid}"
  path = "webhooks/#{uuid}"
  client.delete(path).tap { |response| check_for_error(response) }
  separate
  self
end

#deploy_to_prod(pacticipant:, version:) ⇒ Object



112
113
114
115
116
117
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 112

def deploy_to_prod(pacticipant:, version:)
  puts "Deploying #{pacticipant} version #{version} to prod"
  create_tag(pacticipant: pacticipant, version: version, tag: "prod")
  separate
  self
end

#generate_content(consumer_name, provider_name, content_id) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 397

def generate_content(consumer_name, provider_name, content_id)
  {
    consumer: {
      name: consumer_name
    },
    provider: {
      name: provider_name
    },
    interactions: [
      {
        description: "a request",
        request: {
          method: "GET",
          path: "/things/#{content_id}"
        },
        response: {
          status: 200
        }
      }
    ]
  }
end

#get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 221

def get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil)
  @last_provider_name = provider
  @last_provider_version_tag = provider_version_tag
  @last_provder_version_branch = provider_version_branch
  puts "Fetching pacts for verification for #{provider}"
  request_body = {
    providerVersionTags: [*provider_version_tag],
    providerVersionBranch: provider_version_branch,
    consumerVersionSelectors: consumer_version_selectors,
    includePendingStatus: enable_pending,
    includeWipPactsSince: include_wip_pacts_since
  }.compact
  puts request_body.to_yaml
  puts ""
  @pacts_for_verification_response = client.post("pacts/provider/#{encode(provider)}/for-verification", request_body).tap { |response| check_for_error(response) }

  print_pacts_for_verification
  separate
  self
end


72
73
74
75
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 72

def print_correlation_id
  puts "Correlation ID is #{@correlation_id}"
  self
end


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 242

def print_pacts_for_verification
  pacts = @pacts_for_verification_response.body&.dig("_embedded", "pacts")
  if pacts
    puts "Pacts for verification (#{pacts.count}):"
    pacts.each do | pact |
      puts({
        "url" => pact["_links"]["self"]["href"],
        "wip" => pact["verificationProperties"]["wip"],
        "pending" => pact["verificationProperties"]["pending"],
        "why" => pact["verificationProperties"]["notices"].select { | n | n["when"] == "before_verification" }.collect{ | n | n["text"] }
      }.to_yaml)
    end
  end
  self
end


350
351
352
353
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 350

def print_pacts_for_verification_response
  puts @pacts_for_verification_response.body
  self
end

#publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 165

def publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil)
  content = generate_content(consumer, provider, content_id)
  request_body_hash = {
    :pacticipantName => consumer,
    :pacticipantVersionNumber => consumer_version,
    :branch => branch,
    :tags => tag ? [tag] : nil,
    :contracts => [
      {
        :consumerName => consumer,
        :providerName => provider,
        :specification => "pact",
        :contentType => "application/json",
        :content => Base64.strict_encode64(content.to_json)
      }
    ]
  }.compact
  response = client.post("contracts/publish", request_body_hash).tap { |resp| check_for_error(resp) }
  puts response.body["logs"].collect{ |log| log["message"]}
  separate
  self
end

#publish_contract_and_verify(consumer:, provider:, consumer_version:, content_id: nil, consumer_branch: "main", provider_version:, provider_branch: "main", success: true) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 208

def publish_contract_and_verify(consumer:, provider:, consumer_version:, content_id: nil, consumer_branch: "main", provider_version:, provider_branch: "main", success: true)
  publish_contract(consumer: consumer, provider: provider, consumer_version: consumer_version, content_id: content_id || SecureRandom.rand.to_s, branch: consumer_branch)
    .get_pacts_for_verification(provider: provider, consumer_version_selectors: [ { branch: consumer_branch }])
    .verify_pact(
      provider: provider,
      provider_version_branch: provider_branch,
      provider_version: provider_version,
      success: success
    )
end

#publish_pact_the_old_way(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object Also known as: publish_pact



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 188

def publish_pact_the_old_way(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil)
  @last_consumer_name = consumer
  @last_provider_name = provider
  @last_consumer_version_number = consumer_version

  create_version(pacticipant: consumer, version: consumer_version, branch: branch) if branch

  [*tag].each do | t |
    create_tag(pacticipant: consumer, version: consumer_version, tag: t)
  end
  puts "" if [*tag].any?

  content = generate_content(consumer, provider, content_id)
  puts "Publishing pact for consumer #{consumer} version #{consumer_version} and provider #{provider}"
  pact_path = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/version/#{encode(consumer_version)}"
  @publish_pact_response = client.put(pact_path, content).tap { |response| check_for_error(response) }
  separate
  self
end

#record_deployment(pacticipant:, version:, environment_name:) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 119

def record_deployment(pacticipant:, version:, environment_name:)
  puts "Recording deployment of #{pacticipant} version #{version} to #{environment_name}"
  version_body = client.get("/pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) }.body

  environment_relation = version_body["_links"]["pb:record-deployment"].find { |relation| relation["name"] == environment_name }
  if environment_relation.nil?
    available_environments = version_body["_links"]["pb:record-deployment"].collect{ | relation | relation["name"]}.join
    puts "Environment with name #{environment_name} not found. Available environments: #{available_environments}"
  else
    client.post(environment_relation["href"], {}).tap { |response| check_for_error(response) }
  end

  separate
  self
end

#record_release(pacticipant:, version:, environment_name:) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 135

def record_release(pacticipant:, version:, environment_name:)
  puts "Recording release of #{pacticipant} version #{version} to #{environment_name}"
  version_body = client.get("/pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) }.body
  environment_relation = version_body["_links"]["pb:record-release"].find { |relation| relation["name"] == environment_name }
  client.post(environment_relation["href"]).tap { |response| check_for_error(response) }
  separate
  self
end

#separateObject



77
78
79
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 77

def separate
  puts "\n=============================================================\n\n"
end

#sleep(seconds = 0.5) ⇒ Object



67
68
69
70
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 67

def sleep(seconds = 0.5)
  Kernel.sleep(seconds)
  self
end

#verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag:, provider_version:, provider_version_tag: nil, provider_version_branch: nil) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 258

def verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag: , provider_version:, provider_version_tag: nil, provider_version_branch: nil)
  @last_provider_name = provider
  @last_consumer_name = consumer

  url_of_pact_to_verify = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/latest/#{encode(consumer_version_tag)}"
  publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success)
  separate
  self
end

#verify_pact(index: 0, success: true, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version:) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 268

def verify_pact(index: 0, success: true, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version: )
  @last_provider_name = provider
  @last_provider_version_tag = provider_version_tag
  @last_provider_version_branch = provider_version_branch

  pact_to_verify = @pacts_for_verification_response.body["_embedded"]["pacts"][index]
  raise "No pact found to verify at index #{index}" unless pact_to_verify
  url_of_pact_to_verify = pact_to_verify["_links"]["self"]["href"]

  publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success)
  separate
  self
end