From 748f2e1e0ee041a6be5aae5a3ad0f863c5a02eb0 Mon Sep 17 00:00:00 2001 From: swi2 Date: Thu, 8 Mar 2018 11:57:37 +0100 Subject: [PATCH 1/4] changes in condition_spec.rb with multiple cluster variables into single one with let. --- spec/models/condition_spec.rb | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index 47082de439e..b5f639676aa 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -37,26 +37,27 @@ end context "expression with " do + let(:cluster) { FactoryGirl.create(:ems_cluster) } before do - @cluster = FactoryGirl.create(:ems_cluster) - @host1 = FactoryGirl.create(:host, :ems_cluster => @cluster, :name => "XXX") - @host2 = FactoryGirl.create(:host, :ems_cluster => @cluster) + + @host1 = FactoryGirl.create(:host, :ems_cluster => cluster, :name => "XXX") + @host2 = FactoryGirl.create(:host, :ems_cluster => cluster) @rp1 = FactoryGirl.create(:resource_pool) @rp2 = FactoryGirl.create(:resource_pool) - @cluster.with_relationship_type("ems_metadata") { @cluster.add_child @rp1 } + cluster.with_relationship_type("ems_metadata") { cluster.add_child @rp1 } @rp1.with_relationship_type("ems_metadata") { @rp1.add_child @rp2 } - @vm1 = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_cluster => @cluster) + @vm1 = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_cluster => cluster) @vm1.with_relationship_type("ems_metadata") { @vm1.parent = @rp1 } - @vm2 = FactoryGirl.create(:vm_vmware, :host => @host2, :ems_cluster => @cluster) + @vm2 = FactoryGirl.create(:vm_vmware, :host => @host2, :ems_cluster => cluster) @vm2.with_relationship_type("ems_metadata") { @vm2.parent = @rp2 } end it "valid expression" do expr = "/virtual/vms/active == 'false' >= 2" - expect(Condition.subst(expr, @cluster)).to be_truthy + expect(Condition.subst(expr, cluster)).to be_truthy end it "has_one support" do @@ -66,32 +67,32 @@ it "invalid expression should not raise security error because it is now parsed and not evaluated" do expr = "/virtual/vms/active == 'false' >= 2; system('ls /etc')" - expect { Condition.subst(expr, @cluster) }.not_to raise_error + expect { Condition.subst(expr, cluster) }.not_to raise_error end it "tests all allowed operators in find/check expression clause" do expr = "/virtual/vms/active == 'false' == 0" - expect(Condition.subst(expr, @cluster)).to eq('false') + expect(Condition.subst(expr, cluster)).to eq('false') expr = "/virtual/vms/active == 'false' > 0" - expect(Condition.subst(expr, @cluster)).to eq('true') + expect(Condition.subst(expr, cluster)).to eq('true') expr = "/virtual/vms/active == 'false' >= 0" - expect(Condition.subst(expr, @cluster)).to eq('true') + expect(Condition.subst(expr, cluster)).to eq('true') expr = "/virtual/vms/active == 'true' < 0" - expect(Condition.subst(expr, @cluster)).to eq('false') + expect(Condition.subst(expr, cluster)).to eq('false') expr = "/virtual/vms/active == 'false' <= 0" - expect(Condition.subst(expr, @cluster)).to eq('false') + expect(Condition.subst(expr, cluster)).to eq('false') expr = "/virtual/vms/active == 'false' != 0" - expect(Condition.subst(expr, @cluster)).to eq('true') + expect(Condition.subst(expr, cluster)).to eq('true') end it "rejects and expression with an illegal operator" do expr = "/virtual/vms/active == 'false' !! 0" - expect { expect(Condition.subst(expr, @cluster)).to eq('false') }.to raise_error(RuntimeError, "Illegal operator, '!!'") + expect { expect(Condition.subst(expr, cluster)).to eq('false') }.to raise_error(RuntimeError, "Illegal operator, '!!'") end end From 05d2eaa79faa4d48115eb97ac38683e50149dd7d Mon Sep 17 00:00:00 2001 From: swi2 Date: Thu, 8 Mar 2018 12:06:12 +0100 Subject: [PATCH 2/4] changes in condition_spec.rb with multiple vm variables into ones using let. --- spec/models/condition_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index b5f639676aa..fe1a76ecadc 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -97,20 +97,20 @@ end context "expression with " do + let(:vm) { FactoryGirl.create(:vm_vmware, :registry_items => [@reg_num, @reg_string]) } before do @reg_num = FactoryGirl.create(:registry_item, :name => "HKLM\\SOFTWARE\\WindowsFirewall : EnableFirewall", :data => 0) @reg_string = FactoryGirl.create(:registry_item, :name => "HKLM\\SOFTWARE\\Microsoft\\Ole : EnableDCOM", :data => 'y') - @vm = FactoryGirl.create(:vm_vmware, :registry_items => [@reg_num, @reg_string]) end it "string type registry key data is single quoted" do expr = "#{@reg_string.name}" - expect(Condition.subst(expr, @vm)).to eq('"y"') + expect(Condition.subst(expr, vm)).to eq('"y"') end it "numerical type registry key data is single quoted" do expr = "#{@reg_num.name}" - expect(Condition.subst(expr, @vm)).to eq('"0"') + expect(Condition.subst(expr, vm)).to eq('"0"') end end From 0fcdd24b8bf35fb1901e660228ade91cbd5c0ba1 Mon Sep 17 00:00:00 2001 From: swi2 Date: Thu, 15 Mar 2018 11:41:25 +0100 Subject: [PATCH 3/4] Added three more let in condition_spec.rb --- spec/models/condition_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index fe1a76ecadc..5fdcb9d5191 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -37,21 +37,21 @@ end context "expression with " do - let(:cluster) { FactoryGirl.create(:ems_cluster) } + let(:cluster) { FactoryGirl.create(:ems_cluster) } + let(:host1) { FactoryGirl.create(:host, :ems_cluster => cluster, :name => "XXX") } + let(:host2) { FactoryGirl.create(:host, :ems_cluster => cluster) } before do - @host1 = FactoryGirl.create(:host, :ems_cluster => cluster, :name => "XXX") - @host2 = FactoryGirl.create(:host, :ems_cluster => cluster) @rp1 = FactoryGirl.create(:resource_pool) @rp2 = FactoryGirl.create(:resource_pool) cluster.with_relationship_type("ems_metadata") { cluster.add_child @rp1 } @rp1.with_relationship_type("ems_metadata") { @rp1.add_child @rp2 } - @vm1 = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_cluster => cluster) + @vm1 = FactoryGirl.create(:vm_vmware, :host => host1, :ems_cluster => cluster) @vm1.with_relationship_type("ems_metadata") { @vm1.parent = @rp1 } - @vm2 = FactoryGirl.create(:vm_vmware, :host => @host2, :ems_cluster => cluster) + @vm2 = FactoryGirl.create(:vm_vmware, :host => host2, :ems_cluster => cluster) @vm2.with_relationship_type("ems_metadata") { @vm2.parent = @rp2 } end @@ -97,9 +97,9 @@ end context "expression with " do - let(:vm) { FactoryGirl.create(:vm_vmware, :registry_items => [@reg_num, @reg_string]) } + let(:vm) { FactoryGirl.create(:vm_vmware, :registry_items => [reg_num, @reg_string]) } + let(:reg_num) { FactoryGirl.create(:registry_item, :name => "HKLM\\SOFTWARE\\WindowsFirewall : EnableFirewall", :data => 0) } before do - @reg_num = FactoryGirl.create(:registry_item, :name => "HKLM\\SOFTWARE\\WindowsFirewall : EnableFirewall", :data => 0) @reg_string = FactoryGirl.create(:registry_item, :name => "HKLM\\SOFTWARE\\Microsoft\\Ole : EnableDCOM", :data => 'y') end @@ -109,7 +109,7 @@ end it "numerical type registry key data is single quoted" do - expr = "#{@reg_num.name}" + expr = "#{reg_num.name}" expect(Condition.subst(expr, vm)).to eq('"0"') end end From ee5715d01e85ec3ae7de8a090350e318a3f3896a Mon Sep 17 00:00:00 2001 From: swi2 Date: Thu, 29 Mar 2018 12:14:38 +0200 Subject: [PATCH 4/4] removed :name as an arbitrary value --- spec/models/condition_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index 5fdcb9d5191..464e5035880 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -38,10 +38,9 @@ context "expression with " do let(:cluster) { FactoryGirl.create(:ems_cluster) } - let(:host1) { FactoryGirl.create(:host, :ems_cluster => cluster, :name => "XXX") } + let(:host1) { FactoryGirl.create(:host, :ems_cluster => cluster) } let(:host2) { FactoryGirl.create(:host, :ems_cluster => cluster) } before do - @rp1 = FactoryGirl.create(:resource_pool) @rp2 = FactoryGirl.create(:resource_pool)