f-55: lab: provision générique des nœuds, rôle host, two installé par test/e2e #55

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-12 15:44:07 +02:00
commit f713f482fa
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
22 changed files with 481 additions and 369 deletions

View file

@ -22,15 +22,17 @@ func (p *Plan) Write(w io.Writer) error {
var extras []NodePlan
for _, n := range p.Nodes {
if len(n.Secondary) > 0 || n.Loopback.IsValid() || n.FRR != "" || n.Release != "" || n.Agent != "" {
if len(n.Secondary) > 0 || n.Loopback.IsValid() || n.FRR != "" || n.Provision != "" || n.PostProvision != "" || len(n.Files) > 0 || len(n.Services) > 0 {
extras = append(extras, n)
}
}
if len(extras) > 0 {
fmt.Fprintf(tw, "\nroles\n")
fmt.Fprintf(tw, " name\tloopback\tsecondary\tfrr\trelease\tagent\n")
fmt.Fprintf(tw, " name\tloopback\tsecondary\tfrr\tprovision\tpost_provision\tfiles\tservices\n")
for _, n := range extras {
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\t%s\n", n.Name, orDash(loopback(n)), orDash(secondary(n)), orDash(filepath.Base(n.FRR)), orDash(n.Release), orDash(filepath.Base(n.Agent)))
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", n.Name, orDash(loopback(n)), orDash(secondary(n)),
orDash(filepath.Base(n.FRR)), orDash(filepath.Base(n.Provision)), orDash(filepath.Base(n.PostProvision)),
orDash(strings.Join(sortedKeys(n.Files), ",")), orDash(strings.Join(n.Services, ",")))
}
}

View file

@ -34,17 +34,19 @@ type SegmentPlan struct {
}
type NodePlan struct {
Name string
Role string
Image string
CPUs int
Memory int
SSHPort int
Secondary map[string][]netip.Prefix
Loopback netip.Prefix
FRR string
Release string
Agent string
Name string
Role string
Image string
CPUs int
Memory int
SSHPort int
Secondary map[string][]netip.Prefix
Loopback netip.Prefix
FRR string
Provision string
PostProvision string
Files map[string]string
Services []string
}
type Cable struct {
@ -71,15 +73,17 @@ func Compute(t *Topology) (*Plan, error) {
p := &Plan{Name: t.Name, Images: append([]Image(nil), t.Images...)}
for i, n := range t.Nodes {
node := NodePlan{
Name: n.Name,
Role: n.Role,
Image: n.Image,
CPUs: n.CPUs,
Memory: n.Memory,
SSHPort: SSHBasePort + i,
FRR: n.FRR,
Release: n.Release,
Agent: n.Agent,
Name: n.Name,
Role: n.Role,
Image: n.Image,
CPUs: n.CPUs,
Memory: n.Memory,
SSHPort: SSHBasePort + i,
FRR: n.FRR,
Provision: n.Provision,
PostProvision: n.PostProvision,
Files: n.Files,
Services: n.Services,
}
for segment, raws := range n.Secondary {
for _, raw := range raws {

View file

@ -55,8 +55,8 @@ segments:
nodes:
sw1: { role: switch, image: deb, cpus: 2, memory: 1024 }
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay] }
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
hv2: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
hv2: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
`
func TestCompute_TwoHypervisorsPlan(t *testing.T) {
@ -102,7 +102,7 @@ func TestCompute_TwoHypervisorsPlan(t *testing.T) {
t.Errorf("%s ssh port = %d, want %d", name, got, port)
}
}
if hv := nodeOf(t, p, "hv1"); hv.Role != "hypervisor" || hv.CPUs != 4 || hv.Memory != 16384 || hv.Image != "deb" {
if hv := nodeOf(t, p, "hv1"); hv.Role != "host" || hv.CPUs != 4 || hv.Memory != 16384 || hv.Image != "deb" {
t.Errorf("hv1 = %+v", hv)
}
}
@ -165,7 +165,7 @@ segments:
blue: { switch: sw, cidr: 10.2.0.0/24, mtu: 1500 }
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [blue, red], release: 0.2.0rc002 }
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [blue, red] }
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red] }
`)
got := make([]string, 0, len(p.Cables))
@ -193,7 +193,7 @@ nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
n1: { role: rr, image: deb, cpus: 1, memory: 512, segments: [a, b] }
n2: { role: rr, image: deb, cpus: 1, memory: 512, segments: [a, b] }
n3: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [b, a], release: 0.2.0rc002 }
n3: { role: host, image: deb, cpus: 1, memory: 512, segments: [b, a] }
`)
macs := map[string]bool{}
ports := map[int]bool{}
@ -308,16 +308,11 @@ func TestWrite_TwoHypervisorsPlan(t *testing.T) {
want := `lab lab-test: nodes 4, segments 1, cables 3
nodes
name role image cpus memory ssh
sw1 switch deb 2 1024 MiB 127.0.0.1:2200
rr1 rr deb 1 1024 MiB 127.0.0.1:2201
hv1 hypervisor deb 4 16384 MiB 127.0.0.1:2202
hv2 hypervisor deb 4 16384 MiB 127.0.0.1:2203
roles
name loopback secondary frr release agent
hv1 - - - 0.2.0rc002 -
hv2 - - - 0.2.0rc002 -
name role image cpus memory ssh
sw1 switch deb 2 1024 MiB 127.0.0.1:2200
rr1 rr deb 1 1024 MiB 127.0.0.1:2201
hv1 host deb 4 16384 MiB 127.0.0.1:2202
hv2 host deb 4 16384 MiB 127.0.0.1:2203
segment underlay: 10.250.0.0/24, mtu 9000, switch sw1, bridge br-underlay, gateway 10.250.0.1
node interface address mac udp switch port mac udp

View file

@ -15,7 +15,7 @@ segments:
nodes:
sw1: { role: switch, image: deb, cpus: 2, memory: 1024, secondary: { underlay: [169.254.0.1/28] }, frr: frr/sw1.conf }
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay], secondary: { underlay: [169.254.0.3/28] }, loopback: 10.255.255.1/32, frr: frr/rr1.conf }
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: /etc/lab/hv1.conf, release: 0.2.0rc002 }
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: /etc/lab/hv1.conf, provision: provision/hv.sh, post_provision: /opt/lab/post.sh, files: { agent.yml: agent/two.yml, deploy.sh: /opt/deploy.sh }, services: [agent.service] }
`
func TestCompute_CarriesTheRoleFields(t *testing.T) {
@ -99,7 +99,7 @@ segments:
blue: { switch: sw, cidr: 10.2.0.0/24 }
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red, blue], release: 0.2.0rc002 }
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red, blue] }
` + c.node + `
`
requireContains(t, validationError(t, doc), c.want)
@ -115,7 +115,7 @@ segments:
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512, secondary: { red: [169.254.0.1/28], blue: [169.254.1.1/28] } }
other: { role: switch, image: deb, cpus: 1, memory: 512 }
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red, blue], release: 0.2.0rc002 }
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red, blue] }
`
msg := validationError(t, doc)
requireContains(t, msg, "node sw: secondary address given for segment blue it is not attached to")
@ -130,7 +130,7 @@ segments:
lo1: { switch: sw, cidr: 10.1.0.0/24 }
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [lo1], release: 0.2.0rc002 }
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [lo1] }
`
requireContains(t, validationError(t, doc), "segment lo1: name is reserved for the loopback interface")
}
@ -142,10 +142,10 @@ func TestWrite_ShowsTheRoles(t *testing.T) {
}
want := `
roles
name loopback secondary frr release agent
sw1 - underlay 169.254.0.1/28 sw1.conf - -
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - -
hv1 - - hv1.conf 0.2.0rc002 -
name loopback secondary frr provision post_provision files services
sw1 - underlay 169.254.0.1/28 sw1.conf - - - -
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - - - -
hv1 - - hv1.conf hv.sh post.sh agent.yml,deploy.sh agent.service
`
if !bytes.Contains(buf.Bytes(), []byte(want)) {
t.Errorf("plan:\n%s\ndoes not contain:\n%s", buf.String(), want)
@ -169,27 +169,30 @@ nodes:
}
}
func TestValidate_ReleaseRejections(t *testing.T) {
func TestValidate_ProvisionRejections(t *testing.T) {
cases := map[string]struct {
node string
want string
}{
"hypervisor without release": {
"the hypervisor role is gone": {
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red] }`,
"node hv: a hypervisor needs the release of two to deploy (release: <tag>)",
`node hv: role "hypervisor" must be one of switch, rr, host`,
},
"release with shell characters": {
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: "0.2.0; reboot" }`,
`node hv: release "0.2.0; reboot" must match`,
"file name leaving the files directory": {
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { ../etc/passwd: a.yml } }`,
`node hv: file name "../etc/passwd" must match`,
},
"release on a route reflector": {
`hv: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc002 }`,
"node hv: release is only for hypervisors",
"file without source": {
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { agent.yml: "" } }`,
"node hv: file agent.yml has no source",
},
"release on a switch": {
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc002 }
sw2: { role: switch, image: deb, cpus: 1, memory: 512, release: 0.2.0rc002 }`,
"node sw2: release is only for hypervisors",
"service with shell characters": {
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], services: ["agent.service; reboot"] }`,
`node hv: service "agent.service; reboot" must match`,
},
"service listed twice": {
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], services: [agent.service, agent.service] }`,
"node hv: service agent.service listed twice",
},
}
for name, c := range cases {
@ -206,60 +209,61 @@ nodes:
}
}
func TestCompute_CarriesTheRelease(t *testing.T) {
if got := nodeOf(t, compute(t, withRoles), "hv1").Release; got != "0.2.0rc002" {
t.Errorf("hv1 release = %q", got)
}
if got := nodeOf(t, compute(t, withRoles), "rr1").Release; got != "" {
t.Errorf("rr1 release = %q", got)
}
}
func TestValidate_AgentOnlyForHypervisors(t *testing.T) {
func TestValidate_ProvisionIsAllowedOnEveryRole(t *testing.T) {
doc := header + `
segments:
red: { switch: sw, cidr: 10.1.0.0/24 }
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], agent: agent.yml }
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: agent.yml }
sw: { role: switch, image: deb, cpus: 1, memory: 512, provision: sw.sh, files: { a.conf: a.conf }, services: [lab-switch.service] }
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], provision: rr.sh, post_provision: rr-post.sh }
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { agent.yml: agent.yml }, services: [agent.service] }
`
msg := validationError(t, doc)
requireContains(t, msg, "node rr: agent is only for hypervisors")
if bytes.Contains([]byte(msg), []byte("node hv: agent")) {
t.Errorf("a hypervisor agent was refused:\n%s", msg)
compute(t, doc)
}
func TestCompute_CarriesTheProvision(t *testing.T) {
hv1 := nodeOf(t, compute(t, withRoles), "hv1")
if hv1.Provision != "provision/hv.sh" || hv1.PostProvision != "/opt/lab/post.sh" {
t.Errorf("hv1 provision = %q, post_provision = %q", hv1.Provision, hv1.PostProvision)
}
if !reflect.DeepEqual(hv1.Files, map[string]string{"agent.yml": "agent/two.yml", "deploy.sh": "/opt/deploy.sh"}) {
t.Errorf("hv1 files = %v", hv1.Files)
}
if !reflect.DeepEqual(hv1.Services, []string{"agent.service"}) {
t.Errorf("hv1 services = %v", hv1.Services)
}
rr1 := nodeOf(t, compute(t, withRoles), "rr1")
if rr1.Provision != "" || rr1.PostProvision != "" || rr1.Files != nil || rr1.Services != nil {
t.Errorf("rr1 = %+v, want no provision", rr1)
}
}
func TestLoad_ResolvesTheAgentPathAgainstTheTopologyFile(t *testing.T) {
func TestLoad_ResolvesProvisionPathsAgainstTheTopologyFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "lab.yml")
doc := header + `
segments:
red: { switch: sw, cidr: 10.1.0.0/24 }
nodes:
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
hv1: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: agent/two.yml }
hv2: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: /etc/lab/agent.yml }
`
if err := os.WriteFile(path, []byte(doc), 0o600); err != nil {
if err := os.WriteFile(path, []byte(withRoles), 0o600); err != nil {
t.Fatal(err)
}
topo, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
want := map[string]string{"sw": "", "hv1": filepath.Join(dir, "agent", "two.yml"), "hv2": "/etc/lab/agent.yml"}
for _, n := range topo.Nodes {
if n.Agent != want[n.Name] {
t.Errorf("%s agent = %q, want %q", n.Name, n.Agent, want[n.Name])
}
}
p, err := Compute(topo)
if err != nil {
t.Fatal(err)
}
if got := nodeOf(t, p, "hv1").Agent; got != filepath.Join(dir, "agent", "two.yml") {
t.Errorf("plan hv1 agent = %q", got)
hv1 := nodeOf(t, p, "hv1")
if hv1.Provision != filepath.Join(dir, "provision", "hv.sh") {
t.Errorf("provision = %q", hv1.Provision)
}
if hv1.PostProvision != "/opt/lab/post.sh" {
t.Errorf("post_provision = %q, an absolute path must be kept", hv1.PostProvision)
}
want := map[string]string{"agent.yml": filepath.Join(dir, "agent", "two.yml"), "deploy.sh": "/opt/deploy.sh"}
if !reflect.DeepEqual(hv1.Files, want) {
t.Errorf("files = %v, want %v", hv1.Files, want)
}
if rr1 := nodeOf(t, p, "rr1"); rr1.Provision != "" || rr1.Files != nil {
t.Errorf("rr1 = %+v", rr1)
}
}

View file

@ -11,9 +11,9 @@ import (
)
const (
RoleSwitch = "switch"
RoleRR = "rr"
RoleHypervisor = "hypervisor"
RoleSwitch = "switch"
RoleRR = "rr"
RoleHost = "host"
DefaultMTU = 9000
)
@ -39,18 +39,20 @@ type Segment struct {
}
type Node struct {
Name string
Role string
Image string
CPUs int
Memory int
Segments []string
Addresses map[string]string
Secondary map[string][]string
Loopback string
FRR string
Release string
Agent string
Name string
Role string
Image string
CPUs int
Memory int
Segments []string
Addresses map[string]string
Secondary map[string][]string
Loopback string
FRR string
Provision string
PostProvision string
Files map[string]string
Services []string
}
type fileImage struct {
@ -65,17 +67,19 @@ type fileSegment struct {
}
type fileNode struct {
Role string `yaml:"role"`
Image string `yaml:"image"`
CPUs int `yaml:"cpus"`
Memory int `yaml:"memory"`
Segments []string `yaml:"segments"`
Addresses map[string]string `yaml:"addresses"`
Secondary map[string][]string `yaml:"secondary"`
Loopback string `yaml:"loopback"`
FRR string `yaml:"frr"`
Release string `yaml:"release"`
Agent string `yaml:"agent"`
Role string `yaml:"role"`
Image string `yaml:"image"`
CPUs int `yaml:"cpus"`
Memory int `yaml:"memory"`
Segments []string `yaml:"segments"`
Addresses map[string]string `yaml:"addresses"`
Secondary map[string][]string `yaml:"secondary"`
Loopback string `yaml:"loopback"`
FRR string `yaml:"frr"`
Provision string `yaml:"provision"`
PostProvision string `yaml:"post_provision"`
Files map[string]string `yaml:"files"`
Services []string `yaml:"services"`
}
type file struct {
@ -94,17 +98,25 @@ func Load(path string) (*Topology, error) {
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
dir := filepath.Dir(path)
for i, n := range t.Nodes {
if n.FRR != "" && !filepath.IsAbs(n.FRR) {
t.Nodes[i].FRR = filepath.Join(filepath.Dir(path), n.FRR)
}
if n.Agent != "" && !filepath.IsAbs(n.Agent) {
t.Nodes[i].Agent = filepath.Join(filepath.Dir(path), n.Agent)
t.Nodes[i].FRR = relativeTo(dir, n.FRR)
t.Nodes[i].Provision = relativeTo(dir, n.Provision)
t.Nodes[i].PostProvision = relativeTo(dir, n.PostProvision)
for name, src := range n.Files {
t.Nodes[i].Files[name] = relativeTo(dir, src)
}
}
return t, nil
}
func relativeTo(dir, path string) string {
if path == "" || filepath.IsAbs(path) {
return path
}
return filepath.Join(dir, path)
}
func Parse(data []byte) (*Topology, error) {
var f file
dec := yaml.NewDecoder(bytes.NewReader(data))
@ -138,18 +150,20 @@ func Parse(data []byte) (*Topology, error) {
for _, name := range order["nodes"] {
n := f.Nodes[name]
t.Nodes = append(t.Nodes, Node{
Name: name,
Role: n.Role,
Image: n.Image,
CPUs: n.CPUs,
Memory: n.Memory,
Segments: n.Segments,
Addresses: n.Addresses,
Secondary: n.Secondary,
Loopback: n.Loopback,
FRR: n.FRR,
Release: n.Release,
Agent: n.Agent,
Name: name,
Role: n.Role,
Image: n.Image,
CPUs: n.CPUs,
Memory: n.Memory,
Segments: n.Segments,
Addresses: n.Addresses,
Secondary: n.Secondary,
Loopback: n.Loopback,
FRR: n.FRR,
Provision: n.Provision,
PostProvision: n.PostProvision,
Files: n.Files,
Services: n.Services,
})
}
return t, nil

View file

@ -23,7 +23,8 @@ const (
var (
namePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,14}$`)
segmentPattern = regexp.MustCompile(`^[a-z][a-z0-9]{0,11}$`)
releasePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)
filePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)
unitPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9@._:-]{0,127}$`)
)
func (t *Topology) Validate() error {
@ -108,9 +109,9 @@ func (t *Topology) Validate() error {
add("node %s: name must match %s", n.Name, namePattern)
}
switch n.Role {
case RoleSwitch, RoleRR, RoleHypervisor:
case RoleSwitch, RoleRR, RoleHost:
default:
add("node %s: role %q must be one of %s, %s, %s", n.Name, n.Role, RoleSwitch, RoleRR, RoleHypervisor)
add("node %s: role %q must be one of %s, %s, %s", n.Name, n.Role, RoleSwitch, RoleRR, RoleHost)
}
if !images[n.Image] {
add("node %s: image %q is not declared", n.Name, n.Image)
@ -207,16 +208,23 @@ func validateExtras(n Node, segments map[string]Segment, add func(string, ...any
}
}
}
switch {
case n.Role == RoleHypervisor && n.Release == "":
add("node %s: a hypervisor needs the release of two to deploy (release: <tag>)", n.Name)
case n.Role == RoleHypervisor && !releasePattern.MatchString(n.Release):
add("node %s: release %q must match %s", n.Name, n.Release, releasePattern)
case n.Role != RoleHypervisor && n.Release != "":
add("node %s: release is only for hypervisors", n.Name)
for _, name := range sortedKeys(n.Files) {
switch {
case !filePattern.MatchString(name):
add("node %s: file name %q must match %s", n.Name, name, filePattern)
case n.Files[name] == "":
add("node %s: file %s has no source", n.Name, name)
}
}
if n.Role != RoleHypervisor && n.Agent != "" {
add("node %s: agent is only for hypervisors", n.Name)
seenUnits := map[string]bool{}
for _, unit := range n.Services {
switch {
case !unitPattern.MatchString(unit):
add("node %s: service %q must match %s", n.Name, unit, unitPattern)
case seenUnits[unit]:
add("node %s: service %s listed twice", n.Name, unit)
}
seenUnits[unit] = true
}
if n.Loopback != "" {
prefix, err := netip.ParsePrefix(n.Loopback)