Skip to content

Commit

Permalink
Merge pull request #1143 from yast/save-network-manager-config
Browse files Browse the repository at this point in the history
Save network manager config
  • Loading branch information
imobachgs committed Jan 20, 2021
2 parents a2f7ca5 + 0877487 commit b3ee308
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 15 deletions.
29 changes: 21 additions & 8 deletions src/lib/network/clients/save_network.rb
Expand Up @@ -105,13 +105,6 @@ def CopyConfiguredNetworkFiles
{ dir: ::File.join(ETC, "sysctl.d"), file: "70-yast.conf" }
]

# NetworkManager is usually the default in a live installation. Any
# configuration applied during the installation should be present in the
# target system.
if Y2Network::ProposalSettings.instance.network_service == :network_manager
copy_recipes << { dir: ::File.join(NETWORK_MANAGER, "system-connections"), file: "*" }
end

# just copy files
copy_recipes.each do |recipe|
# can be shell pattern like ifcfg-*
Expand Down Expand Up @@ -298,12 +291,32 @@ def configure_lan
# skipped or even only done in case of missing `networking -> interfaces`
# section
NetworkAutoconfiguration.instance.configure_virtuals
NetworkAutoconfiguration.instance.configure_dns unless Mode.autoinst

if !Mode.autoinst
NetworkAutoconfiguration.instance.configure_dns
configure_network_manager
end

# this depends on DNS configuration
configure_hosts
end

# Configures NetworkManager
#
# When running the live installation, it is just a matter of copying
# system-connections to the installed system. In a regular installation,
# write the settings in the Yast::Lan.yast_config object.
def configure_network_manager
return unless Y2Network::ProposalSettings.instance.network_service == :network_manager

if Yast::Lan.system_config.backend&.id == :network_manager
copy_files_to_target(["*"], File.join(NETWORK_MANAGER, "system-connections"))
else
Yast::Lan.yast_config.backend = :network_manager
Yast::Lan.write_config
end
end

# It does an automatic configuration of installed system
#
# Basically, it runs several proposals.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/config.rb
Expand Up @@ -137,7 +137,7 @@ def initialize(source:, **opts)
#
# @see Y2Network::ConfigWriter
def write(original: nil, target: nil, only: nil)
target ||= source
target = target || backend&.id || source
Y2Network::ConfigWriter.for(target).write(self, original, only: only)
end

Expand Down
Empty file.
31 changes: 30 additions & 1 deletion test/save_network_test.rb
Expand Up @@ -24,6 +24,7 @@

require "yast"
require "y2network/config"
require "y2network/backends"
require "network/clients/save_network"
require "tmpdir"

Expand All @@ -35,7 +36,10 @@
let(:destdir_sysconfig) { File.join(destdir, "etc", "sysconfig", "network") }
let(:scr_root) { File.join(DATA_PATH, "instsys") }
let(:yast_config) { Y2Network::Config.new(source: :sysconfig) }
let(:system_config) { Y2Network::Config.new(source: :sysconfig) }
let(:system_config) do
Y2Network::Config.new(source: :sysconfig, backend: system_backend)
end
let(:system_backend) { Y2Network::Backends::Wicked.new }
let(:s390) { false }

before do
Expand Down Expand Up @@ -131,6 +135,31 @@
end
end

context "when the backend is network manager" do
before do
allow(Y2Network::ProposalSettings.instance).to receive(:network_service)
.and_return(:network_manager)
FileUtils.mkdir_p(File.join(destdir, "etc", "NetworkManager", "system-connections"))
end

it "writes the configuration to the underlying system" do
expect(Yast::Lan).to receive(:write_config)
subject.main
end

context "when running on network manager (e.g., live installation)" do
let(:system_backend) { Y2Network::Backends::NetworkManager.new }

it "copies the NetworkManager configuration from the instsys" do
expect(Yast::Lan).to_not receive(:write_config)
subject.main
expect(File).to exist(
File.join(destdir, "etc", "NetworkManager", "system-connections", "wlan0.nmconnection")
)
end
end
end

context "during update" do
before do
allow(Yast::Mode).to receive(:update).and_return(true)
Expand Down
48 changes: 43 additions & 5 deletions test/y2network/config_test.rb
Expand Up @@ -27,6 +27,7 @@
require "y2network/connection_configs_collection"
require "y2network/sysconfig/config_reader"
require "y2network/sysconfig/config_writer"
require "y2network/network_manager/config_writer"

describe Y2Network::Config do
before do
Expand Down Expand Up @@ -108,16 +109,53 @@
end

describe "#write" do
let(:writer) { instance_double(Y2Network::Sysconfig::ConfigWriter) }
let(:sysconfig_writer) { instance_double(Y2Network::Sysconfig::ConfigWriter) }
let(:nm_writer) { instance_double(Y2Network::NetworkManager::ConfigWriter) }

before do
allow(Y2Network::ConfigWriter).to receive(:for).with(:sysconfig)
.and_return(writer)
.and_return(sysconfig_writer)
allow(Y2Network::ConfigWriter).to receive(:for).with(:network_manager)
.and_return(nm_writer)
end

it "writes the config using the required writer" do
expect(writer).to receive(:write).with(config, nil, only: nil)
config.write
it "writes the configuration using the writer for the given target" do
expect(nm_writer).to receive(:write).with(config, nil, only: nil)
config.write(target: :network_manager)
end

context "when the original configuration is given" do
let(:original) { instance_double(described_class) }

it "passes that configuration to the writer" do
expect(sysconfig_writer).to receive(:write).with(config, original, only: nil)
config.write(original: original)
end
end

context "when a set of sections is given" do
it "applies the configuration for those sections only" do
expect(sysconfig_writer).to receive(:write).with(config, nil, only: [:dns])
config.write(only: [:dns])
end
end

context "when no target is given" do
context "and a backend is set" do
before { config.backend = :network_manager }

it "writes the config using the writer for the backend" do
expect(nm_writer).to receive(:write).with(config, nil, only: nil)
config.write
end
end

context "and no backend is set" do
it "writes the config using the writer for the source" do
expect(sysconfig_writer).to receive(:write).with(config, nil, only: nil)
config.write
end
end
end
end

Expand Down

0 comments on commit b3ee308

Please sign in to comment.