Skip to content

Commit

Permalink
Added vlan support and added wireless write unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jan 20, 2021
1 parent a895de2 commit a51bf03
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/lib/cfa/nm_connection.rb
Expand Up @@ -28,17 +28,22 @@ module CFA
# file.load
# puts file.connection["id"]
class NmConnection < BaseModel
KNOWN_SECTIONS = [
"bridge", "connection", "ethernet", "ipv4", "ipv6", "vlan", "wifi", "wifi_security"
].freeze

KNOWN_SECTIONS = ["connection", "ethernet", "ipv4", "ipv6", "wifi", "wifi_security"].freeze
# @return [String] file path
attr_reader :path

# Constructor
#
# @param path [String] File path
# @param file_handler [.read, .write] Object to read/write the file.
def initialize(path, file_handler: nil)
@path = path
# FIXME: The Networkmanager lense writes the values surrounded by double
# quotes which is not valid
super(AugeasParser.new("Puppet.lns"), path, file_handler: file_handler)
super(AugeasParser.new("Puppet.lns"), @path, file_handler: file_handler)
end

# Returns the augeas tree for the given section
Expand Down Expand Up @@ -75,7 +80,7 @@ def add_collection(section, name, values)
section = section_for(section)

values.each_with_index do |ip, index|
section["#{name}#{index+1}"] = ip
section["#{name}#{index + 1}"] = ip
end
end

Expand Down
10 changes: 9 additions & 1 deletion src/lib/y2network/network_manager/connection_config_writer.rb
Expand Up @@ -31,16 +31,24 @@ class ConnectionConfigWriter
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))
path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
file = CFA::NmConnection.new(path)
handler_class = find_handler_class(conn.type)
return nil if handler_class.nil?

ensure_permissions(path) unless ::File.exist?(path)

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

private

def ensure_permissions(path)
::FileUtils.touch(path)
::File.chmod(0o600, path)
end

# Returns the class to handle a given interface type
#
# @param type [Y2Network::InterfaceType] Interface type
Expand Down
@@ -0,0 +1,37 @@
# 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::Vlan
# object to the underlying system.
class Vlan < Base
# @see Y2Network::ConnectionConfigWriters::Base#update_file
# @param conn [Y2Network::ConnectionConfig::Vlan] Configuration to write
def update_file(conn)
file.vlan["id"] = conn.vlan_id
file.vlan["parent"] = conn.parent_device
end
end
end
end
end
Expand Up @@ -41,6 +41,12 @@ def update_file(conn)
#
# @param conn [Y2Network::ConnectionConfig::Base] Configuration to write
def write_eap_auth_settings(_conn)
# FIXME: incomplete
file.wifi_security["key-mgmt"] = "wpa-eap"
# wrong section name
#
# file.802_1x["eap"] = conn.eap_mode
# file.802_1x["phase2-auth"] = conn.eap_auth
end

# Writes authentication settings
Expand Down Expand Up @@ -70,8 +76,7 @@ def write_psk_auth_settings(conn)
# Writes autentication settings for WEP networks
#
# @param conn [Y2Network::ConnectionConfig::Base] Configuration to write
def write_shared_auth_settings(_conn)
end
def write_shared_auth_settings(_conn); end
end
end
end
Expand Down
17 changes: 16 additions & 1 deletion test/y2network/network_manager/connection_config_writer_test.rb
Expand Up @@ -51,8 +51,10 @@
Y2Network::ConnectionConfig::IPConfig.new(Y2Network::IPAddress.from_string("10.100.0.1/24"))
end

let(:path) { "/etc/NetworkManager/system-connections/eth0.nmconnection" }

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

describe "#write" do
Expand All @@ -67,6 +69,8 @@
allow(Y2Network::NetworkManager::ConnectionConfigWriters::Ethernet).to receive(:new)
.and_return(handler)
allow(CFA::NmConnection).to receive(:new).and_return(file)
allow(writer).to receive(:ensure_permissions)
allow(::File).to receive(:exist?).with(Pathname.new(path)).and_return(true)
end

it "uses the appropiate handler" do
Expand All @@ -75,6 +79,17 @@
writer.write(conn)
end

context "when the file does not exist" do
before do
allow(::File).to receive(:exist?).with(Pathname.new(path)).and_return(false)
end

it "ensures the file is created with the the correct permissions" do
expect(writer).to receive(:ensure_permissions).with(Pathname.new(path))
writer.write(conn)
end
end

it "does nothing if the connection has not changed"
end
end
@@ -0,0 +1,72 @@
# 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/network_manager/connection_config_writers/wireless"
require "cfa/nm_connection"
require "y2network/boot_protocol"
require "y2network/startmode"
require "y2network/connection_config/wireless"

describe Y2Network::NetworkManager::ConnectionConfigWriters::Wireless do
subject(:handler) { described_class.new(file) }
let(:file) { CFA::NmConnection.new("wlan0.nm_connection") }

let(:conn) do
Y2Network::ConnectionConfig::Wireless.new.tap do |c|
c.interface = "wlan0"
c.description = "Wireless Card 0"
c.startmode = Y2Network::Startmode.create("auto")
c.bootproto = Y2Network::BootProtocol::DHCP
c.mode = "managed"
c.essid = "example_essid"
c.auth_mode = :open
c.ap = "00:11:22:33:44:55"
c.ap_scanmode = "1"
end
end

describe "#write" do
it "sets relevant attributes" do
handler.write(conn)
expect(file.wifi["ssid"]).to eql(conn.essid)
expect(file.wifi["mode"]).to eql("infrastructure")
expect(file.ipv4["method"]).to eql("auto")
expect(file.ipv6["method"]).to eql("auto")
end

context "WPA-PSK network configuration" do
let(:conn) do
Y2Network::ConnectionConfig::Wireless.new.tap do |c|
c.startmode = Y2Network::Startmode.create("auto")
c.bootproto = Y2Network::BootProtocol::DHCP
c.mode = "managed"
c.auth_mode = "psk"
c.wpa_psk = "example_psk"
end
end

it "sets specific WPA-PSK attributes" do
handler.write(conn)
expect(file.wifi_security["key-mgmt"]).to eql("wpa-psk")
expect(file.wifi_security["psk"]).to eql("example_psk")
end
end
end
end

0 comments on commit a51bf03

Please sign in to comment.