f-73: metadata: network-config par VM, une entrée ethN par NIC visée par sa MAC #73

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-20 00:36:14 +02:00
commit a6ad0962a6
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 54 additions and 7 deletions

View file

@ -78,14 +78,26 @@ func TestRenderConfig_VendorData_ContainsSSHKey(t *testing.T) {
}
}
func TestRenderConfig_NetworkConfig(t *testing.T) {
func TestRenderConfig_NetworkConfigMatchesEveryInterfaceByMAC(t *testing.T) {
cfg := newCfg()
out, err := RenderConfig("templates/network-config.tmpl", cfg)
cfg.MACs = []string{"52:54:00:49:01:0c", "52:54:00:49:02:16"}
out, err := renderDocument(DocNetworkConfig, cfg)
if err != nil {
t.Fatalf("RenderConfig network-config : %v", err)
t.Fatalf("renderDocument network-config : %v", err)
}
if !strings.Contains(out, "dhcp4: true") {
t.Errorf("dhcp4 absent du network-config :\n%s", out)
want := `version: 2
ethernets:
eth0:
match:
macaddress: "52:54:00:49:01:0c"
dhcp4: true
eth1:
match:
macaddress: "52:54:00:49:02:16"
dhcp4: true
`
if out != want {
t.Errorf("network-config :\n%s\nattendu :\n%s", out, want)
}
}

View file

@ -27,5 +27,6 @@ type NoCloudConfig struct {
Name string
Password string
SSHKEY string
MACs []string
Documents map[string]string
}

View file

@ -1,4 +1,8 @@
version: 2
ethernets:
eth0:
{{- range $i, $mac := .MACs }}
eth{{ $i }}:
match:
macaddress: "{{ $mac }}"
dhcp4: true
{{- end }}

View file

@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dhcpbackend"
@ -71,6 +72,7 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
BindPort: d.metadataPort,
Password: d.password,
SSHKEY: d.sshkey,
MACs: nicMACs(d.nics),
Documents: d.documents,
}, cfg, false); err != nil {
return fmt.Errorf("start metadata: %w", err)
@ -171,3 +173,11 @@ func copyFile(src, dst string) error {
}
return out.Sync()
}
func nicMACs(nics []nicData) []string {
macs := make([]string, len(nics))
for i, n := range nics {
macs[i] = strings.ToLower(n.mac)
}
return macs
}

View file

@ -0,0 +1,20 @@
package vm
import (
"reflect"
"testing"
)
func TestNicMACs_KeepsTheOrderAndLowersTheCase(t *testing.T) {
nics := []nicData{
{index: 0, mac: "52:54:00:49:01:0C"},
{index: 1, mac: "52:54:00:49:02:16", primary: true},
}
got := nicMACs(nics)
want := []string{"52:54:00:49:01:0c", "52:54:00:49:02:16"}
if !reflect.DeepEqual(got, want) {
t.Errorf("nicMACs = %v, attendu %v", got, want)
}
}