diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index 1f2418a..a569415 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -114,13 +114,17 @@ class Utils # # Create a vmpooler service to query each hostname there so as to get the metadata too - 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) + 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 + 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 = [] diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index 561c38b..e26e0f1 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -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}] " } - 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}] " } + 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