Skip to content

Commit

Permalink
Merge pull request #16575 from lpichler/fix_empty_array_in_chargeback…
Browse files Browse the repository at this point in the history
…_storage_report

Fix max method on empty array in chargeback storage report
  • Loading branch information
gtanzillo committed Dec 1, 2017
2 parents 819befd + 71d629e commit a06bd40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/chargeback/consumption_with_rollups.rb
Expand Up @@ -37,7 +37,8 @@ def sum(metric, sub_metric = nil)
end

def max(metric, sub_metric = nil)
values(metric, sub_metric).max
values = values(metric, sub_metric)
values.present? ? values.max : 0
end

def avg(metric, sub_metric = nil)
Expand Down
14 changes: 13 additions & 1 deletion spec/models/chargeback/consumption_with_rollups_spec.rb
Expand Up @@ -10,7 +10,7 @@
let!(:metric_rollup) { FactoryGirl.create(:metric_rollup_vm_hr, :timestamp => starting_date + 1.hour, :resource => vm) }

before do
Timecop.travel(starting_date)
Timecop.travel(starting_date + 10.hours)
end

it "doesn't fail when there are no state data about disks" do
Expand All @@ -31,6 +31,18 @@
end
end

context "vim performance state records don't exist" do
before do
VimPerformanceState.destroy_all
end

it 'all chargeback calculations return 0' do
expect(consumption.send(:max, 'derived_vm_allocated_disk_storage', sub_metric)).to be_zero
expect(consumption.send(:avg, 'derived_vm_allocated_disk_storage', sub_metric)).to be_zero
expect(consumption.send(:sum, 'derived_vm_allocated_disk_storage', sub_metric)).to be_zero
end
end

after do
Timecop.return
end
Expand Down

0 comments on commit a06bd40

Please sign in to comment.