Skip to content

Commit

Permalink
Merge pull request #16598 from lpichler/fix_chargeback_report_when_vm…
Browse files Browse the repository at this point in the history
…_destroyed

Fix chargeback report when VM is destroyed
  • Loading branch information
jrafanie authored Dec 5, 2017
2 parents 3ebe6cc + a9415f4 commit 01cc0ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/chargeback_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def self.report_col_options

def self.vm_owner(consumption)
@vm_owners ||= vms.each_with_object({}) { |vm, res| res[vm.id] = vm.evm_owner_name }
@vm_owners[consumption.resource_id] ||= consumption.resource.evm_owner_name
@vm_owners[consumption.resource_id] ||= consumption.resource.try(:evm_owner_name)
end

def self.vms
Expand Down Expand Up @@ -172,7 +172,7 @@ def self.vms
def init_extra_fields(consumption)
self.vm_id = consumption.resource_id
self.vm_name = consumption.resource_name
self.vm_uid = consumption.resource.ems_ref
self.vm_uid = consumption.resource.try(:ems_ref)
self.vm_guid = consumption.resource.try(:guid)
self.owner_name = self.class.vm_owner(consumption)
self.provider_name = consumption.parent_ems.try(:name)
Expand Down
16 changes: 16 additions & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@

subject { ChargebackVm.build_results_for_report_ChargebackVm(options).first.first }

context 'when the Vm resource of a consumption is destroyed' do
let(:hours_in_day) { (finish_time.end_of_day - start_time) / 1.hour }

before do
@vm1.destroy
end

it "calculates allocated cpu cost and metric values" do
skip('this case needs to be fixed in new chargeback') if Settings.new_chargeback

expect(subject.cpu_allocated_metric).to eq(cpu_count)
expect(subject.cpu_allocated_cost).to eq(cpu_count * count_hourly_rate * hours_in_day)
expect(subject.cpu_cost).to eq(subject.cpu_allocated_cost + subject.cpu_used_cost)
end
end

context 'when first metric rollup has tag_names=nil' do
before do
options[:tag] = nil
Expand Down

0 comments on commit 01cc0ea

Please sign in to comment.