From 2748290d86108598c500ea88bf2838c3388d8f0a Mon Sep 17 00:00:00 2001 From: Manisha Singhal Date: Tue, 30 Jul 2024 08:02:40 +0200 Subject: [PATCH] fix: Fix network interface capitalize issue and add test to flatten nic --- .../foreman_fog_proxmox/proxmox_interfaces.rb | 2 +- .../helpers/proxmox_container_helper_test.rb | 46 +++++++++++++++++++ .../proxmox_interfaces_test.rb | 4 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/models/foreman_fog_proxmox/proxmox_interfaces.rb b/app/models/foreman_fog_proxmox/proxmox_interfaces.rb index 9d98125a5..7f5a1a665 100644 --- a/app/models/foreman_fog_proxmox/proxmox_interfaces.rb +++ b/app/models/foreman_fog_proxmox/proxmox_interfaces.rb @@ -118,7 +118,7 @@ def dhcp?(nic_compute_attributes, v6 = false) def set_mac(nic_compute_attributes, mac, type) mac_attr_name = { 'qemu' => :macaddr, 'lxc' => :hwaddr } mac_key = mac_attr_name[type] || 'mac' - nic_compute_attributes[mac_key] = Net::Validations.normalize_mac(mac) + nic_compute_attributes[mac_key] = Net::Validations.normalize_mac(mac).upcase end def host_interfaces_attrs(host) diff --git a/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb b/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb index c63c99ca1..2ba79ac4d 100644 --- a/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +++ b/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb @@ -84,6 +84,43 @@ class ProxmoxContainerHelperTest < ActiveSupport::TestCase } } end + let(:host_edit) do + { 'vmid' => '123', + 'node_id' => 'proxmox-noris', + 'config_attributes' => { + 'ostype' => 'alpine', + 'hostname' => 'test-container.example.com', + 'arch' => 'amd64', + 'cores' => '1', + 'memory' => '1024', + 'swap' => '512', + }, + 'ostemplate_storage' => 'local', + 'ostemplate_file' => 'local:vztmpl/alpine_amd64.tar.xz', + 'volumes_attributes' => { + '0' => { + 'storage' => 'local', + 'size' => '8', + 'id' => 'rootfs', + 'volid' => 'local:123/vm-123-disk-0.raw', + }, + }, + 'interfaces_attributes' => { + '0' => { + 'id' => 'net0', + 'name' => 'eth0', + 'firewall' => '0', + 'bridge' => 'vmbr0', + 'hwaddr' => '8E:96:2A:D3:42:80', + 'ip' => 'dhcp', + 'ip6' => '', + }, + }, + 'name' => 'test-container.example.com', + 'provision_method' => 'build', + 'firmware_type' => :bios } + end + let(:container) do { 'vmid' => '100', :vmid => '100', @@ -183,6 +220,15 @@ class ProxmoxContainerHelperTest < ActiveSupport::TestCase nics[0][:net0] end + test '#interface with name eth0 and bridge in host edit' do + deletes = [] + nics = [] + add_or_delete_typed_interface(host_edit['interfaces_attributes']['0'], deletes, nics, type) + assert 1, nics.length + assert nics[0].key?(:net0) + assert_equal 'hwaddr=8E:96:2A:D3:42:80,bridge=vmbr0,name=eth0,ip=dhcp,firewall=0', nics[0][:net0] + end + test '#interface with name eth1 and bridge' do deletes = [] nics = [] diff --git a/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb b/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb index 7e4365a88..0115eff2c 100644 --- a/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb +++ b/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb @@ -54,7 +54,7 @@ class ProxmoxInterfacesTest < ActiveSupport::TestCase it 'sets interface identifier with server compute id, ip and ip6 and mac adress' do ip = '192.168.56.100' - mac_address = '36:25:8c:53:0c:50' + mac_address = '36:25:8C:53:0C:50' ip6 = Array.new(4) { format('%s', x: rand(16**4)) }.join(':') + '::1' compute_attributes = ActiveSupport::HashWithIndifferentAccess.new({ 'id' => 'net0' }) physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :ip => ip, :ip6 => ip6, @@ -131,7 +131,7 @@ class ProxmoxInterfacesTest < ActiveSupport::TestCase it 'sets container compute ip DHCP, mac adress and firewall' do ip = '192.168.56.100' - mac_address = '36:25:8c:53:0c:50' + mac_address = '36:25:8C:53:0C:50' ip6 = '2001:0:1234::c1c0:abcd:876' firewall = '1' compute_attributes = ActiveSupport::HashWithIndifferentAccess.new({ 'id' => 'net0', 'dhcp' => '1',