| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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
- }
|