capture_replay_linux_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //go:build linux
  2. package tests
  3. import (
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "hth/internal/app"
  10. "hth/internal/capture"
  11. hthv1 "hth/internal/gen/hth/api/v1"
  12. "hth/internal/grpcserver"
  13. "hth/internal/testutil"
  14. )
  15. func TestCaptureReplayCapabilityHashRoundTrip(t *testing.T) {
  16. a, obs, _ := startMockApp(t)
  17. out := t.TempDir()
  18. conn, err := grpcserver.DialUnix(obs, 2*time.Second)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer conn.Close()
  23. cli := hthv1.NewObserveServiceClient(conn)
  24. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  25. defer cancel()
  26. caps, err := cli.GetCapabilities(ctx, &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"})
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. wantHash := caps.Capabilities.GetCapabilityHash()
  31. if wantHash == "" {
  32. t.Fatal("empty capability hash")
  33. }
  34. man := capture.Manifest{
  35. APIVersion: app.APIVersion, APISchemaVersion: app.APIVersion,
  36. ProfileHash: caps.Capabilities.GetProtocolContractHash(),
  37. ProfileContractHash: caps.Capabilities.GetProtocolContractHash(),
  38. ProfileID: caps.Capabilities.GetProfileId(), ProfileRevision: caps.Capabilities.GetProfileRevision(),
  39. CapabilityHash: wantHash, DeviceID: "prod2u-0",
  40. StartedAt: time.Now().UTC(), Status: capture.StatusIncomplete,
  41. }
  42. if err := capture.WriteManifest(out, man); err != nil {
  43. t.Fatal(err)
  44. }
  45. f, err := os.Create(capture.RecordsPath(out))
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. ev, err := cli.SubscribeEvents(ctx, &hthv1.SubscribeEventsRequest{DeviceId: "prod2u-0"})
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. for i := 0; i < 5; i++ {
  54. e, err := ev.Recv()
  55. if err != nil {
  56. break
  57. }
  58. rec, err := capture.EventRecord(e)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if err := capture.AppendRecord(f, rec); err != nil {
  63. t.Fatal(err)
  64. }
  65. }
  66. f.Close()
  67. man.Status = capture.StatusComplete
  68. if err := capture.Seal(out, man); err != nil {
  69. t.Fatal(err)
  70. }
  71. _ = a
  72. dir := t.TempDir()
  73. robs := filepath.Join(dir, "o.sock")
  74. rctl := filepath.Join(dir, "c.sock")
  75. yaml := `
  76. api_version: hth.api.v1
  77. device_id: prod2u-0
  78. backend: replay
  79. profile: ` + testutil.ProfilePath(t) + `
  80. runtime_root: ` + dir + `
  81. replay:
  82. path: ` + out + `
  83. speed: 1000
  84. step: true
  85. sockets:
  86. observe: ` + robs + `
  87. control: ` + rctl + `
  88. poll:
  89. temperature_ms: 50
  90. motor_status_ms: 40
  91. watchdog:
  92. heartbeat_period_ms: 5000
  93. lease:
  94. min_ttl_ms: 50
  95. max_ttl_ms: 2000
  96. safety:
  97. fan: 10
  98. ptc: 0
  99. door_close_percent: 0
  100. ring_buffer_size: 32
  101. subscriber_buffer_size: 4
  102. grpc:
  103. reflection: false
  104. `
  105. cfgPath := filepath.Join(dir, "hthd.yaml")
  106. if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
  107. t.Fatal(err)
  108. }
  109. ra, err := app.New(cfgPath, "replay", nil)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. rctx, rcancel := context.WithCancel(context.Background())
  114. errCh := make(chan error, 1)
  115. go func() { errCh <- ra.Run(rctx) }()
  116. t.Cleanup(func() { rcancel(); ra.Stop(); <-errCh })
  117. waitUnix(t, robs, 3*time.Second)
  118. rc, err := grpcserver.DialUnix(robs, 2*time.Second)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. defer rc.Close()
  123. rcli := hthv1.NewObserveServiceClient(rc)
  124. got, err := rcli.GetCapabilities(context.Background(), &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"})
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. if got.Capabilities.GetCapabilityHash() != wantHash {
  129. t.Fatalf("capability hash mock %s replay %s", wantHash, got.Capabilities.GetCapabilityHash())
  130. }
  131. if _, err := rcli.StepReplay(context.Background(), &hthv1.StepReplayRequest{DeviceId: "prod2u-0"}); err == nil {
  132. t.Fatal("observe StepReplay must be denied by default")
  133. }
  134. waitUnix(t, rctl, 3*time.Second)
  135. ctlConn, err := grpcserver.DialUnix(rctl, 2*time.Second)
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. defer ctlConn.Close()
  140. ccli := hthv1.NewControlServiceClient(ctlConn)
  141. step, err := ccli.StepReplay(context.Background(), &hthv1.StepReplayRequest{DeviceId: "prod2u-0"})
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. if !step.Advanced && step.Message == "" {
  146. t.Fatal("step")
  147. }
  148. snap, err := rcli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. if snap.Snapshot == nil {
  153. t.Fatal("snapshot")
  154. }
  155. if ev := step.GetEvent(); ev != nil {
  156. if ev.GetEventId() == 0 {
  157. t.Fatal("replay event_id must be assigned by the current daemon")
  158. }
  159. if ev.GetSourceEventId() != 0 && ev.GetSourceEventId() == ev.GetEventId() && ev.GetSourceCaptureId() == "" {
  160. t.Fatal("source provenance must remain distinct from the live replay event_id")
  161. }
  162. }
  163. evCtx, evCancel := context.WithTimeout(context.Background(), 2*time.Second)
  164. defer evCancel()
  165. evStream, err := rcli.SubscribeEvents(evCtx, &hthv1.SubscribeEventsRequest{DeviceId: "prod2u-0"})
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. var lastID uint64
  170. for i := 0; i < 3; i++ {
  171. ev, err := evStream.Recv()
  172. if err != nil {
  173. break
  174. }
  175. if ev.GetEventId() == 0 {
  176. t.Fatal("replay event_id must be assigned by the current daemon")
  177. }
  178. if lastID != 0 && ev.GetEventId() <= lastID {
  179. t.Fatalf("replay event ids must be strictly increasing: %d then %d", lastID, ev.GetEventId())
  180. }
  181. lastID = ev.GetEventId()
  182. if ev.GetSourceEventId() != 0 && ev.GetSourceEventId() == ev.GetEventId() && ev.GetSourceCaptureId() == "" {
  183. t.Fatal("source provenance must remain distinct from the live replay event_id")
  184. }
  185. }
  186. }
  187. func TestWatchdogStopsWhenActorFrozen(t *testing.T) {
  188. a, _, _ := startMockApp(t)
  189. if !a.WatchdogShouldPet() {
  190. t.Fatal("healthy mock should pet")
  191. }
  192. a.Dev.TestingSetProgress(time.Time{})
  193. if a.WatchdogShouldPet() {
  194. t.Fatal("frozen actor must stop watchdog pet")
  195. }
  196. }
  197. func TestWatchdogKeepsPettingDuringBoundedScan(t *testing.T) {
  198. a, _, _ := startMockApp(t)
  199. if !a.WatchdogShouldPet() {
  200. t.Fatal("healthy mock should pet")
  201. }
  202. a.Dev.TestingSetInflightSince(5 * time.Second)
  203. if !a.WatchdogShouldPet() {
  204. t.Fatal("5s temperature scan must not stop systemd watchdog pet")
  205. }
  206. }
  207. func TestWatchdogStopsWhenSerialWorkerExceeded(t *testing.T) {
  208. a, _, _ := startMockApp(t)
  209. a.Dev.TestingSetInflightSince(a.Dev.MaxSerialTxn() + time.Second)
  210. if a.WatchdogShouldPet() {
  211. t.Fatal("serial worker beyond MaxSerialTxn must stop watchdog pet")
  212. }
  213. }