f-55: lab: retrait de la copie de labctl, lab.rst réduit à ce qui est propre à two #55
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
8dc7721efe
commit
efc95284bb
30 changed files with 56 additions and 7755 deletions
|
|
@ -1,193 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/lab/machine"
|
||||
"git.g3e.fr/syonad/two/internal/lab/provision"
|
||||
)
|
||||
|
||||
const (
|
||||
topologyFile = "topology.yml"
|
||||
pollInterval = 5 * time.Second
|
||||
stopTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
execve = syscall.Exec
|
||||
procDir = "/proc"
|
||||
)
|
||||
|
||||
func defaultDir(parts ...string) string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(append([]string{home}, parts...)...)
|
||||
}
|
||||
|
||||
func flags(name string, stderr io.Writer) (*flag.FlagSet, *string) {
|
||||
fs := flag.NewFlagSet(name, flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
fs.Usage = func() { fmt.Fprint(stderr, usage) }
|
||||
return fs, fs.String("run", defaultDir("lab-run"), "run directory of the lab")
|
||||
}
|
||||
|
||||
func lab(runDir string, plan string, stdout, stderr io.Writer) (machine.Lab, bool) {
|
||||
dir, err := filepath.Abs(runDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return machine.Lab{}, false
|
||||
}
|
||||
p, ok := load(plan, stderr)
|
||||
if !ok {
|
||||
return machine.Lab{}, false
|
||||
}
|
||||
return machine.Lab{
|
||||
Plan: p,
|
||||
RunDir: dir,
|
||||
ProcDir: procDir,
|
||||
Runner: provision.ExecRunner{},
|
||||
Poll: pollInterval,
|
||||
Stop: stopTimeout,
|
||||
Out: stdout,
|
||||
}, true
|
||||
}
|
||||
|
||||
func current(runDir string, stdout, stderr io.Writer) (machine.Lab, bool) {
|
||||
saved := filepath.Join(runDir, topologyFile)
|
||||
if _, err := os.Stat(saved); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: no lab in %s: %v\n", runDir, err)
|
||||
return machine.Lab{}, false
|
||||
}
|
||||
return lab(runDir, saved, stdout, stderr)
|
||||
}
|
||||
|
||||
func upCmd(args []string, stdout, stderr io.Writer) int {
|
||||
fs, runDir := flags("up", stderr)
|
||||
cacheDir := fs.String("cache", defaultDir(".cache", "labctl"), "image cache directory")
|
||||
timeout := fs.Duration("timeout", 20*time.Minute, "how long to wait for the nodes to be ready")
|
||||
if err := fs.Parse(args); err != nil || fs.NArg() != 1 {
|
||||
fs.Usage()
|
||||
return 2
|
||||
}
|
||||
cache, err := filepath.Abs(*cacheDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(*runDir, topologyFile)); err == nil {
|
||||
previous, ok := current(*runDir, stdout, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
running, err := previous.Running()
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
if len(running) > 0 {
|
||||
fmt.Fprintf(stderr, "labctl: lab %s is still running in %s: 'lab down' first\n", previous.Plan.Name, previous.RunDir)
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
l, ok := lab(*runDir, fs.Arg(0), stdout, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
source, err := os.ReadFile(fs.Arg(0))
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
if err := os.MkdirAll(l.RunDir, 0o700); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(l.RunDir, topologyFile), source, 0o600); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
fetcher := provision.Fetcher{Client: &http.Client{}, CacheDir: cache}
|
||||
if err := l.Up(ctx, fetcher, *timeout); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\nlab: started nodes keep running: 'lab status', 'lab down'\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func statusCmd(args []string, stdout, stderr io.Writer) int {
|
||||
fs, runDir := flags("status", stderr)
|
||||
if err := fs.Parse(args); err != nil || fs.NArg() != 0 {
|
||||
fs.Usage()
|
||||
return 2
|
||||
}
|
||||
l, ok := current(*runDir, stdout, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
if err := l.Status(stdout); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func downCmd(args []string, stdout, stderr io.Writer) int {
|
||||
fs, runDir := flags("down", stderr)
|
||||
if err := fs.Parse(args); err != nil || fs.NArg() != 0 {
|
||||
fs.Usage()
|
||||
return 2
|
||||
}
|
||||
l, ok := current(*runDir, stdout, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
if err := l.Down(context.Background()); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func sshCmd(args []string, stdout, stderr io.Writer) int {
|
||||
fs, runDir := flags("ssh", stderr)
|
||||
if err := fs.Parse(args); err != nil || fs.NArg() < 1 {
|
||||
fs.Usage()
|
||||
return 2
|
||||
}
|
||||
l, ok := current(*runDir, stdout, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
argv, err := l.SSH(fs.Arg(0), isTerminal(os.Stdin), fs.Args()[1:])
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
path, err := exec.LookPath(argv[0])
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
if err := execve(path, argv, os.Environ()); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
|
@ -1,169 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/lab/provision"
|
||||
"git.g3e.fr/syonad/two/internal/lab/render"
|
||||
"git.g3e.fr/syonad/two/internal/lab/topology"
|
||||
)
|
||||
|
||||
const usage = `usage: labctl <command> [options] <topology.yml> [dir]
|
||||
|
||||
plan <topology.yml>
|
||||
validate the topology and print the deterministic plan: addresses, cables, ports
|
||||
|
||||
render -key <public key file> <topology.yml> <dir>
|
||||
write, for each node, <dir>/<node>/qemu.args (one argument per line) and the
|
||||
cloud-init seed files meta-data, user-data and network-config
|
||||
|
||||
up [-run dir] [-cache dir] [-timeout duration] <topology.yml>
|
||||
download and verify the images, create fresh disks and seeds, start every node
|
||||
(switches first) and wait until cloud-init has finished on each of them;
|
||||
the topology is kept in <run>/topology.yml for the commands below
|
||||
|
||||
status [-run dir]
|
||||
list the nodes of the lab and whether their qemu process is running
|
||||
|
||||
down [-run dir]
|
||||
stop every qemu process of the lab (SIGTERM, then SIGKILL)
|
||||
|
||||
ssh [-run dir] <node> [command...]
|
||||
open a shell on a node, or run a command on it, with the key generated by up
|
||||
|
||||
defaults: -run $HOME/lab-run, -cache $HOME/.cache/labctl, -timeout 20m
|
||||
`
|
||||
|
||||
type keyFiles []string
|
||||
|
||||
func (k *keyFiles) String() string { return strings.Join(*k, ",") }
|
||||
func (k *keyFiles) Set(v string) error { *k = append(*k, v); return nil }
|
||||
|
||||
func main() {
|
||||
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
|
||||
}
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprint(stderr, usage)
|
||||
return 2
|
||||
}
|
||||
switch args[0] {
|
||||
case "plan":
|
||||
if len(args) != 2 {
|
||||
fmt.Fprint(stderr, usage)
|
||||
return 2
|
||||
}
|
||||
return plan(args[1], stdout, stderr)
|
||||
case "render":
|
||||
return renderCmd(args[1:], stdout, stderr)
|
||||
case "up":
|
||||
return upCmd(args[1:], stdout, stderr)
|
||||
case "status":
|
||||
return statusCmd(args[1:], stdout, stderr)
|
||||
case "down":
|
||||
return downCmd(args[1:], stdout, stderr)
|
||||
case "ssh":
|
||||
return sshCmd(args[1:], stdout, stderr)
|
||||
default:
|
||||
fmt.Fprint(stderr, usage)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func load(path string, stderr io.Writer) (*topology.Plan, bool) {
|
||||
t, err := topology.Load(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return nil, false
|
||||
}
|
||||
p, err := topology.Compute(t)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %s:\n%v\n", path, err)
|
||||
return nil, false
|
||||
}
|
||||
return p, true
|
||||
}
|
||||
|
||||
func plan(path string, stdout, stderr io.Writer) int {
|
||||
p, ok := load(path, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
if err := p.Write(stdout); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func renderCmd(args []string, stdout, stderr io.Writer) int {
|
||||
fs := flag.NewFlagSet("render", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
fs.Usage = func() { fmt.Fprint(stderr, usage) }
|
||||
var keys keyFiles
|
||||
fs.Var(&keys, "key", "public key file allowed to log in, repeatable")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return 2
|
||||
}
|
||||
if fs.NArg() != 2 || len(keys) == 0 {
|
||||
fmt.Fprint(stderr, usage)
|
||||
return 2
|
||||
}
|
||||
|
||||
p, ok := load(fs.Arg(0), stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
dir, err := filepath.Abs(fs.Arg(1))
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
authorized, err := readKeys(keys)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
opts := render.Options{RunDir: dir, AuthorizedKeys: authorized}
|
||||
if err := provision.ReadInputs(p, &opts); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
nodes, err := render.Render(p, opts)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
for _, n := range nodes {
|
||||
if err := provision.WriteFiles(n); err != nil {
|
||||
fmt.Fprintf(stderr, "labctl: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(stdout, "%s\n", n.Dir)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func readKeys(files []string) ([]string, error) {
|
||||
var keys []string
|
||||
for _, f := range files {
|
||||
data, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, line)
|
||||
}
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
|
@ -1,371 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func runLab(args ...string) (int, string, string) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := run(args, &stdout, &stderr)
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
func TestRun_UsageOnMissingArguments(t *testing.T) {
|
||||
for _, args := range [][]string{nil, {"plan"}, {"deploy", "x.yml"}, {"plan", "a.yml", "b.yml"}, {"render", "a.yml", "dir"}, {"render", "-key", "k.pub", "a.yml"}, {"render", "-bogus"}} {
|
||||
code, _, stderr := runLab(args...)
|
||||
if code != 2 || !strings.Contains(stderr, "usage: lab") {
|
||||
t.Errorf("args %v: code %d, stderr %q", args, code, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_PlanOfTheShippedExampleTopology(t *testing.T) {
|
||||
code, stdout, stderr := runLab("plan", filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"))
|
||||
if code != 0 {
|
||||
t.Fatalf("code %d, stderr %s", code, stderr)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"lab evpn-2hv: nodes 4, segments 1, cables 3",
|
||||
"gateway 192.168.14.1",
|
||||
"hv2 underlay 192.168.14.12/24 02:4c:00:03:00:00 20004 <-> sw1 p2",
|
||||
"rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf",
|
||||
} {
|
||||
if !strings.Contains(stdout, want) {
|
||||
t.Errorf("output does not contain %q:\n%s", want, stdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_InvalidTopologyExitsWithErrors(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "bad.yml")
|
||||
doc := `name: bad
|
||||
images:
|
||||
deb: { url: https://example.invalid/a, sums: https://example.invalid/b }
|
||||
segments:
|
||||
under: { switch: sw, cidr: 10.0.0.0/31 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
rr: { role: router, image: deb, cpus: 1, memory: 512, segments: [under] }
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(doc), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, stdout, stderr := runLab("plan", path)
|
||||
if code != 1 || stdout != "" {
|
||||
t.Fatalf("code %d, stdout %q", code, stdout)
|
||||
}
|
||||
for _, want := range []string{path, `role "router"`, "prefix length out of range"} {
|
||||
if !strings.Contains(stderr, want) {
|
||||
t.Errorf("stderr does not mention %q:\n%s", want, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_MissingFile(t *testing.T) {
|
||||
code, _, stderr := runLab("plan", filepath.Join(t.TempDir(), "absent.yml"))
|
||||
if code != 1 || !strings.Contains(stderr, "absent.yml") {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RenderWritesEveryNodeFiles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
key := filepath.Join(dir, "lab.pub")
|
||||
if err := os.WriteFile(key, []byte("# lab key\n\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O lab-automation\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := filepath.Join(dir, "run")
|
||||
code, stdout, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), out)
|
||||
if code != 0 {
|
||||
t.Fatalf("code %d, stderr %s", code, stderr)
|
||||
}
|
||||
for _, node := range []string{"sw1", "rr1", "hv1", "hv2"} {
|
||||
if !strings.Contains(stdout, filepath.Join(out, node)) {
|
||||
t.Errorf("stdout does not list %s:\n%s", node, stdout)
|
||||
}
|
||||
for _, f := range []string{"qemu.args", "meta-data", "user-data", "network-config"} {
|
||||
info, err := os.Stat(filepath.Join(out, node, f))
|
||||
if err != nil {
|
||||
t.Errorf("%s/%s: %v", node, f, err)
|
||||
continue
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Errorf("%s/%s mode %o, want 600", node, f, info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
}
|
||||
args, err := os.ReadFile(filepath.Join(out, "hv1", "qemu.args"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(args), "\n-netdev\nuser,id=mgmt0,restrict=on,ipv6=off,hostfwd=tcp:127.0.0.1:2202-:22\n") {
|
||||
t.Errorf("qemu.args is not one argument per line:\n%s", args)
|
||||
}
|
||||
userData, err := os.ReadFile(filepath.Join(out, "hv1", "user-data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(userData), "lab-automation") || strings.Contains(string(userData), "# lab key") {
|
||||
t.Errorf("keys not read as an authorized_keys file:\n%s", userData)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RenderRefusesMissingKeyFile(t *testing.T) {
|
||||
code, _, stderr := runLab("render", "-key", filepath.Join(t.TempDir(), "absent.pub"), filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), t.TempDir())
|
||||
if code != 1 || !strings.Contains(stderr, "absent.pub") {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func savedLab(t *testing.T) string {
|
||||
t.Helper()
|
||||
run := filepath.Join(t.TempDir(), "run")
|
||||
if err := os.MkdirAll(run, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
example, err := os.ReadFile(filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(run, "topology.yml"), example, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return run
|
||||
}
|
||||
|
||||
func TestRun_LifecycleUsage(t *testing.T) {
|
||||
for _, args := range [][]string{{"up"}, {"up", "a.yml", "b.yml"}, {"status", "x"}, {"down", "x"}, {"ssh"}, {"up", "-bogus", "a.yml"}} {
|
||||
code, _, stderr := runLab(args...)
|
||||
if code != 2 || !strings.Contains(stderr, "usage: lab") {
|
||||
t.Errorf("args %v: code %d, stderr %q", args, code, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_CommandsWithoutALabInTheRunDir(t *testing.T) {
|
||||
run := t.TempDir()
|
||||
for _, cmd := range []string{"status", "down", "ssh"} {
|
||||
args := []string{cmd, "-run", run}
|
||||
if cmd == "ssh" {
|
||||
args = append(args, "hv1")
|
||||
}
|
||||
code, _, stderr := runLab(args...)
|
||||
if code != 1 || !strings.Contains(stderr, "labctl: no lab in "+run) {
|
||||
t.Errorf("%s: code %d, stderr %q", cmd, code, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_StatusOfAStoppedLab(t *testing.T) {
|
||||
code, stdout, stderr := runLab("status", "-run", savedLab(t))
|
||||
want := `node role state pid ssh
|
||||
sw1 switch stopped - 127.0.0.1:2200
|
||||
rr1 rr stopped - 127.0.0.1:2201
|
||||
hv1 host stopped - 127.0.0.1:2202
|
||||
hv2 host stopped - 127.0.0.1:2203
|
||||
`
|
||||
if code != 0 || stdout != want {
|
||||
t.Errorf("code %d, stderr %q, stdout:\n%s", code, stderr, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_DownOfAStoppedLab(t *testing.T) {
|
||||
code, stdout, stderr := runLab("down", "-run", savedLab(t))
|
||||
if code != 0 || stdout != "" || stderr != "" {
|
||||
t.Errorf("code %d, stdout %q, stderr %q", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_SSHExecsSSHWithTheNodePort(t *testing.T) {
|
||||
run := savedLab(t)
|
||||
var gotPath string
|
||||
var gotArgv []string
|
||||
execve = func(path string, argv []string, env []string) error {
|
||||
gotPath, gotArgv = path, argv
|
||||
return nil
|
||||
}
|
||||
t.Cleanup(func() { execve = syscall.Exec })
|
||||
|
||||
code, _, stderr := runLab("ssh", "-run", run, "hv2", "ip", "-br", "a")
|
||||
|
||||
if code != 0 {
|
||||
t.Fatalf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
if filepath.Base(gotPath) != "ssh" {
|
||||
t.Errorf("path = %q", gotPath)
|
||||
}
|
||||
want := []string{"ssh",
|
||||
"-i", filepath.Join(run, "lab_ed25519"),
|
||||
"-o", "IdentitiesOnly=yes",
|
||||
"-o", "IdentityAgent=none",
|
||||
"-o", "StrictHostKeyChecking=no",
|
||||
"-o", "UserKnownHostsFile=/dev/null",
|
||||
"-o", "LogLevel=ERROR",
|
||||
"-p", "2203",
|
||||
"debian@127.0.0.1",
|
||||
"ip", "-br", "a",
|
||||
}
|
||||
if strings.Join(gotArgv, " ") != strings.Join(want, " ") {
|
||||
t.Errorf("\n got %q\nwant %q", gotArgv, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_SSHUnknownNode(t *testing.T) {
|
||||
code, _, stderr := runLab("ssh", "-run", savedLab(t), "hv9")
|
||||
if code != 1 || !strings.Contains(stderr, `node "hv9" is not in lab evpn-2hv`) {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_UpRefusesAnInvalidTopologyAndKeepsTheSavedOne(t *testing.T) {
|
||||
run := savedLab(t)
|
||||
before, _ := os.ReadFile(filepath.Join(run, "topology.yml"))
|
||||
bad := filepath.Join(t.TempDir(), "bad.yml")
|
||||
if err := os.WriteFile(bad, []byte("name: x\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
code, _, stderr := runLab("up", "-run", run, bad)
|
||||
|
||||
if code != 1 || !strings.Contains(stderr, "at least one node is required") {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
if after, _ := os.ReadFile(filepath.Join(run, "topology.yml")); string(after) != string(before) {
|
||||
t.Error("the saved topology was replaced by an invalid one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTerminal_DevNullAndPipesAreNotTerminals(t *testing.T) {
|
||||
null, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer null.Close()
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
for name, f := range map[string]*os.File{"/dev/null": null, "pipe": r} {
|
||||
if isTerminal(f) {
|
||||
t.Errorf("%s is detected as a terminal", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_UpRefusesToReplaceARunningLab(t *testing.T) {
|
||||
run := savedLab(t)
|
||||
before, _ := os.ReadFile(filepath.Join(run, "topology.yml"))
|
||||
proc := t.TempDir()
|
||||
procDir = proc
|
||||
t.Cleanup(func() { procDir = "/proc" })
|
||||
|
||||
cmd := exec.Command("sleep", "30")
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() })
|
||||
pid := strconv.Itoa(cmd.Process.Pid)
|
||||
if err := os.MkdirAll(filepath.Join(proc, pid), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(proc, pid, "cmdline"), []byte("qemu-system-x86_64\x00-name\x00hv1\x00"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(run, "hv1"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(run, "hv1", "qemu.pid"), []byte(pid+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
other := filepath.Join(t.TempDir(), "other.yml")
|
||||
if err := os.WriteFile(other, bytes.ReplaceAll(before, []byte("evpn-2hv"), []byte("other")), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _, stderr := runLab("up", "-run", run, other)
|
||||
|
||||
if code != 1 || !strings.Contains(stderr, "lab evpn-2hv is still running in "+run) {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
if after, _ := os.ReadFile(filepath.Join(run, "topology.yml")); string(after) != string(before) {
|
||||
t.Error("the topology of a running lab was replaced")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
key := filepath.Join(dir, "lab.pub")
|
||||
if err := os.WriteFile(key, []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O lab-automation\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := filepath.Join(dir, "run")
|
||||
code, _, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), out)
|
||||
if code != 0 {
|
||||
t.Fatalf("code %d, stderr %s", code, stderr)
|
||||
}
|
||||
for node, wants := range map[string][]string{
|
||||
"sw1": {"router bgp 65100"},
|
||||
"rr1": {"bgp listen range 192.168.14.0/24 peer-group fabric"},
|
||||
"hv1": {"hostname hv1"},
|
||||
"hv2": {"hostname hv2"},
|
||||
} {
|
||||
data, err := os.ReadFile(filepath.Join(out, node, "user-data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range wants {
|
||||
if !strings.Contains(string(data), want) {
|
||||
t.Errorf("%s user-data does not carry %q", node, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, node := range []string{"hv1", "hv2"} {
|
||||
data, err := os.ReadFile(filepath.Join(out, node, "user-data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(data), "mount -o ro LABEL=LABFILES /run/lab-files") {
|
||||
t.Errorf("%s user-data does not mount its files", node)
|
||||
}
|
||||
}
|
||||
for _, agent := range []string{"two.yml", "bgp.yml"} {
|
||||
data, err := os.ReadFile(filepath.Join("..", "..", "test", "e2e", "topologies", "agent", agent))
|
||||
if err != nil || !strings.Contains(string(data), "route_reflectors: [10.255.255.1]") {
|
||||
t.Errorf("agent/%s = %q, %v", agent, data, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RenderReportsAMissingFRRConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
key := filepath.Join(dir, "lab.pub")
|
||||
if err := os.WriteFile(key, []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O x\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
topo := filepath.Join(dir, "lab.yml")
|
||||
doc := `name: x
|
||||
images:
|
||||
deb: { url: https://example.invalid/deb.qcow2, sums: https://example.invalid/SHA512SUMS }
|
||||
segments:
|
||||
underlay: { switch: sw1, cidr: 10.1.0.0/24 }
|
||||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 1, memory: 512, frr: frr/absent.conf }
|
||||
hv1: { role: host, image: deb, cpus: 1, memory: 512, segments: [underlay] }
|
||||
`
|
||||
if err := os.WriteFile(topo, []byte(doc), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _, stderr := runLab("render", "-key", key, topo, filepath.Join(dir, "run"))
|
||||
if code != 1 || !strings.Contains(stderr, "node sw1: ") || !strings.Contains(stderr, filepath.Join(dir, "frr", "absent.conf")) {
|
||||
t.Errorf("code %d, stderr %q", code, stderr)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
//go:build linux || darwin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func isTerminal(f *os.File) bool {
|
||||
_, err := unix.IoctlGetTermios(int(f.Fd()), ioctlReadTermios)
|
||||
return err == nil
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
package main
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
const ioctlReadTermios = unix.TIOCGETA
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
package main
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
const ioctlReadTermios = unix.TCGETS
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
//go:build !linux && !darwin
|
||||
|
||||
package main
|
||||
|
||||
import "os"
|
||||
|
||||
func isTerminal(*os.File) bool { return false }
|
||||
Loading…
Add table
Add a link
Reference in a new issue