two/internal/dhcpbackend/dnsmasq_test.go
GnomeZworc 8a04f6f7f6
All checks were successful
Release Pipeline / set-release-target (push) Successful in 39s
Release Pipeline / upload-assets (run-dhcp-in-netns.sh, scripts/run-dhcp-in-netns.sh) (push) Successful in 4s
Release Pipeline / upload-assets (run-dnsmasq-in-netns.sh, scripts/run-dnsmasq-in-netns.sh) (push) Successful in 4s
Release Pipeline / upload-assets (agent.service, systemd/agent.service) (push) Successful in 4s
Release Pipeline / upload-assets (dhcp@.service, systemd/dhcp@.service) (push) Successful in 4s
Release Pipeline / upload-assets (dnsmasq@.service, systemd/dnsmasq@.service) (push) Successful in 4s
Release Pipeline / upload-assets (metadata@.service, systemd/metadata@.service) (push) Successful in 4s
Release Pipeline / build (dhcp, amd64, linux) (push) Successful in 0s
Release Pipeline / build (agent, amd64, linux) (push) Successful in 0s
Release Pipeline / build (metadata, amd64, linux) (push) Successful in 0s
Release Pipeline / checksums (push) Successful in 4s
Release Pipeline / release (push) Successful in 13s
Release Pipeline / publish (push) Successful in 0s
Release Pipeline / build (push) Successful in 1m2s
f-46: dhcpbackend: drive dnsmasq or the built-in server through one interface #46
internal/subnet et internal/vm ne parlent plus à dnsmasq en direct mais à une
interface Backend, choisie au démarrage par dhcp.backend. Le défaut restant
dnsmasq, un agent.yml de 0.1.0 se comporte à l'identique ; ValidBackend refuse
toute autre valeur que dnsmasq ou two.

Les entrées ip→mac sont écrites hors du backend : elles font autorité pour les
deux, internal/vm/data.go les lisant par GetMACForIP quel que soit le serveur.
D'où l'extraction de dhcp.Entries, que GenerateConfig mêlait à l'écriture du
fichier dnsmasq.

Reservation porte Index et DefaultRoute : dnsmasq en dérive ses tags et n'en
pose que sur les interfaces sans route par défaut, le backend two transmet le
drapeau tel quel. La notion de tag ne remonte plus jusqu'à internal/vm.

Two.ConfigureSubnet sonde get-state avant de pousser la configuration : le job
systemd d'une unit Type=simple est done dès le fork, avant que la socket
existe. TeardownSubnet arrête l'unit avant de supprimer le .state.

Le backend two est testé contre un vrai serveur sur une vraie socket. Onze
mutations, toutes détectées.

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-09-08 21:49:33 +02:00

113 lines
3.4 KiB
Go

package dhcpbackend
import (
"os"
"path/filepath"
"strings"
"testing"
)
func dnsmasqBackend(t *testing.T) Dnsmasq {
t.Helper()
return Dnsmasq{ConfDir: t.TempDir()}
}
func readFile(t *testing.T, path string) string {
t.Helper()
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(raw)
}
func TestDnsmasq_ConfDirDefaultsToTheSystemOne(t *testing.T) {
if got := (Dnsmasq{}).confDir(); got != "/etc/dnsmasq.d" {
t.Errorf("confDir = %s, want /etc/dnsmasq.d", got)
}
}
func TestDnsmasqSetVM_WritesOneReservationPerInterface(t *testing.T) {
b := dnsmasqBackend(t)
s := testSubnet(t)
res := []Reservation{
{Index: 0, MAC: "00:22:33:00:00:0a", IP: "10.0.5.10", DefaultRoute: true},
{Index: 1, MAC: "00:22:33:00:00:0b", IP: "10.0.5.11"},
}
if err := b.SetVM(s, "vm-web", res); err != nil {
t.Fatalf("SetVM: %v", err)
}
hosts := readFile(t, filepath.Join(b.ConfDir, s.Instance()+".hosts.d", "vm-web"))
for _, want := range []string{"00:22:33:00:00:0a,10.0.5.10,set:vm-web-0", "00:22:33:00:00:0b,10.0.5.11,set:vm-web-1"} {
if !strings.Contains(hosts, want) {
t.Errorf("hosts file missing %q:\n%s", want, hosts)
}
}
}
func TestDnsmasqSetVM_TagsOnlyTheInterfacesWithoutADefaultRoute(t *testing.T) {
b := dnsmasqBackend(t)
s := testSubnet(t)
res := []Reservation{
{Index: 0, MAC: "00:22:33:00:00:0a", IP: "10.0.5.10", DefaultRoute: true},
{Index: 1, MAC: "00:22:33:00:00:0b", IP: "10.0.5.11"},
}
if err := b.SetVM(s, "vm-web", res); err != nil {
t.Fatalf("SetVM: %v", err)
}
opts := readFile(t, filepath.Join(b.ConfDir, s.Instance()+".opts.d", "vm-web"))
if strings.Contains(opts, "tag:vm-web-0") {
t.Errorf("the interface carrying the default route must get no override:\n%s", opts)
}
if !strings.Contains(opts, "tag:vm-web-1,3\n") {
t.Errorf("the secondary interface must get a bare option 3:\n%s", opts)
}
if !strings.Contains(opts, "tag:vm-web-1,121,") {
t.Errorf("the secondary interface must get its own option 121:\n%s", opts)
}
}
func TestDnsmasqSetVM_SecondaryOptionsKeepTheMetadataRoute(t *testing.T) {
b := dnsmasqBackend(t)
s := testSubnet(t)
res := []Reservation{{Index: 1, MAC: "00:22:33:00:00:0b", IP: "10.0.5.11"}}
if err := b.SetVM(s, "vm-web", res); err != nil {
t.Fatalf("SetVM: %v", err)
}
opts := readFile(t, filepath.Join(b.ConfDir, s.Instance()+".opts.d", "vm-web"))
if !strings.Contains(opts, "169.254.169.254/32,10.0.5.1") {
t.Errorf("overriding option 121 without the metadata route breaks cloud-init:\n%s", opts)
}
if strings.Contains(opts, "0.0.0.0/0") {
t.Errorf("a secondary interface must not receive a default route:\n%s", opts)
}
}
func TestDnsmasqSetVM_AllInterfacesDefaultRoutedWritesNoOptions(t *testing.T) {
b := dnsmasqBackend(t)
s := testSubnet(t)
res := []Reservation{{Index: 0, MAC: "00:22:33:00:00:0a", IP: "10.0.5.10", DefaultRoute: true}}
if err := b.SetVM(s, "vm-web", res); err != nil {
t.Fatalf("SetVM: %v", err)
}
path := filepath.Join(b.ConfDir, s.Instance()+".opts.d", "vm-web")
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("no options file expected, got %v: the subnet-wide options already carry the default route", err)
}
}
func TestDnsmasqSetVM_RejectsAVMWithoutReservation(t *testing.T) {
b := dnsmasqBackend(t)
if err := b.SetVM(testSubnet(t), "vm-web", nil); err == nil {
t.Fatal("a vm with no reservation would get no address: that must be reported")
}
}