health_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package aerotun
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestHealthHistoryTransitionsAndPersistence(t *testing.T) {
  10. path := filepath.Join(t.TempDir(), "history.json")
  11. h := newHealthMonitor(path)
  12. now := time.Now().UTC()
  13. tunnel := TunnelInstance{ID: "id1", Name: "test0", Running: true, PSK: "private-psk", PrivateKey: "private-identity"}
  14. h.observe([]TunnelInstance{tunnel}, now)
  15. h.observe([]TunnelInstance{tunnel}, now.Add(time.Second))
  16. tunnel.Connected = true
  17. h.observe([]TunnelInstance{tunnel}, now.Add(3*time.Second))
  18. h.observe(nil, now.Add(8*time.Second))
  19. rows := h.history("id1")
  20. if len(rows) != 3 || rows[0].Status != "removed" || rows[1].Status != "connected" || rows[1].Previous != "waiting" || rows[1].PreviousDurationSeconds != 3 {
  21. t.Fatalf("bad transitions: %+v", rows)
  22. }
  23. data, err := os.ReadFile(path)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. if strings.Contains(string(data), "private-") {
  28. t.Fatal("history contains secret")
  29. }
  30. info, _ := os.Stat(path)
  31. if info.Mode().Perm() != 0600 {
  32. t.Fatalf("unsafe permissions: %v", info.Mode())
  33. }
  34. reloaded := newHealthMonitor(path)
  35. if got := reloaded.history("id1"); len(got) != 3 {
  36. t.Fatalf("history not restored: %+v", got)
  37. }
  38. }
  39. func TestHealthStatusDistinguishesDisabledErrorWaitingAndConnected(t *testing.T) {
  40. for _, tc := range []struct {
  41. tunnel TunnelInstance
  42. want string
  43. }{{TunnelInstance{Disabled: true}, "disabled"}, {TunnelInstance{LastError: "failed"}, "error"}, {TunnelInstance{}, "stopped"}, {TunnelInstance{Running: true}, "waiting"}, {TunnelInstance{Running: true, Connected: true}, "connected"}} {
  44. if got := tunnelHealthStatus(tc.tunnel); got != tc.want {
  45. t.Fatalf("got %s want %s", got, tc.want)
  46. }
  47. }
  48. }
  49. func TestDropBreakdownCountsOnlyReportedPackets(t *testing.T) {
  50. r := &TunnelRunner{}
  51. r.recordDrop(&r.dropSourcePolicy, 2)
  52. r.recordDrop(&r.dropSendFailure, 3)
  53. if r.droppedPackets != 5 || r.dropSourcePolicy != 2 || r.dropSendFailure != 3 || r.dropInvalidIncoming != 0 {
  54. t.Fatalf("incorrect breakdown: %+v", r)
  55. }
  56. }