| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package main
- import (
- "flag"
- "strings"
- "testing"
- "time"
- )
- func TestParseHthctlArgsFileAfterCommand(t *testing.T) {
- fs := flag.NewFlagSet("hthctl-test", flag.ContinueOnError)
- file := fs.String("file", "", "")
- timeout := fs.Duration("timeout", 0, "")
- cmd := parseHthctlArgs(fs, []string{"--timeout", "30s", "resume-from-safe", "--file", "/tmp/resume.json"})
- if cmd != "resume-from-safe" {
- t.Fatalf("cmd=%q", cmd)
- }
- if *file != "/tmp/resume.json" {
- t.Fatalf("file after subcommand not parsed: %q", *file)
- }
- if *timeout != 30*time.Second {
- t.Fatalf("timeout=%v", *timeout)
- }
- }
- func TestRequireActionFile(t *testing.T) {
- for _, cmd := range []string{"validate-action", "apply-action", "resume-from-safe"} {
- if err := requireActionFile(cmd, ""); err == nil {
- t.Fatalf("%s without --file must fail", cmd)
- }
- if err := requireActionFile(cmd, "batch.json"); err != nil {
- t.Fatal(err)
- }
- }
- if err := requireActionFile("snapshot", ""); err != nil {
- t.Fatal(err)
- }
- }
- func TestObserveCommandsDoNotRequireHardwareAck(t *testing.T) {
- for _, cmd := range []string{"devices", "capabilities", "snapshot", "watch", "events", "faults", "health", "doctor", "validate-action"} {
- if commandMutatesDevice(cmd) {
- t.Fatalf("%s must not require hardware confirmation", cmd)
- }
- }
- for _, cmd := range []string{"apply-action", "enter-safe-mode", "resume-from-safe", "step-replay"} {
- if !commandMutatesDevice(cmd) {
- t.Fatalf("%s must require hardware confirmation on serial backends", cmd)
- }
- }
- }
- func TestSerialBackendAtTmpStillRequiresAck(t *testing.T) {
- observe := "/tmp/hth-real/observe.sock"
- if !backendRequiresHardwareAck("serial") {
- t.Fatal("serial backend must require hardware ack regardless of socket path")
- }
- if backendRequiresHardwareAck("mock") {
- t.Fatal("mock does not require hardware ack")
- }
- if err := refuseIfSerialNeedsAck("serial", observe, false); err == nil {
- t.Fatal("serial on /tmp without --i-understand-this-is-real-hardware must fail")
- } else if !strings.Contains(err.Error(), "serial") {
- t.Fatalf("error should mention serial: %v", err)
- }
- if err := refuseIfSerialNeedsAck("serial", observe, true); err != nil {
- t.Fatal(err)
- }
- if err := refuseIfSerialNeedsAck("mock", "/run/hth/observe.sock", false); err != nil {
- t.Fatal(err)
- }
- if strings.Contains(strings.ToLower(observe), "mock") {
- t.Fatal("test fixture must not rely on mock substring")
- }
- }
- func TestTimingDeadlineFailClosedWithoutCaps(t *testing.T) {
- _, err := timingDeadlineMS("/tmp/hthctl-no-such-observe.sock", "prod2u-0")
- if err == nil {
- t.Fatal("missing capabilities must fail closed")
- }
- }
- func TestControlCommandsUseCapabilitiesNotFixed5s(t *testing.T) {
- if commandMutatesDevice("snapshot") || commandMutatesDevice("health") || commandMutatesDevice("watch") {
- t.Fatal("observe commands must not be classified as mutating")
- }
- if !commandMutatesDevice("apply-action") {
- t.Fatal("apply-action is mutating and must take TimingCapabilities deadline")
- }
- }
|