main_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "flag"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. func TestParseHthctlArgsFileAfterCommand(t *testing.T) {
  9. fs := flag.NewFlagSet("hthctl-test", flag.ContinueOnError)
  10. file := fs.String("file", "", "")
  11. timeout := fs.Duration("timeout", 0, "")
  12. cmd := parseHthctlArgs(fs, []string{"--timeout", "30s", "resume-from-safe", "--file", "/tmp/resume.json"})
  13. if cmd != "resume-from-safe" {
  14. t.Fatalf("cmd=%q", cmd)
  15. }
  16. if *file != "/tmp/resume.json" {
  17. t.Fatalf("file after subcommand not parsed: %q", *file)
  18. }
  19. if *timeout != 30*time.Second {
  20. t.Fatalf("timeout=%v", *timeout)
  21. }
  22. }
  23. func TestRequireActionFile(t *testing.T) {
  24. for _, cmd := range []string{"validate-action", "apply-action", "resume-from-safe"} {
  25. if err := requireActionFile(cmd, ""); err == nil {
  26. t.Fatalf("%s without --file must fail", cmd)
  27. }
  28. if err := requireActionFile(cmd, "batch.json"); err != nil {
  29. t.Fatal(err)
  30. }
  31. }
  32. if err := requireActionFile("snapshot", ""); err != nil {
  33. t.Fatal(err)
  34. }
  35. }
  36. func TestObserveCommandsDoNotRequireHardwareAck(t *testing.T) {
  37. for _, cmd := range []string{"devices", "capabilities", "snapshot", "watch", "events", "faults", "health", "doctor", "validate-action"} {
  38. if commandMutatesDevice(cmd) {
  39. t.Fatalf("%s must not require hardware confirmation", cmd)
  40. }
  41. }
  42. for _, cmd := range []string{"apply-action", "enter-safe-mode", "resume-from-safe", "step-replay"} {
  43. if !commandMutatesDevice(cmd) {
  44. t.Fatalf("%s must require hardware confirmation on serial backends", cmd)
  45. }
  46. }
  47. }
  48. func TestSerialBackendAtTmpStillRequiresAck(t *testing.T) {
  49. observe := "/tmp/hth-real/observe.sock"
  50. if !backendRequiresHardwareAck("serial") {
  51. t.Fatal("serial backend must require hardware ack regardless of socket path")
  52. }
  53. if backendRequiresHardwareAck("mock") {
  54. t.Fatal("mock does not require hardware ack")
  55. }
  56. if err := refuseIfSerialNeedsAck("serial", observe, false); err == nil {
  57. t.Fatal("serial on /tmp without --i-understand-this-is-real-hardware must fail")
  58. } else if !strings.Contains(err.Error(), "serial") {
  59. t.Fatalf("error should mention serial: %v", err)
  60. }
  61. if err := refuseIfSerialNeedsAck("serial", observe, true); err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := refuseIfSerialNeedsAck("mock", "/run/hth/observe.sock", false); err != nil {
  65. t.Fatal(err)
  66. }
  67. if strings.Contains(strings.ToLower(observe), "mock") {
  68. t.Fatal("test fixture must not rely on mock substring")
  69. }
  70. }
  71. func TestTimingDeadlineFailClosedWithoutCaps(t *testing.T) {
  72. _, err := timingDeadlineMS("/tmp/hthctl-no-such-observe.sock", "prod2u-0")
  73. if err == nil {
  74. t.Fatal("missing capabilities must fail closed")
  75. }
  76. }
  77. func TestControlCommandsUseCapabilitiesNotFixed5s(t *testing.T) {
  78. if commandMutatesDevice("snapshot") || commandMutatesDevice("health") || commandMutatesDevice("watch") {
  79. t.Fatal("observe commands must not be classified as mutating")
  80. }
  81. if !commandMutatesDevice("apply-action") {
  82. t.Fatal("apply-action is mutating and must take TimingCapabilities deadline")
  83. }
  84. }