Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CONT-1241) - Retrying when response body is nil or empty but response code is 200 #221

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 138
Max: 200

# Offense count: 9
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 20
Max: 25

# Offense count: 19
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Expand All @@ -34,7 +34,7 @@ Metrics/ParameterLists:
# Offense count: 7
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 24
Max: 30

# Offense count: 2
# Configuration parameters: IgnoredMetadata.
Expand All @@ -51,7 +51,7 @@ RSpec/DescribeClass:
# Offense count: 4
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 13
Max: 30

# Offense count: 6
RSpec/MultipleExpectations:
Expand Down
17 changes: 15 additions & 2 deletions spec/tasks/abs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,21 @@
expect { ABSProvision.run }.to raise_error(RuntimeError, %r{specify a platform when provisioning})
end

it 'raises an error when node_name not given for tear_down'
it 'raises an error if both node_name and platform are given'
it 'raises an error when node_name not given for tear_down' do
expect($stdin).to receive(:read).and_return('{"action":"teardown"}')

expect { ABSProvision.run }.to raise_error(RuntimeError, %r{specify only one of: node_name, platform})
end

it 'raises an error if both node_name and platform are given' do
expect($stdin).to receive(:read).and_return('{"action":"teardown","platform":"centos-9"}')

expect { ABSProvision.run }.to(
raise_error(SystemExit) do |e|
expect(e.status).to eq(0)
end,
)
end
end

context 'when provisioning' do
Expand Down Expand Up @@ -118,7 +131,7 @@
expect(abs.task(**params)).to eq({ status: 'ok', nodes: 1 })
end

it 'raises an error if abs returns error response'

Check warning on line 134 in spec/tasks/abs_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

provision::abs when provisioning raises an error if abs returns error response Skipped: Not yet implemented

Check warning on line 134 in spec/tasks/abs_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

provision::abs when provisioning raises an error if abs returns error response Skipped: Not yet implemented
end

context 'when tearing down' do
Expand Down Expand Up @@ -166,6 +179,6 @@
end
end

it 'raises an error if abs returns error response'

Check warning on line 182 in spec/tasks/abs_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

provision::abs when tearing down raises an error if abs returns error response Skipped: Not yet implemented

Check warning on line 182 in spec/tasks/abs_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

provision::abs when tearing down raises an error if abs returns error response Skipped: Not yet implemented
end
end
153 changes: 153 additions & 0 deletions spec/tasks/provision_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# frozen_string_literal: true

require 'spec_helper'
require 'webmock/rspec'
require_relative '../../tasks/provision_service'

ENV['GITHUB_RUN_ID'] = '1234567890'
ENV['GITHUB_URL'] = 'https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890'

describe 'ProvisionService' do
describe '.run' do
context 'when inputs are invalid' do
it 'return exception' do
json_input = '{}'
allow($stdin).to receive(:read).and_return(json_input)

expect { ProvisionService.run }.to(
raise_error(SystemExit) { |e|
expect(e.status).to eq(0)
}.and(
output(%r{Unknown action}).to_stdout,
),
)
end

it 'return exception about invalid action' do
json_input = '{"action":"foo","platform":"bar"}'
allow($stdin).to receive(:read).and_return(json_input)

expect { ProvisionService.run }.to(
raise_error(SystemExit) { |e|
expect(e.status).to eq(0)
}.and(
output(%r{Unknown action 'foo'}).to_stdout,
),
)
end

it 'return exception for missing platform' do
json_input = '{"action":"provision"}'
allow($stdin).to receive(:read).and_return(json_input)

expect { ProvisionService.run }.to(
raise_error(SystemExit) { |e|
expect(e.status).to eq(1)
}.and(
output(%r{specify a platform when provisioning}).to_stdout,
),
)
end

it 'return exception for missing node_name' do
json_input = '{"action":"tear_down"}'
allow($stdin).to receive(:read).and_return(json_input)

expect { ProvisionService.run }.to(
raise_error(SystemExit) { |e|
expect(e.status).to eq(1)
}.and(
output(%r{specify a node_name when tearing down}).to_stdout,
),
)
end
end
end

describe '#provision' do
let(:inventory_location) { "#{Dir.pwd}/litmus_inventory.yaml" }
let(:vars) { nil }
let(:platform) { 'centos-8' }
let(:retry_attempts) { 8 }
let(:response_body) do
{
'groups' => [
'targets' => {
'uri' => '127.0.0.1'
},
]
}
end
let(:provision_service) { ProvisionService.new }

context 'when response is empty' do
it 'return exception' do
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
.with(
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
headers: {
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
'User-Agent' => 'Ruby'
},
)
.to_return(status: 200, body: '', headers: {})
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
end
end

context 'when successive retry success' do
it 'return valid response' do
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
.with(
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
headers: {
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
'User-Agent' => 'Ruby'
},
)
.to_return(status: 200, body: '', headers: {})
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
.with(
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
headers: {
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
'User-Agent' => 'Ruby'
},
)
.to_return(status: 200, body: response_body.to_json, headers: {})
allow(File).to receive(:open)
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
end
end

context 'when response is avlid' do
it 'return valid response' do
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
.with(
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
headers: {
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
'User-Agent' => 'Ruby'
},
)
.to_return(status: 200, body: response_body.to_json, headers: {})

allow(File).to receive(:open)
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
end
end
end
end
Loading