Skip to content

Commit

Permalink
Merge pull request #11748 from lpichler/add_more_chargeback_rate_fact…
Browse files Browse the repository at this point in the history
…ories

Add more ChargebackRate factories
  • Loading branch information
chrisarcand authored Oct 27, 2016
2 parents a01ec8f + aff53ff commit d0826c1
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 67 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/chargeback_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@

render_views

let(:chargeback_rate) { FactoryGirl.create(:chargeback_rate_with_details, :description => "foo") }
let(:chargeback_rate) { FactoryGirl.create(:chargeback_rate, :with_details, :description => "foo") }

# this index represent first rate detail( "Allocated Memory in MB") chargeback_rate
let(:index_to_rate_type) { "0" }
Expand Down
53 changes: 47 additions & 6 deletions spec/factories/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,54 @@
factory :chargeback_rate do
guid { MiqUUID.new_guid }
sequence(:description) { |n| "Chargeback Rate ##{n}" }
rate_type 'Compute'
end
rate_type 'Compute'

transient do
per_time 'hourly'
end

trait :with_details do
chargeback_rate_details do
[FactoryGirl.create(:chargeback_rate_detail_memory_allocated, :tiers_with_three_intervals),
FactoryGirl.create(:chargeback_rate_detail_memory_used, :tiers)]
end
end

trait :with_compute_details do
after(:create) do |chargeback_rate, evaluator|
%i(
chargeback_rate_detail_cpu_used
chargeback_rate_detail_cpu_allocated
chargeback_rate_detail_cpu_cores_used
chargeback_rate_detail_disk_io_used
chargeback_rate_detail_fixed_compute_cost
chargeback_rate_detail_fixed_compute_cost
chargeback_rate_detail_memory_allocated
chargeback_rate_detail_memory_used
chargeback_rate_detail_net_io_used
).each do |factory_name|
chargeback_rate.chargeback_rate_details << FactoryGirl.create(factory_name,
:tiers_with_three_intervals,
:per_time => evaluator.per_time)
end
end
end

trait :with_storage_details do
rate_type 'Storage'

factory :chargeback_rate_with_details, :parent => :chargeback_rate do
after(:create) do |chargeback_rate|
chargeback_rate.chargeback_rate_details << FactoryGirl.create(:chargeback_rate_detail_memory_used_with_tiers)
chargeback_rate.chargeback_rate_details << FactoryGirl.create(:chargeback_rate_detail_memory_allocated_with_tiers)
after(:create) do |chargeback_rate, evaluator|
%i(
chargeback_rate_detail_storage_used
chargeback_rate_detail_storage_allocated
chargeback_rate_detail_fixed_storage_cost
chargeback_rate_detail_fixed_storage_cost
).each do |factory_name|
chargeback_rate.chargeback_rate_details << FactoryGirl.create(factory_name,
:tiers_with_three_intervals,
:per_time => evaluator.per_time)
end
end
end
end
end
112 changes: 67 additions & 45 deletions spec/factories/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryGirl.define do
factory :chargeback_rate_detail do
factory :chargeback_rate_detail, :traits => [:euro, :bytes] do
group "unknown"
source "unknown"
chargeback_rate
Expand Down Expand Up @@ -27,91 +27,113 @@
end
end

factory :chargeback_rate_detail_cpu_used, :parent => :chargeback_rate_detail do
trait :used do
source "used"
end

trait :fixed do
group "fixed"
end

trait :allocated do
source "allocated"
end

trait :cpu do
group "cpu"
end

trait :storage_group do
group "storage"
end

trait :memory do
group "memory"
end

trait :megabytes do
per_unit "megabytes"
end

trait :kbps do
per_unit "kbps"
end

trait :gigabytes do
per_unit "gigabytes"
end

trait :daily do
per_time "daily"
end

trait :hourly do
per_time "hourly"
end

factory :chargeback_rate_detail_cpu_used, :traits => [:used, :cpu], :parent => :chargeback_rate_detail do
description "Used CPU in MHz"
group "cpu"
source "used"
metric "cpu_usagemhz_rate_average"
per_unit "megahertz"
end

factory :chargeback_rate_detail_cpu_cores_used, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_cpu_cores_used, :traits => [:used], :parent => :chargeback_rate_detail do
description "Used CPU in Cores"
group "cpu_cores"
source "used"
metric "cpu_usage_rate_average"
group "cpu_cores"
per_unit "cores"
end

factory :chargeback_rate_detail_cpu_allocated, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_cpu_allocated, :traits => [:allocated, :cpu, :daily],
:parent => :chargeback_rate_detail do
description "Allocated CPU Count"
group "cpu"
source "allocated"
metric "derived_vm_numvcpus"
per_unit "cpu"
per_time "daily"
end

factory :chargeback_rate_detail_memory_allocated, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_memory_allocated, :traits => [:allocated, :memory, :megabytes, :daily],
:parent => :chargeback_rate_detail do
description "Allocated Memory in MB"
group "memory"
source "allocated"
metric "derived_memory_available"
per_unit "megabytes"
per_time "daily"
end

factory :chargeback_rate_detail_memory_used, :parent => :chargeback_rate_detail do
per_unit "megabytes"
per_time "hourly"
factory :chargeback_rate_detail_memory_used, :traits => [:used, :memory, :megabytes, :hourly],
:parent => :chargeback_rate_detail do
description "Used Memory in MB"
group "memory"
source "used"
metric "derived_memory_used"
end

factory :chargeback_rate_detail_disk_io_used, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_disk_io_used, :traits => [:used, :kbps], :parent => :chargeback_rate_detail do
description "Used Disk I/O in KBps"
group "disk_io"
source "used"
metric "disk_usage_rate_average"
per_unit "kbps"
end

factory :chargeback_rate_detail_net_io_used, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_net_io_used, :traits => [:used, :kbps], :parent => :chargeback_rate_detail do
description "Used Network I/O in KBps"
group "net_io"
source "used"
metric "net_usage_rate_average"
per_unit "kbps"
end

factory :chargeback_rate_detail_storage_used, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_storage_used, :traits => [:used, :storage_group, :gigabytes],
:parent => :chargeback_rate_detail do
description "Used Disk Storage in Bytes"
group "storage"
source "used"
metric "derived_vm_used_disk_storage"
per_unit "gigabytes"
end

factory :chargeback_rate_detail_storage_allocated, :parent => :chargeback_rate_detail do
factory :chargeback_rate_detail_storage_allocated, :traits => [:allocated, :storage_group, :gigabytes],
:parent => :chargeback_rate_detail do
description "Allocated Disk Storage in Bytes"
group "storage"
source "allocated"
metric "derived_vm_allocated_disk_storage"
per_unit "gigabytes"
end

factory :chargeback_rate_detail_fixed_compute_cost, :parent => :chargeback_rate_detail do
description "Fixed Compute Cost 1"
group "fixed"
source "compute_1"
per_time "daily"
factory :chargeback_rate_detail_fixed_compute_cost, :traits => [:fixed, :daily], :parent => :chargeback_rate_detail do
sequence(:description) { |n| "Fixed Compute Cost #{n}" }
sequence(:source) { |n| "compute_#{n}" }
end

factory :chargeback_rate_detail_memory_allocated_with_tiers, :parent => :chargeback_rate_detail_memory_allocated,
:traits => [:euro, :bytes, :tiers_with_three_intervals]

factory :chargeback_rate_detail_memory_used_with_tiers, :parent => :chargeback_rate_detail_memory_used,
:traits => [:euro, :bytes, :tiers]
factory :chargeback_rate_detail_fixed_storage_cost, :traits => [:fixed, :daily], :parent => :chargeback_rate_detail do
sequence(:description) { |n| "Fixed Storage Cost #{n}" }
sequence(:source) { |n| "storage_#{n}" }
end
end
2 changes: 2 additions & 0 deletions spec/models/chargeback_container_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
}
let!(:cbrd) {
FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:source => "compute_1",
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt])
Expand Down Expand Up @@ -134,6 +135,7 @@
FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:source => "compute_1",
:chargeback_tiers => [cbt])
}
it "fixed_compute" do
Expand Down
34 changes: 22 additions & 12 deletions spec/models/chargeback_container_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@
:finish => Float::INFINITY,
:fixed_rate => 0.0,
:variable_rate => @hourly_rate.to_s) }
let!(:cbrd) {FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt]) }
let!(:cbrd) do
FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:source => "compute_1",
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt])
end

it "fixed_compute" do
expect(subject.fixed_compute_1_cost).to eq(@hourly_rate * hours_in_day)
expect(subject.fixed_compute_metric).to eq(@metric_size)
Expand Down Expand Up @@ -218,10 +222,14 @@
:finish => Float::INFINITY,
:fixed_rate => 0.0,
:variable_rate => @hourly_rate.to_s) }
let!(:cbrd) {FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt]) }
let!(:cbrd) do
FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:source => "compute_1",
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt])
end

it "fixed_compute" do
# .to be_within(0.01) is used since theres a float error here
expect(subject.fixed_compute_1_cost).to be_within(0.01).of(@hourly_rate * @hours_in_month)
Expand Down Expand Up @@ -377,10 +385,12 @@
:finish => Float::INFINITY,
:fixed_rate => 0.0,
:variable_rate => @hourly_rate.to_s) }
let!(:cbrd) {FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt]) }
let!(:cbrd) do
FactoryGirl.create(:chargeback_rate_detail_fixed_compute_cost,
:chargeback_rate_id => @cbr.id,
:per_time => "hourly",
:chargeback_tiers => [cbt])
end

it "fixed_compute" do
# .to be_within(0.01) is used since theres a float error here
Expand Down
7 changes: 4 additions & 3 deletions spec/models/chargeback_rate_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@
cbd.update(:chargeback_tiers => [cbt])
expect(cbd.friendly_rate).to eq("3.0 Monthly")

cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => 'cpu', :per_time => 'monthly')
cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => 'cpu', :per_time => 'monthly', :detail_measure => nil)
cbt = FactoryGirl.create(:chargeback_tier, :start => 0, :chargeback_rate_detail_id => cbd.id,
:finish => Float::INFINITY, :fixed_rate => 1.0, :variable_rate => 2.0)
cbd.update(:chargeback_tiers => [cbt])
expect(cbd.friendly_rate).to eq("Monthly @ 1.0 + 2.0 per Cpu from 0.0 to Infinity")

cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => 'megabytes', :per_time => 'monthly')
cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => 'megabytes', :per_time => 'monthly',
:detail_measure => nil)
cbt1 = FactoryGirl.create(:chargeback_tier, :start => 0.0, :chargeback_rate_detail_id => cbd.id,
:finish => 5.0, :fixed_rate => 1.0, :variable_rate => 2.0)
cbt2 = FactoryGirl.create(:chargeback_tier, :start => 5.0, :chargeback_rate_detail_id => cbd.id,
Expand All @@ -139,7 +140,7 @@
'cpu', 'Cpu',
'ohms', 'Ohms'
].each_slice(2) do |per_unit, per_unit_display|
cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => per_unit)
cbd = FactoryGirl.build(:chargeback_rate_detail, :per_unit => per_unit, :detail_measure => nil)
expect(cbd.per_unit_display).to eq(per_unit_display)
end
end
Expand Down

0 comments on commit d0826c1

Please sign in to comment.