Skip to content

Commit

Permalink
Added bonding connnection config
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jul 31, 2019
1 parent 9197e39 commit 9a3ec0c
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/y2network/connection_config/bonding.rb
Expand Up @@ -25,7 +25,7 @@ module ConnectionConfig
#
# @see https://www.kernel.org/doc/Documentation/networking/bonding.txt
class Bonding < Base
# @return [Array<Interface>]
# @return [Array<String>]
attr_accessor :slaves
# @return [String] bond driver options
attr_accessor :options
Expand Down
49 changes: 49 additions & 0 deletions src/lib/y2network/sysconfig/connection_config_readers/bonding.rb
@@ -0,0 +1,49 @@
# Copyright (c) [2019] 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/sysconfig/connection_config_readers/base"

module Y2Network
module Sysconfig
module ConnectionConfigReaders
# This class is able to build a ConnectionConfig::Bonding object given a
# SysconfigInterfaceFile object.
class Bonding < Base
private

# @see Y2Network::Sysconfig::ConnectionConfigReaders::Base#update_connection_config
def update_connection_config(conn)
conn.slaves = slaves
conn.options = file.bonding_module_opts
end

# Convenience method to obtain the bonding slaves defined in the file
#
# @return [Array<String>] bonding slaves defined in the file
def slaves
return [] unless file.bonding_slaves

file.bonding_slaves.map do |_id, name|
name
end
end
end
end
end
end
49 changes: 49 additions & 0 deletions src/lib/y2network/sysconfig/connection_config_writers/bonding.rb
@@ -0,0 +1,49 @@
# Copyright (c) [2019] 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/sysconfig/connection_config_writers/base"

module Y2Network
module Sysconfig
module ConnectionConfigWriters
# This class is responsible for writing the information from a ConnectionConfig::Bonding
# object to the underlying system.
class Bonding < Base
private

# @see Y2Network::ConnectionConfigWriters::Base#update_file
# @param conn [Y2Network::ConnectionConfig::Bonding] Configuration to write
def update_file(conn)
file.bonding_slaves = file_slaves(conn)
file.bonding_module_opts = conn.options
end

# Convenience method to obtain the map of bonding slaves in the file
# format
#
# @return [Hash<Integer, String>] indexed bonding slaves
def file_slaves(conn)
{}.tap do |h|
conn.slaves.each_with_index { |name, i| h[i] = name }
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions src/lib/y2network/sysconfig/interface_file.rb
Expand Up @@ -254,6 +254,17 @@ def variable_name(param_name)
# @return [String] VLAN ID
define_variable(:vlan_id, :integer)

## BONDING

# @!attribute [r] bonding_slaves
# @return [Array<String>] Bonding slaves
define_collection_variable(:bonding_slave)

# @!attribute [r] bonding_module_opts
# @return [String] options for the bonding module ('mode=active-backup
# miimon=100')
define_variable(:bonding_module_opts)

## BRIDGE

# @!attribute [r] bridge
Expand Down
6 changes: 6 additions & 0 deletions test/data/scr_read/etc/sysconfig/network/ifcfg-bond0
@@ -0,0 +1,6 @@
STARTMODE=auto
BOOTPROTO=static
IPADDR=192.168.20.100/24
BONDING_SLAVE1=eth0
BONDING_SLAVE2=eth1
BONDING_MODULE_OPTS='mode=active-backup miimon=100'
46 changes: 46 additions & 0 deletions test/y2network/sysconfig/connection_config_readers/bonding_test.rb
@@ -0,0 +1,46 @@
# Copyright (c) [2019] 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/sysconfig/connection_config_readers/bonding"
require "y2network/sysconfig/interface_file"

describe Y2Network::Sysconfig::ConnectionConfigReaders::Bonding do
subject(:handler) { described_class.new(file) }

let(:scr_root) { File.join(DATA_PATH, "scr_read") }

around do |example|
change_scr_root(scr_root, &example)
end

let(:interface_name) { "bond0" }
let(:file) do
Y2Network::Sysconfig::InterfaceFile.find(interface_name).tap(&:load)
end

describe "#connection_config" do
it "returns a bonding connection config object" do
bonding_conn = handler.connection_config
expect(bonding_conn.interface).to eq("bond0")
expect(bonding_conn.slaves).to eq(["eth0", "eth1"])
expect(bonding_conn.options).to eq("mode=active-backup miimon=100")
end
end
end
63 changes: 63 additions & 0 deletions test/y2network/sysconfig/connection_config_writers/bonding_test.rb
@@ -0,0 +1,63 @@
# Copyright (c) [2019] 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/sysconfig/connection_config_writers/bonding"
require "y2network/sysconfig/interface_file"
require "y2network/startmode"
require "y2network/connection_config/bonding"

describe Y2Network::Sysconfig::ConnectionConfigWriters::Bonding do
subject(:handler) { described_class.new(file) }

let(:conn) do
instance_double(
Y2Network::ConnectionConfig::Bonding,
name: "bond0",
interface: "bond0",
description: "",
ip_configs: [],
startmode: Y2Network::Startmode.create("auto"),
bootproto: Y2Network::BootProtocol::DHCP,
slaves: ["eth0", "eth1"],
options: "mode=active-backup miimon=100"
)
end

let(:file) { Y2Network::Sysconfig::InterfaceFile.new(conn.name) }

describe "#write" do
it "writes common properties" do
handler.write(conn)
expect(file).to have_attributes(
startmode: "auto",
bootproto: "dhcp"
)
end

it "writes bonding properties" do
handler.write(conn)
expect(file).to have_attributes(
bonding_slaves: { 0 => "eth0", 1 => "eth1" },
bonding_module_opts: "mode=active-backup miimon=100"
)
end
end
end

0 comments on commit 9a3ec0c

Please sign in to comment.