Skip to content

Commit

Permalink
[PoC] Add objects to read configuration from the system
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs authored and mchf committed Mar 21, 2019
1 parent 735ab2f commit 4257ad6
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib/y2network/config_reader.rb
@@ -0,0 +1,32 @@
# 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.
module Y2Network
# This module contains a set of classes to read the network configuration from the system
#
# For the time being, only the wicked reader ({Y2Network::ConfigReader::Wicked}) reader is
# available.
module ConfigReader
def self.for(source = :wicked)
require "y2network/config_reader/#{source}"
name = source.to_s.split("_").map(&:capitalize).join
klass = self.const_get(name)
klass.new
end
end
end
41 changes: 41 additions & 0 deletions src/lib/y2network/config_reader/wicked.rb
@@ -0,0 +1,41 @@
# 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/config"

Yast.import "LanItems"

module Y2Network
module ConfigReader
# This class reads the current configuration from the system
class Wicked
# @return [Y2Network::Config] Network configuration
def config
Yast::LanItems.Read
Config.new(interfaces: interfaces)
end

def interfaces
# TODO: for the time being, we are just relying in the underlying stuff.
Yast::LanItems.current_device_names.map do |name|
Y2Network::Interface.new(name)
end
end
end
end
end
44 changes: 44 additions & 0 deletions test/y2network/config_reader/wicked_test.rb
@@ -0,0 +1,44 @@
# 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/config_reader/wicked"

describe Y2Network::ConfigReader::Wicked do
subject(:reader) { described_class.new }

let(:lan_items) do
instance_double(
Yast::LanItemsClass,
Read: nil,
current_device_names: ["lo", "eth0", "wlan0"]
)
end

describe "" do
before do
allow(Yast).to receive(:import).with("LanItems")
stub_const("Yast::LanItems", lan_items)
end

it "returns a configuration including network devices" do
config = reader.config
expect(config.interfaces.map(&:name)).to eq(["lo", "eth0", "wlan0"])
end
end
end
29 changes: 29 additions & 0 deletions test/y2network/config_reader_test.rb
@@ -0,0 +1,29 @@
# 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/config_reader"

describe Y2Network::ConfigReader do
describe ".for" do
it "returns the reader for the given source" do
reader = Y2Network::ConfigReader.for(:wicked)
expect(reader).to be_a(Y2Network::ConfigReader::Wicked)
end
end
end

0 comments on commit 4257ad6

Please sign in to comment.