From 6757fe61ef58b7b8bd7f52b32ecc003a071c082d Mon Sep 17 00:00:00 2001 From: lpichler Date: Fri, 1 Dec 2017 11:31:15 +0100 Subject: [PATCH 1/3] [].max is nil and we need 0 this can happen when method ConsumptionWithRollups#sub_metric_rollups returns empty array when there are no related VimPerformanceState records --- app/models/chargeback/consumption_with_rollups.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) From 4ccd331aa2f81ceffb9f8073abf1b21873003b90 Mon Sep 17 00:00:00 2001 From: lpichler Date: Fri, 1 Dec 2017 12:03:07 +0100 Subject: [PATCH 2/3] Set current date/time to be later then starting_date --- spec/models/chargeback/consumption_with_rollups_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/chargeback/consumption_with_rollups_spec.rb b/spec/models/chargeback/consumption_with_rollups_spec.rb index ae90226ae72..48f98abc6f0 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 From 71d629e410d570a33e75317740ff9e214bdd3b72 Mon Sep 17 00:00:00 2001 From: lpichler Date: Fri, 1 Dec 2017 12:04:12 +0100 Subject: [PATCH 3/3] Spec cases when there are no VimPerformanceStates --- .../chargeback/consumption_with_rollups_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/models/chargeback/consumption_with_rollups_spec.rb b/spec/models/chargeback/consumption_with_rollups_spec.rb index 48f98abc6f0..2f582e1e140 100644 --- a/spec/models/chargeback/consumption_with_rollups_spec.rb +++ b/spec/models/chargeback/consumption_with_rollups_spec.rb @@ -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