main_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 TestSerialBackendAtTmpStillRequiresAck(t *testing.T) {
  37. observe := "/tmp/hth-real/observe.sock"
  38. if !backendRequiresHardwareAck("serial") {
  39. t.Fatal("serial backend must require hardware ack regardless of socket path")
  40. }
  41. if backendRequiresHardwareAck("mock") {
  42. t.Fatal("mock does not require hardware ack")
  43. }
  44. if err := refuseIfSerialNeedsAck("serial", observe, false); err == nil {
  45. t.Fatal("serial on /tmp without --i-understand-this-is-real-hardware must fail")
  46. } else if !strings.Contains(err.Error(), "serial") {
  47. t.Fatalf("error should mention serial: %v", err)
  48. }
  49. if err := refuseIfSerialNeedsAck("serial", observe, true); err != nil {
  50. t.Fatal(err)
  51. }
  52. if err := refuseIfSerialNeedsAck("mock", "/run/hth/observe.sock", false); err != nil {
  53. t.Fatal(err)
  54. }
  55. if strings.Contains(strings.ToLower(observe), "mock") {
  56. t.Fatal("test fixture must not rely on mock substring")
  57. }
  58. }