|
|
@@ -0,0 +1,56 @@
|
|
|
+package netplan
|
|
|
+
|
|
|
+import (
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "nettool/internal/model"
|
|
|
+
|
|
|
+ "gopkg.in/yaml.v3"
|
|
|
+)
|
|
|
+
|
|
|
+func TestWriteRemovesExistingAddressForStaticInterfaceWithoutIPv4(t *testing.T) {
|
|
|
+ path := filepath.Join(t.TempDir(), "01-netcfg.yaml")
|
|
|
+ inputYAML := []byte(`network:
|
|
|
+ version: 2
|
|
|
+ ethernets:
|
|
|
+ enp2s0:
|
|
|
+ dhcp4: false
|
|
|
+ addresses:
|
|
|
+ - 192.168.50.10/24
|
|
|
+ optional: true
|
|
|
+`)
|
|
|
+ if err := os.WriteFile(path, inputYAML, 0600); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err := New().Write(path, "enp2s0", model.InterfaceConfig{
|
|
|
+ Interface: "enp2s0",
|
|
|
+ Dhcp4: false,
|
|
|
+ Addresses: []model.InterfaceAddressConfig{},
|
|
|
+ }, "", "")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ written, err := os.ReadFile(path)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ var cfg fileConfig
|
|
|
+ if err := yaml.Unmarshal(written, &cfg); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ ethernets := cfg.Network["ethernets"].(map[string]any)
|
|
|
+ target := ethernets["enp2s0"].(map[string]any)
|
|
|
+ if _, ok := target["addresses"]; ok {
|
|
|
+ t.Fatalf("expected addresses to be removed, got netplan:\n%s", written)
|
|
|
+ }
|
|
|
+ if dhcp4, ok := target["dhcp4"].(bool); !ok || dhcp4 {
|
|
|
+ t.Fatalf("expected dhcp4=false, got netplan:\n%s", written)
|
|
|
+ }
|
|
|
+ if optional, ok := target["optional"].(bool); !ok || !optional {
|
|
|
+ t.Fatalf("expected unrelated interface fields to be preserved, got netplan:\n%s", written)
|
|
|
+ }
|
|
|
+}
|