diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index d53358eb3..6ba4c2ca9 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -57,8 +57,20 @@ module VagrantPlugins e_nets[interfaces[network[:interface]]] = e_config end end + + # By default, netplan expects the renderer to be systemd-networkd, + # but if any device is managed by NetworkManager, then we use that renderer + # ref: https://netplan.io/reference + renderer = NETPLAN_DEFAULT_RENDERER + ethernets.keys.each do |k| + if nm_controlled?(comm, k) + renderer = "NetworkManager" + break + end + end + np_config = {"network" => {"version" => NETPLAN_DEFAULT_VERSION, - "renderer" => NETPLAN_DEFAULT_RENDERER, "ethernets" => ethernets}} + "renderer" => renderer, "ethernets" => ethernets}} remote_path = upload_tmp_file(comm, np_config.to_yaml) dest_path = "#{NETPLAN_DIRECTORY}/50-vagrant.yaml" diff --git a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb index 57ed1e6a3..13ccea1b6 100644 --- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb @@ -65,6 +65,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end before do + allow(comm).to receive(:test).with("nmcli d show eth1").and_return(false) + allow(comm).to receive(:test).with("nmcli d show eth2").and_return(false) allow(comm).to receive(:test).with("ps -o comm= 1 | grep systemd").and_return(false) allow(comm).to receive(:test).with("sudo systemctl status systemd-networkd.service").and_return(false) allow(comm).to receive(:test).with("netplan -h").and_return(false) @@ -118,7 +120,30 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do expect(comm).to receive(:test).with("netplan -h").and_return(true) end + let(:nm_yml) { "---\nnetwork:\n version: 2\n renderer: NetworkManager\n ethernets:\n eth1:\n dhcp4: true\n eth2:\n addresses:\n - 33.33.33.10/16\n gateway4: 33.33.0.1\n" } + let(:networkd_yml) { "---\nnetwork:\n version: 2\n renderer: networkd\n ethernets:\n eth1:\n dhcp4: true\n eth2:\n addresses:\n - 33.33.33.10/16\n gateway4: 33.33.0.1\n" } + + it "uses NetworkManager if detected on device" do + allow(cap).to receive(:nm_controlled?).and_return(true) + allow(comm).to receive(:test).with("nmcli d show eth1").and_return(true) + allow(comm).to receive(:test).with("nmcli d show eth2").and_return(true) + + expect(cap).to receive(:upload_tmp_file).with(comm, nm_yml) + .and_return("/tmp/vagrant-network-entry.1234") + + cap.configure_networks(machine, [network_0, network_1]) + + + expect(comm.received_commands[0]).to match("mv -f '/tmp/vagrant-network-entry.*' '/etc/netplan/.*.yaml'") + expect(comm.received_commands[0]).to match("chown") + expect(comm.received_commands[0]).to match("chmod") + expect(comm.received_commands[0]).to match("netplan apply") + end + it "creates and starts the networks for systemd with netplan" do + expect(cap).to receive(:upload_tmp_file).with(comm, networkd_yml) + .and_return("/tmp/vagrant-network-entry.1234") + cap.configure_networks(machine, [network_0, network_1]) expect(comm.received_commands[0]).to match("mv -f '/tmp/vagrant-network-entry.*' '/etc/netplan/.*.yaml'")