diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index 35ffba2d2..23b7bbcdf 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -89,7 +89,9 @@ module VagrantPlugins net_conf << "[Match]" net_conf << "Name=#{dev_name}" net_conf << "[Network]" - if network[:ip] + if network[:type].to_s == "dhcp" + net_conf << "DHCP=yes" + else mask = network[:netmask] if mask && IPAddr.new(network[:ip]).ipv4? begin @@ -102,8 +104,6 @@ module VagrantPlugins net_conf << "DHCP=no" net_conf << "Address=#{address}" net_conf << "Gateway=#{network[:gateway]}" if network[:gateway] - else - net_conf << "DHCP=yes" end remote_path = upload_tmp_file(comm, net_conf.join("\n")) 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 d3a523c95..a60e51597 100644 --- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb @@ -101,6 +101,9 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end context "with systemd-networkd" do + let(:net_conf_dhcp) { "[Match]\nName=eth1\n[Network]\nDHCP=yes" } + let(:net_conf_static) { "[Match]\nName=eth2\n[Network]\nDHCP=no\nAddress=33.33.33.10/16\nGateway=33.33.0.1" } + before do expect(comm).to receive(:test).with("systemctl -q is-active systemd-networkd.service", anything).and_return(true) end @@ -113,6 +116,19 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do expect(comm.received_commands[0]).to match("chmod") expect(comm.received_commands[2]).to match("systemctl restart") end + + it "properly configures DHCP and static IPs if defined" do + expect(cap).to receive(:upload_tmp_file).with(comm, net_conf_dhcp) + expect(cap).to receive(:upload_tmp_file).with(comm, net_conf_static) + + cap.configure_networks(machine, [network_0, network_1]) + + expect(comm.received_commands[0]).to match("mkdir -p /etc/systemd/network") + expect(comm.received_commands[0]).to match("mv -f '' '/etc/systemd/network/50-vagrant-eth1.network'") + expect(comm.received_commands[0]).to match("chown root:root '/etc/systemd/network/50-vagrant-eth1.network'") + expect(comm.received_commands[0]).to match("chmod 0644 '/etc/systemd/network/50-vagrant-eth1.network'") + expect(comm.received_commands[2]).to match("systemctl restart") + end end context "with netplan" do