4
0

integrations_edit_test.go 1.6 KB

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