v1
This commit is contained in:
commit
8d4915543e
132 files changed
+20628
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
)
|
||||
|
||||
func TestExpandHostCommand(t *testing.T) {
|
||||
command := config.HostCommand{Args: []string{"mtr", "--target={address}", "{host}", "{target}", "$(literal)"}}
|
||||
host := resolvedHost{Name: "office/xzi-groß1", IP: "2001:db8::1"}
|
||||
got := expandHostCommand(command, host, []string{"--report", "two words"})
|
||||
want := []string{"mtr", "--target=2001:db8::1", "xzi-groß1", "office/xzi-groß1", "$(literal)", "--report", "two words"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunConfiguredHostCommand(t *testing.T) {
|
||||
prepareTransferTest(t)
|
||||
f, err := os.OpenFile(config.Path(), os.O_APPEND|os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = f.WriteString(`
|
||||
commands:
|
||||
trace:
|
||||
alias_prefix: m
|
||||
args: [mtr, "{address}", "{host}", "{target}"]
|
||||
literal:
|
||||
args: [printf, "%s|%s|%s", "{address}", "{host}", "$(touch should-not-exist)"]
|
||||
`)
|
||||
if closeErr := f.Close(); err != nil || closeErr != nil {
|
||||
t.Fatalf("write config: %v %v", err, closeErr)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{[]string{"run", "--dry-run", "trace", "fsnbx1", "--", "--report"}, `"mtr" "10.0.0.9" "fsn1-bx1.hoster.de" "fsn1-bx1.hoster.de" "--report"`},
|
||||
{[]string{"run", "--dry-run", "trace", "--", "fsnbx1", "--help"}, `"mtr" "10.0.0.9" "fsn1-bx1.hoster.de" "fsn1-bx1.hoster.de" "--help"`},
|
||||
{[]string{"run", "literal", "fsnbx1"}, "10.0.0.9|fsn1-bx1.hoster.de|$(touch should-not-exist)"},
|
||||
} {
|
||||
out, err := runNetssh(t, tc.args...)
|
||||
if err != nil || out != tc.want {
|
||||
t.Fatalf("%v: %q (%v), want %q", tc.args, out, err, tc.want)
|
||||
}
|
||||
}
|
||||
out, err := runNetssh(t, "run", "missing", "fsnbx1")
|
||||
if err == nil || !strings.Contains(out, "unknown configured command") {
|
||||
t.Fatalf("%q %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandAcceptsResolvedHostname(t *testing.T) {
|
||||
prepareTransferTest(t)
|
||||
data := `netbox:
|
||||
url: http://127.0.0.1:1
|
||||
token: t
|
||||
hosts:
|
||||
- name: router
|
||||
address: router.example.com
|
||||
commands:
|
||||
trace:
|
||||
args: [mtr, "{address}"]
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "netssh.yaml"), []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := runNetssh(t, "run", "--dry-run", "trace", "router")
|
||||
if err != nil || out != `"mtr" "router.example.com"` {
|
||||
t.Fatalf("%q %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunOwnScripts(t *testing.T) {
|
||||
for _, mode := range []string{"executable", "bash", "python3"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
if mode != "executable" {
|
||||
if _, err := exec.LookPath(mode); err != nil {
|
||||
t.Skip("interpreter not installed")
|
||||
}
|
||||
}
|
||||
prepareTransferTest(t)
|
||||
script := filepath.Join(t.TempDir(), "my host check")
|
||||
body := "#!/bin/sh\nprintf '<%s>\\n' \"$@\"\nexit 7\n"
|
||||
permissions := os.FileMode(0700)
|
||||
argv := []string{script}
|
||||
if mode != "executable" {
|
||||
permissions = 0600
|
||||
argv = []string{mode, script}
|
||||
}
|
||||
if mode == "python3" {
|
||||
body = "import sys\nfor arg in sys.argv[1:]:\n print('<' + arg + '>')\nsys.exit(7)\n"
|
||||
}
|
||||
if err := os.WriteFile(script, []byte(body), permissions); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
argv = append(argv, "{address}", "{host}", "{target}")
|
||||
var yaml strings.Builder
|
||||
yaml.WriteString("\ncommands:\n check:\n alias_prefix: c\n args:\n")
|
||||
for _, arg := range argv {
|
||||
fmt.Fprintf(&yaml, " - %q\n", arg)
|
||||
}
|
||||
f, err := os.OpenFile(config.Path(), os.O_APPEND|os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = f.WriteString(yaml.String())
|
||||
if closeErr := f.Close(); err != nil || closeErr != nil {
|
||||
t.Fatalf("config write: %v %v", err, closeErr)
|
||||
}
|
||||
out, err := runNetssh(t, "run", "check", "fsnbx1", "--", "--report", "two words", "$(literal)")
|
||||
want := "<10.0.0.9>\n<fsn1-bx1.hoster.de>\n<fsn1-bx1.hoster.de>\n<--report>\n<two words>\n<$(literal)>"
|
||||
if out != want {
|
||||
t.Fatalf("script args: %q, want %q", out, want)
|
||||
}
|
||||
exit, ok := err.(*exec.ExitError)
|
||||
if !ok || exit.ExitCode() != 7 {
|
||||
t.Fatalf("script exit status lost: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user