Skip to content

Commit d5d87ed

Browse files
committed
Fix floaty list --active for vmpooler api v2
1 parent 15d9da4 commit d5d87ed

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

lib/vmfloaty/utils.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr =
150150
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
151151
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
152152
metadata = [host_data['state'], host_data['template'], duration, *tag_pairs]
153-
message = "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
153+
# For backwards compatibility with vmpooler api v1
154+
message =
155+
if host_data['domain']
156+
"- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
157+
else
158+
"- #{host_data['fqdn']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
159+
end
160+
154161
if host_data['state'] && host_data['state'] == 'destroyed'
155162
output_target.puts "- DESTROYED #{hostname}.#{host_data['domain']}".gsub(/^/, ' ' * indent)
156163
else

spec/vmfloaty/utils_spec.rb

+67-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,73 @@ class Service
280280

281281
subject { Utils.pretty_print_hosts(verbose, service, hostname, print_to_stderr) }
282282

283-
describe 'with vmpooler service' do
283+
describe 'with vmpooler api v2 service' do
284+
let(:service) { Service.new(MockOptions.new, 'url' => url) }
285+
286+
let(:hostname) { 'mcpy42eqjxli9g2' }
287+
let(:fqdn) { [hostname, 'delivery.puppetlabs.net'].join('.') }
288+
289+
let(:response_body) do
290+
{
291+
hostname => {
292+
'template' => 'ubuntu-1604-x86_64',
293+
'lifetime' => 12,
294+
'running' => 9.66,
295+
'state' => 'running',
296+
'ip' => '127.0.0.1',
297+
'fqdn' => fqdn
298+
}
299+
}
300+
end
301+
302+
let(:default_output) { "- #{fqdn} (running, ubuntu-1604-x86_64, 9.66/12 hours)" }
303+
304+
it 'prints output with host fqdn, template and duration info' do
305+
expect($stdout).to receive(:puts).with(default_output)
306+
307+
subject
308+
end
309+
310+
context 'when tags are supplied' do
311+
let(:hostname) { 'aiydvzpg23r415q' }
312+
let(:response_body) do
313+
{
314+
hostname => {
315+
'template' => 'redhat-7-x86_64',
316+
'lifetime' => 48,
317+
'running' => 7.67,
318+
'state' => 'running',
319+
'tags' => {
320+
'user' => 'bob',
321+
'role' => 'agent'
322+
},
323+
'ip' => '127.0.0.1',
324+
'fqdn' => fqdn
325+
}
326+
}
327+
end
328+
329+
it 'prints output with host fqdn, template, duration info, and tags' do
330+
output = "- #{fqdn} (running, redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)"
331+
332+
expect($stdout).to receive(:puts).with(output)
333+
334+
subject
335+
end
336+
end
337+
338+
context 'when print_to_stderr option is true' do
339+
let(:print_to_stderr) { true }
340+
341+
it 'outputs to stderr instead of stdout' do
342+
expect($stderr).to receive(:puts).with(default_output)
343+
344+
subject
345+
end
346+
end
347+
end
348+
349+
describe 'with vmpooler api v1 service' do
284350
let(:service) { Service.new(MockOptions.new, 'url' => url) }
285351

286352
let(:hostname) { 'mcpy42eqjxli9g2' }

0 commit comments

Comments
 (0)