diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..6caf68a --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +output \ No newline at end of file diff --git a/tests/network/golden/dhcp-autoinstall b/tests/network/golden/dhcp-autoinstall new file mode 100644 index 0000000..f93d2cc --- /dev/null +++ b/tests/network/golden/dhcp-autoinstall @@ -0,0 +1,6 @@ + network: + network: + version: 2 + ethernets: + eth0: + dhcp4: true diff --git a/tests/network/golden/dhcp-kickstart b/tests/network/golden/dhcp-kickstart new file mode 100644 index 0000000..24f4ec3 --- /dev/null +++ b/tests/network/golden/dhcp-kickstart @@ -0,0 +1 @@ +network --device=eth0 --bootproto=dhcp diff --git a/tests/network/golden/dhcp-preseed b/tests/network/golden/dhcp-preseed new file mode 100644 index 0000000..9cbaebb --- /dev/null +++ b/tests/network/golden/dhcp-preseed @@ -0,0 +1 @@ +d-i netcfg/choose_interface select eth0 diff --git a/tests/network/golden/static-autoinstall b/tests/network/golden/static-autoinstall new file mode 100644 index 0000000..a3629b1 --- /dev/null +++ b/tests/network/golden/static-autoinstall @@ -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 diff --git a/tests/network/golden/static-kickstart b/tests/network/golden/static-kickstart new file mode 100644 index 0000000..91f984f --- /dev/null +++ b/tests/network/golden/static-kickstart @@ -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 diff --git a/tests/network/golden/static-preseed b/tests/network/golden/static-preseed new file mode 100644 index 0000000..b7c207e --- /dev/null +++ b/tests/network/golden/static-preseed @@ -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 diff --git a/tests/network/templates/autoinstall.pkrtpl b/tests/network/templates/autoinstall.pkrtpl new file mode 100644 index 0000000..c4ab4c0 --- /dev/null +++ b/tests/network/templates/autoinstall.pkrtpl @@ -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 ~} \ No newline at end of file diff --git a/tests/network/templates/kickstart.pkrtpl b/tests/network/templates/kickstart.pkrtpl new file mode 100644 index 0000000..a813431 --- /dev/null +++ b/tests/network/templates/kickstart.pkrtpl @@ -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 ~} diff --git a/tests/network/templates/preseed.pkrtpl b/tests/network/templates/preseed.pkrtpl new file mode 100644 index 0000000..159bea2 --- /dev/null +++ b/tests/network/templates/preseed.pkrtpl @@ -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 ~} diff --git a/tests/network/test b/tests/network/test new file mode 100644 index 0000000..9281511 --- /dev/null +++ b/tests/network/test @@ -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 ] +} diff --git a/tests/network/test-dhcp.pkrvars.hcl b/tests/network/test-dhcp.pkrvars.hcl new file mode 100644 index 0000000..9864fd8 --- /dev/null +++ b/tests/network/test-dhcp.pkrvars.hcl @@ -0,0 +1 @@ +// Nothing to set for DHCP \ No newline at end of file diff --git a/tests/network/test-static.pkrvars.hcl b/tests/network/test-static.pkrvars.hcl new file mode 100644 index 0000000..8b210fe --- /dev/null +++ b/tests/network/test-static.pkrvars.hcl @@ -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"] \ No newline at end of file diff --git a/tests/network/test.pkr.hcl b/tests/network/test.pkr.hcl new file mode 100644 index 0000000..1efe062 --- /dev/null +++ b/tests/network/test.pkr.hcl @@ -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", + ] + } +} \ No newline at end of file diff --git a/tests/network/variables.pkr.hcl b/tests/network/variables.pkr.hcl new file mode 100644 index 0000000..2f58e94 --- /dev/null +++ b/tests/network/variables.pkr.hcl @@ -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." +} \ No newline at end of file