diff --git a/app/models/chargeback/consumption_with_rollups.rb b/app/models/chargeback/consumption_with_rollups.rb index 4ba03871399..06bacaa6e6b 100644 --- a/app/models/chargeback/consumption_with_rollups.rb +++ b/app/models/chargeback/consumption_with_rollups.rb @@ -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) diff --git a/spec/models/chargeback/consumption_with_rollups_spec.rb b/spec/models/chargeback/consumption_with_rollups_spec.rb index ae90226ae72..2f582e1e140 100644 --- a/spec/models/chargeback/consumption_with_rollups_spec.rb +++ b/spec/models/chargeback/consumption_with_rollups_spec.rb @@ -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 @@ -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