From cec25e0a0c940b555921ce361dbdf8c1eb86a93d Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Sat, 11 May 2019 17:48:52 -0400 Subject: [PATCH] Merge pull request #18748 from djberg96/conversion_host_ansible_playbook_default_credentials Have ConversionHost explicitly search authentications for auth type (cherry picked from commit 54bc9a86e5b4199d7b4a2f7587ca8aea9136a545) https://bugzilla.redhat.com/show_bug.cgi?id=1712135 --- app/models/conversion_host.rb | 4 +-- spec/factories/authentication.rb | 4 +++ spec/models/conversion_host_spec.rb | 48 +++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index ef584f58e73..6783a4457a0 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -201,7 +201,7 @@ def resource_supports_conversion_host # with the resource using the 'ssh_keypair' auth type, and finally 'default'. # def find_credentials(auth_type = 'v2v') - authentication = authentication_type(auth_type) + authentication = authentications.detect { |a| a.authtype == auth_type } if authentication.blank? res = resource.respond_to?(:authentication_type) ? resource : resource.ext_management_system @@ -245,7 +245,7 @@ def miq_ssh_util_args_manageiq_providers_openstack_cloudmanager_vm # +extra_vars+ option should be a hash of key/value pairs which, if present, # will be passed to the '-e' flag. # - def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = nil) + def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = 'v2v') task = MiqTask.find(miq_task_id) if miq_task_id.present? host = hostname || ipaddress diff --git a/spec/factories/authentication.rb b/spec/factories/authentication.rb index b7c762a0026..4f721fee2b5 100644 --- a/spec/factories/authentication.rb +++ b/spec/factories/authentication.rb @@ -86,6 +86,10 @@ certificate_authority "certificate_authority" end + factory :authentication_v2v, :parent => :authentication_ssh_keypair do + authtype { "v2v" } + end + factory :authentication_redhat_metric, :parent => :authentication do authtype "metrics" end diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index 7978711cc06..817c83e1ff5 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -352,17 +352,53 @@ context "authentication associations" do let(:vm) { FactoryBot.create(:vm_openstack) } + let(:ems) { FactoryBot.create(:ems_redhat, :zone => FactoryBot.create(:zone), :api_version => '4.2.4') } + let(:host) { FactoryBot.create(:host_redhat, :ext_management_system => ems) } + let(:conversion_host_vm) { FactoryBot.create(:conversion_host, :resource => vm) } - let(:auth_authkey) { FactoryBot.create(:authentication_ssh_keypair, :resource => conversion_host_vm) } + let(:conversion_host_host) { FactoryBot.create(:conversion_host, :resource => host) } + + let(:auth_keypair) { FactoryBot.create(:authentication_ssh_keypair, :resource => conversion_host_vm) } + let(:auth_v2v) { FactoryBot.create(:authentication_v2v, :resource => conversion_host_host) } - it "finds associated authentications" do - expect(conversion_host_vm.authentications).to contain_exactly(auth_authkey) + it "finds associated ssh_keypair authentications" do + expect(conversion_host_vm.authentications).to contain_exactly(auth_keypair) + end + + it "finds associated v2v authentications" do + expect(conversion_host_host.authentications).to contain_exactly(auth_v2v) end it "allows a resource to add an authentication" do - auth_authkey2 = FactoryBot.create(:authentication_ssh_keypair) - conversion_host_vm.authentications << auth_authkey2 - expect(conversion_host_vm.authentications).to contain_exactly(auth_authkey, auth_authkey2) + auth_keypair2 = FactoryBot.create(:authentication_ssh_keypair) + conversion_host_vm.authentications << auth_keypair2 + expect(conversion_host_vm.authentications).to contain_exactly(auth_keypair, auth_keypair2) + end + end + + context "find_credentials" do + let(:auth_v2v) { FactoryBot.create(:authentication_v2v, :resource => conversion_host_vm) } + let(:ems_redhat) { FactoryBot.create(:ems_redhat, :zone => FactoryBot.create(:zone), :api_version => '4.2.4') } + let(:ems_openstack) { FactoryBot.create(:ems_openstack, :zone => FactoryBot.create(:zone)) } + let(:auth_default) { FactoryBot.create(:authentication) } + let(:auth_v2v) { FactoryBot.create(:authentication_v2v) } + + let(:host) { FactoryBot.create(:host_redhat, :ext_management_system => ems_redhat) } + let(:vm) { FactoryBot.create(:vm_openstack, :ext_management_system => ems_openstack) } + + let(:conversion_host_vm) { FactoryBot.create(:conversion_host, :resource => vm) } + let(:conversion_host_host) { FactoryBot.create(:conversion_host, :resource => host) } + + it "finds the v2v credentials as expected when associated directly with the conversion host" do + conversion_host_vm.authentications << auth_v2v + expect(conversion_host_vm.send(:find_credentials)).to eq(auth_v2v) + end + + it "finds the credentials associated with the resource if credentials cannot be found for the conversion host" do + vm.ext_management_system.authentications << auth_default + host.authentications << auth_default + expect(conversion_host_vm.send(:find_credentials)).to eq(auth_default) + expect(conversion_host_host.send(:find_credentials)).to eq(auth_default) end end