feat(labs): add the labd provisioner with clone pool, time to live and limits
This commit is contained in:
1 parent
9fdaa62b9b
commit
9385b2ddd4
53 files changed
+14675
-6
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package template
|
||||
|
||||
import "fmt"
|
||||
|
||||
type registryKey struct {
|
||||
id string
|
||||
version int
|
||||
}
|
||||
|
||||
// Registry holds the templates of the labd build by ID and version.
|
||||
type Registry struct {
|
||||
templates map[registryKey]Template
|
||||
}
|
||||
|
||||
// NewRegistry validates the templates and indexes them. A duplicate ID and
|
||||
// version is a build error.
|
||||
func NewRegistry(templates ...Template) (*Registry, error) {
|
||||
registry := &Registry{templates: make(map[registryKey]Template, len(templates))}
|
||||
for _, template := range templates {
|
||||
if err := template.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := registryKey{id: template.ID, version: template.Version}
|
||||
if _, duplicate := registry.templates[key]; duplicate {
|
||||
return nil, fmt.Errorf("template.NewRegistry - duplicate template %s version %d", template.ID, template.Version)
|
||||
}
|
||||
registry.templates[key] = template
|
||||
}
|
||||
return registry, nil
|
||||
}
|
||||
|
||||
// Lookup returns the template with the given ID and version.
|
||||
func (r *Registry) Lookup(id string, version int) (Template, bool) {
|
||||
template, ok := r.templates[registryKey{id: id, version: version}]
|
||||
return template, ok
|
||||
}
|
||||
|
||||
// All returns every template in no particular order.
|
||||
func (r *Registry) All() []Template {
|
||||
all := make([]Template, 0, len(r.templates))
|
||||
for _, template := range r.templates {
|
||||
all = append(all, template)
|
||||
}
|
||||
return all
|
||||
}
|
||||
|
||||
// Builtin returns the templates of this build. Real templates and their
|
||||
// prepared images arrive with the MikroTik and Ubuntu lab images; until then
|
||||
// the registry is empty and every CreateLab answers validation_failed.
|
||||
func Builtin() []Template {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UbuntuNetworkConfig renders a cloud-init network configuration (version 2)
|
||||
// for an Ubuntu node: the management NIC with its static address and no
|
||||
// gateway or DNS, and every data NIC renamed to its device name without an
|
||||
// address. A template adds the addresses of its variant to the data NICs
|
||||
// itself. Interfaces are matched by MAC, because a VM names its NICs by PCI
|
||||
// slot.
|
||||
func UbuntuNetworkConfig(node PlannedNode) string {
|
||||
var text strings.Builder
|
||||
text.WriteString("version: 2\nethernets:\n")
|
||||
fmt.Fprintf(&text, " eth0:\n match:\n macaddress: %q\n set-name: eth0\n dhcp4: false\n dhcp6: false\n accept-ra: false\n addresses:\n - %s\n",
|
||||
node.ManagementMAC, node.Management.String())
|
||||
for _, nic := range node.Interfaces {
|
||||
fmt.Fprintf(&text, " %s:\n match:\n macaddress: %q\n set-name: %s\n dhcp4: false\n dhcp6: false\n accept-ra: false\n",
|
||||
nic.Device, nic.MAC, nic.Device)
|
||||
}
|
||||
return text.String()
|
||||
}
|
||||
|
||||
// RouterOSManagementScript renders the RouterOS commands that give a
|
||||
// MikroTik node its management address and restrict the API services to the
|
||||
// host side of the management bridge. A template appends its variant
|
||||
// configuration. RouterOS matches interfaces by MAC in upper case.
|
||||
func RouterOSManagementScript(node PlannedNode) string {
|
||||
var text strings.Builder
|
||||
fmt.Fprintf(&text, "/interface ethernet set [find mac-address=%q] name=mgmt\n", strings.ToUpper(node.ManagementMAC))
|
||||
for _, nic := range node.Interfaces {
|
||||
fmt.Fprintf(&text, "/interface ethernet set [find mac-address=%q] name=%s\n", strings.ToUpper(nic.MAC), nic.Device)
|
||||
}
|
||||
fmt.Fprintf(&text, "/ip address add address=%s interface=mgmt comment=lincodex-management\n", node.Management.String())
|
||||
host := node.ManagementHost.String() + "/32"
|
||||
fmt.Fprintf(&text, "/ip service set api address=%s\n/ip service set api-ssl address=%s\n/ip service set ssh address=%s\n", host, host, host)
|
||||
text.WriteString("/ip service set telnet disabled=yes\n/ip service set ftp disabled=yes\n/ip service set www disabled=yes\n/ip service set www-ssl disabled=yes\n/ip service set winbox disabled=yes\n")
|
||||
return text.String()
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
// Package template describes lab templates: their nodes, their L2 links,
|
||||
// and the hooks that render the variant configuration of every node from a
|
||||
// seed and check a variant on running nodes.
|
||||
//
|
||||
// A template is code in the labd build, never request data. The prepared
|
||||
// base images and snapshots it names, and its real renderers and checkers,
|
||||
// belong to the template itself; this package fixes the model and the
|
||||
// rendering helpers.
|
||||
package template
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Kind is the guest system of a node.
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
MikroTik Kind = "mikrotik"
|
||||
Ubuntu Kind = "ubuntu"
|
||||
)
|
||||
|
||||
// Bounds of a template.
|
||||
const (
|
||||
MaxNodes = 8
|
||||
// MaxInterfaces bounds the NICs of a node, management included, so that
|
||||
// device names eth0..eth7 keep their PCI order in a VM.
|
||||
MaxInterfaces = 8
|
||||
MaxLinks = 16
|
||||
MaxCriteria = 50
|
||||
// MaxBootstrapBytes bounds each rendered cloud-init document.
|
||||
MaxBootstrapBytes = 64 << 10
|
||||
// SeedBytes is the size of a variant seed.
|
||||
SeedBytes = 32
|
||||
|
||||
ubuntuManagementPort = 22
|
||||
mikroTikManagementPort = 8728
|
||||
)
|
||||
|
||||
var (
|
||||
idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,47}$`)
|
||||
nodeNamePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,14}$`)
|
||||
criterionPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,31}$`)
|
||||
// sourcePattern is an LXD snapshot reference: instance/snapshot.
|
||||
sourcePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}/[A-Za-z0-9][A-Za-z0-9._-]{0,62}$`)
|
||||
profilePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}$`)
|
||||
)
|
||||
|
||||
// Node is a node of a template.
|
||||
type Node struct {
|
||||
// Name is the name the task statement uses, such as r1.
|
||||
Name string
|
||||
Kind Kind
|
||||
// Profile is the LXD profile with the CPU, memory, and disk limits, such
|
||||
// as lincodex-lab-small.
|
||||
Profile string
|
||||
// Source is the prepared snapshot the node is copied from, as
|
||||
// instance/snapshot. On the btrfs pool a copy is a subvolume snapshot
|
||||
// and takes seconds.
|
||||
Source string
|
||||
// MemoryBytes is the guest memory of the profile. labd admits a lab only
|
||||
// when the memory of all its nodes fits the host budget.
|
||||
MemoryBytes int64
|
||||
// ManagementPort is the TCP port labd probes for readiness and the
|
||||
// checker uses; zero selects 22 for ubuntu and 8728 for mikrotik.
|
||||
ManagementPort uint16
|
||||
}
|
||||
|
||||
// Port returns the management port of the node.
|
||||
func (n Node) Port() uint16 {
|
||||
switch {
|
||||
case n.ManagementPort != 0:
|
||||
return n.ManagementPort
|
||||
case n.Kind == MikroTik:
|
||||
return mikroTikManagementPort
|
||||
default:
|
||||
return ubuntuManagementPort
|
||||
}
|
||||
}
|
||||
|
||||
// Link is an L2 segment between nodes. Every link becomes its own bridge
|
||||
// without an address on the host, so lab traffic never reaches the host,
|
||||
// another lab, or the internet.
|
||||
type Link struct {
|
||||
Name string
|
||||
Nodes []string
|
||||
}
|
||||
|
||||
// Interface is a data NIC of a planned node.
|
||||
type Interface struct {
|
||||
// Device is the LXD device and the guest interface name, eth1 and up.
|
||||
Device string
|
||||
// Link is the name of the template link.
|
||||
Link string
|
||||
MAC string
|
||||
}
|
||||
|
||||
// PlannedNode is a node with its management address and data NICs.
|
||||
type PlannedNode struct {
|
||||
Name string
|
||||
Kind Kind
|
||||
Management netip.Prefix
|
||||
ManagementHost netip.Addr
|
||||
ManagementMAC string
|
||||
Interfaces []Interface
|
||||
}
|
||||
|
||||
// Variant is one lab of a template: the seed and the plan of every node.
|
||||
type Variant struct {
|
||||
Seed [SeedBytes]byte
|
||||
Nodes []PlannedNode
|
||||
}
|
||||
|
||||
// Node returns the planned node with the given name.
|
||||
func (v Variant) Node(name string) (PlannedNode, bool) {
|
||||
for _, node := range v.Nodes {
|
||||
if node.Name == name {
|
||||
return node, true
|
||||
}
|
||||
}
|
||||
return PlannedNode{}, false
|
||||
}
|
||||
|
||||
// Rand returns a deterministic random stream for one purpose of the variant,
|
||||
// such as "addresses" or "faults". The same seed and label always give the
|
||||
// same stream, so the renderer and the checker agree on the variant.
|
||||
func (v Variant) Rand(label string) *rand.Rand {
|
||||
mac := hmac.New(sha256.New, v.Seed[:])
|
||||
_, _ = mac.Write([]byte(label)) // A hash never fails to write.
|
||||
var key [32]byte
|
||||
copy(key[:], mac.Sum(nil))
|
||||
return rand.New(rand.NewChaCha8(key)) //nolint:gosec // Deterministic variant choice, not a secret.
|
||||
}
|
||||
|
||||
// Bootstrap is the first-boot configuration of a node. LXD passes it to the
|
||||
// guest as cloud-init data; for a MikroTik node UserData holds the RouterOS
|
||||
// script that the image applies on first boot.
|
||||
type Bootstrap struct {
|
||||
UserData string
|
||||
NetworkConfig string
|
||||
}
|
||||
|
||||
// NodeAccess is how the checker reaches a node: its management address on
|
||||
// the lab management bridge. It never leaves labd.
|
||||
type NodeAccess struct {
|
||||
Name string
|
||||
Kind Kind
|
||||
Address netip.AddrPort
|
||||
}
|
||||
|
||||
// Criterion is the result of one checker criterion.
|
||||
type Criterion struct {
|
||||
ID string
|
||||
Passed bool
|
||||
}
|
||||
|
||||
// RenderFunc renders the bootstrap of every node of a variant by node name.
|
||||
type RenderFunc func(variant Variant) (map[string]Bootstrap, error)
|
||||
|
||||
// CheckFunc evaluates the criteria of a variant on running nodes. It reads
|
||||
// node state only. An error means that nothing was evaluated, never a failed
|
||||
// criterion.
|
||||
type CheckFunc func(ctx context.Context, variant Variant, nodes []NodeAccess) ([]Criterion, error)
|
||||
|
||||
// Template is a lab template of one version.
|
||||
type Template struct {
|
||||
ID string
|
||||
Version int
|
||||
Nodes []Node
|
||||
Links []Link
|
||||
Render RenderFunc
|
||||
Check CheckFunc
|
||||
}
|
||||
|
||||
// MemoryBytes returns the memory of all nodes.
|
||||
func (t Template) MemoryBytes() int64 {
|
||||
var total int64
|
||||
for _, node := range t.Nodes {
|
||||
total += node.MemoryBytes
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// Node returns the node with the given name.
|
||||
func (t Template) Node(name string) (Node, bool) {
|
||||
for _, node := range t.Nodes {
|
||||
if node.Name == name {
|
||||
return node, true
|
||||
}
|
||||
}
|
||||
return Node{}, false
|
||||
}
|
||||
|
||||
// Validate reports a template that labd cannot provision.
|
||||
func (t Template) Validate() error {
|
||||
if !idPattern.MatchString(t.ID) || t.Version < 1 || t.Version > 10000 {
|
||||
return errors.New("template.Validate - ID or version is outside the contract")
|
||||
}
|
||||
if t.Render == nil || t.Check == nil {
|
||||
return fmt.Errorf("template.Validate - %s needs a renderer and a checker", t.ID)
|
||||
}
|
||||
if len(t.Nodes) < 1 || len(t.Nodes) > MaxNodes || len(t.Links) > MaxLinks {
|
||||
return fmt.Errorf("template.Validate - %s must have 1 to %d nodes and at most %d links", t.ID, MaxNodes, MaxLinks)
|
||||
}
|
||||
if err := t.validateGraph(); err != nil {
|
||||
return fmt.Errorf("template.Validate - %s: %w", t.ID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateGraph checks the nodes and the links between them.
|
||||
func (t Template) validateGraph() error {
|
||||
nics := make(map[string]int, len(t.Nodes))
|
||||
for _, node := range t.Nodes {
|
||||
if err := node.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, duplicate := nics[node.Name]; duplicate {
|
||||
return fmt.Errorf("Template.validateGraph - duplicate node %s", node.Name)
|
||||
}
|
||||
// Every node starts with its management NIC.
|
||||
nics[node.Name] = 1
|
||||
}
|
||||
links := make(map[string]struct{}, len(t.Links))
|
||||
for _, link := range t.Links {
|
||||
if _, duplicate := links[link.Name]; duplicate || !nodeNamePattern.MatchString(link.Name) {
|
||||
return fmt.Errorf("Template.validateGraph - link names must be unique and match %s", nodeNamePattern)
|
||||
}
|
||||
links[link.Name] = struct{}{}
|
||||
if len(link.Nodes) < 1 {
|
||||
return fmt.Errorf("Template.validateGraph - link %s has no node", link.Name)
|
||||
}
|
||||
for _, name := range link.Nodes {
|
||||
count, known := nics[name]
|
||||
if !known {
|
||||
return fmt.Errorf("Template.validateGraph - link %s names unknown node %s", link.Name, name)
|
||||
}
|
||||
if count+1 > MaxInterfaces {
|
||||
return fmt.Errorf("Template.validateGraph - node %s has more than %d interfaces", name, MaxInterfaces)
|
||||
}
|
||||
nics[name] = count + 1
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n Node) validate() error {
|
||||
switch {
|
||||
case !nodeNamePattern.MatchString(n.Name):
|
||||
return fmt.Errorf("Node.validate - node name %q must match %s", n.Name, nodeNamePattern)
|
||||
case n.Kind != MikroTik && n.Kind != Ubuntu:
|
||||
return fmt.Errorf("Node.validate - node %s has unknown kind %q", n.Name, n.Kind)
|
||||
case !profilePattern.MatchString(n.Profile):
|
||||
return fmt.Errorf("Node.validate - node %s needs an LXD profile", n.Name)
|
||||
case !sourcePattern.MatchString(n.Source):
|
||||
return fmt.Errorf("Node.validate - node %s needs a source snapshot as instance/snapshot", n.Name)
|
||||
case n.MemoryBytes <= 0:
|
||||
return fmt.Errorf("Node.validate - node %s needs its guest memory", n.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Plan returns the variant of a lab: management addresses in node order and
|
||||
// one data NIC per link membership, numbered from eth1 in link order. MAC
|
||||
// addresses are locally administered: a management MAC encodes the unique
|
||||
// management address, and a data MAC needs to be unique only inside its
|
||||
// own lab bridge.
|
||||
func (t Template) Plan(seed [SeedBytes]byte, addresses []netip.Addr, prefixBits int, host netip.Addr) (Variant, error) {
|
||||
if len(addresses) != len(t.Nodes) {
|
||||
return Variant{}, errors.New("template.Plan - one management address per node is required")
|
||||
}
|
||||
variant := Variant{Seed: seed, Nodes: make([]PlannedNode, 0, len(t.Nodes))}
|
||||
for index, node := range t.Nodes {
|
||||
address := addresses[index]
|
||||
if !address.Is4() {
|
||||
return Variant{}, errors.New("template.Plan - management addresses must be IPv4")
|
||||
}
|
||||
octets := address.As4()
|
||||
variant.Nodes = append(variant.Nodes, PlannedNode{
|
||||
Name: node.Name,
|
||||
Kind: node.Kind,
|
||||
Management: netip.PrefixFrom(address, prefixBits),
|
||||
ManagementHost: host,
|
||||
ManagementMAC: fmt.Sprintf("02:4c:00:%02x:%02x:%02x", octets[1], octets[2], octets[3]),
|
||||
})
|
||||
}
|
||||
for _, link := range t.Links {
|
||||
for _, name := range link.Nodes {
|
||||
for index := range variant.Nodes {
|
||||
planned := &variant.Nodes[index]
|
||||
if planned.Name != name {
|
||||
continue
|
||||
}
|
||||
nic := len(planned.Interfaces) + 1
|
||||
planned.Interfaces = append(planned.Interfaces, Interface{
|
||||
Device: fmt.Sprintf("eth%d", nic),
|
||||
Link: link.Name,
|
||||
MAC: fmt.Sprintf("02:4c:01:%02x:%02x:00", index, nic),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return variant, nil
|
||||
}
|
||||
|
||||
// ValidateCriteria reports checker output that cannot be a result.
|
||||
func ValidateCriteria(criteria []Criterion) error {
|
||||
if len(criteria) < 1 || len(criteria) > MaxCriteria {
|
||||
return fmt.Errorf("template.ValidateCriteria - a check must have 1 to %d criteria", MaxCriteria)
|
||||
}
|
||||
seen := make(map[string]struct{}, len(criteria))
|
||||
for _, criterion := range criteria {
|
||||
if _, duplicate := seen[criterion.ID]; duplicate || !criterionPattern.MatchString(criterion.ID) {
|
||||
return errors.New("template.ValidateCriteria - criterion IDs must be unique and match the contract")
|
||||
}
|
||||
seen[criterion.ID] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateBootstrap reports a rendered bootstrap that LXD would not take.
|
||||
func ValidateBootstrap(bootstrap Bootstrap) error {
|
||||
if len(bootstrap.UserData) > MaxBootstrapBytes || len(bootstrap.NetworkConfig) > MaxBootstrapBytes {
|
||||
return fmt.Errorf("template.ValidateBootstrap - cloud-init documents must have at most %d bytes", MaxBootstrapBytes)
|
||||
}
|
||||
if strings.ContainsRune(bootstrap.UserData, 0) || strings.ContainsRune(bootstrap.NetworkConfig, 0) {
|
||||
return errors.New("template.ValidateBootstrap - cloud-init documents must not contain NUL")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package template_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lincodex/labd/internal/template"
|
||||
)
|
||||
|
||||
func pair() template.Template {
|
||||
return template.Template{
|
||||
ID: "mikrotik-pair", Version: 1,
|
||||
Nodes: []template.Node{
|
||||
{Name: "r1", Kind: template.MikroTik, Profile: "lincodex-lab-small", Source: "tpl-chr/base", MemoryBytes: 768 << 20},
|
||||
{Name: "srv", Kind: template.Ubuntu, Profile: "lincodex-lab-small", Source: "tpl-ubuntu/base", MemoryBytes: 768 << 20},
|
||||
},
|
||||
Links: []template.Link{{Name: "lan", Nodes: []string{"r1", "srv"}}, {Name: "wan", Nodes: []string{"r1"}}},
|
||||
Render: func(template.Variant) (map[string]template.Bootstrap, error) { return nil, nil },
|
||||
Check: func(context.Context, template.Variant, []template.NodeAccess) ([]template.Criterion, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRejectsTemplatesLabdCannotProvision(t *testing.T) {
|
||||
if err := pair().Validate(); err != nil {
|
||||
t.Fatalf("Validate() error = %v", err)
|
||||
}
|
||||
tests := map[string]func(*template.Template){
|
||||
"no checker": func(t *template.Template) { t.Check = nil },
|
||||
"no nodes": func(t *template.Template) { t.Nodes = nil },
|
||||
"duplicate node": func(t *template.Template) { t.Nodes[1].Name = "r1" },
|
||||
"unknown kind": func(t *template.Template) { t.Nodes[0].Kind = "cisco" },
|
||||
"source not a snap": func(t *template.Template) { t.Nodes[0].Source = "tpl-chr" },
|
||||
"no memory": func(t *template.Template) { t.Nodes[0].MemoryBytes = 0 },
|
||||
"link to no node": func(t *template.Template) { t.Links[0].Nodes = []string{"r9"} },
|
||||
"duplicate link": func(t *template.Template) { t.Links[1].Name = "lan" },
|
||||
"too many NICs on r1": func(t *template.Template) {
|
||||
for index := range 8 {
|
||||
t.Links = append(t.Links, template.Link{Name: "x" + string(rune('a'+index)), Nodes: []string{"r1"}})
|
||||
}
|
||||
},
|
||||
}
|
||||
for name, change := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
candidate := pair()
|
||||
candidate.Nodes = append([]template.Node(nil), candidate.Nodes...)
|
||||
candidate.Links = append([]template.Link(nil), candidate.Links...)
|
||||
change(&candidate)
|
||||
if err := candidate.Validate(); err == nil {
|
||||
t.Fatal("Validate() accepted the template")
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := template.NewRegistry(pair(), pair()); err == nil {
|
||||
t.Fatal("NewRegistry() accepted a duplicate template version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanNumbersInterfacesAndKeepsTheVariantStable(t *testing.T) {
|
||||
addresses := []netip.Addr{netip.MustParseAddr("10.81.0.16"), netip.MustParseAddr("10.81.0.17")}
|
||||
variant, err := pair().Plan([32]byte{7}, addresses, 24, netip.MustParseAddr("10.81.0.1"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r1, _ := variant.Node("r1")
|
||||
srv, _ := variant.Node("srv")
|
||||
if r1.Management.String() != "10.81.0.16/24" || r1.ManagementMAC != "02:4c:00:51:00:10" ||
|
||||
len(r1.Interfaces) != 2 || r1.Interfaces[1].Device != "eth2" || r1.Interfaces[1].Link != "wan" ||
|
||||
len(srv.Interfaces) != 1 || srv.Interfaces[0].MAC == r1.Interfaces[0].MAC {
|
||||
t.Fatalf("plan: r1 %+v, srv %+v", r1, srv)
|
||||
}
|
||||
again, _ := pair().Plan([32]byte{7}, addresses, 24, netip.MustParseAddr("10.81.0.1"))
|
||||
if variant.Rand("faults").Uint64() != again.Rand("faults").Uint64() {
|
||||
t.Fatal("the same seed gave another variant")
|
||||
}
|
||||
other, _ := pair().Plan([32]byte{8}, addresses, 24, netip.MustParseAddr("10.81.0.1"))
|
||||
if variant.Rand("faults").Uint64() == other.Rand("faults").Uint64() {
|
||||
t.Fatal("another seed gave the same variant")
|
||||
}
|
||||
|
||||
network := template.UbuntuNetworkConfig(srv)
|
||||
if !strings.Contains(network, "10.81.0.17/24") || strings.Contains(network, "gateway") ||
|
||||
!strings.Contains(network, `macaddress: "02:4c:00:51:00:11"`) || !strings.Contains(network, "set-name: eth1") {
|
||||
t.Fatalf("network config:\n%s", network)
|
||||
}
|
||||
script := template.RouterOSManagementScript(r1)
|
||||
if !strings.Contains(script, "address=10.81.0.16/24 interface=mgmt") || !strings.Contains(script, "api address=10.81.0.1/32") ||
|
||||
!strings.Contains(script, `mac-address="02:4C:00:51:00:10"`) {
|
||||
t.Fatalf("RouterOS script:\n%s", script)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCriteria(t *testing.T) {
|
||||
if err := template.ValidateCriteria([]template.Criterion{{ID: "ospf"}, {ID: "ping", Passed: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, criteria := range [][]template.Criterion{nil, {{ID: "a"}, {ID: "a"}}, {{ID: "Bad ID"}}} {
|
||||
if err := template.ValidateCriteria(criteria); err == nil {
|
||||
t.Fatalf("ValidateCriteria(%v) accepted", criteria)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user