Skip to content

Commit

Permalink
Changed validate_quota method to check individual quota items before …
Browse files Browse the repository at this point in the history
…denying quota.

For Vm Reconfigure, If we add a disk it shouldn't deny because memory, cpus or number of vms is exceeded.

Fixes ManageIQ/manageiq-automation_engine#150

Updated spec and added 4 new tests.
  • Loading branch information
billfitzgerald0120 committed Jul 26, 2018
1 parent 54eb45d commit f19ebde
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def source_info

def quota_check(item, used, requested, quota_max, quota_warn)
$evm.log(:info, "Item: #{item} Used: (#{used}) Requested: (#{requested}) Max: (#{quota_max}) Warn: (#{quota_warn})")
return unless requested.positive?
return unless quota_max + quota_warn > 0
if quota_exceeded?(item, used, requested, quota_max)
quota_exceeded(item, reason(item, used, requested, quota_max), false)
Expand Down
105 changes: 87 additions & 18 deletions spec/automation/unit/method_validation/validate_quota_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
describe "Quota Validation" do
include Spec::Support::QuotaHelper

def run_automate_method
@quota_used = YAML.dump(:storage => 32_768, :vms => 2, :cpu => 2, :memory => 4096)
@quota_requested = YAML.dump(:storage => 10_240, :vms => 1, :cpu => 1, :memory => 1024)
def run_automate_method(attrs, used, requested)
attrs = []
attrs << "MiqProvisionRequest::miq_provision_request=#{@miq_provision_request.id}&" \
"MiqRequest::miq_request=#{@miq_provision_request.id}&" \
attrs << "MiqRequest::miq_request=#{@miq_provision_request.id}&" \
"quota_limit_max_yaml=#{@quota_limit_max}&" \
"quota_limit_warn_yaml=#{@quota_limit_warn}&" \
"quota_used_yaml=#{@quota_used}&" \
"quota_used_yaml=#{used}&" \
"Tenant::quota_source=#{@tenant.id}&" \
"quota_requested_yaml=#{@quota_requested}"
"quota_requested_yaml=#{requested}"
attrs << @extra_attrs if @extra_attrs
MiqAeEngine.instantiate("/ManageIQ/system/request/Call_Instance?namespace=System/CommonMethods&" \
"class=QuotaMethods&instance=validate_quota&#{attrs.join('&')}", @user)
end

let(:used) { YAML.dump(:storage => 32_768, :vms => 2, :cpu => 2, :memory => 4096) }
let(:requested) { YAML.dump(:storage => 10_240, :vms => 1, :cpu => 1, :memory => 1024) }

def attrs
["MiqProvisionRequest::miq_provision_request=#{@miq_provision_request.id}&"]
end

def reconfigure_attrs
["MiqRequest::miq_request=#{@reconfigure_request.id}&"\
"vmdb_object_type=VmReconfigureRequest?"]
end

before do
setup_model
end
Expand All @@ -25,7 +34,7 @@ def run_automate_method
it "no quota limits set" do
@quota_limit_max = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eq('ok')
end
end
Expand All @@ -36,7 +45,7 @@ def run_automate_method
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (memory - Used: 4 KB plus requested: 1 KB exceeds quota: 4 KB) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
Expand All @@ -48,7 +57,7 @@ def run_automate_method
@quota_limit_max = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds warning limits for the following:" \
" (memory - Used: 4 KB plus requested: 1 KB exceeds quota: 4 KB) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('ok')
@miq_request.reload
expect(@miq_request.options[:quota_warn_exceeded]).to eql(err_msg)
Expand All @@ -60,7 +69,7 @@ def run_automate_method
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (vms - Used: 2 plus requested: 1 exceeds quota: 2) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
Expand All @@ -72,7 +81,7 @@ def run_automate_method
@quota_limit_max = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds warning limits for the following:" \
" (vms - Used: 2 plus requested: 1 exceeds quota: 1) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('ok')
@miq_request.reload
expect(@miq_request.options[:quota_warn_exceeded]).to eql(err_msg)
Expand All @@ -84,7 +93,7 @@ def run_automate_method
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (cpu - Used: 2 plus requested: 1 exceeds quota: 2) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
Expand All @@ -96,7 +105,7 @@ def run_automate_method
@quota_limit_max = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds warning limits for the following:" \
" (cpu - Used: 2 plus requested: 1 exceeds quota: 1) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('ok')
@miq_request.reload
expect(@miq_request.options[:quota_warn_exceeded]).to eql(err_msg)
Expand All @@ -108,7 +117,7 @@ def run_automate_method
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (storage - Used: 32 KB plus requested: 10 KB exceeds quota: 20 KB) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
Expand All @@ -120,7 +129,7 @@ def run_automate_method
@quota_limit_max = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds warning limits for the following:" \
" (storage - Used: 32 KB plus requested: 10 KB exceeds quota: 10 KB) "
ws = run_automate_method
ws = run_automate_method(attrs, used, requested)
expect(ws.root['ae_result']).to eql('ok')
@miq_request.reload
expect(@miq_request.options[:quota_warn_exceeded]).to eql(err_msg)
Expand All @@ -129,17 +138,77 @@ def run_automate_method
end

context "check_quota for VMReconfigure" do
let(:used) { YAML.dump(:storage => 10_240, :vms => 2, :cpu => 2, :memory => 1024) }
let(:requested) { YAML.dump(:storage => 20_480, :vms => 0, :cpu => 0, :memory => 4096) }

it "check_quota false " do
ws = run_automate_method
setup_model("vmware_reconfigure")
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eq('ok')
expect(ws.root['check_quota']).to be_nil
end

it "check_quota true " do
setup_model("vmware_reconfigure")
@extra_attrs = "check_quota=true"
ws = run_automate_method
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eq('ok')
expect(ws.root['check_quota']).to eq('true')
end

it "failure max memory" do
setup_model("vmware_reconfigure")
@quota_limit_max = YAML.dump(:storage => 0, :vms => 1, :cpu => 1, :memory => 4096)
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (memory - Used: 1 KB plus requested: 4 KB exceeds quota: 4 KB) "
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
expect(@miq_request.message).to eql(err_msg)
end

it "failure max memory" do
setup_model("vmware_reconfigure")
@quota_limit_max = YAML.dump(:storage => 0, :vms => 1, :cpu => 1, :memory => 4096)
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (memory - Used: 1 KB plus requested: 4 KB exceeds quota: 4 KB) "
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
expect(@miq_request.message).to eql(err_msg)
end

it "failure max storage" do
setup_model("vmware_reconfigure")
@quota_limit_max = YAML.dump(:storage => 20_480, :vms => 1, :cpu => 1, :memory => 0)
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 0)
err_msg = "Request exceeds maximum allowed for the following:" \
" (storage - Used: 10 KB plus requested: 20 KB exceeds quota: 20 KB) "
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eql('error')
@miq_request.reload
expect(@miq_request.options[:quota_max_exceeded]).to eql(err_msg)
expect(@miq_request.message).to eql(err_msg)
end

it "success when requesting storage even when cpu and vm is exceeded" do
setup_model("vmware_reconfigure")
@quota_limit_max = YAML.dump(:storage => 40_960, :vms => 1, :cpu => 1, :memory => 0)
@quota_limit_warn = YAML.dump(:storage => 40_960, :vms => 0, :cpu => 0, :memory => 0)
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eq(nil)
end

it "success when requesting memory even when cpu and vm is exceeded" do
setup_model("vmware_reconfigure")
@quota_limit_max = YAML.dump(:storage => 0, :vms => 1, :cpu => 1, :memory => 8192)
@quota_limit_warn = YAML.dump(:storage => 0, :vms => 0, :cpu => 0, :memory => 8192)
ws = run_automate_method(reconfigure_attrs, used, requested)
expect(ws.root['ae_result']).to eq(nil)
end
end
end

0 comments on commit f19ebde

Please sign in to comment.