From e4269ae23c6fae6bf466d0a7cdfec48a992c2f19 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Mon, 4 Nov 2024 08:30:13 -0500 Subject: [PATCH] Improve allowed_storages filter spec The previous spec did not excercise the fix because the created objects' ems_metadata tree did not match the `belongsto_filter` so the `MiqFilter.belongsto2object_list` returned an empty array which bypassed the `MiqPreloader.preload_and_map(sources, :storages)` call which caused the exception. Now without the fix in EmsFolder to add the `virtual_has_many` this spec will raise the same exception we observed during provisioning. --- spec/models/miq_request_workflow_spec.rb | 30 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/spec/models/miq_request_workflow_spec.rb b/spec/models/miq_request_workflow_spec.rb index d093d63c5bd..2c6f6049e8b 100644 --- a/spec/models/miq_request_workflow_spec.rb +++ b/spec/models/miq_request_workflow_spec.rb @@ -350,19 +350,39 @@ end context "with a user with a belongs_to_filter" do + let(:root_folder) { FactoryBot.create(:ems_folder, :ems_id => ems.id).tap { |f| f.add_parent(ems) } } + let(:datacenter) { FactoryBot.create(:vmware_datacenter, :ems_id => ems.id).tap { |d| d.add_parent(root_folder) } } + let(:storage) { FactoryBot.create(:storage, :ems_id => ems.id).tap { |s| s.add_parent(datacenter) } } + before do group = workflow.requester.miq_groups.first group.entitlement = Entitlement.new group.entitlement.set_managed_filters([]) - group.entitlement.set_belongsto_filters(["/belongsto/ExtManagementSystem|#{ems.name}/EmsFolder|Datacenters"]) + group.entitlement.set_belongsto_filters(belongsto_filter) group.save! end - it "filters out storages" do - allow(workflow).to receive(:get_source_and_targets).and_return(:ems => workflow.ci_to_hash_struct(ems)) - allow(workflow).to receive(:allowed_hosts_obj).and_return([host]) + context "with a filter that doesn't include the storage" do + let(:datacenter2) { FactoryBot.create(:vmware_datacenter, :ems_id => ems.id).tap { |f| f.add_parent(root_folder) } } + let(:belongsto_filter) { ["/belongsto/ExtManagementSystem|#{ems.name}/EmsFolder|#{root_folder.name}/EmsFolder|#{datacenter2.name}"] } + + it "filters out storages" do + allow(workflow).to receive(:get_source_and_targets).and_return(:ems => workflow.ci_to_hash_struct(ems)) + allow(workflow).to receive(:allowed_hosts_obj).and_return([host]) + + expect(workflow.allowed_storages.map(&:id)).to be_empty + end + end + + context "with a filter that includes the storage" do + let(:belongsto_filter) { ["/belongsto/ExtManagementSystem|#{ems.name}/EmsFolder|#{root_folder.name}/EmsFolder|#{datacenter.name}/Storage|#{storage.name}"] } - expect(workflow.allowed_storages.map(&:id)).to be_empty + it "returns the storage" do + allow(workflow).to receive(:get_source_and_targets).and_return(:ems => workflow.ci_to_hash_struct(ems)) + allow(workflow).to receive(:allowed_hosts_obj).and_return([host]) + + expect(workflow.allowed_storages.map(&:id)).to eq([storage.id]) + end end end end