Skip to content

Commit

Permalink
Add a configuration writer for NetworkManager
Browse files Browse the repository at this point in the history
* It just writes some basic information.
  • Loading branch information
imobachgs committed Jan 14, 2021
1 parent 144ad48 commit 62fac51
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/lib/y2network/network_manager/config_writer.rb
@@ -0,0 +1,43 @@
# 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/config_writer"
require "y2network/network_manager/connection_config_writer"

module Y2Network
module NetworkManager
# This class configures NetworkManager according to a given configuration
class ConfigWriter < Y2Network::ConfigWriter
private # rubocop:disable Layout/IndentationWidth

# Writes connections configuration
#
# @todo Handle old connections (removing those that are not needed, etc.)
#
# @param config [Y2Network::Config] Current config object
# @param _old_config [Y2Network::Config,nil] Config object with original configuration
def write_connections(config, _old_config)
writer = Y2Network::NetworkManager::ConnectionConfigWriter.new
config.connections.each do |conn|
writer.write(conn, nil) # FIXME
end
end
end
end
end
58 changes: 58 additions & 0 deletions src/lib/y2network/network_manager/connection_config_writer.rb
@@ -0,0 +1,58 @@
# 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 "cfa/nm_connection"
require "pathname"

module Y2Network
module NetworkManager
class ConnectionConfigWriter
include Yast::Logger

SYSTEM_CONNECTIONS_PATH = Pathname.new("/etc/NetworkManager/system-connections").freeze
FILE_EXT = ".nmconnection".freeze

def write(conn, old_conn = nil)
return if conn == old_conn

file = CFA::NmConnection.new(SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT))
handler_class = find_handler_class(conn.type)
return nil if handler_class.nil?

handler_class.new(file).write(conn)
file.save
end

private

# Returns the class to handle a given interface type
#
# @param type [Y2Network::InterfaceType] Interface type
# @return [Class] A class which belongs to the ConnectionConfigWriters module
def find_handler_class(type)
require "y2network/network_manager/connection_config_writers/#{type.file_name}"
ConnectionConfigWriters.const_get(type.class_name)
rescue LoadError, NameError => e
log.info "Unknown connection type: '#{type}'. " \
"Connection handler could not be loaded: #{e.message}"
nil
end
end
end
end
@@ -0,0 +1,56 @@
# 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.

module Y2Network
module NetworkManager
module ConnectionConfigWriters
# Base class for connection config writers.
#
# The derived classes should implement a {#update_file} method.
class Base
# @return [CFA::NmConnection] Connection configuration file
attr_reader :file

# Constructor
#
# @param file [CFA::NmConnection] Connection configuration file
def initialize(file)
@file = file
end

# Writes connection information to the interface configuration file
#
# @param conn [Y2Network::ConnectionConfig::Base] Connection to take settings from
def write(conn)
file.connection["id"] = conn.name
update_file(conn)
end

private

# Sets file values from the given connection configuration
#
# @note This method should be redefined by derived classes.
#
# @param _conn [Y2Network::ConnectionConfig::Base]
def update_file(_conn); end
end
end
end
end
@@ -0,0 +1,33 @@
# 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/network_manager/connection_config_writers/base"

module Y2Network
module NetworkManager
module ConnectionConfigWriters
# This class is responsible for writing the information from a ConnectionConfig::Ethernet
# object to the underlying system.
class Ethernet < Base
# @see Y2Network::ConnectionConfigWriters::Base#update_file
def update_file(_conn); end
end
end
end
end
75 changes: 75 additions & 0 deletions test/y2network/network_manager/config_writer_test.rb
@@ -0,0 +1,75 @@
# 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/network_manager/config_writer"
require "y2network/config"
require "y2network/connection_configs_collection"
require "y2network/interface"
require "y2network/interfaces_collection"

describe Y2Network::NetworkManager::ConfigWriter do
subject(:writer) { described_class.new }

describe "#write" do
let(:old_config) do
Y2Network::Config.new(
source: :network_manager,
interfaces: Y2Network::InterfacesCollection.new([eth0]),
connections: Y2Network::ConnectionConfigsCollection.new([])
)
end

let(:config) do
old_config.copy.tap do |cfg|
cfg.add_or_update_connection_config(eth0_conn)
end
end

let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
conn.bootproto = :static
conn.ip = ip
end
end

let(:ip) { Y2Network::ConnectionConfig::IPConfig.new(address: IPAddr.new("192.168.122.2")) }
let(:eth0) { Y2Network::Interface.new("eth0") }

let(:conn_config_writer) do
instance_double(Y2Network::NetworkManager::ConnectionConfigWriter, write: nil)
end

before do
allow(Y2Network::NetworkManager::ConnectionConfigWriter).to receive(:new)
.and_return(conn_config_writer)
allow(writer).to receive(:write_dns)
allow(writer).to receive(:write_drivers)
allow(writer).to receive(:write_hostname)
allow(writer).to receive(:write_routing)
end

it "writes connections configuration" do
expect(conn_config_writer).to receive(:write).with(eth0_conn, nil)
writer.write(config)
end
end
end
80 changes: 80 additions & 0 deletions test/y2network/network_manager/connection_config_writer_test.rb
@@ -0,0 +1,80 @@
# 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/network_manager/connection_config_writer"
require "y2network/network_manager/connection_config_writers/ethernet"
require "y2network/connection_config/ethernet"
require "y2network/interface_type"
require "cfa/nm_connection"

describe Y2Network::NetworkManager::ConnectionConfigWriter do
subject(:writer) { described_class.new }

let(:conn) do
instance_double(
Y2Network::ConnectionConfig::Ethernet,
name: "eth0",
interface: "eth0",
type: Y2Network::InterfaceType::ETHERNET,
ip: ip_config
)
end

let(:old_conn) do
instance_double(
Y2Network::ConnectionConfig::Ethernet,
name: "eth0",
interface: "eth0",
type: Y2Network::InterfaceType::ETHERNET
)
end

let(:ip_config) do
Y2Network::ConnectionConfig::IPConfig.new(Y2Network::IPAddress.from_string("10.100.0.1/24"))
end

let(:file) do
instance_double(CFA::NmConnection, save: nil)
end

describe "#write" do
let(:handler) do
instance_double(
Y2Network::NetworkManager::ConnectionConfigWriters::Ethernet, write: nil
)
end

before do
allow(writer).to receive(:require).and_call_original
allow(Y2Network::NetworkManager::ConnectionConfigWriters::Ethernet).to receive(:new)
.and_return(handler)
allow(CFA::NmConnection).to receive(:new).and_return(file)
end

it "uses the appropiate handler" do
expect(writer).to receive(:require).and_return(handler)
expect(handler).to receive(:write).with(conn)
writer.write(conn)
end

it "does nothing if the connection has not changed"
end
end

0 comments on commit 62fac51

Please sign in to comment.