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 24, 2023
1 parent 4b3bf5e commit bf2f371
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 146 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Metrics/ClassLength:
# 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 Down
112 changes: 112 additions & 0 deletions spec/tasks/provision_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

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

describe 'provision::provision_service' do

Check failure on line 7 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/DescribeClass: The first argument to describe should be the class or module being tested.

Check failure on line 7 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/DescribeClass: The first argument to describe should be the class or module being tested.
describe '.run' do
context 'invalid inputs' do

Check failure on line 9 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/ContextWording: Context description should match /^when\b/, /^with\b/, or /^without\b/.

Check failure on line 9 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/ContextWording: Context description should match /^when\b/, /^with\b/, or /^without\b/.
it 'with no action' do
json_input = '{}'
expect($stdin).to receive(:read).and_return(json_input)

Check failure on line 12 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

Check failure on line 12 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

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

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

Check failure on line 25 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

Check failure on line 25 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

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

it 'provision action without platform' do
json_input = '{"action":"provision"}'
expect($stdin).to receive(:read).and_return(json_input)

Check failure on line 38 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

Check failure on line 38 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

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 'tear_down action without node_name' do
json_input = '{"action":"tear_down"}'
expect($stdin).to receive(:read).and_return(json_input)

Check failure on line 51 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

Check failure on line 51 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.

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'=> {

Check failure on line 72 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.

Check failure on line 72 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/SpaceAroundOperators: Surrounding space missing for operator `=>`.

Check failure on line 72 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.

Check failure on line 72 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/SpaceAroundOperators: Surrounding space missing for operator `=>`.
'uri' => '127.0.0.1'
}

Check failure on line 74 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array.

Check failure on line 74 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array.
]
}
end
let(:provision_service) { ProvisionService.new }

it 'action provision with the platform and return empty response' do
stub_request(:post, "https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision").

Check failure on line 81 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check failure on line 81 in spec/tasks/provision_service_spec.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
with(
body: "{\"url\":\"https://github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/5575363758\",\"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

it 'action provision with the platform and return valid response' do
stub_request(:post, "https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision").
with(
body: "{\"url\":\"https://github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/5575363758\",\"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
Loading

0 comments on commit bf2f371

Please sign in to comment.