Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix max method on empty array in chargeback storage report #16575

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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