persistence_test.go 1.1 KB

123456789101112131415161718192021222324252627282930
  1. package network
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestPersistencePreservesHostConfiguration(t *testing.T) {
  7. original := "auto lo\niface lo inet loopback\nauto eth0\niface eth0 inet dhcp\n mtu 1450\n post-up ip route add 10.0.0.0/8 via 192.0.2.1\n"
  8. ips := map[string][]string{"eth0": {"192.0.2.5/24", "2001:db8::5/64"}}
  9. updated := withSourceInclude(withManagedAddresses(original, ips))
  10. for _, line := range strings.Split(strings.TrimSpace(original), "\n") {
  11. if !strings.Contains(updated, line) {
  12. t.Fatalf("lost host setting: %s", line)
  13. }
  14. }
  15. if strings.Count(updated, "iface eth0") != 1 || strings.Count(updated, "source /etc/network/interfaces.d/*") != 1 {
  16. t.Fatal(updated)
  17. }
  18. if !strings.Contains(updated, "2001:db8::5/64") {
  19. t.Fatal("IPv6 address lost")
  20. }
  21. if again := withSourceInclude(withManagedAddresses(updated, ips)); again != updated {
  22. t.Fatal("repeated persistence changed configuration")
  23. }
  24. removed := withManagedAddresses(updated, map[string][]string{})
  25. if strings.Contains(removed, "192.0.2.5") || !strings.Contains(removed, "inet dhcp") {
  26. t.Fatal("address deletion touched host config or retained removed address")
  27. }
  28. }