Skip to content

Commit

Permalink
Merge pull request ManageIQ#282 from borod108/bug/vm_provisioning_sto…
Browse files Browse the repository at this point in the history
…rage_specs

Fix setting storage attributes during vm provision
  • Loading branch information
pkliczewski authored Aug 30, 2018
2 parents 0cd0b3a + e22fe21 commit fdc51df
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,45 @@ def create_vm(options)
vms_service = connection.system_service.vms_service
cluster = ovirt_services.cluster_from_href(options[:cluster], connection)
template = get
vm = build_vm_from_hash(:name => options[:name],
:template => template,
:cluster => cluster)
vms_service.add(vm, :clone => options[:clone_type] == :full)
clone = options[:clone_type] == :full
disk_attachments = clone ? build_disk_attachments(template, options[:sparse], options[:storage]) : nil
vm = build_vm_from_hash(:name => options[:name],
:template => template,
:cluster => cluster,
:disk_attachments => disk_attachments)
vms_service.add(vm, :clone => clone)
end

def build_disk_attachments(template, sparse, storage_href)
disk_attachments = connection.follow_link(template.disk_attachments)
apply_sparsity_on_disk_attachments(disk_attachments, sparse) unless sparse.nil?
apply_storage_domain_on_disk_attachments(disk_attachments, storage_href) unless storage_href.nil?
disk_attachments
end

def apply_storage_domain_on_disk_attachments(disk_attachments, storage_href)
return if storage_href.nil?
storage_domain = ovirt_services.storage_from_href(storage_href, connection)
disk_attachments.each do |disk_attachment|
disk_attachment.disk.storage_domains = [storage_domain]
end
end

def apply_sparsity_on_disk_attachments(disk_attachments, sparse)
return if sparse.nil?
disk_attachments.each do |disk_attachment|
disk_attachment.disk.sparse = sparse
end
end

def build_vm_from_hash(args)
OvirtSDK4::Vm.new(:name => args[:name],
:template => args[:template],
:cluster => args[:cluster])
vm_options = {
:name => args[:name],
:template => args[:template],
:cluster => args[:cluster],
:disk_attachments => args[:disk_attachments]
}.compact
OvirtSDK4::Vm.new(vm_options)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,72 @@
end
end
end

RSpec::Matchers.define :vm_with_properly_set_disk_attachments do |opts|
sparsity = opts[:sparse]
storage_domain_href = opts[:storage]
match do |actual|
actual.disk_attachments.inject(true) do |res, disk_attachment|
res &&= (disk_attachment.disk.sparse == sparsity)
res &&= all_storage_domains_match_href?(disk_attachment.disk, storage_domain_href) if storage_domain_href
res
end
end
end

RSpec::Matchers.define :vm_with_disk_attachments_not_set do
match { |actual| actual.disk_attachments.blank? }
end

def all_storage_domains_match_href?(disk, href)
disk.storage_domains.inject(true) { |res, storage_domain| res && storage_domain.href == href }
end

context "#start_clone" do
before do
@source_template = double("template")
storage_domain = OvirtSDK4::StorageDomain.new(:href => "/api/storagedomains/href")
storage_domain_old = OvirtSDK4::StorageDomain.new(:href => "/api/storagedomains/href_old")
disk = OvirtSDK4::Disk.new(:storage_domains => [storage_domain_old])
disk2 = OvirtSDK4::Disk.new(:storage_domains => [storage_domain_old])
@disk_attachments = [OvirtSDK4::DiskAttachment.new(:disk => disk), OvirtSDK4::DiskAttachment.new(:disk => disk2)]
@sdk_template = OvirtSDK4::Template.new(:disk_attachments => @disk_attachments)
@ems = FactoryGirl.create(:ems_redhat_with_authentication)
@ovirt_services = ManageIQ::Providers::Redhat::InfraManager::
OvirtServices::Strategies::V4.new(:ems => @ems)
connection = double(OvirtSDK4::Connection)
template_service = OvirtSDK4::TemplateService
cluster = OvirtSDK4::Cluster.new(:cluster => "/api/clusters/href")
system_services = OvirtSDK4::SystemService
@vms_service = double(OvirtSDK4::VmsService)
allow(connection).to receive(:system_service).and_return(system_services)
allow(system_services).to receive(:vms_service).and_return(@vms_service)
rhevm_template = ManageIQ::Providers::Redhat::InfraManager::
OvirtServices::Strategies::V4::TemplateProxyDecorator.new(template_service, connection, @ovirt_services)
allow(rhevm_template).to receive(:get).and_return(@sdk_template)
allow(@ovirt_services).to receive(:cluster_from_href).with("/api/clusters/href", connection).and_return(cluster)
allow(@ovirt_services).to receive(:storage_from_href).with("/api/storagedomains/href", connection).and_return(storage_domain)
allow(@source_template).to receive(:with_provider_object).and_yield(rhevm_template)
allow(connection).to receive(:follow_link).with(@sdk_template.disk_attachments).and_return(@disk_attachments)
allow(@ovirt_services).to receive(:populate_phase_context)
end

context "clone_type is full" do
let(:create_options) { { :sparse => true } }
it "requests to clone the vm as independant and sets the disk attachments attributes" do
opts = {:name => "provision_vm", :cluster => "/api/clusters/href", :clone_type => :full, :sparse => false, :storage => "/api/storagedomains/href"}
expect(@vms_service).to receive(:add).with(vm_with_properly_set_disk_attachments(opts), :clone => true)
@ovirt_services.start_clone(@source_template, opts, {})
end
end

context "clone_type is thin" do
it "requests to clone the vm as dependant and does not set the disk attachments attributes" do
opts = {:name => "provision_vm", :cluster => "/api/clusters/href", :clone_type => :thin, :sparse => true, :storage => "/api/storagedomains/href"}
expect(@vms_service).to receive(:add).with(vm_with_disk_attachments_not_set, :clone => false)
@ovirt_services.start_clone(@source_template, opts, {})
end
end
end
end
end

0 comments on commit fdc51df

Please sign in to comment.