Skip to content

Commit 013325b

Browse files
authored
Merge pull request #99 from puppetlabs/fix-returned-abs-json
(maint) Fix for ABS PR#306 that includes json responses
2 parents f3cd540 + cd7c0fa commit 013325b

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

lib/vmfloaty/abs.rb

+23-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def self.get_active_requests(verbose, url, user)
7777
requests.each do |req|
7878
next if req == 'null'
7979

80-
if valid_json?(req)
80+
if valid_json?(req) # legacy ABS had another JSON string always-be-scheduling/pull/306
8181
req_hash = JSON.parse(req)
82+
elsif req.is_a?(Hash)
83+
req_hash = req
8284
else
8385
FloatyLogger.warn "Warning: couldn't parse request returned from abs/status/queue"
8486
next
@@ -170,7 +172,11 @@ def self.list(verbose, url, os_filter = nil)
170172
res_body = JSON.parse(res.body)
171173
if res_body.key?('vmpooler_platforms')
172174
os_list << '*** VMPOOLER Pools ***'
173-
os_list += JSON.parse(res_body['vmpooler_platforms'])
175+
if res_body['vmpooler_platforms'].is_a?(String)
176+
os_list += JSON.parse(res_body['vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
177+
else
178+
os_list += res_body['vmpooler_platforms']
179+
end
174180
end
175181
end
176182

@@ -180,7 +186,11 @@ def self.list(verbose, url, os_filter = nil)
180186
if res_body.key?('ondemand_vmpooler_platforms') && res_body['ondemand_vmpooler_platforms'] != '[]'
181187
os_list << ''
182188
os_list << '*** VMPOOLER ONDEMAND Pools ***'
183-
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms'])
189+
if res_body['ondemand_vmpooler_platforms'].is_a?(String)
190+
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
191+
else
192+
os_list += res_body['ondemand_vmpooler_platforms']
193+
end
184194
end
185195
end
186196

@@ -190,7 +200,11 @@ def self.list(verbose, url, os_filter = nil)
190200
if res_body.key?('nspooler_platforms')
191201
os_list << ''
192202
os_list << '*** NSPOOLER Pools ***'
193-
os_list += JSON.parse(res_body['nspooler_platforms'])
203+
if res_body['nspooler_platforms'].is_a?(String)
204+
os_list += JSON.parse(res_body['nspooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
205+
else
206+
os_list += res_body['nspooler_platforms']
207+
end
194208
end
195209
end
196210

@@ -200,7 +214,11 @@ def self.list(verbose, url, os_filter = nil)
200214
if res_body.key?('aws_platforms')
201215
os_list << ''
202216
os_list << '*** AWS Pools ***'
203-
os_list += JSON.parse(res_body['aws_platforms'])
217+
if res_body['aws_platforms'].is_a?(String)
218+
os_list += JSON.parse(res_body['aws_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
219+
else
220+
os_list += res_body['aws_platforms']
221+
end
204222
end
205223
end
206224

spec/vmfloaty/abs_spec.rb

+47-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,50 @@
99
before :each do
1010
end
1111

12+
describe '#list' do
13+
it 'skips empty platforms and lists aws' do
14+
stub_request(:get, "http://foo/status/platforms/vmpooler").
15+
to_return(:status => 200, :body => "", :headers => {})
16+
stub_request(:get, "http://foo/status/platforms/ondemand_vmpooler").
17+
to_return(:status => 200, :body => "", :headers => {})
18+
stub_request(:get, "http://foo/status/platforms/nspooler").
19+
to_return(:status => 200, :body => "", :headers => {})
20+
body = '{
21+
"aws_platforms": [
22+
"amazon-6-x86_64",
23+
"amazon-7-x86_64",
24+
"amazon-7-arm64",
25+
"centos-7-x86-64-west",
26+
"redhat-8-arm64"
27+
]
28+
}'
29+
stub_request(:get, "http://foo/status/platforms/aws").
30+
to_return(:status => 200, :body => body, :headers => {})
31+
32+
33+
results = ABS.list(false, "http://foo")
34+
35+
expect(results).to include("amazon-6-x86_64", "amazon-7-x86_64", "amazon-7-arm64", "centos-7-x86-64-west", "redhat-8-arm64")
36+
end
37+
it 'legacy JSON string, prior to PR 306' do
38+
stub_request(:get, "http://foo/status/platforms/vmpooler").
39+
to_return(:status => 200, :body => "", :headers => {})
40+
stub_request(:get, "http://foo/status/platforms/ondemand_vmpooler").
41+
to_return(:status => 200, :body => "", :headers => {})
42+
stub_request(:get, "http://foo/status/platforms/nspooler").
43+
to_return(:status => 200, :body => "", :headers => {})
44+
body = '{
45+
"aws_platforms": "[\"amazon-6-x86_64\",\"amazon-7-x86_64\",\"amazon-7-arm64\",\"centos-7-x86-64-west\",\"redhat-8-arm64\"]"
46+
}'
47+
stub_request(:get, "http://foo/status/platforms/aws").
48+
to_return(:status => 200, :body => body, :headers => {})
49+
50+
results = ABS.list(false, "http://foo")
51+
52+
expect(results).to include("amazon-6-x86_64", "amazon-7-x86_64", "amazon-7-arm64", "centos-7-x86-64-west", "redhat-8-arm64")
53+
end
54+
end
55+
1256
describe '#format' do
1357
it 'returns an hash formatted like a vmpooler return, plus the job_id' do
1458
job_id = "generated_by_floaty_12345"
@@ -71,9 +115,9 @@
71115
# rubocop:disable Layout/LineLength
72116
@active_requests_response = '
73117
[
74-
"{ \"state\":\"allocated\",\"last_processed\":\"2019-12-16 23:00:34 +0000\",\"allocated_resources\":[{\"hostname\":\"take-this.delivery.puppetlabs.net\",\"type\":\"win-2012r2-x86_64\",\"engine\":\"vmpooler\"}],\"audit_log\":{\"2019-12-13 16:45:29 +0000\":\"Allocated take-this.delivery.puppetlabs.net for job 1576255517241\"},\"request\":{\"resources\":{\"win-2012r2-x86_64\":1},\"job\":{\"id\":\"1576255517241\",\"tags\":{\"user\":\"test-user\"},\"user\":\"test-user\",\"time-received\":1576255519},\"priority\":1}}",
118+
{ "state":"allocated","last_processed":"2019-12-16 23:00:34 +0000","allocated_resources":[{"hostname":"take-this.delivery.puppetlabs.net","type":"win-2012r2-x86_64","engine":"vmpooler"}],"audit_log":{"2019-12-13 16:45:29 +0000":"Allocated take-this.delivery.puppetlabs.net for job 1576255517241"},"request":{"resources":{"win-2012r2-x86_64":1},"job":{"id":"1576255517241","tags":{"user":"test-user"},"user":"test-user","time-received":1576255519},"priority":1}},
75119
"null",
76-
"{\"state\":\"allocated\",\"last_processed\":\"2019-12-16 23:00:34 +0000\",\"allocated_resources\":[{\"hostname\":\"not-this.delivery.puppetlabs.net\",\"type\":\"win-2012r2-x86_64\",\"engine\":\"vmpooler\"}],\"audit_log\":{\"2019-12-13 16:46:14 +0000\":\"Allocated not-this.delivery.puppetlabs.net for job 1576255565159\"},\"request\":{\"resources\":{\"win-2012r2-x86_64\":1},\"job\":{\"id\":\"1576255565159\",\"tags\":{\"user\":\"not-test-user\"},\"user\":\"not-test-user\",\"time-received\":1576255566},\"priority\":1}}"
120+
{"state":"allocated","last_processed":"2019-12-16 23:00:34 +0000","allocated_resources":[{"hostname":"not-this.delivery.puppetlabs.net","type":"win-2012r2-x86_64","engine":"vmpooler"}],"audit_log":{"2019-12-13 16:46:14 +0000":"Allocated not-this.delivery.puppetlabs.net for job 1576255565159"},"request":{"resources":{"win-2012r2-x86_64":1},"job":{"id":"1576255565159","tags":{"user":"not-test-user"},"user":"not-test-user","time-received":1576255566},"priority":1}}
77121
]'
78122
# rubocop:enable Layout/LineLength
79123
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
@@ -101,7 +145,7 @@
101145
# rubocop:disable Layout/LineLength
102146
@active_requests_response = '
103147
[
104-
"{ \"state\":\"allocated\", \"last_processed\":\"2020-01-17 22:29:13 +0000\", \"allocated_resources\":[{\"hostname\":\"craggy-chord.delivery.puppetlabs.net\", \"type\":\"centos-7-x86_64\", \"engine\":\"vmpooler\"}, {\"hostname\":\"visible-revival.delivery.puppetlabs.net\", \"type\":\"centos-7-x86_64\", \"engine\":\"vmpooler\"}], \"audit_log\":{\"2020-01-17 22:28:45 +0000\":\"Allocated craggy-chord.delivery.puppetlabs.net, visible-revival.delivery.puppetlabs.net for job 1579300120799\"}, \"request\":{\"resources\":{\"centos-7-x86_64\":2}, \"job\":{\"id\":\"1579300120799\", \"tags\":{\"user\":\"test-user\"}, \"user\":\"test-user\", \"time-received\":1579300120}, \"priority\":3}}"
148+
{ "state":"allocated", "last_processed":"2020-01-17 22:29:13 +0000", "allocated_resources":[{"hostname":"craggy-chord.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}, {"hostname":"visible-revival.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}], "audit_log":{"2020-01-17 22:28:45 +0000":"Allocated craggy-chord.delivery.puppetlabs.net, visible-revival.delivery.puppetlabs.net for job 1579300120799"}, "request":{"resources":{"centos-7-x86_64":2}, "job":{"id":"1579300120799", "tags":{"user":"test-user"}, "user":"test-user", "time-received":1579300120}, "priority":3}}
105149
]'
106150
@return_request = { '{"job_id":"1579300120799","hosts":{"hostname":"craggy-chord.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"},{"hostname":"visible-revival.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"}}'=>true }
107151
# rubocop:enable Layout/LineLength

0 commit comments

Comments
 (0)