Skip to content

Commit

Permalink
Merge pull request #17114 from oslegl/letChanges
Browse files Browse the repository at this point in the history
Let changes
  • Loading branch information
bdunne authored Mar 29, 2018
2 parents 84aae96 + ee5715d commit 265fba3
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions spec/models/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@
end

context "expression with <find>" do
let(:cluster) { FactoryGirl.create(:ems_cluster) }
let(:host1) { FactoryGirl.create(:host, :ems_cluster => cluster) }
let(:host2) { FactoryGirl.create(:host, :ems_cluster => cluster) }
before do
@cluster = FactoryGirl.create(:ems_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 = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> >= 2</check></find>"
expect(Condition.subst(expr, @cluster)).to be_truthy
expect(Condition.subst(expr, cluster)).to be_truthy
end

it "has_one support" do
Expand All @@ -66,50 +66,50 @@

it "invalid expression should not raise security error because it is now parsed and not evaluated" do
expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> >= 2; system('ls /etc')</check></find>"
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 = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> == 0</check></find>"
expect(Condition.subst(expr, @cluster)).to eq('false')
expect(Condition.subst(expr, cluster)).to eq('false')

expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> > 0</check></find>"
expect(Condition.subst(expr, @cluster)).to eq('true')
expect(Condition.subst(expr, cluster)).to eq('true')

expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> >= 0</check></find>"
expect(Condition.subst(expr, @cluster)).to eq('true')
expect(Condition.subst(expr, cluster)).to eq('true')

expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'true'</search><check mode=count><count> < 0</check></find>"
expect(Condition.subst(expr, @cluster)).to eq('false')
expect(Condition.subst(expr, cluster)).to eq('false')

expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> <= 0</check></find>"
expect(Condition.subst(expr, @cluster)).to eq('false')
expect(Condition.subst(expr, cluster)).to eq('false')

expr = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> != 0</check></find>"
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 = "<find><search><value ref=emscluster, type=boolean>/virtual/vms/active</value> == 'false'</search><check mode=count><count> !! 0</check></find>"
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

context "expression with <registry>" do
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')
@vm = FactoryGirl.create(:vm_vmware, :registry_items => [@reg_num, @reg_string])
end

it "string type registry key data is single quoted" do
expr = "<registry>#{@reg_string.name}</registry>"
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 = "<registry>#{@reg_num.name}</registry>"
expect(Condition.subst(expr, @vm)).to eq('"0"')
expr = "<registry>#{reg_num.name}</registry>"
expect(Condition.subst(expr, vm)).to eq('"0"')
end
end

Expand Down

0 comments on commit 265fba3

Please sign in to comment.