Files
felhom.eu/hub/internal/store/small_disk_test.go
T

72 lines
2.7 KiB
Go

package store
import (
"testing"
"time"
)
// The stored effect: a box told as small is read back with its size and the hub's time; another box — also one whose
// id only starts with this id — is not; a size that is not below the line is kept as „not told”.
func TestSmallDisk_NotedPerBox_ReadBack_LookAlikeApart(t *testing.T) {
s := newTestStore(t)
now := time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC)
if err := s.NoteSmallDisk("demo-hp-bb76ea", 128035676160, now); err != nil {
t.Fatal(err)
}
got := s.SmallDiskOf("demo-hp-bb76ea")
if got == nil || got.Bytes != 128035676160 || !got.NotedAt.Equal(now) {
t.Fatalf("read back %+v, want 128035676160 bytes noted at %s", got, now)
}
for _, other := range []string{"demo-hp-bb76e", "demo-hp-bb76ea2", "demo-hp", "tester-1-d70be4", ""} {
if g := s.SmallDiskOf(other); g != nil {
t.Fatalf("%q reads the mark of another box: %+v", other, g)
}
}
// told again with no size: the newest wins, and ONE mark stays
if err := s.NoteSmallDisk("demo-hp-bb76ea", 0, now.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if got := s.SmallDiskOf("demo-hp-bb76ea"); got == nil || got.Bytes != 0 || !got.NotedAt.Equal(now.Add(time.Hour)) {
t.Fatalf("told again: %+v", got)
}
if n := len(s.OSAlarmKeys("small_disk:")); n != 1 {
t.Fatalf("%d marks for one box, want 1", n)
}
// a size that contradicts the flag, or is not a size, is kept as „not told”
for _, bad := range []int64{200 * 1000 * 1000 * 1000, 5e15, -1} {
_ = s.NoteSmallDisk("b-1", bad, now)
if got := s.SmallDiskOf("b-1"); got == nil || got.Bytes != 0 {
t.Fatalf("size %d was kept: %+v", bad, got)
}
}
// an id with a colon cannot be a box's id and makes no mark
_ = s.NoteSmallDisk("x:y", 1, now)
if n := len(s.OSAlarmKeys("small_disk:x")); n != 0 {
t.Fatalf("an id with a colon made %d mark(s)", n)
}
}
// A deleted box leaves no small-disk mark (D73: nothing that names a box outlives it) — and the mark of a box whose id
// only starts with the deleted one's stays.
func TestSmallDisk_GoesWithTheBox(t *testing.T) {
s := newTestStore(t)
now := time.Now().UTC().Truncate(time.Second)
for _, h := range []string{"cust-a-111111", "cust-a-1111112"} {
if err := s.UpsertHost(&Host{HostID: h, CustomerID: "cust-a", APIKey: "k-" + h}); err != nil {
t.Fatal(err)
}
if err := s.NoteSmallDisk(h, 128000000000, now); err != nil {
t.Fatal(err)
}
}
if err := s.DeleteHost("cust-a-111111", false); err != nil {
t.Fatal(err)
}
if got := s.SmallDiskOf("cust-a-111111"); got != nil {
t.Fatalf("the deleted box's mark stayed: %+v", got)
}
if got := s.SmallDiskOf("cust-a-1111112"); got == nil {
t.Fatal("the mark of the OTHER box (its id starts with the deleted one's) went too")
}
}