Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add status for primary interface type #96

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/models/foreman_wreckingball/primary_interface_type_status.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/foreman_wreckingball/vmware_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/foreman_wreckingball/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions test/factories/foreman_wreckingball_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'cpuid.Intel'
]
end
primary_interface_type { 'VirtualE1000' }
host
end

Expand Down
2 changes: 1 addition & 1 deletion test/helpers/foreman_wreckingball/status_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion test/models/foreman_wreckingball/vmware_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down