integrations_edit_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package httpapi
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/rintegration"
  6. )
  7. func TestSSHEnvWriteScriptRewritesFileAndRestarts(t *testing.T) {
  8. v, ok := rintegration.LookupVendor("asuswrt")
  9. if !ok {
  10. t.Fatal("asuswrt vendor missing")
  11. }
  12. env := map[string]string{
  13. "ASUSWRT_HOST": "127.0.0.1",
  14. "ASUSWRT_USER": "admin",
  15. "ASUSWRT_PASS": "line1\nline2",
  16. "NETPULSE_INTERVAL": "45s",
  17. "NETPULSE_TOKEN": "token-1",
  18. }
  19. script := sshEnvWriteScript(v, env)
  20. for _, want := range []string{
  21. `cat > "$ENV_FILE.new" <<'ENVEOF'`,
  22. "NETPULSE_INTERVAL=45s",
  23. "ASUSWRT_USER=admin",
  24. `chmod 600 "$ENV_FILE.new"`,
  25. `mv -f "$ENV_FILE.new" "$ENV_FILE"`,
  26. `"$INIT" start`,
  27. } {
  28. if !strings.Contains(script, want) {
  29. t.Fatalf("script missing %q\n%s", want, script)
  30. }
  31. }
  32. if !strings.Contains(script, `ASUSWRT_PASS=line1\nline2`) {
  33. t.Fatal("newlines in values must be escaped, not written raw")
  34. }
  35. }
  36. func TestSSHEnvReadScriptTargetsEnvFile(t *testing.T) {
  37. v, _ := rintegration.LookupVendor("asuswrt")
  38. if got := sshEnvReadScript(v); got != "cat /etc/"+v.Binary+".env" {
  39. t.Fatalf("read script = %q", got)
  40. }
  41. }
  42. func TestPusherEnvIntervalMatchesEditFormat(t *testing.T) {
  43. s := &server{}
  44. v, _ := rintegration.LookupVendor("routeros")
  45. env := s.pusherEnv(v, rintegration.Row{Slug: "chr-lab", Host: "10.0.0.1", Username: "u", IntervalS: 60}, "pw", "https://np", "tok")
  46. if env["NETPULSE_INTERVAL"] != "60s" {
  47. t.Fatalf("interval = %q, want 60s", env["NETPULSE_INTERVAL"])
  48. }
  49. }