incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject svn commit: r1180860 - in /incubator/deltacloud/trunk/server: lib/deltacloud/hardware_profile.rb lib/deltacloud/validation.rb lib/sinatra/rabbit.rb tests/common.rb tests/drivers/mock/instances_test.rb tests/rabbit_test.rb
Date Mon, 10 Oct 2011 10:08:14 GMT
Author: mfojtik
Date: Mon Oct 10 10:08:13 2011
New Revision: 1180860

URL: http://svn.apache.org/viewvc?rev=1180860&view=rev
Log:
Core: Fixed hardware_profile validation engine

Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/hardware_profile.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/validation.rb
    incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb
    incubator/deltacloud/trunk/server/tests/common.rb
    incubator/deltacloud/trunk/server/tests/drivers/mock/instances_test.rb
    incubator/deltacloud/trunk/server/tests/rabbit_test.rb

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/hardware_profile.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/hardware_profile.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/hardware_profile.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/hardware_profile.rb Mon Oct 10 10:08:13
2011
@@ -68,22 +68,21 @@ module Deltacloud
         kind == :fixed
       end
 
-      def to_param
-        return nil if kind == :fixed
-        if kind == :range
-          # FIXME: We can't validate ranges currently
-          args = [param, :string, :optional]
-        else
-          args = [param, :string, :optional, values.collect { |v| v.to_s} ]
+      def valid?(v)
+        case kind
+          when :fixed then (v == @default.to_s)
+          when [:range, :enum] then (value.include?(v.to_i))
+          else false
         end
-        Validation::Param.new(args)
+      end
+
+      def to_param
+        Validation::Param.new([param, :string, :optional, []])
       end
 
       def include?(v)
         if kind == :fixed
           return v == value
-        elsif kind == :range
-          return v >= first && v <= last
         else
           return values.include?(v)
         end

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/validation.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/validation.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/validation.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/validation.rb Mon Oct 10 10:08:13 2011
@@ -16,13 +16,12 @@
 
 module Deltacloud::Validation
 
-  class Failure < StandardError
+  class Failure < Deltacloud::ExceptionHandler::DeltacloudException
     attr_reader :param
-    def initialize(param, msg='')
-      super(msg)
-      @param = param
+    def initialize(e, message=nil)
+      message ||= e.message
+      super(400, e.class.name, message, [])
     end
-
     def name
       param.name if @param
     end
@@ -46,6 +45,19 @@ module Deltacloud::Validation
     def optional?
       type.eql?(:optional)
     end
+
+    def valid_value?(value)
+      true if (options.kind_of?(Range) or options.kind_of?(Array)) and options.include?(value)
+      true if options.kind_of?(String) and not options.empty?
+    end
+
+    def valid_hwp_value?(profile, value)
+      profile.property(@name.to_s.gsub(/^hwp_/, '')).valid?(value)
+    end
+
+    def hwp_property?
+      true if name.to_s =~ /^hwp_(cpu|memory|storage|architecture)/
+    end
   end
 
   def param(*args)
@@ -72,15 +84,23 @@ module Deltacloud::Validation
     params.each_value { |p| yield p }
   end
 
-  def validate(all_params, values)
+  def validate(all_params, values, credentials)
     all_params.each_value do |p|
       if p.required? and not values[p.name]
         raise Failure.new(p, "Required parameter #{p.name} not found")
       end
-      if values[p.name] and not p.options.empty? and
-          not p.options.include?(values[p.name])
-        raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not
in #{p.options.join(", ")}")
+      next unless values[p.name]
+      if p.hwp_property?
+        profile = driver.hardware_profile(credentials, values['hwp_id'])
+        unless p.valid_hwp_value?(profile, values[p.name])
+          raise Failure.new(p, "Hardware profile property #{p.name} has invalid value #{values[p.name]}")
+        end
+      else
+        if not p.options.empty? and p.valid_value?(values[p.name])
+          raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is
not in #{p.options.join(", ")}")
+        end
       end
     end
   end
+
 end

Modified: incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/rabbit.rb Mon Oct 10 10:08:13 2011
@@ -121,7 +121,7 @@ module Sinatra
         @control = Proc.new do
           op.collection.check_supported(driver)
           op.check_capability(driver)
-          op.validate(op.effective_params(driver), params)
+          op.validate(op.effective_params(driver), params, credentials)
           instance_eval(&block)
         end
       end

Modified: incubator/deltacloud/trunk/server/tests/common.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/common.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/common.rb (original)
+++ incubator/deltacloud/trunk/server/tests/common.rb Mon Oct 10 10:08:13 2011
@@ -100,7 +100,9 @@ module DeltacloudTestCommon
       get(uri, params || {}, opts[:auth] ? authenticate(opts) : {})
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
         puts "========================================"
       end
     end
@@ -112,7 +114,9 @@ module DeltacloudTestCommon
     get_url(uri, params, opts)
     if last_response.status.to_s =~ /5(\d{2})/
       puts "============= [ ERROR ] ================"
-      puts last_response.inspect
+      puts last_response.body
+      puts "============= [ RESPONSE ] ================"
+      puts last_response.errors
       puts "========================================"
     end
   end
@@ -127,7 +131,9 @@ module DeltacloudTestCommon
       post(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
         puts "========================================"
       end
     end
@@ -143,8 +149,9 @@ module DeltacloudTestCommon
       delete(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
-        puts "========================================"
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
       end
     end
   end
@@ -159,8 +166,9 @@ module DeltacloudTestCommon
        put(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
-        puts "========================================"
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
       end
     end
   end

Modified: incubator/deltacloud/trunk/server/tests/drivers/mock/instances_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/drivers/mock/instances_test.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/drivers/mock/instances_test.rb (original)
+++ incubator/deltacloud/trunk/server/tests/drivers/mock/instances_test.rb Mon Oct 10 10:08:13
2011
@@ -109,18 +109,33 @@ module DeltacloudUnitTest
       test_instance_attributes(last_xml_response/'instance')
     end
 
-    def test_it_create_a_new_instance_using_image_id_and_name_and_hwp
+    def test_it_create_a_new_instance_using_image_id_and_name_and_hwp_storage_and_hwp_cpu
       params = {
         :image_id => 'img1',
-        :name => "unit_test_instance1",
-        :hwp_id => "m1-xlarge"
+        :realm_id => '',
+        :name => "unit_test_instance3",
+        :hwp_id => "m1-small",
+        :hwp_storage => '160',
+        :hwp_memory => '1740.8',
+        :hwp_cpu => "1.0",
+      }
+      post_url '/api/instances', params
+      last_response.status.should == 400
+    end
+
+    def test_it_create_a_new_instance_using_image_id_and_name_and_hwp_storage
+      params = {
+        :image_id => 'img1',
+        :name => "unit_test_instance2",
+        :hwp_id => "m1-small",
+        :hwp_storage => "160"
       }
       post_url '/api/instances', params
       last_response.status.should == 201
       last_response.headers['Location'].should_not == nil
       get_auth_url last_response.headers['Location'], {}
-      (last_xml_response/'instance/name').text.should == 'unit_test_instance1'
-      (last_xml_response/'instance/hardware_profile').first['id'].should == 'm1-xlarge'
+      (last_xml_response/'instance/name').text.should == 'unit_test_instance2'
+      (last_xml_response/'instance/hardware_profile').first['id'].should == 'm1-small'
       add_created_instance (last_xml_response/'instance').first['id']
       test_instance_attributes(last_xml_response/'instance')
     end

Modified: incubator/deltacloud/trunk/server/tests/rabbit_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/rabbit_test.rb?rev=1180860&r1=1180859&r2=1180860&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/rabbit_test.rb (original)
+++ incubator/deltacloud/trunk/server/tests/rabbit_test.rb Mon Oct 10 10:08:13 2011
@@ -44,7 +44,7 @@ module DeltacloudUnitTest
       features.should =~ [:hardware_profiles, :user_name, :authentication_key, :user_data]
 
       op = @app.collections[:instances].operations[:create]
-      op.effective_params(@driver).keys.should =~ [:image_id, :hwp_memory, :hwp_id, :keyname,
:name, :hwp_storage, :realm_id, :user_data]
+      op.effective_params(@driver).keys.should =~ [:image_id, :hwp_memory, :hwp_id, :keyname,
:name, :hwp_storage, :realm_id, :user_data, :hwp_architecture, :hwp_cpu]
 
       op.params.keys =~ [:realm_id, :image_id, :hwp_id]
     end



Mime
View raw message