Merge pull request #1 from soapy1/archlinux-network-tests

Fix tests
This commit is contained in:
Robin Munn 2020-06-01 09:03:53 +07:00 committed by GitHub
commit 7c9db48b6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 115 additions and 10 deletions

View File

@ -9,10 +9,12 @@ module VagrantPlugins
module Cap
class ConfigureNetworks
include Vagrant::Util
extend Vagrant::Util::GuestInspection::Linux
def self.configure_networks(machine, networks)
comm = machine.communicate
commands = []
uses_systemd_networkd = systemd_networkd?(comm)
interfaces = machine.guest.capability(:network_interfaces)
networks.each.with_index do |network, i|
@ -25,9 +27,15 @@ module VagrantPlugins
network[:netmask] = (32-Math.log2((IPAddr.new(network[:netmask], Socket::AF_INET).to_i^0xffffffff)+1)).to_i
end
entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}",
options: network,
)
if uses_systemd_networkd
entry = TemplateRenderer.render("guests/arch/systemd_networkd/network_#{network[:type]}",
options: network,
)
else
entry = TemplateRenderer.render("guests/arch/default_network/network_#{network[:type]}",
options: network,
)
end
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
@ -39,7 +47,7 @@ module VagrantPlugins
comm.upload(f.path, remote_path)
end
if systemd_networkd?(comm)
if uses_systemd_networkd
commands << <<-EOH.gsub(/^ {16}/, '').rstrip
# Configure #{network[:device]}
chmod 0644 '#{remote_path}' &&

View File

@ -0,0 +1,4 @@
Description='A basic dhcp ethernet connection'
Interface=<%= options[:device] %>
Connection=ethernet
IP=dhcp

View File

@ -0,0 +1,8 @@
Connection=ethernet
Description='A basic static ethernet connection'
Interface=<%= options[:device] %>
IP=static
Address=('<%= options[:ip]%>/<%= options[:netmask] %>')
<% if options[:gateway] -%>
Gateway='<%= options[:gateway] %>'
<% end -%>

View File

@ -0,0 +1,8 @@
Connection=ethernet
Description='A basic IPv6 ethernet connection'
Interface=<%= options[:device] %>
IP6=static
Address6=('<%= options[:ip]%>/<%= options[:netmask] %>')
<% if options[:gateway] -%>
Gateway6='<%= options[:gateway] %>'
<% end -%>

View File

@ -25,6 +25,7 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do
before do
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
allow(cap).to receive(:systemd_networkd?).and_return(true)
end
let(:network_1) do
@ -59,5 +60,23 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[0]).not_to match(/^\s*&&\s*$/)
end
context "network is not contolled by systemd" do
before do
allow(cap).to receive(:systemd_networkd?).and_return(false)
end
it "creates and stars the networks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth1'/)
expect(comm.received_commands[0]).to match(/ip link set 'eth1' down/)
expect(comm.received_commands[0]).to match(/netctl restart 'eth1'/)
expect(comm.received_commands[0]).to match(/netctl enable 'eth1'/)
expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth2'/)
expect(comm.received_commands[0]).to match(/ip link set 'eth2' down/)
expect(comm.received_commands[0]).to match(/netctl restart 'eth2'/)
expect(comm.received_commands[0]).to match(/netctl enable 'eth2'/)
end
end
end
end

View File

@ -0,0 +1,19 @@
require_relative "../../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/default_network/network_dhcp" do
let(:template) { "guests/arch/default_network/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "eth1",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Description='A basic dhcp ethernet connection'
Interface=eth1
Connection=ethernet
IP=dhcp
EOH
end
end

View File

@ -0,0 +1,39 @@
require_relative "../../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/default_network/network_static" do
let(:template) { "guests/arch/default_network/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "eth1",
ip: "1.1.1.1",
netmask: "24",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Connection=ethernet
Description='A basic static ethernet connection'
Interface=eth1
IP=static
Address=('1.1.1.1/24')
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "eth1",
ip: "1.1.1.1",
gateway: "1.2.3.4",
netmask: "24",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Connection=ethernet
Description='A basic static ethernet connection'
Interface=eth1
IP=static
Address=('1.1.1.1/24')
Gateway='1.2.3.4'
EOH
end
end

View File

@ -1,9 +1,9 @@
require_relative "../../../base"
require_relative "../../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/network_dhcp" do
let(:template) { "guests/arch/network_dhcp" }
describe "templates/guests/arch/systemd_networkd/network_dhcp" do
let(:template) { "guests/arch/systemd_networkd/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {

View File

@ -1,9 +1,9 @@
require_relative "../../../base"
require_relative "../../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/network_static" do
let(:template) { "guests/arch/network_static" }
describe "templates/guests/arch/systemd_networkd/network_static" do
let(:template) { "guests/arch/systemd_networkd/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {