| new file mode 100644 |
| index 000000000..67a729d21 |
| --- /dev/null |
| +++ b/internal/termfence/confirm_test.go |
| @@ -0,0 +1,213 @@ |
| +package termfence |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| + "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| + |
| + "github.com/carlosframework/platform/internal/boxid" |
| +) |
| + |
| +var confirmSelf = boxid.Cloud{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-a"} |
| + |
| +type confirmRig struct { |
| + f *fakeEC2 |
| + c *Confirmer |
| + mu sync.Mutex |
| + logs []string |
| + at time.Time |
| + apis int |
| +} |
| + |
| +func newConfirmRig(f *fakeEC2) *confirmRig { |
| + r := &confirmRig{f: f, at: time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)} |
| + r.c = &Confirmer{ |
| + API: func(context.Context, string) (EC2, error) { r.apis++; return r.f, nil }, |
| + Self: func() boxid.Cloud { return confirmSelf }, |
| + Now: func() time.Time { return r.at }, |
| + Logf: func(format string, a ...any) { |
| + r.mu.Lock() |
| + defer r.mu.Unlock() |
| + r.logs = append(r.logs, strings.TrimSpace(fmt.Sprintf(format, a...))) |
| + }, |
| + } |
| + return r |
| +} |
| + |
| +func (r *confirmRig) count(prefix string) int { |
| + n := 0 |
| + for _, l := range r.logs { |
| + if strings.HasPrefix(l, prefix) { |
| + n++ |
| + } |
| + } |
| + return n |
| +} |
| + |
| +func TestConfirmTerminatedOwner(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: withRequestID(describe("111122223333", "i-b", types.InstanceStateNameTerminated), "req-1")}) |
| + ok, err := r.c.Terminated(context.Background(), "i-b") |
| + if err != nil || !ok { |
| + t.Fatalf("got %v, %v", ok, err) |
| + } |
| + if len(r.f.regions) != 1 || r.f.regions[0] != "eu-west-1" || len(r.f.ids[0]) != 1 || r.f.ids[0][0] != "i-b" { |
| + t.Fatalf("called in %v for %v", r.f.regions, r.f.ids) |
| + } |
| + if r.count("termfence: describe i-b: terminated") != 1 { |
| + t.Fatalf("logs %q", r.logs) |
| + } |
| + if r.count("termfence: i-b confirmed terminated (first evidence, request req-1)") != 1 || len(r.logs) != 2 { |
| + t.Fatalf("logs %q", r.logs) |
| + } |
| +} |
| + |
| +func TestConfirmStatesOtherThanTerminated(t *testing.T) { |
| + for _, st := range []types.InstanceStateName{ |
| + types.InstanceStateNameRunning, types.InstanceStateNameStopped, types.InstanceStateNameStopping, |
| + types.InstanceStateNameShuttingDown, types.InstanceStateNamePending, |
| + } { |
| + r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-b", st)}) |
| + ok, err := r.c.Terminated(context.Background(), "i-b") |
| + if ok || err != nil { |
| + t.Fatalf("%s: got %v, %v", st, ok, err) |
| + } |
| + if r.count("termfence: describe i-b: "+string(st)) != 1 || r.count("termfence: i-b confirmed") != 0 { |
| + t.Fatalf("%s: logs %q", st, r.logs) |
| + } |
| + } |
| + r := newConfirmRig(&fakeEC2{out: &ec2.DescribeInstancesOutput{}}) |
| + ok, err := r.c.Terminated(context.Background(), "i-b") |
| + if ok || err != nil { |
| + t.Fatalf("empty reply: got %v, %v", ok, err) |
| + } |
| + if r.count("termfence: describe i-b: not found") != 1 { |
| + t.Fatalf("logs %q", r.logs) |
| + } |
| +} |
| + |
| +func TestConfirmRefusesOtherAccountOrRegion(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: describe("999999999999", "i-b", types.InstanceStateNameTerminated)}) |
| + ok, err := r.c.Terminated(context.Background(), "i-b") |
| + if ok || err != nil { |
| + t.Fatalf("other account: got %v, %v", ok, err) |
| + } |
| + if len(r.f.regions) != 1 || r.f.regions[0] != "eu-west-1" { |
| + t.Fatalf("called in %v", r.f.regions) |
| + } |
| + if r.count("termfence: i-b confirmed") != 0 { |
| + t.Fatalf("logs %q", r.logs) |
| + } |
| + // The identity check itself, without a describe. |
| + good := FromDescribe(describe("111122223333", "i-b", types.InstanceStateNameTerminated), "i-b", "eu-west-1") |
| + if err := SameAccountTerminated(confirmSelf, "i-b", good); err != nil { |
| + t.Fatal(err) |
| + } |
| + bad := map[string]Observation{} |
| + o := good |
| + o.Region = "us-east-1" |
| + bad["region"] = o |
| + o = good |
| + o.Via = ViaEvent |
| + bad["event"] = o |
| + o = good |
| + o.InstanceID = "i-c" |
| + bad["instance"] = o |
| + o = good |
| + o.Found = false |
| + bad["notfound"] = o |
| + for name, o := range bad { |
| + if err := SameAccountTerminated(confirmSelf, "i-b", o); !errors.Is(err, ErrNotEvidence) { |
| + t.Fatalf("%s: %v", name, err) |
| + } |
| + } |
| + if err := SameAccountTerminated(confirmSelf, "i-a", good); !errors.Is(err, ErrNotEvidence) { |
| + t.Fatalf("self: %v", err) |
| + } |
| +} |
| + |
| +func TestConfirmRefusesSelfAndIncompleteIdentity(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-a", types.InstanceStateNameTerminated)}) |
| + ok, err := r.c.Terminated(context.Background(), "i-a") |
| + if ok || err != nil || len(r.f.regions) != 0 { |
| + t.Fatalf("self: got %v, %v, %d describes", ok, err, len(r.f.regions)) |
| + } |
| + r.c.Self = func() boxid.Cloud { c := confirmSelf; c.Account = ""; return c } |
| + ok, err = r.c.Terminated(context.Background(), "i-b") |
| + if ok || err == nil || len(r.f.regions) != 0 { |
| + t.Fatalf("incomplete: got %v, %v, %d describes", ok, err, len(r.f.regions)) |
| + } |
| +} |
| + |
| +func TestConfirmCachesPositiveForADay(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated)}) |
| + t0 := r.at |
| + for _, at := range []time.Duration{0, 23 * time.Hour} { |
| + r.at = t0.Add(at) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil { |
| + t.Fatalf("at %v: %v, %v", at, ok, err) |
| + } |
| + } |
| + if len(r.f.regions) != 1 || r.count("termfence: i-b confirmed") != 1 { |
| + t.Fatalf("describes %d, logs %q", len(r.f.regions), r.logs) |
| + } |
| + r.at = t0.Add(24*time.Hour + time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil { |
| + t.Fatalf("after a day: %v, %v", ok, err) |
| + } |
| + if len(r.f.regions) != 2 || r.count("termfence: i-b confirmed") != 1 || r.count("termfence: describe i-b") != 2 { |
| + t.Fatalf("describes %d, logs %q", len(r.f.regions), r.logs) |
| + } |
| + if r.apis != 1 { |
| + t.Fatalf("API built %d times", r.apis) |
| + } |
| +} |
| + |
| +func TestConfirmErrorIsCachedBriefly(t *testing.T) { |
| + boom := errors.New("throttled") |
| + r := newConfirmRig(&fakeEC2{err: boom}) |
| + t0 := r.at |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || !errors.Is(err, boom) { |
| + t.Fatalf("got %v, %v", ok, err) |
| + } |
| + if r.count("termfence: describe i-b: throttled") != 1 { |
| + t.Fatalf("logs %q", r.logs) |
| + } |
| + r.at = t0.Add(5 * time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || !errors.Is(err, boom) { |
| + t.Fatalf("cached: got %v, %v", ok, err) |
| + } |
| + if len(r.f.regions) != 1 { |
| + t.Fatalf("describes %d", len(r.f.regions)) |
| + } |
| + r.at = t0.Add(11 * time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || !errors.Is(err, boom) { |
| + t.Fatalf("after ttl: got %v, %v", ok, err) |
| + } |
| + if len(r.f.regions) != 2 { |
| + t.Fatalf("describes %d", len(r.f.regions)) |
| + } |
| +} |
| + |
| +func TestConfirmNegativeCachedTenSeconds(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-b", types.InstanceStateNameRunning)}) |
| + t0 := r.at |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || err != nil { |
| + t.Fatalf("got %v, %v", ok, err) |
| + } |
| + r.at = t0.Add(9 * time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || err != nil || len(r.f.regions) != 1 { |
| + t.Fatalf("cached: got %v, %v, %d describes", ok, err, len(r.f.regions)) |
| + } |
| + r.f.out = describe("111122223333", "i-b", types.InstanceStateNameTerminated) |
| + r.at = t0.Add(11 * time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil || len(r.f.regions) != 2 { |
| + t.Fatalf("after ttl: got %v, %v, %d describes", ok, err, len(r.f.regions)) |
| + } |
| +} |