From 8b6c75024915bafb0fae8a2c0b800c7fec532cda Mon Sep 17 00:00:00 2001 From: Timo Goebel Date: Thu, 26 Dec 2019 15:16:21 +0100 Subject: [PATCH] add status for primary interface type --- .../primary_interface_type_status.rb | 70 +++++++++++++++ .../foreman_wreckingball/vmware_facet.rb | 1 + ...primary_interface_type_to_vmware_facets.rb | 7 ++ lib/foreman_wreckingball/engine.rb | 3 +- .../foreman_wreckingball_factories.rb | 1 + .../foreman_wreckingball/status_helper.rb | 2 +- .../primary_interface_type_status_test.rb | 88 +++++++++++++++++++ .../foreman_wreckingball/vmware_facet_test.rb | 4 +- 8 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 app/models/foreman_wreckingball/primary_interface_type_status.rb create mode 100644 db/migrate/20191226150709_add_primary_interface_type_to_vmware_facets.rb create mode 100644 test/models/foreman_wreckingball/primary_interface_type_status_test.rb diff --git a/app/models/foreman_wreckingball/primary_interface_type_status.rb b/app/models/foreman_wreckingball/primary_interface_type_status.rb new file mode 100644 index 0000000..2ef5eab --- /dev/null +++ b/app/models/foreman_wreckingball/primary_interface_type_status.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +module ForemanWreckingball + class PrimaryInterfaceTypeStatus < ::HostStatus::Status + OK = 0 + WARNING = 1 + + def self.status_name + N_('VM Primary Interface Type') + end + + def self.host_association + :vmware_primary_interface_type_status_object + end + + def self.description + N_('In order to use full network speed, your VM should be configured with a paravirtualized network card driver such as VMXNET 3.') + end + + def self.supports_remediate? + false + end + + #def self.dangerous_remediate? + # true + #end + + #def self.remediate_action + # ::Actions::ForemanWreckingball::Host::RemediateHardwareVersion + #end + + def to_status(_options = {}) + uses_e1000? ? WARNING : OK + end + + def to_global(_options = {}) + self.class.to_global(status) + end + + def self.to_global(status) + case status + when WARNING + HostStatus::Global::WARN + else + HostStatus::Global::OK + end + end + + def self.global_ok_list + [OK] + end + + def to_label(_options = {}) + case status + when WARNING + N_('Using slow E1000 driver') + else + N_('OK') + end + end + + def relevant?(_options = {}) + host && host&.vmware_facet && host.vmware_facet.primary_interface_type.present? + end + + def uses_e1000? + host.vmware_facet.primary_interface_type == 'VirtualE1000' + end + end +end diff --git a/app/models/foreman_wreckingball/vmware_facet.rb b/app/models/foreman_wreckingball/vmware_facet.rb index 5cf23da..5b11226 100644 --- a/app/models/foreman_wreckingball/vmware_facet.rb +++ b/app/models/foreman_wreckingball/vmware_facet.rb @@ -53,6 +53,7 @@ def refresh! cpu_features: [] } data_for_update[:cpu_features] = raw_vm_object.runtime.featureRequirement.map(&:key) if vm.ready? + data_for_update[:primary_interface_type] = vm.interfaces.find { |vm_interface| vm_interface.mac == host.primary_interface.mac }&.type&.to_s if host.primary_interface update(data_for_update) end diff --git a/db/migrate/20191226150709_add_primary_interface_type_to_vmware_facets.rb b/db/migrate/20191226150709_add_primary_interface_type_to_vmware_facets.rb new file mode 100644 index 0000000..2554312 --- /dev/null +++ b/db/migrate/20191226150709_add_primary_interface_type_to_vmware_facets.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddPrimaryInterfaceTypeToVmwareFacets < ActiveRecord::Migration[5.1] + def change + add_column :vmware_facets, :primary_interface_type, :string, index: true, null: true + end +end diff --git a/lib/foreman_wreckingball/engine.rb b/lib/foreman_wreckingball/engine.rb index 8046bcc..b16525d 100644 --- a/lib/foreman_wreckingball/engine.rb +++ b/lib/foreman_wreckingball/engine.rb @@ -12,7 +12,8 @@ class Engine < ::Rails::Engine 'ForemanWreckingball::OperatingsystemStatus', 'ForemanWreckingball::CpuHotAddStatus', 'ForemanWreckingball::SpectreV2Status', - 'ForemanWreckingball::HardwareVersionStatus' + 'ForemanWreckingball::HardwareVersionStatus', + 'ForemanWreckingball::PrimaryInterfaceTypeStatus' ].freeze config.autoload_paths += Dir["#{config.root}/app/lib"] diff --git a/test/factories/foreman_wreckingball_factories.rb b/test/factories/foreman_wreckingball_factories.rb index 6f5cd01..9a3c492 100644 --- a/test/factories/foreman_wreckingball_factories.rb +++ b/test/factories/foreman_wreckingball_factories.rb @@ -35,6 +35,7 @@ 'cpuid.Intel' ] end + primary_interface_type { 'VirtualE1000' } host end diff --git a/test/helpers/foreman_wreckingball/status_helper.rb b/test/helpers/foreman_wreckingball/status_helper.rb index 6cfd6b7..95a50f0 100644 --- a/test/helpers/foreman_wreckingball/status_helper.rb +++ b/test/helpers/foreman_wreckingball/status_helper.rb @@ -4,7 +4,7 @@ module ForemanWreckingball module StatusHelper def assert_statuses(expected) actual = request.env['action_controller.instance'].instance_variable_get('@statuses') - assert_equal expected, actual + assert_same_elements expected, actual end end end diff --git a/test/models/foreman_wreckingball/primary_interface_type_status_test.rb b/test/models/foreman_wreckingball/primary_interface_type_status_test.rb new file mode 100644 index 0000000..e301f60 --- /dev/null +++ b/test/models/foreman_wreckingball/primary_interface_type_status_test.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require 'test_plugin_helper' + +module ForemanWreckingball + class PrimaryInterfaceTypeStatusTest < ActiveSupport::TestCase + setup do + User.current = users(:admin) + Setting::Wreckingball.load_defaults + end + + should belong_to(:host) + + let(:host) do + FactoryBot.create( + :host, + :managed, + :with_vmware_facet + ) + end + let(:status) { ForemanWreckingball::PrimaryInterfaceTypeStatus.new(host: host) } + + test 'has a host association' do + status.save! + assert_equal status, host.public_send(status.class.host_association) + end + + test '#relevant is only for hosts with a vmware facet' do + h = FactoryBot.build(:host, :managed) + refute ForemanWreckingball::PrimaryInterfaceTypeStatus.new(host: h).relevant? + assert status.relevant? + end + + test '#relevant is for hosts with a primary interface type' do + assert status.relevant? + host.vmware_facet.primary_interface_type = nil + refute status.relevant? + end + + describe 'status calculation' do + test 'is warning when driver is e1000' do + status.host.vmware_facet.primary_interface_type = 'VirtualE1000' + assert_equal PrimaryInterfaceTypeStatus::WARNING, status.to_status + end + + test 'is ok when driver is vmxnet3' do + status.host.vmware_facet.primary_interface_type = 'VirtualVmxnet3' + assert_equal PrimaryInterfaceTypeStatus::OK, status.to_status + end + end + + describe 'status labels' do + test 'when driver is e1000' do + status.status = PrimaryInterfaceTypeStatus::WARNING + assert_equal 'Using slow E1000 driver', status.to_label + end + + test 'when driver is vmxnet3' do + status.status = PrimaryInterfaceTypeStatus::OK + assert_equal 'OK', status.to_label + end + end + + describe 'global status' do + test 'is warning when driver is not paravirtualized' do + status.status = PrimaryInterfaceTypeStatus::WARNING + assert_equal HostStatus::Global::WARN, status.to_global + end + + test 'is ok when driver is paravirtualized' do + status.status = PrimaryInterfaceTypeStatus::OK + assert_equal HostStatus::Global::OK, status.to_global + end + end + + describe '#uses_e1000?' do + test 'is true when primary interface driver is E1000' do + status.host.vmware_facet.primary_interface_type = 'VirtualE1000' + assert status.uses_e1000? + end + + test 'is false when primary interface driver is Vmxnet3' do + status.host.vmware_facet.primary_interface_type = 'VirtualVmxnet3' + refute status.uses_e1000? + end + end + end +end diff --git a/test/models/foreman_wreckingball/vmware_facet_test.rb b/test/models/foreman_wreckingball/vmware_facet_test.rb index d039298..af931ce 100644 --- a/test/models/foreman_wreckingball/vmware_facet_test.rb +++ b/test/models/foreman_wreckingball/vmware_facet_test.rb @@ -55,7 +55,8 @@ class VmwareFacetTest < ActiveSupport::TestCase :managed, :with_vmware_facet, compute_resource: compute_resource, - uuid: uuid + uuid: uuid, + mac: '00:50:56:a9:00:28' ) end let(:vmware_facet) { host.vmware_facet } @@ -82,6 +83,7 @@ class VmwareFacetTest < ActiveSupport::TestCase assert_equal false, vmware_facet.cpu_hot_add assert_equal ['cpuid.SSE3', 'cpuid.AES', 'cpuid.Intel'], vmware_facet.cpu_features assert_equal 'vmx-9', vmware_facet.hardware_version + assert_equal 'VirtualE1000', vmware_facet.primary_interface_type end end end