From: Martyn Taylor mtaylor@redhat.com
Updated the HWP methods for matching Aoelus HWP to Provider HWPs. This change now calculate the matching range and enum over laps as well as the matching HWP. It also determines which hwp CONDOR should use, by the added match_hwp(hwp) method.
Logic for choosing a matched HWP and for choosing non_default values is very basic in this patch, at current it just returns the first in the list. --- src/app/models/hardware_profile.rb | 95 +++++++++-- src/spec/models/hardware_profile_spec.rb | 259 +++++++++++++++++++----------- 2 files changed, 250 insertions(+), 104 deletions(-)
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb index 39e0b3d..d7ae995 100644 --- a/src/app/models/hardware_profile.rb +++ b/src/app/models/hardware_profile.rb @@ -102,21 +102,61 @@ class HardwareProfile < ActiveRecord::Base the_property end
+ #TODO: This function returns the first hwp in the list of matched hardware profiles + # Better logic should be used here to decide which hardware profile to return. + def self.match_hwp(hwp) + hwps = matching_hwps(hwp) + if hwps.empty? + return nil + end + return hwps[0] + end + def self.matching_hwps(hwp) provider_hwps = HardwareProfile.all(:conditions => 'provider_id IS NOT NULL') - return provider_hwps.select { |phwp| check_properties(hwp, phwp) } + match_maps = [] + provider_hwps.each do |phwp| + match_map = check_properties(hwp, phwp) + if match_map + match_maps << match_map + end + end + return match_maps end
+ #TODO: This function returns the first value in hwpp list, better logic is required for for choosing a more appropriate match private + def self.set_non_default_value(hwpp) + case hwpp.kind + when 'range' + hwpp.value = hwpp.range_first + when 'enum' + hwpp.value = hwpp.property_enum_entries[0] + when 'fixed' + hwpp.value = hwpp.value + end + hwpp.save + end + def self.check_properties(hwp1, hwp2) if [hwp1.memory, hwp1.cpu, hwp1.storage, hwp1.architecture, hwp2.memory, hwp2.cpu, hwp2.storage, hwp2.architecture].include?(nil) - return false + return nil end
- check_hwp_property(hwp1.memory, hwp2.memory) && - check_hwp_property(hwp1.cpu, hwp2.cpu) && - check_hwp_property(hwp1.storage, hwp2.storage) && - hwp1.architecture.value == hwp2.architecture.value + hwpp_mem = check_hwp_property(hwp1.memory, hwp2.memory) + hwpp_cpu = check_hwp_property(hwp1.cpu, hwp2.cpu) + hwpp_storage = check_hwp_property(hwp1.storage, hwp2.storage) + hwpp_arch = check_hwp_property(hwp1.architecture, hwp2.architecture) + hwpps = [hwpp_mem, hwpp_cpu, hwpp_storage, hwpp_arch] + + if hwpps.include?(nil) + return nil + else + hwpps.each do |hwpp| + set_non_default_value(hwpp) + end + return { :memory => hwpp_mem, :cpu => hwpp_cpu, :storage => hwpp_storage, :architecture => hwpp_arch, :hardware_profile => hwp2} + end end
def self.check_hwp_property(p1, p2) @@ -125,25 +165,51 @@ class HardwareProfile < ActiveRecord::Base elsif p2.kind == 'range' calculate_range_match(p2, p1) else - return !(create_array_from_property(p1) & create_array_from_property(p2)).empty? + matched_values = (create_array_from_property(p1) & create_array_from_property(p2)) + if !matched_values.empty? + if p1.kind == 'fixed' || p2.kind == 'fixed' + HardwareProfileProperty.new(:kind => 'fixed', :value => matched_values[0]) + else + hwpp = HardwareProfileProperty.new(:kind => 'enum') + matched_values.each do |enum_value| + hwpp.property_enum_entries << PropertyEnumEntry.new(:hardware_profile_property => hwpp, :value => enum_value) + end + return hwpp + end + else + return nil + end end end
def self.calculate_range_match(p1, p2) case p2.kind when 'range' - return !(p1.range_first.to_f > p2.range_last.to_f || p1.range_last.to_f < p2.range_first.to_f) + if !(BigDecimal.new(p1.range_first) > BigDecimal.new(p2.range_last) || BigDecimal.new(p1.range_last) < BigDecimal.new(p2.range_first)) + hwpp = HardwareProfileProperty.new(:kind => 'range') + + hwpp.range_first = BigDecimal.new(p1.range_first) >= BigDecimal.new(p2.range_first) ? p1.range_first : p2.range_first + hwpp.range_last = BigDecimal.new(p1.range_last) <= BigDecimal.new(p2.range_last) ? p1.range_last : p2.range_last + + return hwpp + else + return nil + end
when 'enum' + hwpp = HardwareProfileProperty.new(:kind => 'enum') p2.property_enum_entries.each do |enum| - if (p1.range_first.to_f..p1.range_last.to_f) === enum.value.to_f - return true + if (BigDecimal.new(p1.range_first)..BigDecimal.new(p1.range_last)) === BigDecimal.new(enum.value) + hwpp.property_enum_entries << PropertyEnumEntry.new(:hardware_profile_property => hwpp, :value => enum.value) end end - return false + return hwpp.property_enum_entries.empty? ? nil : hwpp
when 'fixed' - return (p1.range_first.to_f..p1.range_last.to_f) === p2.value.to_f + return (BigDecimal.new(p1.range_first)..BigDecimal.new(p1.range_last)) === BigDecimal.new(p2.value) ? HardwareProfileProperty.new(:kind => 'fixed', :value => p2.value) : nil + + else + return nil
end end @@ -151,10 +217,11 @@ class HardwareProfile < ActiveRecord::Base def self.create_array_from_property(p) case p.kind when 'fixed' - return [p.value.to_f] + return [p.value]
when 'enum' - return p.property_enum_entries.map { |enum| enum.value.to_f } + return p.property_enum_entries.map { |enum| enum.value } end end + end diff --git a/src/spec/models/hardware_profile_spec.rb b/src/spec/models/hardware_profile_spec.rb index ea7ed1e..a3e75e7 100644 --- a/src/spec/models/hardware_profile_spec.rb +++ b/src/spec/models/hardware_profile_spec.rb @@ -88,36 +88,40 @@ describe HardwareProfile do
it "should calculate all the correct matches of provider hardware profiles against a given hardware profile" do provider = Factory(:mock_provider) + HardwareProfile.find(:all, :conditions => {:provider_id => provider.id}).each do |hwp| + hwp.destroy + end
# hwpp memory - hwpp_mem_match_all = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 1, :range_last => 4096, :value => 256) - hwpp_mem_match_none = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 8192) - hwpp_mem_match_2 = create_hwpp_enum([256, 1024], {:name => 'memory', :unit => 'MB'}) + hwpp_mem_match_all = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '1', :range_last => '4096', :value => '256') + hwpp_mem_match_none = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '8192') + hwpp_mem_match_2 = create_hwpp_enum(['256', '1024'], {:name => 'memory', :unit => 'MB'})
- hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 256, :range_last => 512, :value => 256) - hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 4096) - hwpp_mem_enum = create_hwpp_enum([1024, 3072, 4096], {:name => 'memory', :unit => 'MB'}) + hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '256', :range_last => '512', :value => '256') + hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '4096') + hwpp_mem_enum = create_hwpp_enum(['1024', '3072', '4096'], {:name => 'memory', :unit => 'MB'})
# hwpp cpu - hwpp_cpu_match_all = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 32, :value => 2) - hwpp_cpu_match_none = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 64) - hwpp_cpu_match_2 = create_hwpp_enum([8, 16], {:name => 'cpu', :unit => 'count'}) + hwpp_cpu_match_all = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '32', :value => '2') + hwpp_cpu_match_none = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '64') + hwpp_cpu_match_2 = create_hwpp_enum(['8', '16'], {:name => 'cpu', :unit => 'count'})
- hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 16, :value => 4) - hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 32) - hwpp_cpu_enum = create_hwpp_enum([16, 32], {:name => 'cpu', :unit => 'count'}) + hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '16', :value => '4') + hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '32') + hwpp_cpu_enum = create_hwpp_enum(['16', '32'], {:name => 'cpu', :unit => 'count'})
# hwpp storage - hwpp_storage_match_all = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 4000, :value => 250) - hwpp_storage_match_none = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 4000) - hwpp_storage_match_2 = create_hwpp_enum([1000, 2000], {:name => 'storage', :unit => 'GB'}) + hwpp_storage_match_all = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '4000', :value => '250') + hwpp_storage_match_none = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '4000') + hwpp_storage_match_2 = create_hwpp_enum(['1000', '2000'], {:name => 'storage', :unit => 'GB'})
- hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 1000, :value => 250) - hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 3000) - hwpp_storage_enum = create_hwpp_enum([2000, 4000], {:name => 'storage', :unit => 'GB'}) + hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '1000', :value => '250') + hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '3000') + hwpp_storage_enum = create_hwpp_enum(['2000', '4000'], {:name => 'storage', :unit => 'GB'})
# hwpp arch hwpp_arch_i386 = Factory(:hwpp_arch, :value => 'i386') + hwpp_arch_i386_enum = create_hwpp_enum(['i386', 'x86_64'], {:name => 'architecture', :unit => 'label'})
hwp_match_all = Factory(:hardware_profile, :memory => hwpp_mem_match_all, :cpu => hwpp_cpu_match_all, @@ -149,7 +153,7 @@ describe HardwareProfile do hwp3 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => hwpp_storage_enum, - :architecture => hwpp_arch_i386, + :architecture => hwpp_arch_i386_enum, :provider => provider)
hwp4 = Factory(:hardware_profile, :memory => hwpp_mem_enum, @@ -158,34 +162,34 @@ describe HardwareProfile do :architecture => hwpp_arch_i386, :provider => provider)
- hwps = [hwp1, hwp2, hwp3, hwp4] - (HardwareProfile.matching_hwps(hwp_match_all) & hwps).should == [hwp1, hwp2, hwp3] - (HardwareProfile.matching_hwps(hwp_match_none) & hwps).should == [] - (HardwareProfile.matching_hwps(hwp_match_2) & hwps).should == [hwp1, hwp3] + HardwareProfile.matching_hwps(hwp_match_all).size.should == 3 + HardwareProfile.matching_hwps(hwp_match_none).empty?.should == true + HardwareProfile.matching_hwps(hwp_match_2).size.should == 2 + end
it "should calculate the correct array for hardware profile properties of kind: 'fixed' and 'enum'" do - hwp_fixed = Factory(:hwpp_fixed, :value => 256) + hwp_fixed = Factory(:hwpp_fixed, :value => '256')
- enum_array = [256.0, 512.0, 1024.0, 2048.0] + enum_array = ['256', '512', '1024', '2048'] hwp_enum = create_hwpp_enum(enum_array)
- HardwareProfile.create_array_from_property(hwp_fixed).should == [256.0] + HardwareProfile.create_array_from_property(hwp_fixed).should == ['256'] (HardwareProfile.create_array_from_property(hwp_enum) & enum_array).should == enum_array end
it "should determine match for 2 hardware profiles" do - hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 256, :range_last => 512, :value => 256) - hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 1024) - hwpp_mem_enum = create_hwpp_enum([2048, 3072, 4096], {:name => 'memory', :unit => 'MB'}) + hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '256', :range_last => '512', :value => '256') + hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '1024') + hwpp_mem_enum = create_hwpp_enum(['2048', '3072', '4096'], {:name => 'memory', :unit => 'MB'})
- hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 4, :value => 2) - hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 8) - hwpp_cpu_enum = create_hwpp_enum([16, 32], {:name => 'cpu', :unit => 'count'}) + hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '4', :value => '2') + hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '8') + hwpp_cpu_enum = create_hwpp_enum(['16', '32'], {:name => 'cpu', :unit => 'count'})
- hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 500, :value => 250) - hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 1000) - hwpp_storage_enum = create_hwpp_enum([2000, 4000], {:name => 'storage', :unit => 'GB'}) + hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '500', :value => '250') + hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '1000') + hwpp_storage_enum = create_hwpp_enum(['2000', '4000'], {:name => 'storage', :unit => 'GB'})
hwpp_arch_i386 = Factory(:hwpp_arch, :value => 'i386') hwpp_arch_x86_64 = Factory(:hwpp_arch, :value => 'x86_64') @@ -196,79 +200,147 @@ describe HardwareProfile do hwp4 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => hwpp_storage_enum, :architecture => hwpp_arch_x86_64) hwp5 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => nil, :architecture => hwpp_arch_x86_64)
- HardwareProfile.check_properties(hwp1, hwp1).should == true - HardwareProfile.check_properties(hwp2, hwp2).should == true - HardwareProfile.check_properties(hwp3, hwp3).should == true + # Check memory match + match_map = HardwareProfile.check_properties(hwp1, hwp1) + match_map[:memory].kind.should == 'range' + match_map[:memory].range_first.should == '256' + match_map[:memory].range_last.should == '512'
- HardwareProfile.check_properties(hwp1, hwp2).should == false - HardwareProfile.check_properties(hwp2, hwp3).should == false - HardwareProfile.check_properties(hwp3, hwp4).should == false - HardwareProfile.check_properties(hwp4, hwp5).should == false - end + match_map[:cpu].kind.should == 'range' + match_map[:cpu].range_first.should == '1' + match_map[:cpu].range_last.should == '4'
- it "should calculate matches for range on hardware profile properties" do - hwp_range = Factory(:hwpp_range, :range_first => 256, :range_last => 512, :value => 256) + match_map[:storage].kind.should == 'range' + match_map[:storage].range_first.should == '100' + match_map[:storage].range_last.should == '500'
- hwp_range_match = Factory(:hwpp_range, :range_first => 512, :range_last => 1024, :value => 512) - hwp_range_fail = Factory(:hwpp_range, :range_first => 2048, :range_last => 2048, :value => 8192) + # Check fixed match + match_map = HardwareProfile.check_properties(hwp2, hwp2) + match_map[:memory].kind.should == 'fixed' + match_map[:memory].value.should == '1024'
- hwp_fixed_match = Factory(:hwpp_fixed, :value => 256) - hwp_fixed_fail = Factory(:hwpp_fixed, :value => 4096) + match_map[:cpu].kind.should == 'fixed' + match_map[:cpu].value.should == '8'
- hwp_enum_match = create_hwpp_enum([256, 512, 1024, 2048]) - hwp_enum_fail = create_hwpp_enum([2048, 4096, 8192, 16384]) + match_map[:storage].kind.should == 'fixed' + match_map[:storage].value.should == '1000'
- [hwp_range_match, hwp_fixed_match, hwp_enum_match].each do |hwpp| - HardwareProfile.calculate_range_match(hwp_range, hwpp).should == true - end + # Check enums match + match_map = HardwareProfile.check_properties(hwp3, hwp3) + match_map[:memory].kind.should == 'enum' + check_enum_entries_match(match_map[:memory], ['2048', '3072', '4096'])
- [hwp_range_fail, hwp_fixed_fail, hwp_enum_fail].each do |hwpp| - HardwareProfile.calculate_range_match(hwp_range, hwpp).should == false - end - end + match_map[:cpu].kind.should == 'enum' + check_enum_entries_match(match_map[:cpu], ['16', '32'])
- it "should calculate correct matches for each hwp property" do - hwp_range1 = Factory(:hwpp_range, :range_first => 256, :range_last => 512, :value => 512) - hwp_range2 = Factory(:hwpp_range, :range_first => 512, :range_last => 1024, :value => 768) - hwp_range3 = Factory(:hwpp_range, :range_first => 2048, :range_last => 4096, :value => 3072) + match_map[:storage].kind.should == 'enum' + check_enum_entries_match(match_map[:storage], ['2000', '4000'])
- hwp_fixed1 = Factory(:hwpp_fixed, :value => 256) - hwp_fixed2 = Factory(:hwpp_fixed, :value => 4096) - hwp_fixed3 = Factory(:hwpp_fixed, :value => 8192) - - hwp_enum1 = create_hwpp_enum([256, 512, 1024]) - hwp_enum2 = create_hwpp_enum([1024, 2048, 3072]) - hwp_enum3 = create_hwpp_enum([8192, 16384, 32768]) + HardwareProfile.check_properties(hwp1, hwp2).should == nil + HardwareProfile.check_properties(hwp2, hwp3).should == nil + HardwareProfile.check_properties(hwp3, hwp4).should == nil + HardwareProfile.check_properties(hwp4, hwp5).should == nil + end
- # Test HWPP Againsts Ranges - HardwareProfile.check_hwp_property(hwp_range1, hwp_range2).should == true - HardwareProfile.check_hwp_property(hwp_range1, hwp_range3).should == false + it "should calculate matches for range on hardware profile properties" do + hwp_range = Factory(:hwpp_range, :range_first => '256', :range_last => '512', :value => '256')
- HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed1).should == true - HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed3).should == false + hwp_range_match = Factory(:hwpp_range, :range_first => '512', :range_last => '1024', :value => '512') + hwp_range_fail = Factory(:hwpp_range, :range_first => '2048', :range_last => '2048', :value => '8192')
- HardwareProfile.check_hwp_property(hwp_range1, hwp_enum1).should == true - HardwareProfile.check_hwp_property(hwp_range1, hwp_enum3).should == false + hwp_fixed_match = Factory(:hwpp_fixed, :value => '256') + hwp_fixed_fail = Factory(:hwpp_fixed, :value => '4096')
- # Test HWPP Against Fixed - HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range1).should == true - HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range3).should == false + hwp_enum_match = create_hwpp_enum(['256', '512', '1024', '2048']) + hwp_enum_fail = create_hwpp_enum(['2048', '4096', '8192', '16384'])
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed1).should == true - HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed2).should == false + hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_range_match) + hwpp.kind.should == 'range' + hwpp.range_first.should == '512' + hwpp.range_last.should == '512'
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum1).should == true - HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum3).should == false + hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_fixed_match) + hwpp.kind.should == 'fixed' + hwpp.value.should == '256'
- # Test HWPP Aginsts Enums - HardwareProfile.check_hwp_property(hwp_enum1, hwp_range1).should == true - HardwareProfile.check_hwp_property(hwp_enum1, hwp_range3).should == false + hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_enum_match) + check_enum_entries_match(hwpp, ['256', '512'])
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed1).should == true - HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed2).should == false + [hwp_range_fail, hwp_fixed_fail, hwp_enum_fail].each do |hwpp| + HardwareProfile.calculate_range_match(hwp_range, hwpp).should == nil + end + end
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum2).should == true - HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum3).should == false + it "should calculate correct matches for each hwp property" do + hwp_range1 = Factory(:hwpp_range, :range_first => '256', :range_last => '512', :value => '512') + hwp_range2 = Factory(:hwpp_range, :range_first => '512', :range_last => '1024', :value => '768') + hwp_range3 = Factory(:hwpp_range, :range_first => '2048', :range_last => '4096', :value => '3072') + + hwp_fixed1 = Factory(:hwpp_fixed, :value => '256') + hwp_fixed2 = Factory(:hwpp_fixed, :value => '4096') + hwp_fixed3 = Factory(:hwpp_fixed, :value => '8192') + + hwp_enum1 = create_hwpp_enum(['256', '512', '1024']) + hwp_enum2 = create_hwpp_enum(['1024', '2048', '3072']) + hwp_enum3 = create_hwpp_enum(['8192', '16384', '32768']) + + # == Test HWPP against Ranges == + # -- Expected Matches + hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_range2) + hwpp.kind.should == 'range' + hwpp.range_first.should == '512' + hwpp.range_last.should == '512' + + hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed1) + hwpp.kind.should == 'fixed' + hwpp.value.should == '256' + + hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_enum1) + hwpp.kind.should == 'enum' + check_enum_entries_match(hwpp, ['256', '512']) + + # -- Expected Failures + HardwareProfile.check_hwp_property(hwp_range1, hwp_enum3).should == nil + HardwareProfile.check_hwp_property(hwp_range1, hwp_range3).should == nil + HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed3).should == nil + + # == Test HWPP Against Fixed == + # -- Expected Matches + hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range1) + hwpp.kind.should == 'fixed' + hwpp.value.should == '256' + + hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed1) + hwpp.kind.should == 'fixed' + hwpp.value.should == '256' + + hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum1) + hwpp.kind.should == 'fixed' + hwpp.value == '512' + + # -- Expected Failures + HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range3).should == nil + HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed2).should == nil + HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum3).should == nil + + # == Test HWPP Aginsts Enums == + # --Expected Matches + hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_range1) + hwpp.kind.should == 'enum' + check_enum_entries_match(hwpp, ['256', '512']) + + hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed1) + hwpp.kind.should == 'fixed' + hwpp.value.should == '256' + + hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum2) + hwpp.kind.should == 'enum' + check_enum_entries_match(hwpp, ['1024']) + + # -- Expected Failures + HardwareProfile.check_hwp_property(hwp_enum1, hwp_range3).should == nil + HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed2).should == nil + HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum3).should == nil end
def create_hwpp_enum(value_array, properties = {}) @@ -279,4 +351,11 @@ describe HardwareProfile do return hwpp_enum end
+ def check_enum_entries_match(hwpp, value_array) + enum_set = [] + hwpp.property_enum_entries.each do |enum| + enum_set << enum.value + end + enum_set.should == value_array + end end
ACK with a small suggestion inline.
On 12/13/2010 01:35 PM, mtaylor@redhat.com wrote:
From: Martyn Taylormtaylor@redhat.com
Updated the HWP methods for matching Aoelus HWP to Provider HWPs. This change now calculate the matching range and enum over laps as well as the matching HWP. It also determines which hwp CONDOR should use, by the added match_hwp(hwp) method.
Logic for choosing a matched HWP and for choosing non_default values is very basic in this patch, at current it just returns the first in the list.
src/app/models/hardware_profile.rb | 95 +++++++++-- src/spec/models/hardware_profile_spec.rb | 259 +++++++++++++++++++----------- 2 files changed, 250 insertions(+), 104 deletions(-)
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb index 39e0b3d..d7ae995 100644 --- a/src/app/models/hardware_profile.rb +++ b/src/app/models/hardware_profile.rb @@ -102,21 +102,61 @@ class HardwareProfile< ActiveRecord::Base the_property end
- #TODO: This function returns the first hwp in the list of matched hardware profiles
- # Better logic should be used here to decide which hardware profile to return.
- def self.match_hwp(hwp)
- hwps = matching_hwps(hwp)
- if hwps.empty?
return nil
- end
- return hwps[0]
- end
- def self.matching_hwps(hwp) provider_hwps = HardwareProfile.all(:conditions => 'provider_id IS NOT NULL')
- return provider_hwps.select { |phwp| check_properties(hwp, phwp) }
- match_maps = []
- provider_hwps.each do |phwp|
match_map = check_properties(hwp, phwp)
if match_map
match_maps<< match_map
end
- end
- return match_maps end
I think you can use this instead of the whole each block:
provider_hwps.map { |phwp| check_properties(hwp, phwp) }.compact
`map` will give call `check_properties` for every HWP and return an array of the results. `compact` filters out the `nil` items.
- #TODO: This function returns the first value in hwpp list, better logic is required for for choosing a more appropriate match private
- def self.set_non_default_value(hwpp)
- case hwpp.kind
when 'range'
hwpp.value = hwpp.range_first
when 'enum'
hwpp.value = hwpp.property_enum_entries[0]
when 'fixed'
hwpp.value = hwpp.value
- end
- hwpp.save
- end
- def self.check_properties(hwp1, hwp2) if [hwp1.memory, hwp1.cpu, hwp1.storage, hwp1.architecture, hwp2.memory, hwp2.cpu, hwp2.storage, hwp2.architecture].include?(nil)
return false
return nil end
- check_hwp_property(hwp1.memory, hwp2.memory)&&
- check_hwp_property(hwp1.cpu, hwp2.cpu)&&
- check_hwp_property(hwp1.storage, hwp2.storage)&&
- hwp1.architecture.value == hwp2.architecture.value
hwpp_mem = check_hwp_property(hwp1.memory, hwp2.memory)
hwpp_cpu = check_hwp_property(hwp1.cpu, hwp2.cpu)
hwpp_storage = check_hwp_property(hwp1.storage, hwp2.storage)
hwpp_arch = check_hwp_property(hwp1.architecture, hwp2.architecture)
hwpps = [hwpp_mem, hwpp_cpu, hwpp_storage, hwpp_arch]
if hwpps.include?(nil)
return nil
else
hwpps.each do |hwpp|
set_non_default_value(hwpp)
end
return { :memory => hwpp_mem, :cpu => hwpp_cpu, :storage => hwpp_storage, :architecture => hwpp_arch, :hardware_profile => hwp2}
end end
def self.check_hwp_property(p1, p2)
@@ -125,25 +165,51 @@ class HardwareProfile< ActiveRecord::Base elsif p2.kind == 'range' calculate_range_match(p2, p1) else
return !(create_array_from_property(p1)& create_array_from_property(p2)).empty?
matched_values = (create_array_from_property(p1)& create_array_from_property(p2))
if !matched_values.empty?
if p1.kind == 'fixed' || p2.kind == 'fixed'
HardwareProfileProperty.new(:kind => 'fixed', :value => matched_values[0])
else
hwpp = HardwareProfileProperty.new(:kind => 'enum')
matched_values.each do |enum_value|
hwpp.property_enum_entries<< PropertyEnumEntry.new(:hardware_profile_property => hwpp, :value => enum_value)
end
return hwpp
end
else
return nil
end end
end
def self.calculate_range_match(p1, p2) case p2.kind when 'range'
return !(p1.range_first.to_f> p2.range_last.to_f || p1.range_last.to_f< p2.range_first.to_f)
if !(BigDecimal.new(p1.range_first)> BigDecimal.new(p2.range_last) || BigDecimal.new(p1.range_last)< BigDecimal.new(p2.range_first))
hwpp = HardwareProfileProperty.new(:kind => 'range')
hwpp.range_first = BigDecimal.new(p1.range_first)>= BigDecimal.new(p2.range_first) ? p1.range_first : p2.range_first
hwpp.range_last = BigDecimal.new(p1.range_last)<= BigDecimal.new(p2.range_last) ? p1.range_last : p2.range_last
return hwpp
else
return nil
end when 'enum'
hwpp = HardwareProfileProperty.new(:kind => 'enum') p2.property_enum_entries.each do |enum|
if (p1.range_first.to_f..p1.range_last.to_f) === enum.value.to_f
return true
if (BigDecimal.new(p1.range_first)..BigDecimal.new(p1.range_last)) === BigDecimal.new(enum.value)
hwpp.property_enum_entries<< PropertyEnumEntry.new(:hardware_profile_property => hwpp, :value => enum.value) end end
return false
return hwpp.property_enum_entries.empty? ? nil : hwpp when 'fixed'
return (p1.range_first.to_f..p1.range_last.to_f) === p2.value.to_f
return (BigDecimal.new(p1.range_first)..BigDecimal.new(p1.range_last)) === BigDecimal.new(p2.value) ? HardwareProfileProperty.new(:kind => 'fixed', :value => p2.value) : nil
else
return nil end
end
@@ -151,10 +217,11 @@ class HardwareProfile< ActiveRecord::Base def self.create_array_from_property(p) case p.kind when 'fixed'
return [p.value.to_f]
return [p.value] when 'enum'
return p.property_enum_entries.map { |enum| enum.value.to_f }
endreturn p.property_enum_entries.map { |enum| enum.value } end
- end
diff --git a/src/spec/models/hardware_profile_spec.rb b/src/spec/models/hardware_profile_spec.rb index ea7ed1e..a3e75e7 100644 --- a/src/spec/models/hardware_profile_spec.rb +++ b/src/spec/models/hardware_profile_spec.rb @@ -88,36 +88,40 @@ describe HardwareProfile do
it "should calculate all the correct matches of provider hardware profiles against a given hardware profile" do provider = Factory(:mock_provider)
HardwareProfile.find(:all, :conditions => {:provider_id => provider.id}).each do |hwp|
hwp.destroy
end
# hwpp memory
- hwpp_mem_match_all = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 1, :range_last => 4096, :value => 256)
- hwpp_mem_match_none = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 8192)
- hwpp_mem_match_2 = create_hwpp_enum([256, 1024], {:name => 'memory', :unit => 'MB'})
- hwpp_mem_match_all = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '1', :range_last => '4096', :value => '256')
- hwpp_mem_match_none = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '8192')
- hwpp_mem_match_2 = create_hwpp_enum(['256', '1024'], {:name => 'memory', :unit => 'MB'})
- hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 256, :range_last => 512, :value => 256)
- hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 4096)
- hwpp_mem_enum = create_hwpp_enum([1024, 3072, 4096], {:name => 'memory', :unit => 'MB'})
hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '256', :range_last => '512', :value => '256')
hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '4096')
hwpp_mem_enum = create_hwpp_enum(['1024', '3072', '4096'], {:name => 'memory', :unit => 'MB'})
# hwpp cpu
- hwpp_cpu_match_all = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 32, :value => 2)
- hwpp_cpu_match_none = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 64)
- hwpp_cpu_match_2 = create_hwpp_enum([8, 16], {:name => 'cpu', :unit => 'count'})
- hwpp_cpu_match_all = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '32', :value => '2')
- hwpp_cpu_match_none = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '64')
- hwpp_cpu_match_2 = create_hwpp_enum(['8', '16'], {:name => 'cpu', :unit => 'count'})
- hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 16, :value => 4)
- hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 32)
- hwpp_cpu_enum = create_hwpp_enum([16, 32], {:name => 'cpu', :unit => 'count'})
hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '16', :value => '4')
hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '32')
hwpp_cpu_enum = create_hwpp_enum(['16', '32'], {:name => 'cpu', :unit => 'count'})
# hwpp storage
- hwpp_storage_match_all = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 4000, :value => 250)
- hwpp_storage_match_none = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 4000)
- hwpp_storage_match_2 = create_hwpp_enum([1000, 2000], {:name => 'storage', :unit => 'GB'})
- hwpp_storage_match_all = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '4000', :value => '250')
- hwpp_storage_match_none = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '4000')
- hwpp_storage_match_2 = create_hwpp_enum(['1000', '2000'], {:name => 'storage', :unit => 'GB'})
- hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 1000, :value => 250)
- hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 3000)
- hwpp_storage_enum = create_hwpp_enum([2000, 4000], {:name => 'storage', :unit => 'GB'})
hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '1000', :value => '250')
hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '3000')
hwpp_storage_enum = create_hwpp_enum(['2000', '4000'], {:name => 'storage', :unit => 'GB'})
# hwpp arch hwpp_arch_i386 = Factory(:hwpp_arch, :value => 'i386')
hwpp_arch_i386_enum = create_hwpp_enum(['i386', 'x86_64'], {:name => 'architecture', :unit => 'label'})
hwp_match_all = Factory(:hardware_profile, :memory => hwpp_mem_match_all, :cpu => hwpp_cpu_match_all,
@@ -149,7 +153,7 @@ describe HardwareProfile do hwp3 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => hwpp_storage_enum,
:architecture => hwpp_arch_i386,
:architecture => hwpp_arch_i386_enum, :provider => provider) hwp4 = Factory(:hardware_profile, :memory => hwpp_mem_enum,
@@ -158,34 +162,34 @@ describe HardwareProfile do :architecture => hwpp_arch_i386, :provider => provider)
- hwps = [hwp1, hwp2, hwp3, hwp4]
- (HardwareProfile.matching_hwps(hwp_match_all)& hwps).should == [hwp1, hwp2, hwp3]
- (HardwareProfile.matching_hwps(hwp_match_none)& hwps).should == []
- (HardwareProfile.matching_hwps(hwp_match_2)& hwps).should == [hwp1, hwp3]
HardwareProfile.matching_hwps(hwp_match_all).size.should == 3
HardwareProfile.matching_hwps(hwp_match_none).empty?.should == true
HardwareProfile.matching_hwps(hwp_match_2).size.should == 2
end
it "should calculate the correct array for hardware profile properties of kind: 'fixed' and 'enum'" do
- hwp_fixed = Factory(:hwpp_fixed, :value => 256)
- hwp_fixed = Factory(:hwpp_fixed, :value => '256')
- enum_array = [256.0, 512.0, 1024.0, 2048.0]
- enum_array = ['256', '512', '1024', '2048'] hwp_enum = create_hwpp_enum(enum_array)
- HardwareProfile.create_array_from_property(hwp_fixed).should == [256.0]
HardwareProfile.create_array_from_property(hwp_fixed).should == ['256'] (HardwareProfile.create_array_from_property(hwp_enum)& enum_array).should == enum_array end
it "should determine match for 2 hardware profiles" do
- hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => 256, :range_last => 512, :value => 256)
- hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => 1024)
- hwpp_mem_enum = create_hwpp_enum([2048, 3072, 4096], {:name => 'memory', :unit => 'MB'})
- hwpp_mem_range = Factory(:hwpp_range, :name => 'memory', :unit => 'MB', :range_first => '256', :range_last => '512', :value => '256')
- hwpp_mem_fixed = Factory(:hwpp_fixed, :name => 'memory', :unit => 'MB', :value => '1024')
- hwpp_mem_enum = create_hwpp_enum(['2048', '3072', '4096'], {:name => 'memory', :unit => 'MB'})
- hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => 1, :range_last => 4, :value => 2)
- hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => 8)
- hwpp_cpu_enum = create_hwpp_enum([16, 32], {:name => 'cpu', :unit => 'count'})
- hwpp_cpu_range = Factory(:hwpp_range, :name => 'cpu', :unit => 'count', :range_first => '1', :range_last => '4', :value => '2')
- hwpp_cpu_fixed = Factory(:hwpp_fixed, :name => 'cpu', :unit => 'count', :value => '8')
- hwpp_cpu_enum = create_hwpp_enum(['16', '32'], {:name => 'cpu', :unit => 'count'})
- hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => 100, :range_last => 500, :value => 250)
- hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => 1000)
- hwpp_storage_enum = create_hwpp_enum([2000, 4000], {:name => 'storage', :unit => 'GB'})
hwpp_storage_range = Factory(:hwpp_range, :name => 'storage', :unit => 'GB', :range_first => '100', :range_last => '500', :value => '250')
hwpp_storage_fixed = Factory(:hwpp_fixed, :name => 'storage', :unit => 'GB', :value => '1000')
hwpp_storage_enum = create_hwpp_enum(['2000', '4000'], {:name => 'storage', :unit => 'GB'})
hwpp_arch_i386 = Factory(:hwpp_arch, :value => 'i386') hwpp_arch_x86_64 = Factory(:hwpp_arch, :value => 'x86_64')
@@ -196,79 +200,147 @@ describe HardwareProfile do hwp4 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => hwpp_storage_enum, :architecture => hwpp_arch_x86_64) hwp5 = Factory(:hardware_profile, :memory => hwpp_mem_enum, :cpu => hwpp_cpu_enum, :storage => nil, :architecture => hwpp_arch_x86_64)
- HardwareProfile.check_properties(hwp1, hwp1).should == true
- HardwareProfile.check_properties(hwp2, hwp2).should == true
- HardwareProfile.check_properties(hwp3, hwp3).should == true
- # Check memory match
- match_map = HardwareProfile.check_properties(hwp1, hwp1)
- match_map[:memory].kind.should == 'range'
- match_map[:memory].range_first.should == '256'
- match_map[:memory].range_last.should == '512'
- HardwareProfile.check_properties(hwp1, hwp2).should == false
- HardwareProfile.check_properties(hwp2, hwp3).should == false
- HardwareProfile.check_properties(hwp3, hwp4).should == false
- HardwareProfile.check_properties(hwp4, hwp5).should == false
- end
- match_map[:cpu].kind.should == 'range'
- match_map[:cpu].range_first.should == '1'
- match_map[:cpu].range_last.should == '4'
- it "should calculate matches for range on hardware profile properties" do
- hwp_range = Factory(:hwpp_range, :range_first => 256, :range_last => 512, :value => 256)
- match_map[:storage].kind.should == 'range'
- match_map[:storage].range_first.should == '100'
- match_map[:storage].range_last.should == '500'
- hwp_range_match = Factory(:hwpp_range, :range_first => 512, :range_last => 1024, :value => 512)
- hwp_range_fail = Factory(:hwpp_range, :range_first => 2048, :range_last => 2048, :value => 8192)
- # Check fixed match
- match_map = HardwareProfile.check_properties(hwp2, hwp2)
- match_map[:memory].kind.should == 'fixed'
- match_map[:memory].value.should == '1024'
- hwp_fixed_match = Factory(:hwpp_fixed, :value => 256)
- hwp_fixed_fail = Factory(:hwpp_fixed, :value => 4096)
- match_map[:cpu].kind.should == 'fixed'
- match_map[:cpu].value.should == '8'
- hwp_enum_match = create_hwpp_enum([256, 512, 1024, 2048])
- hwp_enum_fail = create_hwpp_enum([2048, 4096, 8192, 16384])
- match_map[:storage].kind.should == 'fixed'
- match_map[:storage].value.should == '1000'
- [hwp_range_match, hwp_fixed_match, hwp_enum_match].each do |hwpp|
HardwareProfile.calculate_range_match(hwp_range, hwpp).should == true
- end
- # Check enums match
- match_map = HardwareProfile.check_properties(hwp3, hwp3)
- match_map[:memory].kind.should == 'enum'
- check_enum_entries_match(match_map[:memory], ['2048', '3072', '4096'])
- [hwp_range_fail, hwp_fixed_fail, hwp_enum_fail].each do |hwpp|
HardwareProfile.calculate_range_match(hwp_range, hwpp).should == false
- end
- end
- match_map[:cpu].kind.should == 'enum'
- check_enum_entries_match(match_map[:cpu], ['16', '32'])
- it "should calculate correct matches for each hwp property" do
- hwp_range1 = Factory(:hwpp_range, :range_first => 256, :range_last => 512, :value => 512)
- hwp_range2 = Factory(:hwpp_range, :range_first => 512, :range_last => 1024, :value => 768)
- hwp_range3 = Factory(:hwpp_range, :range_first => 2048, :range_last => 4096, :value => 3072)
- match_map[:storage].kind.should == 'enum'
- check_enum_entries_match(match_map[:storage], ['2000', '4000'])
- hwp_fixed1 = Factory(:hwpp_fixed, :value => 256)
- hwp_fixed2 = Factory(:hwpp_fixed, :value => 4096)
- hwp_fixed3 = Factory(:hwpp_fixed, :value => 8192)
- hwp_enum1 = create_hwpp_enum([256, 512, 1024])
- hwp_enum2 = create_hwpp_enum([1024, 2048, 3072])
- hwp_enum3 = create_hwpp_enum([8192, 16384, 32768])
- HardwareProfile.check_properties(hwp1, hwp2).should == nil
- HardwareProfile.check_properties(hwp2, hwp3).should == nil
- HardwareProfile.check_properties(hwp3, hwp4).should == nil
- HardwareProfile.check_properties(hwp4, hwp5).should == nil
- end
- # Test HWPP Againsts Ranges
- HardwareProfile.check_hwp_property(hwp_range1, hwp_range2).should == true
- HardwareProfile.check_hwp_property(hwp_range1, hwp_range3).should == false
- it "should calculate matches for range on hardware profile properties" do
- hwp_range = Factory(:hwpp_range, :range_first => '256', :range_last => '512', :value => '256')
- HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed1).should == true
- HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed3).should == false
- hwp_range_match = Factory(:hwpp_range, :range_first => '512', :range_last => '1024', :value => '512')
- hwp_range_fail = Factory(:hwpp_range, :range_first => '2048', :range_last => '2048', :value => '8192')
- HardwareProfile.check_hwp_property(hwp_range1, hwp_enum1).should == true
- HardwareProfile.check_hwp_property(hwp_range1, hwp_enum3).should == false
- hwp_fixed_match = Factory(:hwpp_fixed, :value => '256')
- hwp_fixed_fail = Factory(:hwpp_fixed, :value => '4096')
- # Test HWPP Against Fixed
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range1).should == true
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range3).should == false
- hwp_enum_match = create_hwpp_enum(['256', '512', '1024', '2048'])
- hwp_enum_fail = create_hwpp_enum(['2048', '4096', '8192', '16384'])
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed1).should == true
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed2).should == false
- hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_range_match)
- hwpp.kind.should == 'range'
- hwpp.range_first.should == '512'
- hwpp.range_last.should == '512'
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum1).should == true
- HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum3).should == false
- hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_fixed_match)
- hwpp.kind.should == 'fixed'
- hwpp.value.should == '256'
- # Test HWPP Aginsts Enums
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_range1).should == true
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_range3).should == false
- hwpp = HardwareProfile.calculate_range_match(hwp_range, hwp_enum_match)
- check_enum_entries_match(hwpp, ['256', '512'])
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed1).should == true
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed2).should == false
- [hwp_range_fail, hwp_fixed_fail, hwp_enum_fail].each do |hwpp|
HardwareProfile.calculate_range_match(hwp_range, hwpp).should == nil
- end
- end
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum2).should == true
- HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum3).should == false
it "should calculate correct matches for each hwp property" do
hwp_range1 = Factory(:hwpp_range, :range_first => '256', :range_last => '512', :value => '512')
hwp_range2 = Factory(:hwpp_range, :range_first => '512', :range_last => '1024', :value => '768')
hwp_range3 = Factory(:hwpp_range, :range_first => '2048', :range_last => '4096', :value => '3072')
hwp_fixed1 = Factory(:hwpp_fixed, :value => '256')
hwp_fixed2 = Factory(:hwpp_fixed, :value => '4096')
hwp_fixed3 = Factory(:hwpp_fixed, :value => '8192')
hwp_enum1 = create_hwpp_enum(['256', '512', '1024'])
hwp_enum2 = create_hwpp_enum(['1024', '2048', '3072'])
hwp_enum3 = create_hwpp_enum(['8192', '16384', '32768'])
# == Test HWPP against Ranges ==
# -- Expected Matches
hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_range2)
hwpp.kind.should == 'range'
hwpp.range_first.should == '512'
hwpp.range_last.should == '512'
hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed1)
hwpp.kind.should == 'fixed'
hwpp.value.should == '256'
hwpp = HardwareProfile.check_hwp_property(hwp_range1, hwp_enum1)
hwpp.kind.should == 'enum'
check_enum_entries_match(hwpp, ['256', '512'])
# -- Expected Failures
HardwareProfile.check_hwp_property(hwp_range1, hwp_enum3).should == nil
HardwareProfile.check_hwp_property(hwp_range1, hwp_range3).should == nil
HardwareProfile.check_hwp_property(hwp_range1, hwp_fixed3).should == nil
# == Test HWPP Against Fixed ==
# -- Expected Matches
hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range1)
hwpp.kind.should == 'fixed'
hwpp.value.should == '256'
hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed1)
hwpp.kind.should == 'fixed'
hwpp.value.should == '256'
hwpp = HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum1)
hwpp.kind.should == 'fixed'
hwpp.value == '512'
# -- Expected Failures
HardwareProfile.check_hwp_property(hwp_fixed1, hwp_range3).should == nil
HardwareProfile.check_hwp_property(hwp_fixed1, hwp_fixed2).should == nil
HardwareProfile.check_hwp_property(hwp_fixed1, hwp_enum3).should == nil
# == Test HWPP Aginsts Enums ==
# --Expected Matches
hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_range1)
hwpp.kind.should == 'enum'
check_enum_entries_match(hwpp, ['256', '512'])
hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed1)
hwpp.kind.should == 'fixed'
hwpp.value.should == '256'
hwpp = HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum2)
hwpp.kind.should == 'enum'
check_enum_entries_match(hwpp, ['1024'])
# -- Expected Failures
HardwareProfile.check_hwp_property(hwp_enum1, hwp_range3).should == nil
HardwareProfile.check_hwp_property(hwp_enum1, hwp_fixed2).should == nil
HardwareProfile.check_hwp_property(hwp_enum1, hwp_enum3).should == nil end
def create_hwpp_enum(value_array, properties = {})
@@ -279,4 +351,11 @@ describe HardwareProfile do return hwpp_enum end
- def check_enum_entries_match(hwpp, value_array)
- enum_set = []
- hwpp.property_enum_entries.each do |enum|
enum_set<< enum.value
- end
- enum_set.should == value_array
- end end
deltacloud-devel@lists.fedorahosted.org