helpers_linux.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package tests
  2. import (
  3. "context"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "hth/internal/app"
  10. hthv1 "hth/internal/gen/hth/api/v1"
  11. "hth/internal/grpcserver"
  12. "hth/internal/serialtransport"
  13. "hth/internal/testutil"
  14. )
  15. func startAppFromConfig(t *testing.T, cfgPath string) *app.App {
  16. t.Helper()
  17. a, err := app.New(cfgPath, "", nil)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. ctx, cancel := context.WithCancel(context.Background())
  22. errCh := make(chan error, 1)
  23. go func() { errCh <- a.Run(ctx) }()
  24. t.Cleanup(func() {
  25. cancel()
  26. a.Stop()
  27. <-errCh
  28. })
  29. return a
  30. }
  31. func waitUnix(t *testing.T, path string, d time.Duration) {
  32. t.Helper()
  33. deadline := time.Now().Add(d)
  34. for time.Now().Before(deadline) {
  35. if _, err := os.Stat(path); err == nil {
  36. if c, err := grpcserver.DialUnix(path, 150*time.Millisecond); err == nil {
  37. c.Close()
  38. return
  39. }
  40. }
  41. time.Sleep(20 * time.Millisecond)
  42. }
  43. t.Fatalf("socket not ready: %s", path)
  44. }
  45. func waitSlewBudget() {
  46. time.Sleep(time.Second)
  47. }
  48. func waitUntilReady(t *testing.T, obs string, d time.Duration) *hthv1.Snapshot {
  49. t.Helper()
  50. serialtransport.SetTestWriteCap(0)
  51. serialtransport.SetTestForceWriteError(false)
  52. oc, err := grpcserver.DialUnix(obs, time.Second)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. defer oc.Close()
  57. cli := hthv1.NewObserveServiceClient(oc)
  58. deadline := time.Now().Add(d)
  59. var last hthv1.DeviceLifecycleState
  60. var epoch uint64
  61. var snapErr error
  62. for time.Now().Before(deadline) {
  63. s, err := cli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  64. snapErr = err
  65. if err == nil && s != nil && s.Snapshot != nil {
  66. last = s.Snapshot.Lifecycle
  67. epoch = s.Snapshot.DeviceEpoch
  68. if last == hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_READY {
  69. return s.Snapshot
  70. }
  71. }
  72. time.Sleep(30 * time.Millisecond)
  73. }
  74. t.Fatalf("not ready (lifecycle=%v epoch=%d snap_err=%v)", last, epoch, snapErr)
  75. return nil
  76. }
  77. func buildHthd(t *testing.T) string {
  78. t.Helper()
  79. return buildHostCmd(t, "hthd", "./cmd/hthd")
  80. }
  81. func buildHostCmd(t *testing.T, name, pkg string) string {
  82. t.Helper()
  83. bin := filepath.Join(t.TempDir(), name)
  84. cmd := exec.Command("go", "build", "-o", bin, pkg)
  85. cmd.Dir = testutil.HostRoot(t)
  86. cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
  87. out, err := cmd.CombinedOutput()
  88. if err != nil {
  89. t.Fatalf("build %s: %v %s", name, err, out)
  90. }
  91. return bin
  92. }