Skip to content

Commit

Permalink
Merge branch 'SLE-15-SP2' into merge-SLE-15-SP2
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 23, 2021
2 parents be09edc + 7e64e43 commit 49679ad
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 47 deletions.
6 changes: 6 additions & 0 deletions package/yast2-network.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Thu Sep 23 10:01:21 UTC 2021 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- Consider aliases sections as case insensitive (bsc#1190739).
- 4.3.75

-------------------------------------------------------------------
Wed Sep 22 08:46:16 UTC 2021 - Michal Filka <mfilka@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-network.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-network
Version: 4.3.74
Version: 4.3.75
Release: 0
Summary: YaST2 - Network Configuration
License: GPL-2.0-only
Expand Down
18 changes: 9 additions & 9 deletions src/lib/y2network/autoinst/interfaces_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ def load_generic(config, interface_section)
config.ip = load_ipaddr(interface_section)

# handle aliases
interface_section.aliases.values.each_with_index do |alias_h, index|
next if alias_h.fetch("IPADDR", "").empty?
interface_section.aliases.each_with_index do |section, index|
next if section.ipaddr.to_s.empty?

config.ip_aliases << load_alias(alias_h, id: "_#{index}")
config.ip_aliases << load_alias(section, id: "_#{index}")
end

# startmode
Expand Down Expand Up @@ -207,16 +207,16 @@ def prefix_for(value)

# Loads and initializates an IP alias according to given hash with alias details
#
# @param alias_h [Hash] hash of AY profile's alias section as obtained from parser
# @param section[AliasSection] hash of AY profile's alias section as obtained from parser
#
# @return [ConnectionConfig::IPConfig] alias details
def load_alias(alias_h, id: nil)
ipaddr = IPAddress.from_string(alias_h["IPADDR"])
def load_alias(section, id: nil)
ipaddr = IPAddress.from_string(section.ipaddr)
# Assign first netmask, as prefixlen has precedence so it will overwrite it
ipaddr.prefix = prefix_for(alias_h["NETMASK"]) if !alias_h.fetch("NETMASK", "").empty?
ipaddr.prefix = prefix_for(alias_h["PREFIXLEN"]) if !alias_h.fetch("PREFIXLEN", "").empty?
ipaddr.prefix = prefix_for(section.netmask) unless section.netmask.to_s.empty?
ipaddr.prefix = prefix_for(section.prefixlen) unless section.prefixlen.to_s.empty?

ConnectionConfig::IPConfig.new(ipaddr, id: id, label: alias_h["LABEL"])
ConnectionConfig::IPConfig.new(ipaddr, id: id, label: section.label)
end

def load_wireless(config, interface_section)
Expand Down
91 changes: 91 additions & 0 deletions src/lib/y2network/autoinst_profile/alias_section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/autoinst_profile/section_with_attributes"

module Y2Network
module AutoinstProfile
# This class represents an alias specification within the <interface> section.
#
# <aliases>
# <alias0>
# <IPADDR>192.168.1.100</IPADDR>
# <LABEL>1</LABEL>
# <PREFIXLEN>24</PREFIXLEN>
# </alias0>
# </aliases>
#
# It is case insensitive.
#
# @see InterfaceSection
class AliasSection < SectionWithAttributes
def self.attributes
[
{ name: :ipaddr },
{ name: :label },
{ name: :prefixlen },
{ name: :netmask },
]
end

define_attr_accessors

# @!attribute ipaddr
# @return [String] IP address

# @!attribute label
# @return [String] alias label

# @!attribute prefixlen
# @return [String] prefix length
#
# @!attribute netmask
# @return [String] IP netmask

# Clones an IP config into an AutoYaST alias section
#
# @param ip_config [Y2Network::ConnectionConfig::IPConfig] Network connection config
# @return [AliasSection]
def self.new_from_network(config)
result = new
result.init_from_config(config)
result
end

# Method used by {.new_from_network} to populate the attributes when cloning an IP config
#
# @param config [Y2Network::ConnectionConfig]
# @return [Boolean]
def init_from_config(config)
@ipaddr = config.address&.address&.to_s
@label = config.label
@prefixlen = config.address&.prefix&.to_s
end

# Method used by {.new_from_hashes} to populate the attributes using a hash
#
# @param config [Hash]
# @return [Boolean]
def init_from_hashes(config)
normalized_config = config.each_with_object({}) { |(k, v), c| c[k.downcase] = v }
super(normalized_config)
end
end
end
end
40 changes: 13 additions & 27 deletions src/lib/y2network/autoinst_profile/interface_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# find current contact information at www.suse.com.

require "installation/autoinst_profile/section_with_attributes"
require "y2network/autoinst_profile/alias_section"

module Y2Network
module AutoinstProfile
Expand Down Expand Up @@ -167,23 +168,7 @@ def self.attributes
# @return [String] bonding options

# @!attribute aliases
# @example xml section for aliases from SLE15
# <aliases>
# <alias0>
# <IPADDR>10.100.0.1</IPADDR>
# <LABEL>test</LABEL>
# <NETMASK>255.255.255.0</NETMASK>
# <PREFIXLEN>24</PREFIXLEN>
# </alias0>
# <alias1>
# <IPADDR>10.100.0.2</IPADDR>
# <LABEL>test2</LABEL>
# <NETMASK>255.255.255.0</NETMASK>
# <PREFIXLEN>24</PREFIXLEN>
# </alias1>
# </aliases>
#
# @return [Object] aliases for interface
# @return [Array<AliasSection>] list of IP aliases

# @!attribute mtu
# @return [String] MTU for interface
Expand Down Expand Up @@ -273,15 +258,17 @@ def initialize(*_args)
public_send(:"#{attr[:name]}=", "")
end

self.aliases = {}
self.aliases = []
end

# Overwrite base method to load also nested aliases
def init_from_hashes(hash)
hash = rename_key(hash, "bridge_forwarddelay", "bridge_forward_delay")
super(hash)

self.aliases = hash["aliases"].is_a?(Hash) ? hash["aliases"] : {}
if hash["aliases"].is_a?(Hash)
self.aliases = hash["aliases"].values.map { |h| AliasSection.new_from_hashes(h) }
end
end

# Method used by {.new_from_network} to populate the attributes when cloning a network
Expand Down Expand Up @@ -313,13 +300,7 @@ def init_from_config(config)
@ethtool_options = config.ethtool_options if config.ethtool_options
@zone = config.firewall_zone.to_s
# see aliases for example output
@aliases = config.ip_aliases.each_with_index.each_with_object({}) do |(ip, index), res|
res["alias#{index}"] = {
"IPADDR" => ip.address.address.to_s,
"LABEL" => ip.label || "",
"PREFIXLEN" => ip.address.prefix.to_s
}
end
@aliases = config.ip_aliases.map { |ip| AliasSection.new_from_network(ip) }

case config
when ConnectionConfig::Vlan
Expand All @@ -345,7 +326,12 @@ def init_from_config(config)
# @see SectionWithAttributes#to_hashes
def to_hashes
hash = super
hash.delete("aliases") if hash.key?("aliases") && hash["aliases"].empty?
alias_sections = hash.delete("aliases")
if alias_sections && !alias_sections.empty?
hash["aliases"] = alias_sections.each_with_object({}).each_with_index do |(a, all), idx|
all["alias#{idx}"] = a
end
end
hash
end

Expand Down
67 changes: 67 additions & 0 deletions test/y2network/autoinst_profile/alias_section_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../test_helper"
require "y2network/autoinst_profile/alias_section"
require "y2network/connection_config/ip_config"

describe Y2Network::AutoinstProfile::AliasSection do
subject(:section) { described_class.new }

describe ".new_from_network" do
let(:config) do
Y2Network::ConnectionConfig::IPConfig.new(
Y2Network::IPAddress.from_string("10.100.0.1/24"), label: "test"
)
end

it "initializes values properly" do
section = described_class.new_from_network(config)
expect(section.ipaddr).to eq("10.100.0.1")
expect(section.prefixlen).to eq("24")
expect(section.label).to eq("test")
end
end

describe ".new_from_hashes" do
let(:hash) do
{ "ipaddr" => "10.100.0.1", "prefixlen" => "24", "label" => "test" }
end

it "returns a section with the corresponding values" do
section = described_class.new_from_hashes(hash)
expect(section.ipaddr).to eq("10.100.0.1")
expect(section.prefixlen).to eq("24")
expect(section.label).to eq("test")
end

context "when keys use capital letters" do
let(:hash) do
{ "IPADDR" => "10.100.0.1", "PREFIXLEN" => "24", "LABEL" => "test" }
end

it "returns a section with the corresponding values" do
section = described_class.new_from_hashes(hash)
expect(section.ipaddr).to eq("10.100.0.1")
expect(section.prefixlen).to eq("24")
expect(section.label).to eq("test")
end
end
end
end
21 changes: 11 additions & 10 deletions test/y2network/autoinst_profile/interface_section_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
c.ip_aliases = [
Y2Network::ConnectionConfig::IPConfig.new(
Y2Network::IPAddress.from_string("10.100.0.1/24"), label: "test"
),
Y2Network::ConnectionConfig::IPConfig.new(
Y2Network::IPAddress.from_string("10.100.0.2/24"), label: "test1"
)
]
end
Expand All @@ -56,11 +53,11 @@
expect(section.bootproto).to eq("static")
expect(section.ipaddr).to eq("10.100.0.1")
expect(section.prefixlen).to eq("24")
expect(section.lladdr).to eq("02:0b:0c:0d:0e:02")
expect(section.aliases).to eq(
"alias0" => { "IPADDR" => "10.100.0.1", "PREFIXLEN" => "24", "LABEL" => "test" },
"alias1" => { "IPADDR" => "10.100.0.2", "PREFIXLEN" => "24", "LABEL" => "test1" }
)

alias0 = section.aliases.first
expect(alias0.ipaddr).to eq("10.100.0.1")
expect(alias0.prefixlen).to eq("24")
expect(alias0.label).to eq("test")
end

it "sets the parent section" do
Expand Down Expand Up @@ -140,12 +137,16 @@
subject(:section) do
described_class.new_from_hashes(
"device" => "eth0",
"aliases" => { "alias0" => { "IPADDR" => "10.100.0.1", "PREFIXLEN" => "24" } }
"aliases" => aliases_hash
)
end

let(:aliases_hash) do
{ "alias0" => { "ipaddr" => "10.100.0.1", "prefixlen" => "24" } }
end

it "exports the aliases key" do
expect(section.to_hashes["aliases"]).to eq(section.aliases)
expect(section.to_hashes["aliases"]).to eq(aliases_hash)
end

context "when the list of aliases is empty" do
Expand Down

0 comments on commit 49679ad

Please sign in to comment.