Skip to content

Commit

Permalink
extract more metric rollups
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 5, 2019
1 parent d3bb181 commit 7c86b55
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 117 deletions.
107 changes: 107 additions & 0 deletions spec/models/metric/ci_mixin/rollup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,113 @@
end

describe ".perf_rollup" do
context "with enabled and disabled targets", :with_enabled_disabled_vmware do
context "executing perf_capture_timer" do
before do
@expected_targets = all_targets
stub_settings_merge(:performance => {:history => {:initial_capture_days => 7}})
Metric::Capture.perf_capture_timer(@ems_vmware.id)
end

context "executing capture_ems_targets for realtime targets with parent objects" do
before do
@expected_targets = Metric::Targets.capture_ems_targets(@ems_vmware)
end

it "should create tasks and queue callbacks for perf_capture_timer" do
# stub_settings_merge(:performance => {:history => {:initial_capture_days => 7}})
# Metric::Capture.perf_capture_timer(@ems_vmware.id)
@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

task = MiqTask.find_by(:name => "Performance rollup for EmsCluster:#{cluster.id}")
expect(task).not_to be_nil
expect(task.context_data[:targets]).to match_array(cluster.hosts.collect { |h| "ManageIQ::Providers::Vmware::InfraManager::Host:#{h.id}" })

expected_hosts.each do |host|
messages = MiqQueue.where(:class_name => 'ManageIQ::Providers::Vmware::InfraManager::Host',
:instance_id => host.id,
:method_name => "perf_capture_realtime")
expect(messages.size).to eq(1)
messages.each do |m|
expect(m.miq_callback).not_to be_nil
expect(m.miq_callback[:method_name]).to eq(:perf_capture_callback)
expect(m.miq_callback[:args]).to eq([[task.id]])

m.delivered("ok", "Message delivered successfully", nil)
end
end

task.reload
expect(task.state).to eq("Finished")

message = MiqQueue.find_by(:method_name => "perf_rollup_range", :class_name => "EmsCluster", :instance_id => cluster.id)
expect(message).not_to be_nil
expect(message.args).to eq([task.context_data[:start], task.context_data[:end], task.context_data[:interval], nil])
end
end

it "calling perf_capture_timer when existing capture messages are on the queue should merge messages and append new task id to cb args" do
# stub_settings_merge(:performance => {:history => {:initial_capture_days => 7}})
# Metric::Capture.perf_capture_timer(@ems_vmware.id)
Metric::Capture.perf_capture_timer(@ems_vmware.id)
@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

tasks = MiqTask.where(:name => "Performance rollup for EmsCluster:#{cluster.id}").order("id DESC")
expect(tasks.length).to eq(2)
tasks.each do |task|
expect(task.context_data[:targets]).to match_array(cluster.hosts.collect { |h| "ManageIQ::Providers::Vmware::InfraManager::Host:#{h.id}" })
end

task_ids = tasks.collect(&:id)

expected_hosts.each do |host|
messages = MiqQueue.where(:class_name => 'ManageIQ::Providers::Vmware::InfraManager::Host',
:instance_id => host.id,
:method_name => "perf_capture_realtime")
expect(messages.size).to eq(1)
host.update_attribute(:last_perf_capture_on, 1.minute.from_now.utc)
messages.each do |m|
next if m.miq_callback[:args].blank?

expect(m.miq_callback).not_to be_nil
expect(m.miq_callback[:method_name]).to eq(:perf_capture_callback)
expect(m.miq_callback[:args].first.sort).to eq(task_ids.sort)

status, message, result = m.deliver
m.delivered(status, message, result)
end
end

tasks.each do |task|
task.reload
expect(task.state).to eq("Finished")
end
end
end

it "calling perf_capture_timer a second time should create another task with the correct time window" do
# Metric::Capture.perf_capture_timer(@ems_vmware.id)
Metric::Capture.perf_capture_timer(@ems_vmware.id)

@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

tasks = MiqTask.where(:name => "Performance rollup for EmsCluster:#{cluster.id}").order("id")
expect(tasks.length).to eq(2)

t1, t2 = tasks
expect(t2.context_data[:start]).to eq(t1.context_data[:end])
end
end
end
end
end

context "with Vm realtime performances", :with_small_vmware do
before do
cases = [
Expand Down
117 changes: 0 additions & 117 deletions spec/models/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,123 +33,6 @@
@ems_vmware = FactoryBot.create(:ems_vmware, :zone => @zone)
end

context "with enabled and disabled targets" do
before do
storages = FactoryBot.create_list(:storage_target_vmware, 2)
@vmware_clusters = FactoryBot.create_list(:cluster_target, 2)
@ems_vmware.ems_clusters = @vmware_clusters

6.times do |n|
host = FactoryBot.create(:host_target_vmware, :ext_management_system => @ems_vmware)
@ems_vmware.hosts << host

@vmware_clusters[n / 2].hosts << host if n < 4
host.storages << storages[n / 3]
end

MiqQueue.delete_all
end

context "executing perf_capture_timer" do
before do
stub_settings_merge(:performance => {:history => {:initial_capture_days => 7}})
Metric::Capture.perf_capture_timer(@ems_vmware.id)
end

context "executing capture_ems_targets for realtime targets with parent objects" do
before do
@expected_targets = Metric::Targets.capture_ems_targets(@ems_vmware)
end

it "should create tasks and queue callbacks" do
@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

task = MiqTask.find_by(:name => "Performance rollup for EmsCluster:#{cluster.id}")
expect(task).not_to be_nil
expect(task.context_data[:targets]).to match_array(cluster.hosts.collect { |h| "ManageIQ::Providers::Vmware::InfraManager::Host:#{h.id}" })

expected_hosts.each do |host|
messages = MiqQueue.where(:class_name => 'ManageIQ::Providers::Vmware::InfraManager::Host',
:instance_id => host.id,
:method_name => "perf_capture_realtime")
expect(messages.size).to eq(1)
messages.each do |m|
expect(m.miq_callback).not_to be_nil
expect(m.miq_callback[:method_name]).to eq(:perf_capture_callback)
expect(m.miq_callback[:args]).to eq([[task.id]])

m.delivered("ok", "Message delivered successfully", nil)
end
end

task.reload
expect(task.state).to eq("Finished")

message = MiqQueue.find_by(:method_name => "perf_rollup_range", :class_name => "EmsCluster", :instance_id => cluster.id)
expect(message).not_to be_nil
expect(message.args).to eq([task.context_data[:start], task.context_data[:end], task.context_data[:interval], nil])
end
end

it "calling perf_capture_timer when existing capture messages are on the queue should merge messages and append new task id to cb args" do
Metric::Capture.perf_capture_timer(@ems_vmware.id)
@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

tasks = MiqTask.where(:name => "Performance rollup for EmsCluster:#{cluster.id}").order("id DESC")
expect(tasks.length).to eq(2)
tasks.each do |task|
expect(task.context_data[:targets]).to match_array(cluster.hosts.collect { |h| "ManageIQ::Providers::Vmware::InfraManager::Host:#{h.id}" })
end

task_ids = tasks.collect(&:id)

expected_hosts.each do |host|
messages = MiqQueue.where(:class_name => 'ManageIQ::Providers::Vmware::InfraManager::Host',
:instance_id => host.id,
:method_name => "perf_capture_realtime")
expect(messages.size).to eq(1)
host.update_attribute(:last_perf_capture_on, 1.minute.from_now.utc)
messages.each do |m|
next if m.miq_callback[:args].blank?

expect(m.miq_callback).not_to be_nil
expect(m.miq_callback[:method_name]).to eq(:perf_capture_callback)
expect(m.miq_callback[:args].first.sort).to eq(task_ids.sort)

status, message, result = m.deliver
m.delivered(status, message, result)
end
end

tasks.each do |task|
task.reload
expect(task.state).to eq("Finished")
end
end
end

it "calling perf_capture_timer a second time should create another task with the correct time window" do
Metric::Capture.perf_capture_timer(@ems_vmware.id)

@vmware_clusters.each do |cluster|
expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) }
next if expected_hosts.empty?

tasks = MiqTask.where(:name => "Performance rollup for EmsCluster:#{cluster.id}").order("id")
expect(tasks.length).to eq(2)

t1, t2 = tasks
expect(t2.context_data[:start]).to eq(t1.context_data[:end])
end
end
end
end
end

context "with a vm" do
before do
@vm = FactoryBot.create(:vm_perf, :ext_management_system => @ems_vmware)
Expand Down

0 comments on commit 7c86b55

Please sign in to comment.