Skip to content
Open
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
5 changes: 5 additions & 0 deletions app/models/miq_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def self.base_model
MiqTemplate
end

# Don't tack on `type IN []` query for the top level classes (default scope already filters out the vms)
def self.finder_needs_type_condition?
self < base_model
end

def self.corresponding_model
module_parent::Vm
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def self.base_model
Vm
end

# Don't tack on `type IN []` query for the top level classes (default scope already filters out the templates)
def self.finder_needs_type_condition?
self < base_model
end

def self.corresponding_model
if self == Vm
MiqTemplate
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/vm_or_template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :vm_or_template do
factory :vm_or_template, :class => "ManageIQ::Providers::Vmware::InfraManager::Vm" do
sequence(:name) { |n| "vm_#{seq_padded_for_sorting(n)}" }
location { "unknown" }
uid_ems { SecureRandom.uuid }
Expand All @@ -12,13 +12,13 @@
end
end

factory :template, :class => "MiqTemplate", :parent => :vm_or_template do
factory :template, :class => "ManageIQ::Providers::Vmware::InfraManager::Template", :parent => :vm_or_template do
sequence(:name) { |n| "template_#{seq_padded_for_sorting(n)}" }
template { true }
raw_power_state { "never" }
end

factory(:vm, :class => "Vm", :parent => :vm_or_template)
factory(:vm, :class => "ManageIQ::Providers::Vmware::InfraManager::Vm", :parent => :vm_or_template)
factory(:vm_cloud, :class => "VmCloud", :parent => :vm) { cloud { true } }
factory(:vm_infra, :class => "VmInfra", :parent => :vm)
factory(:vm_server, :class => "VmServer", :parent => :vm)
Expand All @@ -38,7 +38,7 @@
vendor { "openstack" }
end

factory :miq_template do
factory :miq_template, :class => "ManageIQ::Providers::Vmware::InfraManager::Template" do
name { "ubuntu-16.04-stable" }
location { "Minneapolis, MN" }
vendor { "openstack" }
Expand Down
4 changes: 3 additions & 1 deletion spec/models/miq_report/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@
FactoryBot.create(:template) # filtered out by report.where_clause
vm = FactoryBot.create(:vm, :vendor => "redhat")

expect(vm.type).to match(/Vm/)

rpt = FactoryBot.create(
:miq_report,
:db => "VmOrTemplate",
:where_clause => ["vms.type = ?", "Vm"],
:where_clause => ["vms.type = ?", vm.type],
:col_order => %w[id name host.name vendor]
)
rpt.generate_table(:userid => @user.userid, :where_clause => {"vms.vendor" => "redhat"})
Expand Down
12 changes: 8 additions & 4 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,35 @@
end

context "with attrs template => true, ems_id => nil, host_id => nil" do
let(:attrs) { {:template => true, :ems_id => nil, :host_id => nil} }
subject { FactoryBot.create(:miq_template, attrs) }
let(:attrs) { {: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} }
subject { FactoryBot.create(:miq_template, attrs) }
let(:attrs) { {: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} }
subject { FactoryBot.create(:miq_template, attrs) }
let(:attrs) { {: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} }
subject { FactoryBot.create(:miq_template, attrs) }
let(:attrs) { {: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 }
Expand Down