Skip to content

Fix floaty list --active for vmpooler api v2 #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/vmfloaty/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr =
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
metadata = [host_data['state'], host_data['template'], duration, *tag_pairs]
message = "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
# For backwards compatibility with vmpooler api v1
message =
if host_data['domain']
"- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
else
"- #{host_data['fqdn']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
end

if host_data['state'] && host_data['state'] == 'destroyed'
output_target.puts "- DESTROYED #{hostname}.#{host_data['domain']}".gsub(/^/, ' ' * indent)
else
Expand Down
68 changes: 67 additions & 1 deletion spec/vmfloaty/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,73 @@ class Service

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

describe 'with vmpooler service' do
describe 'with vmpooler api v2 service' do
let(:service) { Service.new(MockOptions.new, 'url' => url) }

let(:hostname) { 'mcpy42eqjxli9g2' }
let(:fqdn) { [hostname, 'delivery.puppetlabs.net'].join('.') }

let(:response_body) do
{
hostname => {
'template' => 'ubuntu-1604-x86_64',
'lifetime' => 12,
'running' => 9.66,
'state' => 'running',
'ip' => '127.0.0.1',
'fqdn' => fqdn
}
}
end

let(:default_output) { "- #{fqdn} (running, ubuntu-1604-x86_64, 9.66/12 hours)" }

it 'prints output with host fqdn, template and duration info' do
expect($stdout).to receive(:puts).with(default_output)

subject
end

context 'when tags are supplied' do
let(:hostname) { 'aiydvzpg23r415q' }
let(:response_body) do
{
hostname => {
'template' => 'redhat-7-x86_64',
'lifetime' => 48,
'running' => 7.67,
'state' => 'running',
'tags' => {
'user' => 'bob',
'role' => 'agent'
},
'ip' => '127.0.0.1',
'fqdn' => fqdn
}
}
end

it 'prints output with host fqdn, template, duration info, and tags' do
output = "- #{fqdn} (running, redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)"

expect($stdout).to receive(:puts).with(output)

subject
end
end

context 'when print_to_stderr option is true' do
let(:print_to_stderr) { true }

it 'outputs to stderr instead of stdout' do
expect($stderr).to receive(:puts).with(default_output)

subject
end
end
end

describe 'with vmpooler api v1 service' do
let(:service) { Service.new(MockOptions.new, 'url' => url) }

let(:hostname) { 'mcpy42eqjxli9g2' }
Expand Down