Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select datastore by its association with the provider #15245

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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