Skip to content

Commit

Permalink
Merge pull request #15245 from masayag/find_datastore_within_provider
Browse files Browse the repository at this point in the history
Select datastore by its association with the provider
(cherry picked from commit ec5d106)

https://bugzilla.redhat.com/show_bug.cgi?id=1462774
  • Loading branch information
gmcculloug authored and simaishi committed Jun 19, 2017
1 parent d0843bc commit 44b7dfe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/vm_or_template/operations/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def disconnect_floppies
def raw_add_disk(disk_name, disk_size_mb, options = {})
raise _("VM has no EMS, unable to add disk") unless ext_management_system
if options[:datastore]
datastore = Storage.find_by(:name => options[:datastore])
raise _("Data Store does not exist, unable to add disk") unless datastore
datastore = ext_management_system.hosts.collect do |h|
h.writable_storages.find_by(:name => options[:datastore])
end.uniq.compact.first
raise _("Datastore does not exist or cannot be accessed, unable to add disk") unless datastore
end

run_command_via_parent(:vm_add_disk, :diskName => disk_name, :diskSize => disk_size_mb,
Expand Down
59 changes: 59 additions & 0 deletions spec/models/vm_or_template/operations/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
describe VmOrTemplate::Operations::Configuration do
context "#raw_add_disk" do
let(:disk_name) { "abc" }
let(:disk_size) { "123" }

context "when ext_management_system does not exist" do
let(:vm) { FactoryGirl.create(:vm_or_template) }

it "raises an exception when does not find ext_management_system" do
message = "VM has no EMS, unable to add disk"
expect { vm.raw_add_disk(disk_name, disk_size, {}) }.to raise_error(message)
end
end

context "when ext_management_system exists" do
let(:vm) { FactoryGirl.create(:vm_or_template, :ext_management_system => ems) }
let(:ems) { FactoryGirl.create(:ext_management_system) }
let(:storage_name) { "test_storage" }
let(:storage) { FactoryGirl.create(:storage, :name => storage_name) }
let(:storages) { double("storages") }
let(:host) { double("host", :writable_storages => storages) }
let(:hosts) { [host] }

before do
allow(ems).to receive(:hosts).and_return(hosts)
end

context "when storage exists" do
it "adds a disk on the storage" do
allow(storages).to receive(:find_by).with(:name => storage_name).and_return(storage)
allow(ems).to receive(:authentication_status_ok?).and_return(true)
allow(ems).to receive(:vm_add_disk)

expected_options = {
:diskName => disk_name,
:diskSize => disk_size,
:thinProvisioned => nil,
:dependent => nil,
:persistent => nil,
:bootable => nil,
:datastore => storage,
:interface => nil
}
expect(vm).to receive(:run_command_via_parent).with(:vm_add_disk, expected_options).once
vm.raw_add_disk(disk_name, disk_size, :datastore => storage_name)
end
end

context "when storage does not exist" do
it "raises an exception when doesn't find storage by its name" do
allow(storages).to receive(:find_by).with(:name => storage_name).and_return(nil)

message = "Datastore does not exist or cannot be accessed, unable to add disk"
expect { vm.raw_add_disk(disk_name, disk_size, :datastore => storage_name) }.to raise_error(message)
end
end
end
end
end

0 comments on commit 44b7dfe

Please sign in to comment.