pty_fault_linux_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //go:build linux
  2. package tests
  3. import (
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "google.golang.org/protobuf/types/known/durationpb"
  10. "google.golang.org/protobuf/types/known/timestamppb"
  11. "hth/internal/app"
  12. "hth/internal/devsim"
  13. hthv1 "hth/internal/gen/hth/api/v1"
  14. "hth/internal/grpcserver"
  15. "hth/internal/ids"
  16. "hth/internal/serialtransport"
  17. "hth/internal/testutil"
  18. )
  19. func startSerialApp(t *testing.T, sim *devsim.Sim) (obs, ctl string) {
  20. t.Helper()
  21. serialtransport.SetTestWriteCap(0)
  22. serialtransport.SetTestForceWriteError(false)
  23. dir := t.TempDir()
  24. obs = filepath.Join(dir, "o.sock")
  25. ctl = filepath.Join(dir, "c.sock")
  26. yaml := `
  27. api_version: hth.api.v1
  28. device_id: prod2u-0
  29. backend: serial
  30. profile: ` + testutil.ProfilePath(t) + `
  31. runtime_root: ` + dir + `
  32. serial:
  33. path: ` + sim.Name + `
  34. sockets:
  35. observe: ` + obs + `
  36. control: ` + ctl + `
  37. poll:
  38. temperature_ms: 60000
  39. motor_status_ms: 60000
  40. watchdog:
  41. heartbeat_period_ms: 5000
  42. lease:
  43. min_ttl_ms: 50
  44. max_ttl_ms: 2000
  45. safety:
  46. fan: 10
  47. ptc: 0
  48. door_close_percent: 0
  49. ring_buffer_size: 32
  50. subscriber_buffer_size: 4
  51. grpc:
  52. reflection: false
  53. `
  54. cfgPath := filepath.Join(dir, "hthd.yaml")
  55. if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
  56. t.Fatal(err)
  57. }
  58. a, err := app.New(cfgPath, "serial", nil)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. ctx, cancel := context.WithCancel(context.Background())
  63. errCh := make(chan error, 1)
  64. go func() { errCh <- a.Run(ctx) }()
  65. t.Cleanup(func() {
  66. serialtransport.SetTestWriteCap(0)
  67. serialtransport.SetTestForceWriteError(false)
  68. cancel()
  69. a.Stop()
  70. <-errCh
  71. })
  72. waitUnix(t, obs, 4*time.Second)
  73. return obs, ctl
  74. }
  75. func waitReadySnap(t *testing.T, obs string) *hthv1.Snapshot {
  76. t.Helper()
  77. _ = waitUntilReady(t, obs, 20*time.Second)
  78. waitSlewBudget()
  79. return waitUntilReady(t, obs, 20*time.Second)
  80. }
  81. func applyDoors(t *testing.T, obs, ctl, cmd string, door float64) *hthv1.ActionResult {
  82. t.Helper()
  83. oc, err := grpcserver.DialUnix(obs, time.Second)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. defer oc.Close()
  88. cc, err := grpcserver.DialUnix(ctl, time.Second)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. defer cc.Close()
  93. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  94. defer cancel()
  95. obsCli := hthv1.NewObserveServiceClient(oc)
  96. ctrl := hthv1.NewControlServiceClient(cc)
  97. lease, err := ctrl.AcquireControlLease(ctx, &hthv1.AcquireControlLeaseRequest{
  98. DeviceId: "prod2u-0", Holder: "pty-fault", Ttl: durationpb.New(20 * time.Second),
  99. })
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. snap, err := obsCli.GetSnapshot(ctx, &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. res, err := ctrl.ApplyAction(ctx, &hthv1.ApplyActionRequest{Batch: &hthv1.ActionBatch{
  108. DeviceId: "prod2u-0", CommandId: cmd, LeaseId: lease.Lease.LeaseId, LeaseEpoch: lease.Lease.LeaseEpoch,
  109. BasedOnSnapshotSeq: snap.Snapshot.SnapshotSeq, ExpectedControlRevision: snap.Snapshot.ControlRevision, AllowClamp: true,
  110. ValidUntil: timestamppb.New(time.Now().Add(20 * time.Second)),
  111. ContinuousTargets: []*hthv1.ContinuousTarget{
  112. {Id: ids.DoorX, Value: door}, {Id: ids.DoorY, Value: door}, {Id: ids.DoorZ, Value: door},
  113. },
  114. }})
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. return res
  119. }
  120. func TestPTYShortWriteOneByteReconnects(t *testing.T) {
  121. sim, err := devsim.Start(devsim.Options{StartupDone: true})
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. defer sim.Close()
  126. obs, ctl := startSerialApp(t, sim)
  127. waitReadySnap(t, obs)
  128. serialtransport.SetTestWriteCap(1)
  129. res := applyDoors(t, obs, ctl, "one-byte", 55)
  130. if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
  131. t.Fatalf("short write: %+v", res)
  132. }
  133. serialtransport.SetTestWriteCap(0)
  134. waitReadySnap(t, obs)
  135. res2 := applyDoors(t, obs, ctl, "after-one-byte", 10)
  136. if !res2.Accepted && res2.Confirmation == hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
  137. t.Fatalf("reconnect apply failed: %+v", res2)
  138. }
  139. }
  140. func TestPTYHalfFrameReconnects(t *testing.T) {
  141. sim, err := devsim.Start(devsim.Options{StartupDone: true})
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. defer sim.Close()
  146. obs, ctl := startSerialApp(t, sim)
  147. waitReadySnap(t, obs)
  148. serialtransport.SetTestWriteCap(8)
  149. res := applyDoors(t, obs, ctl, "half", 60)
  150. if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
  151. t.Fatalf("half frame: %+v", res)
  152. }
  153. serialtransport.SetTestWriteCap(0)
  154. }
  155. func TestPTYFullWriteErrorReconnects(t *testing.T) {
  156. sim, err := devsim.Start(devsim.Options{StartupDone: true})
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. defer sim.Close()
  161. obs, ctl := startSerialApp(t, sim)
  162. waitReadySnap(t, obs)
  163. serialtransport.SetTestForceWriteError(true)
  164. res := applyDoors(t, obs, ctl, "full-err", 65)
  165. if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
  166. t.Fatalf("full write+err: %+v", res)
  167. }
  168. }
  169. func TestPTYDisconnectInvalidatesOldLease(t *testing.T) {
  170. sim, err := devsim.Start(devsim.Options{StartupDone: true})
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. defer sim.Close()
  175. obs, ctl := startSerialApp(t, sim)
  176. waitReadySnap(t, obs)
  177. oc, err := grpcserver.DialUnix(obs, time.Second)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. defer oc.Close()
  182. cc, err := grpcserver.DialUnix(ctl, time.Second)
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. defer cc.Close()
  187. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  188. defer cancel()
  189. ctrl := hthv1.NewControlServiceClient(cc)
  190. obsCli := hthv1.NewObserveServiceClient(oc)
  191. lease, err := ctrl.AcquireControlLease(ctx, &hthv1.AcquireControlLeaseRequest{
  192. DeviceId: "prod2u-0", Holder: "disc", Ttl: durationpb.New(5 * time.Second),
  193. })
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. e0 := waitReadySnap(t, obs).DeviceEpoch
  198. sim.Disconnect()
  199. deadline := time.Now().Add(8 * time.Second)
  200. bumped := false
  201. for time.Now().Before(deadline) {
  202. s, err := obsCli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  203. if err == nil && s.Snapshot.DeviceEpoch > e0 {
  204. bumped = true
  205. break
  206. }
  207. time.Sleep(30 * time.Millisecond)
  208. }
  209. if !bumped {
  210. t.Fatal("epoch did not bump after PTY disconnect")
  211. }
  212. actx, acancel := context.WithTimeout(context.Background(), 3*time.Second)
  213. defer acancel()
  214. res, err := ctrl.ApplyAction(actx, &hthv1.ApplyActionRequest{Batch: &hthv1.ActionBatch{
  215. DeviceId: "prod2u-0", CommandId: "after-disc", LeaseId: lease.Lease.LeaseId, LeaseEpoch: lease.Lease.LeaseEpoch,
  216. ValidUntil: timestamppb.New(time.Now().Add(20 * time.Second)),
  217. ContinuousTargets: []*hthv1.ContinuousTarget{{Id: ids.Fan1, Value: 10}},
  218. }})
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. if res.Accepted {
  223. t.Fatalf("old lease must not work after disconnect: %+v", res)
  224. }
  225. }