Skip to content

Commit

Permalink
Add best fit logic for transformations moving vms to openstack
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Aug 20, 2018
1 parent 5903757 commit 92fc0cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/models/transformation_mapping/cloud_best_fit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class TransformationMapping
class CloudBestFit
attr_reader :source_vm, :destination_manager
def initialize(source_vm, destination_manager)
@source_vm = source_vm
@destination_manager = destination_manager
end

def available_fit_flavors
@available_fit_flavors ||= destination_manager.flavors.where(
:cpus => source_vm.cpu_total_cores..Float::INFINITY,
:memory => (source_vm.hardware.memory_mb * 1.megabyte)..Float::INFINITY
).order(:cpus, :memory)
end

def best_fit_flavor
available_fit_flavors.first
end
end
end
37 changes: 37 additions & 0 deletions spec/models/transformation_mapping/cloud_best_fit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe TransformationMapping::CloudBestFit do
let(:ems) { FactoryGirl.create(:ems_openstack) }
let(:vm) { FactoryGirl.create(:vm_vmware, :hardware => vm_hardware) }
let(:vm_hardware) { FactoryGirl.create(:hardware, :cpu1x2, :ram1GB) }

subject { described_class.new(vm, ems) }

it "no flavors on the provider" do
expect(subject.available_fit_flavors).to match_array([])
expect(subject.best_fit_flavor).to be_nil
end

it "no flavors match" do
ems.flavors.create!(:cpus => 1, :memory => 512.megabytes)

expect(subject.available_fit_flavors).to match_array([])
expect(subject.best_fit_flavor).to be_nil
end

it "one flavor matches" do
ems.flavors.create!(:cpus => 2, :memory => 512.megabytes)
flavor = ems.flavors.create!(:cpus => 2, :memory => 1.gigabyte)

expect(subject.available_fit_flavors).to match_array([flavor])
expect(subject.best_fit_flavor).to eq(flavor)
end

it "multiple flavors match" do
ems.flavors.create!(:cpus => 2, :memory => 512.megabytes)
flavor1 = ems.flavors.create!(:cpus => 2, :memory => 1.gigabyte)
flavor2 = ems.flavors.create!(:cpus => 2, :memory => 2.gigabytes)
flavor3 = ems.flavors.create!(:cpus => 4, :memory => 1.gigabyte)

expect(subject.available_fit_flavors).to match_array([flavor1, flavor2, flavor3])
expect(subject.best_fit_flavor).to eq(flavor1)
end
end

0 comments on commit 92fc0cd

Please sign in to comment.