package tests import ( "context" "os" "os/exec" "path/filepath" "testing" "time" "hth/internal/app" hthv1 "hth/internal/gen/hth/api/v1" "hth/internal/grpcserver" "hth/internal/serialtransport" "hth/internal/testutil" ) func startAppFromConfig(t *testing.T, cfgPath string) *app.App { t.Helper() a, err := app.New(cfgPath, "", nil) if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { errCh <- a.Run(ctx) }() t.Cleanup(func() { cancel() a.Stop() <-errCh }) return a } func waitUnix(t *testing.T, path string, d time.Duration) { t.Helper() deadline := time.Now().Add(d) for time.Now().Before(deadline) { if _, err := os.Stat(path); err == nil { if c, err := grpcserver.DialUnix(path, 150*time.Millisecond); err == nil { c.Close() return } } time.Sleep(20 * time.Millisecond) } t.Fatalf("socket not ready: %s", path) } func waitSlewBudget() { time.Sleep(time.Second) } func waitUntilReady(t *testing.T, obs string, d time.Duration) *hthv1.Snapshot { t.Helper() serialtransport.SetTestWriteCap(0) serialtransport.SetTestForceWriteError(false) oc, err := grpcserver.DialUnix(obs, time.Second) if err != nil { t.Fatal(err) } defer oc.Close() cli := hthv1.NewObserveServiceClient(oc) deadline := time.Now().Add(d) var last hthv1.DeviceLifecycleState var epoch uint64 var snapErr error for time.Now().Before(deadline) { s, err := cli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"}) snapErr = err if err == nil && s != nil && s.Snapshot != nil { last = s.Snapshot.Lifecycle epoch = s.Snapshot.DeviceEpoch if last == hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_READY { return s.Snapshot } } time.Sleep(30 * time.Millisecond) } t.Fatalf("not ready (lifecycle=%v epoch=%d snap_err=%v)", last, epoch, snapErr) return nil } func buildHthd(t *testing.T) string { t.Helper() return buildHostCmd(t, "hthd", "./cmd/hthd") } func buildHostCmd(t *testing.T, name, pkg string) string { t.Helper() bin := filepath.Join(t.TempDir(), name) cmd := exec.Command("go", "build", "-o", bin, pkg) cmd.Dir = testutil.HostRoot(t) cmd.Env = append(os.Environ(), "CGO_ENABLED=0") out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("build %s: %v %s", name, err, out) } return bin }