Skip to content

Commit

Permalink
(CONT-1241) - Retrying when response is failed to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramesh7 committed Jul 25, 2023
1 parent 4b3bf5e commit 75a4793
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 150 deletions.
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 @@ def with_env(env_vars)
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
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(NoMethodError)
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(NoMethodError) # .exactly(2).times
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

0 comments on commit 75a4793

Please sign in to comment.