Skip to content

Commit 8fc2927

Browse files
authored
Merge pull request #13633 from hashicorp/alpine-networking
Fix alpine networking for public network
2 parents e98ee62 + 64d1603 commit 8fc2927

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

templates/guests/alpine/network_dhcp.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ iface eth<%= options[:interface] %> inet dhcp
77
<% else %>
88
# We need to disable eth0, see GH-2648
99
post-up route del default dev eth0
10-
post-up dhclient $IFACE
10+
1111
pre-down route add default dev eth0
1212
<% end %>
1313
#VAGRANT-END

test/unit/plugins/guests/alpine/cap/configure_networks_test.rb

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,69 @@
44
require_relative "../../../../base"
55

66
describe 'VagrantPlugins::GuestAlpine::Cap::ConfigureNetworks' do
7-
let(:described_class) do
8-
VagrantPlugins::GuestAlpine::Plugin.components.guest_capabilities[:alpine].get(:configure_networks)
9-
end
10-
let(:machine) { double('machine') }
11-
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
7+
let(:described_class) do
8+
VagrantPlugins::GuestAlpine::Plugin.components.guest_capabilities[:alpine].get(:configure_networks)
9+
end
10+
let(:machine) { double('machine') }
11+
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
12+
13+
before do
14+
allow(machine).to receive(:communicate).and_return(communicator)
15+
end
16+
17+
after do
18+
communicator.verify_expectations!
19+
end
20+
21+
it 'should configure networks' do
22+
networks = [
23+
{ type: :static, ip: '192.168.10.10', netmask: '255.255.255.0', interface: 0, name: 'eth0' },
24+
{ type: :dhcp, interface: 1, name: 'eth1' }
25+
]
26+
27+
expect(communicator).to receive(:sudo).with("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre")
28+
expect(communicator).to receive(:sudo).with("sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tail -n +2 > /tmp/vagrant-network-interfaces.post")
29+
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth0/)
30+
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth0 2> /dev/null')
31+
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth1/)
32+
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth1 2> /dev/null')
33+
expect(communicator).to receive(:sudo).with('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces')
34+
expect(communicator).to receive(:sudo).with('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post')
35+
expect(communicator).to receive(:sudo).with('/sbin/ifup eth0')
36+
expect(communicator).to receive(:sudo).with('/sbin/ifup eth1')
37+
38+
allow_message_expectations_on_nil
39+
40+
described_class.configure_networks(machine, networks)
41+
end
42+
43+
context "dhcp assigned default route" do
44+
let(:networks) {
45+
[{type: :dhcp, use_dhcp_assigned_default_route: is_enabled}]
46+
}
47+
let(:is_enabled) { false }
48+
let(:tempfile) { double(:tempfile, binmode: true, close: true, path: "/dev/null") }
1249

1350
before do
14-
allow(machine).to receive(:communicate).and_return(communicator)
51+
allow(Tempfile).to receive(:new).and_return(tempfile)
1552
end
1653

17-
after do
18-
communicator.verify_expectations!
54+
context "when not enabled" do
55+
it "should not configure default route" do
56+
expect(tempfile).not_to receive(:write).with(/post-up route del default dev eth0/)
57+
58+
described_class.configure_networks(machine, networks)
59+
end
1960
end
2061

21-
it 'should configure networks' do
22-
networks = [
23-
{ type: :static, ip: '192.168.10.10', netmask: '255.255.255.0', interface: 0, name: 'eth0' },
24-
{ type: :dhcp, interface: 1, name: 'eth1' }
25-
]
26-
27-
expect(communicator).to receive(:sudo).with("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre")
28-
expect(communicator).to receive(:sudo).with("sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tail -n +2 > /tmp/vagrant-network-interfaces.post")
29-
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth0/)
30-
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth0 2> /dev/null')
31-
expect(communicator).to receive(:sudo).with(/\/sbin\/ifdown eth1/)
32-
expect(communicator).to receive(:sudo).with('/sbin/ip addr flush dev eth1 2> /dev/null')
33-
expect(communicator).to receive(:sudo).with('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces')
34-
expect(communicator).to receive(:sudo).with('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post')
35-
expect(communicator).to receive(:sudo).with('/sbin/ifup eth0')
36-
expect(communicator).to receive(:sudo).with('/sbin/ifup eth1')
37-
38-
allow_message_expectations_on_nil
62+
context "when enabled" do
63+
let(:is_enabled) { true }
64+
65+
it "should configure default route" do
66+
expect(tempfile).to receive(:write).with(/post-up route del default dev eth0/)
3967

4068
described_class.configure_networks(machine, networks)
69+
end
4170
end
71+
end
4272
end

0 commit comments

Comments
 (0)