Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions plugins/guests/suse/cap/configure_networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: BUSL-1.1

require "tempfile"
require "securerandom"

require_relative "../../../../lib/vagrant/util/template_renderer"

Expand All @@ -10,9 +11,24 @@ module GuestSUSE
module Cap
class ConfigureNetworks
extend Vagrant::Util::Retryable
extend Vagrant::Util::GuestNetworks::Linux
include Vagrant::Util

def self.configure_networks(machine, networks)
network_scripts_dir = machine.guest.capability(:network_scripts_dir)

# The legacy configuration will handle guests using wicked
# (SLES/Leap 15.x and earlier, Tumbleweed). Guests without
# wicked (SLES/Leap 16 and later, MicroOS) are configured
# via NetworkManager.
if network_scripts_dir.end_with?("sysconfig/network")
configure_networks_legacy(machine, networks)
else
configure_network_manager(machine, networks)
end
end

def self.configure_networks_legacy(machine, networks)
comm = machine.communicate

network_scripts_dir = machine.guest.capability(:network_scripts_dir)
Expand Down
6 changes: 5 additions & 1 deletion plugins/guests/suse/cap/network_scripts_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module GuestSUSE
module Cap
class NetworkScriptsDir
def self.network_scripts_dir(machine)
"/etc/sysconfig/network"
if machine.communicate.test("test -d /etc/sysconfig/network")
"/etc/sysconfig/network"
else
"/etc/NetworkManager/system-connections"
end
end
end
end
Expand Down
40 changes: 27 additions & 13 deletions test/unit/plugins/guests/suse/cap/configure_networks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }

before do
allow(guest).to receive(:capability).with(:network_scripts_dir)
.and_return("/scripts")
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end

let(:network_1) do
{
interface: 0,
Expand All @@ -49,12 +42,33 @@
}
end

it "creates and starts the networks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/)
context "with wicked network configuration path" do
before do
allow(guest).to receive(:capability).with(:network_scripts_dir)
.and_return("/etc/sysconfig/network")
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end

it "creates and starts the networks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/)
end
end

context "with system-connections network configuration path" do
before do
allow(guest).to receive(:capability).with(:network_scripts_dir)
.and_return("/etc/NetworkManager/system-connections")
end

it "should configure with network manager" do
expect(cap).to receive(:configure_network_manager).with(machine, [network_1, network_2])
cap.configure_networks(machine, [network_1, network_2])
end
end
end
end
25 changes: 22 additions & 3 deletions test/unit/plugins/guests/suse/cap/network_scripts_dir_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,32 @@
.guest_capabilities[:suse]
end

let(:machine) { double("machine") }
let(:comm) { double("comm") }
let(:machine) { double("machine", communicate: comm) }

describe ".network_scripts_dir" do
let(:cap) { caps.get(:network_scripts_dir) }

it "runs /etc/sysconfig/network" do
expect(cap.network_scripts_dir(machine)).to eq("/etc/sysconfig/network")
context "when /etc/sysconfig/network exists" do
before do
allow(comm).to receive(:test).with("test -d /etc/sysconfig/network")
.and_return(true)
end

it "returns /etc/sysconfig/network" do
expect(cap.network_scripts_dir(machine)).to eq("/etc/sysconfig/network")
end
end

context "when /etc/sysconfig/network does not exist" do
before do
allow(comm).to receive(:test).with("test -d /etc/sysconfig/network")
.and_return(false)
end

it "returns /etc/NetworkManager/system-connections" do
expect(cap.network_scripts_dir(machine)).to eq("/etc/NetworkManager/system-connections")
end
end
end
end