Skip to content

Commit 754ff56

Browse files
committed
(CONT-1241) - Retrying when response is failed to parse
1 parent 4b3bf5e commit 754ff56

File tree

4 files changed

+332
-150
lines changed

4 files changed

+332
-150
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Metrics/AbcSize:
1414
# Offense count: 1
1515
# Configuration parameters: CountComments, CountAsOne.
1616
Metrics/ClassLength:
17-
Max: 138
17+
Max: 200
1818

1919
# Offense count: 9
2020
# Configuration parameters: AllowedMethods, AllowedPatterns.
2121
Metrics/CyclomaticComplexity:
22-
Max: 20
22+
Max: 25
2323

2424
# Offense count: 19
2525
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
@@ -34,7 +34,7 @@ Metrics/ParameterLists:
3434
# Offense count: 7
3535
# Configuration parameters: AllowedMethods, AllowedPatterns.
3636
Metrics/PerceivedComplexity:
37-
Max: 24
37+
Max: 30
3838

3939
# Offense count: 2
4040
# Configuration parameters: IgnoredMetadata.
@@ -51,7 +51,7 @@ RSpec/DescribeClass:
5151
# Offense count: 4
5252
# Configuration parameters: CountAsOne.
5353
RSpec/ExampleLength:
54-
Max: 13
54+
Max: 30
5555

5656
# Offense count: 6
5757
RSpec/MultipleExpectations:

spec/tasks/abs_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,21 @@ def with_env(env_vars)
6767
expect { ABSProvision.run }.to raise_error(RuntimeError, %r{specify a platform when provisioning})
6868
end
6969

70-
it 'raises an error when node_name not given for tear_down'
71-
it 'raises an error if both node_name and platform are given'
70+
it 'raises an error when node_name not given for tear_down' do
71+
expect($stdin).to receive(:read).and_return('{"action":"teardown"}')
72+
73+
expect { ABSProvision.run }.to raise_error(RuntimeError, %r{specify only one of: node_name, platform})
74+
end
75+
76+
it 'raises an error if both node_name and platform are given' do
77+
expect($stdin).to receive(:read).and_return('{"action":"teardown","platform":"centos-9"}')
78+
79+
expect { ABSProvision.run }.to(
80+
raise_error(SystemExit) do |e|
81+
expect(e.status).to eq(0)
82+
end,
83+
)
84+
end
7285
end
7386

7487
context 'when provisioning' do
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'webmock/rspec'
5+
require_relative '../../tasks/provision_service'
6+
7+
ENV['GITHUB_RUN_ID'] = '1234567890'
8+
ENV['GITHUB_URL'] = 'https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890'
9+
10+
describe 'ProvisionService' do
11+
describe '.run' do
12+
context 'when inputs are invalid' do
13+
it 'return exception' do
14+
json_input = '{}'
15+
allow($stdin).to receive(:read).and_return(json_input)
16+
17+
expect { ProvisionService.run }.to(
18+
raise_error(SystemExit) { |e|
19+
expect(e.status).to eq(0)
20+
}.and(
21+
output(%r{Unknown action}).to_stdout,
22+
),
23+
)
24+
end
25+
26+
it 'return exception about invalid action' do
27+
json_input = '{"action":"foo","platform":"bar"}'
28+
allow($stdin).to receive(:read).and_return(json_input)
29+
30+
expect { ProvisionService.run }.to(
31+
raise_error(SystemExit) { |e|
32+
expect(e.status).to eq(0)
33+
}.and(
34+
output(%r{Unknown action 'foo'}).to_stdout,
35+
),
36+
)
37+
end
38+
39+
it 'return exception for missing platform' do
40+
json_input = '{"action":"provision"}'
41+
allow($stdin).to receive(:read).and_return(json_input)
42+
43+
expect { ProvisionService.run }.to(
44+
raise_error(SystemExit) { |e|
45+
expect(e.status).to eq(1)
46+
}.and(
47+
output(%r{specify a platform when provisioning}).to_stdout,
48+
),
49+
)
50+
end
51+
52+
it 'return exception for missing node_name' do
53+
json_input = '{"action":"tear_down"}'
54+
allow($stdin).to receive(:read).and_return(json_input)
55+
56+
expect { ProvisionService.run }.to(
57+
raise_error(SystemExit) { |e|
58+
expect(e.status).to eq(1)
59+
}.and(
60+
output(%r{specify a node_name when tearing down}).to_stdout,
61+
),
62+
)
63+
end
64+
end
65+
end
66+
67+
describe '#provision' do
68+
let(:inventory_location) { "#{Dir.pwd}/litmus_inventory.yaml" }
69+
let(:vars) { nil }
70+
let(:platform) { 'centos-8' }
71+
let(:retry_attempts) { 8 }
72+
let(:response_body) do
73+
{
74+
'groups' => [
75+
'targets' => {
76+
'uri' => '127.0.0.1'
77+
},
78+
]
79+
}
80+
end
81+
let(:provision_service) { ProvisionService.new }
82+
83+
context 'when response is empty' do
84+
it 'return exception' do
85+
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
86+
.with(
87+
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
88+
headers: {
89+
'Accept' => 'application/json',
90+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
91+
'Content-Type' => 'application/json',
92+
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
93+
'User-Agent' => 'Ruby'
94+
},
95+
)
96+
.to_return(status: 200, body: '', headers: {})
97+
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
98+
end
99+
end
100+
101+
context 'when successive retry success' do
102+
it 'return valid response' do
103+
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
104+
.with(
105+
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
106+
headers: {
107+
'Accept' => 'application/json',
108+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
109+
'Content-Type' => 'application/json',
110+
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
111+
'User-Agent' => 'Ruby'
112+
},
113+
)
114+
.to_return(status: 200, body: '', headers: {})
115+
expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError)
116+
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
117+
.with(
118+
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
119+
headers: {
120+
'Accept' => 'application/json',
121+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
122+
'Content-Type' => 'application/json',
123+
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
124+
'User-Agent' => 'Ruby'
125+
},
126+
)
127+
.to_return(status: 200, body: response_body.to_json, headers: {})
128+
allow(File).to receive(:open)
129+
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
130+
end
131+
end
132+
133+
context 'when response is avlid' do
134+
it 'return valid response' do
135+
stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision')
136+
.with(
137+
body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}',
138+
headers: {
139+
'Accept' => 'application/json',
140+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
141+
'Content-Type' => 'application/json',
142+
'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app',
143+
'User-Agent' => 'Ruby'
144+
},
145+
)
146+
.to_return(status: 200, body: response_body.to_json, headers: {})
147+
148+
allow(File).to receive(:open)
149+
expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok')
150+
end
151+
end
152+
end
153+
end

0 commit comments

Comments
 (0)