Skip to content

Show tag values in list --active #23

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 2 commits into from
Jun 5, 2016
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gem 'commander'
gem 'faraday'

gem 'rspec'
gem 'webmock'
gem 'webmock', '1.21.0'
gem 'rake'

gemspec
8 changes: 7 additions & 1 deletion lib/vmfloaty/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def self.get_vm_info(hosts, verbose, url)
vms[host]['template'] = vm_info[host]['template']
vms[host]['lifetime'] = vm_info[host]['lifetime']
vms[host]['running'] = vm_info[host]['running']
vms[host]['tags'] = vm_info[host]['tags']
end
end
vms
Expand All @@ -64,8 +65,13 @@ def self.prettyprint_hosts(hosts, verbose, url)
template = info['template']
lifetime = info['lifetime']
running = info['running']
tags = info['tags'] || {}

puts "- #{vm}.#{domain} (#{template}, #{running}/#{lifetime} hours)"
tag_pairs = tags.map {|key,value| "#{key}: #{value}" }
duration = "#{running}/#{lifetime} hours"
metadata = [template, duration, *tag_pairs]

puts "- #{vm}.#{domain} (#{metadata.join(", ")})"
end
end
end
57 changes: 57 additions & 0 deletions spec/vmfloaty/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,61 @@
expect(Utils.generate_os_hash(host_arg)).to be_empty
end
end

describe '#prettyprint_hosts' do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some benefit here in specifying the desired output format, but honestly this test is going to be super brittle, so I don't know if it - cost vs benefit wise - is going to be very valuable. Happy to remove it, I just didn't feel like I could submit a patch without a test around the change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah....I feel like that's fine for the most part. I'm not too experienced in webmock unit tests checking stdout from puts but I feel like it should be ok? Is it brittle just because the moment someone changes how the prettyprint hosts method works it'll break? or because there's the potential for other things to appear in stdout while running unit tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I agree its probably fine, though brittle for those reasons. Thanks!

let(:host_without_tags) { 'mcpy42eqjxli9g2' }
let(:host_with_tags) { 'aiydvzpg23r415q' }
let(:url) { 'http://pooler.example.com' }

let(:host_info_with_tags) do
{
host_with_tags => {
"template" => "redhat-7-x86_64",
"lifetime" => 48,
"running" => 7.67,
"tags" => {
"user" => "bob",
"role" => "agent"
},
"domain" => "delivery.puppetlabs.net"
}
}
end

let(:host_info_without_tags) do
{
host_without_tags => {
"template" => "ubuntu-1604-x86_64",
"lifetime" => 12,
"running" => 9.66,
"domain" => "delivery.puppetlabs.net"
}
}
end

let(:output_with_tags) { "- #{host_with_tags}.delivery.puppetlabs.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)" }
let(:output_without_tags) { "- #{host_without_tags}.delivery.puppetlabs.net (ubuntu-1604-x86_64, 9.66/12 hours)" }

it 'prints an output with host fqdn, template and duration info' do
allow(Utils).to receive(:get_vm_info).
with(host_without_tags, false, url).
and_return(host_info_without_tags)

expect(Utils).to receive(:puts).with("Running VMs:")
expect(Utils).to receive(:puts).with(output_without_tags)

Utils.prettyprint_hosts(host_without_tags, false, url)
end

it 'prints an output with host fqdn, template, duration info, and tags when supplied' do
allow(Utils).to receive(:get_vm_info).
with(host_with_tags, false, url).
and_return(host_info_with_tags)

expect(Utils).to receive(:puts).with("Running VMs:")
expect(Utils).to receive(:puts).with(output_with_tags)

Utils.prettyprint_hosts(host_with_tags, false, url)
end
end
end