Skip to content

Commit

Permalink
specs: extract perf_rollup_to_parents, gap
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 5, 2019
1 parent 7c86b55 commit dbcaa4a
Show file tree
Hide file tree
Showing 2 changed files with 360 additions and 284 deletions.
360 changes: 360 additions & 0 deletions spec/models/metric/ci_mixin/rollup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,364 @@
end
end
end

describe "perf_rollup_gap" do
context "with a small environment and time_profile" do
before do
@vm1 = FactoryBot.create(:vm_vmware)
@vm2 = FactoryBot.create(:vm_vmware, :hardware => FactoryBot.create(:hardware, :cpu1x2, :memory_mb => 4096))
@host1 = FactoryBot.create(:host, :hardware => FactoryBot.create(:hardware, :memory_mb => 8124, :cpu_total_cores => 1, :cpu_speed => 9576), :vms => [@vm1])
@host2 = FactoryBot.create(:host, :hardware => FactoryBot.create(:hardware, :memory_mb => 8124, :cpu_total_cores => 1, :cpu_speed => 9576))

@ems_cluster = FactoryBot.create(:ems_cluster, :ext_management_system => @ems_vmware)
@ems_cluster.hosts << @host1
@ems_cluster.hosts << @host2

@time_profile = FactoryBot.create(:time_profile_utc)

MiqQueue.delete_all
end

context "with Vm hourly performances" do
before do
cases = [
"2010-04-13T21:00:00Z", 100.0,
"2010-04-14T18:00:00Z", 1.0,
"2010-04-14T19:00:00Z", 2.0,
"2010-04-14T20:00:00Z", 4.0,
"2010-04-14T21:00:00Z", 8.0,
"2010-04-14T22:00:00Z", 15.0,
"2010-04-15T21:00:00Z", 100.0,
]
cases.each_slice(2) do |t, v|
@vm1.metric_rollups << FactoryBot.create(:metric_rollup_vm_hr,
:timestamp => t,
:cpu_usage_rate_average => v,
:cpu_ready_delta_summation => v * 10000,
:sys_uptime_absolute_latest => v,
:min_max => {
:abs_max_cpu_usage_rate_average_value => v,
:abs_max_cpu_usage_rate_average_timestamp => Time.parse(t) + 20.seconds,
:abs_min_cpu_usage_rate_average_value => v,
:abs_min_cpu_usage_rate_average_timestamp => Time.parse(t) + 40.seconds,
}
)
end
end

context "and Host realtime performances" do
before do
cases = [
"2010-04-14T20:52:40Z", 100.0,
"2010-04-14T21:51:20Z", 2.0,
"2010-04-14T21:51:40Z", 4.0,
"2010-04-14T21:52:00Z", 8.0,
"2010-04-14T21:52:20Z", 16.0,
"2010-04-14T21:52:40Z", 30.0,
"2010-04-14T22:52:40Z", 100.0,
]
cases.each_slice(2) do |t, v|
@host1.metrics << FactoryBot.create(:metric_host_rt,
:timestamp => t,
:cpu_usage_rate_average => v,
:cpu_usagemhz_rate_average => v,
:sys_uptime_absolute_latest => v
)
end

cases = [
"2010-04-14T20:52:40Z", 200.0,
"2010-04-14T21:51:20Z", 3.0,
"2010-04-14T21:51:40Z", 8.0,
"2010-04-14T21:52:00Z", 16.0,
"2010-04-14T21:52:20Z", 32.0,
"2010-04-14T21:52:40Z", 60.0,
"2010-04-14T22:52:40Z", 200.0,
]
cases.each_slice(2) do |t, v|
@host2.metrics << FactoryBot.create(:metric_host_rt,
:timestamp => t,
:cpu_usage_rate_average => v,
:cpu_usagemhz_rate_average => v,
:sys_uptime_absolute_latest => v
)
end
end

context "executing perf_rollup_gap_queue" do
before do
@args = [2.days.ago.utc, Time.now.utc, 'daily', @time_profile.id]
Metric::Rollup.perf_rollup_gap_queue(*@args)
end

it "should queue up perf_rollup_gap" do
q_all = MiqQueue.order(:class_name)
expect(q_all.length).to eq(1)

expected = {
:args => @args,
:class_name => "Metric::Rollup",
:method_name => "perf_rollup_gap",
:role => nil
}

expect(q_all[0]).to have_attributes(expected)
end
end

context "executing perf_rollup_gap" do
before do
@args = [2.days.ago.utc, Time.now.utc, 'daily', @time_profile.id]
Metric::Rollup.perf_rollup_gap(*@args)
end

it "should queue up the rollups" do
expect(MiqQueue.count).to eq(3)

[@host1, @host2, @vm1].each do |ci|
message = MiqQueue.where(:class_name => ci.class.name, :instance_id => ci.id).first
expect(message).to have_attributes(:method_name => "perf_rollup_range", :args => @args)
end
end
end
end
end
end
end

describe ".perf_rollup_to_parents" do
context "as vmware" do
before do
@ems_vmware = FactoryBot.create(:ems_vmware, :zone => @zone)
@vm = FactoryBot.create(:vm_perf, :ext_management_system => @ems_vmware)
end

it "should have queued rollups for vm hourly" do
MiqQueue.delete_all
@vm.perf_rollup_to_parents("realtime", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_items_are_hourly_rollups(q_all, "2010-04-14T21:00:00Z", @vm.id, "ManageIQ::Providers::Vmware::InfraManager::Vm")
end

it "should have one set of queued rollups when rolled up twice" do
MiqQueue.delete_all
@vm.perf_rollup_to_parents("realtime", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")
@vm.perf_rollup_to_parents("realtime", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_items_are_hourly_rollups(q_all, "2010-04-14T21:00:00Z", @vm.id, "ManageIQ::Providers::Vmware::InfraManager::Vm")
end

it "queues service rollups" do
service = FactoryBot.create(:service)
service.add_resource(@vm)
service.save
MiqQueue.delete_all

@vm.perf_rollup_to_parents("hourly", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")

expect(MiqQueue.all.pluck(:class_name).uniq).to eq(%w(Service))
end
end

context "with a full vmware rollup chain and time profile" do
before do
@ems_vmware = FactoryBot.create(:ems_vmware, :zone => @zone)
@host = FactoryBot.create(:host, :ext_management_system => @ems_vmware)
@vm = FactoryBot.create(:vm_vmware, :ext_management_system => @ems_vmware, :host => @host)
@ems_cluster = FactoryBot.create(:ems_cluster, :ext_management_system => @ems_vmware)
@ems_cluster.hosts << @host

@time_profile = FactoryBot.create(:time_profile_utc)

MiqQueue.delete_all
end

it "should queue up from Vm realtime to Vm hourly" do
@vm.perf_rollup_to_parents('realtime', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(1)
assert_queue_item_rollup_chain(q_all[0], @vm, 'hourly')
end

it "should queue up from Host realtime to Host hourly" do
@host.perf_rollup_to_parents('realtime', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(1)
assert_queue_item_rollup_chain(q_all[0], @host, 'hourly')
end

it "should queue up from Vm hourly to Host hourly and Vm daily" do
@vm.perf_rollup_to_parents('hourly', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_item_rollup_chain(q_all[0], @host, 'hourly')
assert_queue_item_rollup_chain(q_all[1], @vm, 'daily', @time_profile)
end

it "should queue up from Host hourly to EmsCluster hourly and Host daily" do
@host.perf_rollup_to_parents('hourly', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_item_rollup_chain(q_all[0], @ems_cluster, 'hourly')
assert_queue_item_rollup_chain(q_all[1], @host, 'daily', @time_profile)
end

it "should queue up from EmsCluster hourly to EMS hourly and EmsCluster daily" do
@ems_cluster.perf_rollup_to_parents('hourly', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_item_rollup_chain(q_all[0], @ems_vmware, 'hourly')
assert_queue_item_rollup_chain(q_all[1], @ems_cluster, 'daily', @time_profile)
end

it "should queue up from Vm daily to nothing" do
@vm.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end

it "should queue up from Host daily to nothing" do
@host.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end

it "should queue up from EmsCluster daily to nothing" do
@ems_cluster.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end

it "should queue up from EMS daily to nothing" do
@ems_vmware.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end
end

context "as openstack" do
before do
@ems_openstack = FactoryBot.create(:ems_openstack, :zone => @zone)
@vm = FactoryBot.create(:vm_perf_openstack, :ext_management_system => @ems_openstack)
end

context "queueing up realtime rollups to parent" do
before do
MiqQueue.delete_all
@vm.perf_rollup_to_parents("realtime", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")
end

it "should have queued rollups for vm hourly" do
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_items_are_hourly_rollups(q_all, "2010-04-14T21:00:00Z", @vm.id, "ManageIQ::Providers::Openstack::CloudManager::Vm")
end

context "twice" do
before do
@vm.perf_rollup_to_parents("realtime", "2010-04-14T21:51:10Z", "2010-04-14T22:50:50Z")
end

it "should have one set of queued rollups" do
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_items_are_hourly_rollups(q_all, "2010-04-14T21:00:00Z", @vm.id, "ManageIQ::Providers::Openstack::CloudManager::Vm")
end
end
end
end

context "with a full openstack rollup chain and time profile" do
before do
@ems_openstack = FactoryBot.create(:ems_openstack, :zone => @zone)
@availability_zone = FactoryBot.create(:availability_zone, :ext_management_system => @ems_openstack)
@vm = FactoryBot.create(:vm_openstack, :ext_management_system => @ems_openstack, :availability_zone => @availability_zone)

@time_profile = FactoryBot.create(:time_profile_utc)

MiqQueue.delete_all
end

it "should queue up from Vm realtime to Vm hourly" do
@vm.perf_rollup_to_parents('realtime', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(1)
assert_queue_item_rollup_chain(q_all[0], @vm, 'hourly')
end

it "should queue up from AvailabilityZone realtime to AvailabilityZone hourly" do
@availability_zone.perf_rollup_to_parents('realtime', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(1)
assert_queue_item_rollup_chain(q_all[0], @availability_zone, 'hourly')
end

it "should queue up from Vm hourly to AvailabilityZone hourly and Vm daily" do
@vm.perf_rollup_to_parents('hourly', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_item_rollup_chain(q_all[0], @availability_zone, 'hourly')
assert_queue_item_rollup_chain(q_all[1], @vm, 'daily', @time_profile)
end

it "should queue up from AvailabilityZone hourly to EMS hourly and AvailabilityZone daily" do
@availability_zone.perf_rollup_to_parents('hourly', ROLLUP_CHAIN_TIMESTAMP)
q_all = MiqQueue.order(:id)
expect(q_all.length).to eq(2)
assert_queue_item_rollup_chain(q_all[0], @ems_openstack, 'hourly')
assert_queue_item_rollup_chain(q_all[1], @availability_zone, 'daily', @time_profile)
end

it "should queue up from Vm daily to nothing" do
@vm.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end

it "should queue up from AvailabilityZone daily to nothing" do
@availability_zone.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end

it "should queue up from EMS daily to nothing" do
@ems_openstack.perf_rollup_to_parents('daily', ROLLUP_CHAIN_TIMESTAMP)
expect(MiqQueue.count).to eq(0)
end
end
end

private

def assert_queued_rollup(q_item, instance_id, class_name, args, deliver_on, method = "perf_rollup")
deliver_on = Time.parse(deliver_on).utc if deliver_on.kind_of?(String)

expect(q_item.method_name).to eq(method)
expect(q_item.instance_id).to eq(instance_id)
expect(q_item.class_name).to eq(class_name)
expect(q_item.args).to eq(args)
expect(q_item.deliver_on.utc).to eq(deliver_on) unless deliver_on.nil?
end

def assert_queue_items_are_hourly_rollups(q_items, first_time, instance_id, class_name)
ts = first_time.kind_of?(Time) ? first_time.utc.iso8601 : first_time
q_items.each do |q|
assert_queued_rollup(q, instance_id, class_name, [ts, "hourly"], Time.parse(ts).utc + 1.hour)
ts = (Time.parse(ts).utc + 1.hour).iso8601
end
end

ROLLUP_CHAIN_TIMESTAMP = "2011-08-12T20:33:20Z"
ROLLUP_CHAIN_REALTIME_VALUES = [["2011-08-12T20:33:20Z", nil, 'realtime', nil], nil, "perf_rollup_range"]
ROLLUP_CHAIN_HOURLY_VALUES = [["2011-08-12T20:00:00Z", 'hourly'], "2011-08-12T21:00:00Z"]
ROLLUP_CHAIN_DAILY_VALUES = [["2011-08-12T00:00:00Z", 'daily', nil], "2011-08-13T00:00:00Z"] # nil will be filled in later during test

def assert_queue_item_rollup_chain(q_item, obj, interval_name, time_profile = nil)
case interval_name
when 'realtime'
values = ROLLUP_CHAIN_REALTIME_VALUES
when 'hourly'
values = ROLLUP_CHAIN_HOURLY_VALUES
when 'daily'
values = ROLLUP_CHAIN_DAILY_VALUES
values[0][2] = time_profile.id
end
assert_queued_rollup(q_item, obj.id, obj.class.name, *values)
end
end
Loading

0 comments on commit dbcaa4a

Please sign in to comment.