Skip to content

Commit

Permalink
Merge pull request #16509 from NickLaMuro/fix_vm_or_template_registred
Browse files Browse the repository at this point in the history
Fix VmOrTemplate#registered? and add scope forms
  • Loading branch information
blomquisg authored Jan 30, 2018
2 parents caedee5 + bd81591 commit 7411f9a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ class VmOrTemplate < ApplicationRecord
scope :orphaned, -> { where(:ems_id => nil).where.not(:storage_id => nil) }
scope :with_ems, -> { where.not(:ems_id => nil) }

# The SQL form of `#registered?`, with it's inverse as well.
# TODO: Vmware Specific (copied (old) TODO from #registered?)
scope :registered, (lambda do
where(arel_table[:template].eq(false).or(arel_table[:ems_id].not_eq(nil)).and(arel_table[:host_id].not_eq(nil)))
end)
scope :unregistered, (lambda do
where(arel_table[:template].eq(true).and(arel_table[:ems_id].eq(nil)).or(arel_table[:host_id].eq(nil)))
end)

alias_method :datastores, :storages # Used by web-services to return datastores as the property name

Expand Down Expand Up @@ -302,7 +310,7 @@ def is_evm_appliance?
# Determines if the VM is on an EMS or Host
def registered?
# TODO: Vmware specific
return false if self.template? && ext_management_system_id.nil?
return false if template? && ems_id.nil?
host_id.present?
end

Expand Down
77 changes: 77 additions & 0 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,83 @@
let(:ems) { FactoryGirl.create(:ext_management_system) }
let(:storage) { FactoryGirl.create(:storage) }

# Basically these specs are a truth table for the #registered? method, but
# need it to verify functionality when converting these to scopes
describe "being registered" do
subject { FactoryGirl.create(:vm_or_template, attrs) }
let(:host) { FactoryGirl.create(:host) }
let(:registered_vms) { described_class.registered.to_a }
let(:unregistered_vms) { described_class.unregistered.to_a }

# Preloads subject so that the registered_vms and unregistered_vms specs
# have it available to query against.
before { subject }

context "with attrs of template => false, ems_id => nil, host_id => nil" do
let(:attrs) { { :template => false, :ems_id => nil, :host_id => nil } }

it("is not #registered?") { expect(subject.registered?).to be false }
it("is not in registered_vms") { expect(registered_vms).to_not include subject }
it("is in unregistered_vms") { expect(unregistered_vms).to include subject }
end

context "with attrs template => false, ems_id => nil, host_id => [ID]" do
let(:attrs) { { :template => false, :ems_id => nil, :host_id => host.id } }

it("is #registered?") { expect(subject.registered?).to be true }
it("is in registered_vms") { expect(registered_vms).to include subject }
it("is not in unregistered_vms") { expect(unregistered_vms).to_not include subject }
end

context "with attrs template => false, ems_id => [ID], host_id => nil" do
let(:attrs) { { :template => false, :ems_id => ems.id, :host_id => nil } }

it("is not #registered?") { expect(subject.registered?).to be false }
it("is not in registered_vms") { expect(registered_vms).to_not include subject }
it("is in unregistered_vms") { expect(unregistered_vms).to include subject }
end

context "with attrs template => false, ems_id => [ID], host_id => [ID]" do
let(:attrs) { { :template => false, :ems_id => ems.id, :host_id => host.id } }

it("is #registered?") { expect(subject.registered?).to be true }
it("is in registered_vms") { expect(registered_vms).to include subject }
it("is not in unregistered_vms") { expect(unregistered_vms).to_not include subject }
end

context "with attrs template => true, ems_id => nil, host_id => nil" do
let(:attrs) { { :template => true, :ems_id => nil, :host_id => nil } }

it("is not #registered?") { expect(subject.registered?).to be false }
it("is not in registered_vms") { expect(registered_vms).to_not include subject }
it("is in unregistered_vms") { expect(unregistered_vms).to include subject }
end

context "with attrs if template => true, ems_id => nil, host_id => [ID]" do
let(:attrs) { { :template => true, :ems_id => nil, :host_id => host.id } }

it("is not #registered?") { expect(subject.registered?).to be false }
it("is not in registered_vms") { expect(registered_vms).to_not include subject }
it("is in unregistered_vms") { expect(unregistered_vms).to include subject }
end

context "with attrs if template => true, ems_id => [ID], host_id => nil" do
let(:attrs) { { :template => true, :ems_id => ems.id, :host_id => nil } }

it("is not #registered?") { expect(subject.registered?).to be false }
it("is not in registered_vms") { expect(registered_vms).to_not include subject }
it("is in unregistered_vms") { expect(unregistered_vms).to include subject }
end

context "with attrs if template => true, ems_id => [ID], host_id => [ID]" do
let(:attrs) { { :template => true, :ems_id => ems.id, :host_id => host.id } }

it("is #registered?") { expect(subject.registered?).to be true }
it("is in registered_vms") { expect(registered_vms).to include subject }
it("is not in unregistered_vms") { expect(unregistered_vms).to_not include subject }
end
end

context ".event_by_property" do
context "should add an EMS event" do
before(:each) do
Expand Down

0 comments on commit 7411f9a

Please sign in to comment.