Skip to content

Commit

Permalink
Refactor used quota method and test
Browse files Browse the repository at this point in the history
This PR is based on on the issue below.
ManageIQ/manageiq#12038
  • Loading branch information
billfitzgerald0120 committed Oct 4, 2017
1 parent 2c1d828 commit 67e73c3
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,47 @@
# Description: calculate entity used quota values
#

def consumption(source)
{
:cpu => source.allocated_vcpu,
:memory => source.allocated_memory,
:vms => source.vms.count { |vm| vm.id if vm.ems_id },
:storage => source.allocated_storage,
:provisioned_storage => source.provisioned_storage
}
module ManageIQ
module Automate
module System
module CommonMethods
module QuotaMethods
class Used
def initialize(handle = $evm)
@handle = handle
end

def main
used(quota_source)
end

private

def used(quota_source)
@handle.root['quota_used'] = consumption(quota_source)
end

def quota_source
raise "ERROR - quota_source not found" unless @handle.root['quota_source']
@handle.root['quota_source']
end

def consumption(source)
{
:cpu => source.allocated_vcpu,
:memory => source.allocated_memory,
:vms => source.vms.count { |vm| vm.id if vm.ems_id },
:storage => source.allocated_storage,
:provisioned_storage => source.provisioned_storage
}
end
end
end
end
end
end
end

$evm.root['quota_used'] = consumption($evm.root['quota_source']) if $evm.root['quota_source']
if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::System::CommonMethods::QuotaMethods::Used.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require_domain_file

describe ManageIQ::Automate::System::CommonMethods::QuotaMethods::Used do
include Spec::Support::QuotaHelper

let!(:model) { setup_model }
let(:root_hash) do
{
'miq_provision_request' => @miq_provision_request,
'miq_request' => @miq_provision_request,
'quota_source' => quota_source,
'quota_source_type' => quota_source_type
}
end

let(:counts_hash) do
{:storage => 1_000_000, :cpu => 0, :vms => 4, :memory => 1_073_741_824}
end

let(:root_object) do
Spec::Support::MiqAeMockObject.new(root_hash)
end

let(:ae_service) do
Spec::Support::MiqAeMockService.new(root_object).tap do |service|
current_object = Spec::Support::MiqAeMockObject.new
current_object.parent = root_object
service.object = current_object
end
end

shared_examples_for "used" do
it "check" do
described_class.new(ae_service).main
expect(ae_service.root['quota_used']).to include(counts_hash)
end
end

context "returns ok for tenant counts" do
let(:quota_source) { @tenant }
let(:quota_source_type) { 'tenant' }
let(:status_and_message) { [true, ""] }
let(:ae_result) { "ok" }
let(:errormsg) { 'ERROR - quota_source not found' }
it_behaves_like "used"
end

context "returns ok for user counts" do
let(:quota_source) { @tenant }
let(:quota_source_type) { 'user' }
let(:status_and_message) { [true, ""] }
let(:ae_result) { "ok" }
let(:errormsg) { 'ERROR - quota_source not found' }
it_behaves_like "used"
end

context "returns ok for group counts" do
let(:quota_source) { @tenant }
let(:quota_source_type) { 'group' }
let(:status_and_message) { [true, ""] }
let(:ae_result) { "ok" }
let(:errormsg) { 'ERROR - quota_source not found' }
it_behaves_like "used"
end

context "returns error " do
let(:quota_source_type) { nil }
let(:quota_source) { nil }
let(:status_and_message) { [true, ""] }
let(:errormsg) { 'ERROR - quota_source not found' }
it "when no quota source" do
allow(ae_service).to receive(:used).and_return(status_and_message)
expect { described_class.new(ae_service).main }.to raise_error(errormsg)
end
end
end

0 comments on commit 67e73c3

Please sign in to comment.