Fix pretty print when multiple backend services

are returned by ABS. Added test with vmpooler and nspooler result
This commit is contained in:
Samuel Beaulieu 2020-09-22 09:53:13 -05:00
parent 45f0ef031e
commit 2b56448d50
2 changed files with 104 additions and 9 deletions

View file

@ -114,13 +114,17 @@ class Utils
#
# Create a vmpooler service to query each hostname there so as to get the metadata too
output_target.puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
host_data['allocated_resources'].each do |allocated_resources, _i|
if allocated_resources['engine'] == "vmpooler"
vmpooler_service = service.clone
vmpooler_service.silent = true
vmpooler_service.maybe_use_vmpooler
output_target.puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
host_data['allocated_resources'].each do |vm_name, _i|
self.pretty_print_hosts(verbose, vmpooler_service, vm_name['hostname'].split('.')[0], print_to_stderr, indent+2)
self.pretty_print_hosts(verbose, vmpooler_service, allocated_resources['hostname'].split('.')[0], print_to_stderr, indent+2)
else
#TODO we could add more specific metadata for the other services, nspooler and aws
output_target.puts " - #{allocated_resources['hostname']} (#{allocated_resources['type']})"
end
end
when 'Pooler'
tag_pairs = []

View file

@ -386,7 +386,7 @@ describe Utils do
let(:service) { Service.new(MockOptions.new, 'url' => url, 'type' => 'abs') }
let(:hostname) { '1597952189390' }
let(:fqdn) { 'example-noun.delivery.puppetlabs.net' }
let(:fqdn) { 'example-noun.delivery.mycompany.net' }
let(:fqdn_hostname) {'example-noun'}
let(:template) { 'ubuntu-1604-x86_64' }
@ -416,7 +416,7 @@ describe Utils do
let(:response_body_vmpooler) do
{
fqdn_hostname => {
'template' => 'redhat-7-x86_64',
'template' => template,
'lifetime' => 48,
'running' => 7.67,
'state' => 'running',
@ -441,7 +441,7 @@ describe Utils do
end
let(:default_output_first_line) { "- [JobID:#{hostname}] <allocated>" }
let(:default_output_second_line) { " - example-noun.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)" }
let(:default_output_second_line) { " - #{fqdn} (#{template}, 7.67/48 hours, user: bob, role: agent)" }
it 'prints output with job id, host, and template' do
expect(STDOUT).to receive(:puts).with(default_output_first_line)
@ -461,6 +461,97 @@ describe Utils do
end
end
end
describe 'with ABS service returning vmpooler and nspooler resources' do
let(:service) { Service.new(MockOptions.new, 'url' => url, 'type' => 'abs') }
let(:hostname) { '1597952189390' }
let(:fqdn) { 'this-noun.delivery.mycompany.net' }
let(:fqdn_ns) { 'that-noun.delivery.mycompany.net' }
let(:fqdn_hostname) {'this-noun'}
let(:fqdn_ns_hostname) {'that-noun'}
let(:template) { 'ubuntu-1604-x86_64' }
let(:template_ns) { 'solaris-10-sparc' }
# This seems to be the miminal stub response from ABS for the current output
let(:response_body) do
{
hostname => {
'state' => 'allocated',
'allocated_resources' => [
{
'hostname' => fqdn,
'type' => template,
'engine' => 'vmpooler',
},
{
'hostname' => fqdn_ns,
'type' => template_ns,
'engine' => 'nspooler',
},
],
'request' => {
'job' => {
'id' => hostname,
}
},
}
}
end
# The vmpooler response contains metadata that is printed
let(:domain) { 'delivery.mycompany.net' }
let(:response_body_vmpooler) do
{
fqdn_hostname => {
'template' => template,
'lifetime' => 48,
'running' => 7.67,
'state' => 'running',
'tags' => {
'user' => 'bob',
'role' => 'agent',
},
'ip' => '127.0.0.1',
'domain' => domain,
}
}
end
before(:each) do
allow(Utils).to receive(:get_vmpooler_service_config).and_return({
'url' => 'http://vmpooler.example.com',
'token' => 'krypto-knight'
})
allow(service).to receive(:query)
.with(anything, fqdn_hostname)
.and_return(response_body_vmpooler)
end
let(:default_output_first_line) { "- [JobID:#{hostname}] <allocated>" }
let(:default_output_second_line) { " - #{fqdn} (#{template}, 7.67/48 hours, user: bob, role: agent)" }
let(:default_output_third_line) { " - #{fqdn_ns} (#{template_ns})" }
it 'prints output with job id, host, and template' do
expect(STDOUT).to receive(:puts).with(default_output_first_line)
expect(STDOUT).to receive(:puts).with(default_output_second_line)
expect(STDOUT).to receive(:puts).with(default_output_third_line)
subject
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_first_line)
expect(STDERR).to receive(:puts).with(default_output_second_line)
expect(STDERR).to receive(:puts).with(default_output_third_line)
subject
end
end
end
end
describe '#get_vmpooler_service_config' do