test: add network template tests

This commit is contained in:
ajschroeder
2024-12-09 22:39:52 +00:00
parent 6c313c97d9
commit dbc24c6688
15 changed files with 227 additions and 0 deletions

1
tests/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
output

View File

@@ -0,0 +1,6 @@
network:
network:
version: 2
ethernets:
eth0:
dhcp4: true

View File

@@ -0,0 +1 @@
network --device=eth0 --bootproto=dhcp

View File

@@ -0,0 +1 @@
d-i netcfg/choose_interface select eth0

View File

@@ -0,0 +1,13 @@
network:
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.100.192/24
gateway4: 192.168.100.1
nameservers:
addresses:
- 192.168.100.3
- 192.168.100.1

View File

@@ -0,0 +1 @@
network --device=eth0 --bootproto=static --ip=192.168.100.192 --netmask=255.255.255.0 --gateway=192.168.100.1 --nameserver=192.168.100.3,192.168.100.1

View File

@@ -0,0 +1,7 @@
d-i netcfg/choose_interface select eth0
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/get_ipaddress string 192.168.100.192
d-i netcfg/get_netmask string 255.255.255.0
d-i netcfg/get_gateway string 192.168.100.1
d-i netcfg/get_nameservers string 192.168.100.3 192.168.100.1
d-i netcfg/confirm_static boolean true

View File

@@ -0,0 +1,19 @@
network:
network:
version: 2
ethernets:
%{ if ip != null ~}
${device}:
dhcp4: false
addresses:
- ${ip}/${netmask}
gateway4: ${gateway}
nameservers:
addresses:
%{ for item in dns ~}
- ${item}
%{ endfor ~}
%{ else ~}
${device}:
dhcp4: true
%{ endif ~}

View File

@@ -0,0 +1,5 @@
%{~ if ip != null ~}
network --device=${device} --bootproto=static --ip=${ip} --netmask=${cidrnetmask("${ip}/${netmask}")} --gateway=${gateway} --nameserver=${join(",", dns)}
%{~ else ~}
network --device=${device} --bootproto=dhcp
%{~ endif ~}

View File

@@ -0,0 +1,9 @@
d-i netcfg/choose_interface select ${device}
%{ if ip != null ~}
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/get_ipaddress string ${ip}
d-i netcfg/get_netmask string ${cidrnetmask("${ip}/${netmask}")}
d-i netcfg/get_gateway string ${gateway}
d-i netcfg/get_nameservers string ${join(" ", dns)}
d-i netcfg/confirm_static boolean true
%{ endif ~}

68
tests/network/test Normal file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env bats
setup() {
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
echo "$DIR"
packer init -upgrade "$DIR"
}
teardown() {
rm -f /tmp/tmpfile
}
clean_blank_lines() {
# Remove any blank lines that HCL likes to leave around
awk 'NF' "$1" > /tmp/tmpfile && cat /tmp/tmpfile > "$1"
}
## Test DHCP
###############################################################################
@test "network with Autoinstall using DHCP" {
mkdir -p "$DIR/output/dhcp"
packer build -var-file="$DIR/test-dhcp.pkrvars.hcl" -var "output_folder=$DIR/output/dhcp" -only "autoinstall.*" "$DIR"
run clean_blank_lines "$DIR/output/dhcp/autoinstall"
run diff -u "$DIR/output/dhcp/autoinstall" "$DIR/golden/dhcp-autoinstall"
[ "$status" -eq 0 ]
}
@test "network with Kickstart using DHCP" {
mkdir -p "$DIR/output/dhcp"
packer build -var-file="$DIR/test-dhcp.pkrvars.hcl" -var "output_folder=$DIR/output/dhcp" -only "kickstart.*" "$DIR"
run clean_blank_lines "$DIR/output/dhcp/kickstart"
run diff -u "$DIR/output/dhcp/kickstart" "$DIR/golden/dhcp-kickstart"
[ "$status" -eq 0 ]
}
@test "network with Preseed using DHCP" {
mkdir -p "$DIR/output/dhcp"
packer build -var-file="$DIR/test-dhcp.pkrvars.hcl" -var "output_folder=$DIR/output/dhcp" -only "preseed.*" "$DIR"
run clean_blank_lines "$DIR/output/dhcp/preseed"
run diff -u "$DIR/output/dhcp/preseed" "$DIR/golden/dhcp-preseed"
[ "$status" -eq 0 ]
}
## Test static IP
###############################################################################
@test "network with Autoinstall using static IP" {
mkdir -p "$DIR/output/static"
packer build -var-file="$DIR/test-static.pkrvars.hcl" -var "output_folder=$DIR/output/static" -only "autoinstall.*" "$DIR"
run clean_blank_lines "$DIR/output/static/autoinstall"
run diff -u "$DIR/output/static/autoinstall" "$DIR/golden/static-autoinstall"
[ "$status" -eq 0 ]
}
@test "network with Kickstart using static IP" {
mkdir -p "$DIR/output/static"
packer build -var-file="$DIR/test-static.pkrvars.hcl" -var "output_folder=$DIR/output/static" -only "kickstart.*" "$DIR"
run clean_blank_lines "$DIR/output/static/kickstart"
run diff -u "$DIR/output/static/kickstart" "$DIR/golden/static-kickstart"
[ "$status" -eq 0 ]
}
@test "network with Preseed using static IP" {
mkdir -p "$DIR/output/static"
packer build -var-file="$DIR/test-static.pkrvars.hcl" -var "output_folder=$DIR/output/static" -only "preseed.*" "$DIR"
run clean_blank_lines "$DIR/output/static/preseed"
run diff -u "$DIR/output/static/preseed" "$DIR/golden/static-preseed"
[ "$status" -eq 0 ]
}

View File

@@ -0,0 +1 @@
// Nothing to set for DHCP

View File

@@ -0,0 +1,4 @@
vm_ip_address = "192.168.100.192"
vm_ip_netmask = 24
vm_ip_gateway = "192.168.100.1"
vm_dns_list = ["192.168.100.3", "192.168.100.1"]

View File

@@ -0,0 +1,57 @@
source "null" "test" {
communicator = "none"
}
locals {
autoinstall = templatefile("${abspath(path.root)}/templates/autoinstall.pkrtpl", {
device = var.vm_network_device,
ip = var.vm_ip_address,
netmask = var.vm_ip_netmask,
gateway = var.vm_ip_gateway,
dns = var.vm_dns_list,
})
kickstart = templatefile("${abspath(path.root)}/templates/kickstart.pkrtpl", {
device = var.vm_network_device,
ip = var.vm_ip_address,
netmask = var.vm_ip_netmask,
gateway = var.vm_ip_gateway,
dns = var.vm_dns_list,
})
preseed = templatefile("${abspath(path.root)}/templates/preseed.pkrtpl", {
device = var.vm_network_device,
ip = var.vm_ip_address,
netmask = var.vm_ip_netmask,
gateway = var.vm_ip_gateway,
dns = var.vm_dns_list,
})
}
build {
name = "autoinstall"
sources = ["source.null.test"]
provisioner "shell-local" {
inline = [
"echo '${local.autoinstall}' > ${var.output_folder}/autoinstall",
]
}
}
build {
name = "kickstart"
sources = ["source.null.test"]
provisioner "shell-local" {
inline = [
"echo '${local.kickstart}' > ${var.output_folder}/kickstart",
]
}
}
build {
name = "preseed"
sources = ["source.null.test"]
provisioner "shell-local" {
inline = [
"echo '${local.preseed}' > ${var.output_folder}/preseed",
]
}
}

View File

@@ -0,0 +1,34 @@
variable "vm_network_device" {
type = string
description = "The network device of the VM."
default = "eth0"
}
variable "vm_ip_address" {
type = string
description = "The IP address of the VM (e.g. 172.16.100.192)."
default = null
}
variable "vm_ip_netmask" {
type = number
description = "The netmask of the VM (e.g. 24)."
default = null
}
variable "vm_ip_gateway" {
type = string
description = "The gateway of the VM (e.g. 172.16.100.1)."
default = null
}
variable "vm_dns_list" {
type = list(string)
description = "The nameservers of the VM."
default = []
}
variable "output_folder" {
type = string
description = "The output folder for the generated files."
}