|
1 | 1 | describe ManageIQ::Providers::Kubernetes::ContainerManager::MetricsCapture do |
2 | | - before do |
3 | | - # @miq_server is required for worker_settings to work |
4 | | - @miq_server = EvmSpecHelper.local_miq_server(:is_master => true) |
5 | | - @ems_kubernetes = FactoryBot.create( |
6 | | - :ems_kubernetes, |
7 | | - :connection_configurations => [{:endpoint => {:role => :prometheus}, |
8 | | - :authentication => {:role => :prometheus}}], |
9 | | - ).tap { |ems| ems.authentications.each { |auth| auth.update!(:status => "Valid") } } |
10 | | - @container_project = FactoryBot.create(:container_project, :ext_management_system => @ems_kubernetes) |
11 | | - |
12 | | - @node = FactoryBot.create( |
13 | | - :kubernetes_node, |
14 | | - :name => 'node', |
15 | | - :ext_management_system => @ems_kubernetes, |
16 | | - :ems_ref => 'target' |
17 | | - ) |
18 | | - |
19 | | - @node.computer_system.hardware = FactoryBot.create( |
20 | | - :hardware, |
21 | | - :cpu_total_cores => 2, |
22 | | - :memory_mb => 2048 |
23 | | - ) |
24 | | - |
25 | | - @group = FactoryBot.create( |
26 | | - :container_group, |
27 | | - :ext_management_system => @ems_kubernetes, |
28 | | - :container_node => @node, |
29 | | - :ems_ref => 'group' |
30 | | - ) |
31 | | - |
32 | | - @container = FactoryBot.create( |
33 | | - :kubernetes_container, |
34 | | - :name => 'container', |
35 | | - :container_group => @group, |
36 | | - :container_project => @container_project, |
37 | | - :ext_management_system => @ems_kubernetes, |
38 | | - :ems_ref => 'target' |
39 | | - ) |
| 2 | + let(:ems) { FactoryBot.create(:ems_kubernetes_with_zone, :with_metrics_endpoint) } |
| 3 | + let(:container_project) { FactoryBot.create(:container_project, :ext_management_system => ems) } |
| 4 | + let!(:group) { FactoryBot.create(:kubernetes_container_group, :ext_management_system => ems, :container_node => node) } |
| 5 | + let!(:container) { FactoryBot.create(:kubernetes_container, :ext_management_system => ems, :container_group => group, :container_project => container_project) } |
| 6 | + let(:node) do |
| 7 | + FactoryBot.create(:kubernetes_node, :name => 'node', :ext_management_system => ems, :ems_ref => 'target').tap do |node| |
| 8 | + node.computer_system.hardware = FactoryBot.create(:hardware, :cpu_total_cores => 2, :memory_mb => 2_048) |
| 9 | + end |
40 | 10 | end |
41 | 11 |
|
42 | 12 | context "#perf_capture_object" do |
43 | 13 | it "returns the correct class" do |
44 | | - expect(@ems_kubernetes.perf_capture_object.class).to eq(described_class) |
| 14 | + expect(ems.perf_capture_object.class).to eq(described_class) |
45 | 15 | end |
46 | 16 | end |
47 | 17 |
|
48 | | - context "#capture_context" do |
| 18 | + context "#build_capture_context!" do |
49 | 19 | it "detect prometheus metrics provider" do |
50 | | - metric_capture = described_class.new(@node) |
51 | | - context = metric_capture.capture_context( |
52 | | - @ems_kubernetes, |
53 | | - @node, |
54 | | - 5.minutes.ago, |
55 | | - 0.minutes.ago |
56 | | - ) |
| 20 | + metric_capture = described_class.new(node) |
| 21 | + context = metric_capture.build_capture_context!(ems, node, 5.minutes.ago, 0.minutes.ago) |
57 | 22 |
|
58 | 23 | expect(context).to be_a(described_class::PrometheusCaptureContext) |
59 | 24 | end |
| 25 | + |
| 26 | + context "on an invalid target" do |
| 27 | + let(:group) { FactoryBot.create(:kubernetes_container_group, :ext_management_system => ems) } |
| 28 | + |
| 29 | + it "raises an exception" do |
| 30 | + metric_capture = described_class.new(group) |
| 31 | + expect { metric_capture.build_capture_context!(ems, group, 5.minutes.ago, 0.minutes.ago) } |
| 32 | + .to raise_error(described_class::TargetValidationWarning, "no associated node") |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context "#perf_capture_all_queue" do |
| 38 | + it "returns the objects" do |
| 39 | + expect(ems.perf_capture_object.perf_capture_all_queue).to include("Container" => [container], "ContainerGroup" => [group], "ContainerNode" => [node]) |
| 40 | + end |
| 41 | + |
| 42 | + context "with a missing metrics endpoint" do |
| 43 | + let(:ems) { FactoryBot.create(:ems_kubernetes) } |
| 44 | + |
| 45 | + it "returns no objects" do |
| 46 | + expect(ems.perf_capture_object.perf_capture_all_queue).to be_empty |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "with invalid authentication on the metrics endpoint" do |
| 51 | + let(:ems) { FactoryBot.create(:ems_kubernetes_with_zone, :with_metrics_endpoint, :with_invalid_auth) } |
| 52 | + |
| 53 | + it "returns no objects" do |
| 54 | + expect(ems.perf_capture_object.perf_capture_all_queue).to be_empty |
| 55 | + end |
| 56 | + end |
60 | 57 | end |
61 | 58 |
|
62 | 59 | context "#perf_collect_metrics" do |
63 | 60 | it "fails when no ems is defined" do |
64 | | - @node.ext_management_system = nil |
65 | | - expect { @node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
| 61 | + node.ext_management_system = nil |
| 62 | + expect { node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
66 | 63 | end |
67 | 64 |
|
68 | 65 | it "fails when no cpu cores are defined" do |
69 | | - @node.hardware.cpu_total_cores = nil |
70 | | - expect { @node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
| 66 | + node.hardware.cpu_total_cores = nil |
| 67 | + expect { node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
71 | 68 | end |
72 | 69 |
|
73 | 70 | it "fails when memory is not defined" do |
74 | | - @node.hardware.memory_mb = nil |
75 | | - expect { @node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
| 71 | + node.hardware.memory_mb = nil |
| 72 | + expect { node.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError) |
76 | 73 | end |
77 | 74 | end |
78 | 75 | end |
0 commit comments