diff --git a/src/lib/network/clients/save_network.rb b/src/lib/network/clients/save_network.rb index 584611cde..be8200de2 100644 --- a/src/lib/network/clients/save_network.rb +++ b/src/lib/network/clients/save_network.rb @@ -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-* @@ -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. diff --git a/src/lib/y2network/config.rb b/src/lib/y2network/config.rb index 0d907527a..715eb91c8 100644 --- a/src/lib/y2network/config.rb +++ b/src/lib/y2network/config.rb @@ -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 diff --git a/test/data/instsys/etc/NetworkManager/system-connections/wlan0.nmconnection b/test/data/instsys/etc/NetworkManager/system-connections/wlan0.nmconnection new file mode 100644 index 000000000..e69de29bb diff --git a/test/save_network_test.rb b/test/save_network_test.rb index c32ac5dcd..bbb09b94c 100755 --- a/test/save_network_test.rb +++ b/test/save_network_test.rb @@ -24,6 +24,7 @@ require "yast" require "y2network/config" +require "y2network/backends" require "network/clients/save_network" require "tmpdir" @@ -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 @@ -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) diff --git a/test/y2network/config_test.rb b/test/y2network/config_test.rb index 9a30566a2..47b216fc3 100644 --- a/test/y2network/config_test.rb +++ b/test/y2network/config_test.rb @@ -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 @@ -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