Skip to content

Commit

Permalink
metrics: test objects and messages (vs counts)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Oct 29, 2019
1 parent d8cdeb0 commit 60e5df4
Showing 1 changed file with 36 additions and 48 deletions.
84 changes: 36 additions & 48 deletions spec/models/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

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

Expand All @@ -42,12 +42,17 @@
@ems_vmware.hosts << host

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

MiqQueue.delete_all
end

# this is what we're expecting to return from:
# Metric::Targets.capture_ems_targets(@ems_vmware)
let(:storage_targets) { @storages.select(&:perf_capture_enabled?) }
let(:targets) { Metric::Targets.capture_ems_targets(@ems_vmware.reload, :exclude_storages => true) }

context "executing capture_ems_targets" do
it "should find enabled targets" do
targets = Metric::Targets.capture_ems_targets(@ems_vmware.reload)
Expand All @@ -63,31 +68,24 @@
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

let(:expected_queue_items) do
{
%w(ManageIQ::Providers::Vmware::InfraManager::Host perf_capture_realtime) => 3,
%w(ManageIQ::Providers::Vmware::InfraManager::Host perf_capture_historical) => 24,
%w(Storage perf_capture_hourly) => 1,
%w(ManageIQ::Providers::Vmware::InfraManager::Vm perf_capture_realtime) => 2,
%w(ManageIQ::Providers::Vmware::InfraManager::Vm perf_capture_historical) => 16,
%w(MiqTask destroy_older_by_condition) => 1
}
end

it "should queue up enabled targets" do
Timecop.freeze do
expect(MiqQueue.group(:class_name, :method_name).count).to eq(expected_queue_items)
targets = Metric::Targets.capture_ems_targets(@ems_vmware.reload)
expect(queue_intervals).to match_array(queue_items_for_targets(targets))
expect(queue_timings).to eq(queue_timings_for_targets(targets))
Metric::Capture.perf_capture_timer(@ems_vmware.id)

expected_queue_items = {
"realtime" => targets.each_with_object({}) { |o, h| h[o] = [[4.hours.ago.utc.beginning_of_day]] },
"historical" => targets.each_with_object({}) { |o, h| h[o] = date_range },
"hourly" => storage_targets.each_with_object({}) { |o, h| h[o] = [[4.hours.ago.utc.beginning_of_day]] },
}
expect(queue_timings).to eq(expected_queue_items)
end
end

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

Expand Down Expand Up @@ -195,11 +193,11 @@
Timecop.freeze do
Metric::Capture.perf_capture_gap(7.days.ago.utc, 5.days.ago.utc)

targets = Metric::Targets.capture_ems_targets(@ems_vmware.reload, :exclude_storages => true)
expected = targets.flat_map { |t| [[t, "historical"]] * 2 } # Vm, Host, Host, Vm, Host
expected_queue_items = {
"historical" => targets.each_with_object({}) { |o, h| h[o] = date_range(7.days.ago.utc, 5.days.ago.utc, true) },
}

expect(queue_intervals).to match_array(expected)
expect(queue_timings).to eq(queue_timings_for_targets(targets, 7, 5, true))
expect(queue_timings).to eq(expected_queue_items)
end
end
end
Expand Down Expand Up @@ -1029,6 +1027,8 @@
MiqQueue.delete_all
end

let(:targets) { @vms_in_az + @vms_not_in_az }

context "executing capture_ems_targets" do
it "should find enabled targets" do
targets = Metric::Targets.capture_ems_targets(@ems_openstack)
Expand All @@ -1039,19 +1039,18 @@
context "executing perf_capture_timer" do
before do
stub_settings(:performance => {:history => {:initial_capture_days => 7}})
Metric::Capture.perf_capture_timer(@ems_openstack.id)
end

it "should queue up enabled targets" do
expected_queue_items = {
%w[ManageIQ::Providers::Openstack::CloudManager::Vm perf_capture_realtime] => 5,
%w[ManageIQ::Providers::Openstack::CloudManager::Vm perf_capture_historical] => 40,
%w[MiqTask destroy_older_by_condition] => 1,
}
expect(MiqQueue.group(:class_name, :method_name).count).to eq(expected_queue_items)
targets = Metric::Targets.capture_ems_targets(@ems_openstack.reload)
expect(queue_intervals).to match_array(queue_items_for_targets(targets))
expect(queue_timings).to eq(queue_timings_for_targets(targets))
Timecop.freeze do
Metric::Capture.perf_capture_timer(@ems_openstack.id)

expected_queue_items = {
"realtime" => targets.each_with_object({}) { |o, h| h[o] = [[4.hours.ago.utc.beginning_of_day]] },
"historical" => targets.each_with_object({}) { |o, h| h[o] = date_range },
}
expect(queue_timings).to eq(expected_queue_items)
end
end
end
end
Expand Down Expand Up @@ -1321,21 +1320,6 @@ def assert_perf_capture_now(target, mode)
end
end

def queue_items_for_targets(targets)
targets.flat_map do |t|
# Storage is hourly only
# Non-storage historical is expecting 7 days back, plus partial day = 8
t.kind_of?(Storage) ? [[t, "hourly"]] : [[t, "realtime"]] + [[t, "historical"]] * 8
end
end

def queue_intervals(items = MiqQueue.where(:method_name => %w[perf_capture_hourly perf_capture_realtime perf_capture_historical]))
items.map do |q|
interval_name = q.method_name.sub("perf_capture_", "")
[Object.const_get(q.class_name).find(q.instance_id), interval_name]
end
end

# for a given set of targets, determine the timings we think we should generate
#
def queue_timings_for_targets(targets, days_ago_start = 7, days_ago_end = -1, gap = false)
Expand All @@ -1349,7 +1333,7 @@ def queue_timings_for_targets(targets, days_ago_start = 7, days_ago_end = -1, ga
messages["realtime"][t] = [[4.hours.ago.utc.beginning_of_day]]
end
messages["historical"] ||= {}
messages["historical"][t] = [[days_ago_start.days.ago.utc, days_ago_end.days.ago.utc].map { |i| gap ? i : i.beginning_of_day} ]
messages["historical"][t] = date_range(days_ago_start, days_ago_end, gap)
end
end
end
Expand All @@ -1370,6 +1354,10 @@ def queue_timings(items = MiqQueue.where(:method_name => %w[perf_capture_hourly
end
end

def date_range(days_ago_start = 7.days.ago.utc, days_ago_end = -1.days.ago.utc, gap = false)
[[days_ago_start, days_ago_end].map { |i| gap ? i : i.beginning_of_day} ]
end

def combine_consecutive(a)
x = a.sort!.shift
a.each_with_object([]) do |i, ac|
Expand Down

0 comments on commit 60e5df4

Please sign in to comment.