Skip to content

Commit

Permalink
Merge pull request #16434 from lpichler/fix_nil_cases_of_allocated_di…
Browse files Browse the repository at this point in the history
…sk_types

Fix nil cases of allocated disk types in chargeback reporting
(cherry picked from commit a807056)

https://bugzilla.redhat.com/show_bug.cgi?id=1517956
  • Loading branch information
gtanzillo authored and simaishi committed Nov 27, 2017
1 parent c7f04c7 commit 590c2b5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/chargeback/consumption_with_rollups.rb
Expand Up @@ -71,7 +71,7 @@ def born_at


def sub_metric_rollups(sub_metric) def sub_metric_rollups(sub_metric)
q = VimPerformanceState.where(:timestamp => start_time...end_time, :resource => resource, :capture_interval => 3_600) q = VimPerformanceState.where(:timestamp => start_time...end_time, :resource => resource, :capture_interval => 3_600)
q.map { |x| x.allocated_disk_types[sub_metric] || 0 } q.map { |x| x.state_data.try(:[], :allocated_disk_types).try(:[], sub_metric) || 0 }
end end


def values(metric, sub_metric = nil) def values(metric, sub_metric = nil)
Expand Down
38 changes: 38 additions & 0 deletions spec/models/chargeback/consumption_with_rollups_spec.rb
@@ -0,0 +1,38 @@
describe Chargeback::ConsumptionWithRollups do
let(:vm) { FactoryGirl.build(:vm_microsoft) }
let(:consumption) { described_class.new([metric_rollup], starting_date, starting_date + 1.day) }

describe '#sub_metric_rollups' do
let(:starting_date) { Time.parse('2012-09-01 23:59:59Z').utc }
let(:sub_metric) { 'ssd' }
let(:ssd_volume) { FactoryGirl.create(:cloud_volume_openstack, :volume_type => sub_metric) }
let!(:state) { FactoryGirl.create(:vim_performance_state, :resource => vm, :state_data => nil, :timestamp => starting_date, :capture_interval => 3_600) }
let!(:metric_rollup) { FactoryGirl.create(:metric_rollup_vm_hr, :timestamp => starting_date + 1.hour, :resource => vm) }

before do
Timecop.travel(starting_date)
end

it "doesn't fail when there are no state data about disks" do
expect(consumption.send(:values, 'derived_vm_allocated_disk_storage', sub_metric)).to match_array([0])
end

context 'vim performance state contains any state data for storage' do
let(:volume_size) { 1_024 }

before do
state.capture
state.allocated_disk_types = {sub_metric => volume_size}
state.save
end

it 'returns values' do
expect(consumption.send(:values, 'derived_vm_allocated_disk_storage', sub_metric)).to match_array([volume_size])
end
end

after do
Timecop.return
end
end
end
13 changes: 13 additions & 0 deletions spec/models/vim_performance_state_spec.rb
Expand Up @@ -59,5 +59,18 @@
subject { vm.perf_capture_state.allocated_disk_types } subject { vm.perf_capture_state.allocated_disk_types }


it { is_expected.to match('ssd' => ssd_size, 'unclassified' => hdd1_size + hdd2_size) } it { is_expected.to match('ssd' => ssd_size, 'unclassified' => hdd1_size + hdd2_size) }

context 'when disk size is nil' do
let!(:hdd1_disk) { FactoryGirl.create(:disk, :size => nil, :backing => hdd_volume) }

it { is_expected.to match('ssd' => ssd_size, 'unclassified' => hdd2_size) }

context "when all disk's sizes are nil" do
let!(:hdd2_disk) { FactoryGirl.create(:disk, :size => nil, :backing => hdd_volume) }
let!(:ssd_disk) { FactoryGirl.create(:disk, :size => nil, :backing => ssd_volume) }

it { is_expected.to be_empty }
end
end
end end
end end

0 comments on commit 590c2b5

Please sign in to comment.