Skip to content

Commit

Permalink
Merge pull request #13445 from petrblaho/openstack-infra-ssh-keypair-…
Browse files Browse the repository at this point in the history
…validation-fixes

Openstack infra ssh keypair validation fixes
  • Loading branch information
blomquisg authored Jan 13, 2017
2 parents e5090ae + 740d3b6 commit df1fcf1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/manageiq/providers/openstack/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def required_credential_fields(type)
end

def verify_ssh_keypair_credentials(_options)
hosts.sort_by(&:ems_cluster_id)
# Select one powered-on host in each cluster to verify
# ssh credentials against
hosts.select(&:ems_cluster_id)
.sort_by(&:ems_cluster_id)
.slice_when { |i, j| i.ems_cluster_id != j.ems_cluster_id }
.map { |c| c.find { |h| h.power_state == 'on' } }
.map { |c| c.find { |h| h.power_state == 'on' } }.compact
.all? { |h| h.verify_credentials('ssh_keypair') }
end
private :verify_ssh_keypair_credentials
Expand Down
1 change: 1 addition & 0 deletions spec/factories/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
vmm_vendor "unknown"
ems_ref "openstack-perf-host"
ems_ref_obj "openstack-perf-host-nova-instance"
association :ems_cluster, factory: :ems_cluster_openstack
end

factory :host_openstack_infra_compute, :parent => :host_openstack_infra,
Expand Down
30 changes: 30 additions & 0 deletions spec/models/manageiq/providers/openstack/infra_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@
end
end

context "verifying SSH keypair credentials" do
it "verifies Openstack SSH credentials successfully when all hosts report that the credentials are valid" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(true)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end

it "fails to verify Openstack SSH credentials when any hosts report that the credentials are invalid" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
host = FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_falsey
end

it "disregards powered off hosts when verifying Openstack SSH credentials" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "off")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end

it "disregards host with no ems_cluster" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on", :ems_cluster => nil)
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end
end

context "validation" do
before :each do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
Expand Down

0 comments on commit df1fcf1

Please sign in to comment.