From b624debb224bca8e70fbb2b936a911c1fd6fa900 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 13 Jul 2010 12:32:11 +0200 Subject: [PATCH] feature #203: added default and unlimited quota limits --- src/authm_mad/quota.rb | 40 +++++++++++++++++++++++++------ src/authm_mad/spec/quota_spec.rb | 21 ++++++++++++---- src/authm_mad/spec/spec_common.rb | 1 + 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/authm_mad/quota.rb b/src/authm_mad/quota.rb index 36a0a588d3..23eb021e3e 100644 --- a/src/authm_mad/quota.rb +++ b/src/authm_mad/quota.rb @@ -14,17 +14,29 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'one_usage' + # Quota functionality for auth driver. Stores in database limits for each # user and using OneUsage is able to retrieve resource usage from # OpenNebula daemon and check if it is below limits class Quota + attr_accessor :defaults + TABLE_NAME=:quotas # 'db' is a Sequel database where to store user limits and client # is OpenNebula::Client used to connect to OpenNebula daemon - def initialize(db, client) + def initialize(db, client, conf={}) @db=db @client=client + @conf={ + :defaults => { + :cpu => nil, + :memory => nil + } + }.merge(conf) + + @defaults=@conf[:defaults] @usage=OneUsage.new(@client) @@ -45,18 +57,30 @@ class Quota end # Adds new user limits - def add(uid, cpu, memory, num_vms) - @table.insert( - :uid => uid, + def set(uid, cpu, memory, num_vms) + data={ :cpu => cpu, :memory => memory, :num_vms => num_vms - ) + } + + quotas=@table.filter(:uid => uid) + + if quotas.first + quotas.update(data) + else + @table.insert(data.merge!(:uid => uid)) + end end # Gets user limits def get(uid) - @table.filter(:uid => uid).first + limit=@table.filter(:uid => uid).first + if limit + limit + else + @defaults + end end # Checks if the user is below resource limits. If new_vm is defined @@ -68,7 +92,9 @@ class Quota usage.cpu+=new_vm.cpu usage.memory+=new_vm.memory end - usage.cpu<=user_quota[:cpu] && usage.memory<=user_quota[:memory] + + (!user_quota[:cpu] || usage.cpu<=user_quota[:cpu]) && + (!user_quota[:memory] || usage.memory<=user_quota[:memory]) end # Updates user resource consuption diff --git a/src/authm_mad/spec/quota_spec.rb b/src/authm_mad/spec/quota_spec.rb index 0e8e352f64..e4e796f888 100644 --- a/src/authm_mad/spec/quota_spec.rb +++ b/src/authm_mad/spec/quota_spec.rb @@ -16,6 +16,7 @@ require 'spec_common' +require 'client_mock' require 'quota' def check_quota(uid, cpu, memory, num_vms) @@ -45,15 +46,15 @@ describe 'Quota' do end it 'should let add and retrieve quotas' do - @quota.add(0, 10.0, 1024, 10) - @quota.add(1, 20.0, 2048, 20) - @quota.add(2, 40.0, 4096, 40) + @quota.set(0, 10.0, 1024, 10) + @quota.set(1, 20.0, 2048, 20) + @quota.set(2, 40.0, 4096, 40) check_quota(0, 10.0, 1024, 10) check_quota(1, 20.0, 2048, 20) check_quota(2, 40.0, 4096, 40) - @quota.get(3).should == nil + @quota.get(3).should == @quota.defaults end it 'should check for quotas' do @@ -77,6 +78,18 @@ describe 'Quota' do @quota.check(1).should == true end + it 'should let update limits' do + @quota.set(0, nil, nil, nil) + check_quota(0, nil, nil, nil) + end + + it 'should understand unlimited quotas' do + vms=@quota.get_user(0) + vms[7]=VmUsage.new(9999999999.0, 99999999999) + @quota.check(0).should == true + @quota.check(0, VmUsage.new(999999999.0, 99999999)).should == true + end + end diff --git a/src/authm_mad/spec/spec_common.rb b/src/authm_mad/spec/spec_common.rb index aad9fa1fc1..7fffd828f0 100644 --- a/src/authm_mad/spec/spec_common.rb +++ b/src/authm_mad/spec/spec_common.rb @@ -19,6 +19,7 @@ require 'pp' require 'db_helpers' $: << '../oca/ruby' +$: << './spec'