| 1 | package state |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestReadWriteRoundTrip(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | got, err := Read(dir) |
| 16 | if err != nil || got != "" { |
| 17 | t.Fatalf("leeg: %q %v", got, err) |
| 18 | } |
| 19 | if err := Write(dir, "/data/bluegit"); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | got, err = Read(dir) |
| 23 | if err != nil || got != "/data/bluegit" { |
| 24 | t.Fatalf("teruggelezen %q %v", got, err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestInspect(t *testing.T) { |
| 29 | if _, err := Inspect("relatief/pad"); err == nil { |
| 30 | t.Fatal("relatief pad hoort geweigerd") |
| 31 | } |
| 32 | if _, err := Inspect("/dit/pad/bestaat/niet/bluegit"); err == nil || !strings.Contains(err.Error(), "does not exist") { |
| 33 | t.Fatalf("ontbrekend pad: %v", err) |
| 34 | } |
| 35 | file := filepath.Join(t.TempDir(), "bestand") |
| 36 | if err := os.WriteFile(file, []byte("x"), 0o644); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if _, err := Inspect(file); err == nil || !strings.Contains(err.Error(), "not a directory") { |
| 40 | t.Fatalf("bestand: %v", err) |
| 41 | } |
| 42 | dir := t.TempDir() |
| 43 | info, err := Inspect(dir) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if info.Free < MinFreeBytes || info.Total < info.Free { |
| 48 | t.Fatalf("ruimte free=%d total=%d", info.Free, info.Total) |
| 49 | } |
| 50 | if !strings.Contains(Format(info.Free), "iB") { |
| 51 | t.Fatal(Format(info.Free)) |
| 52 | } |
| 53 | if _, err := os.Stat(filepath.Join(dir, ".bluegit-write-test")); !os.IsNotExist(err) { |
| 54 | t.Fatal("schrijftest bleef achter") |
| 55 | } |
| 56 | if info.Used()+info.Free+info.Reserved != info.Total { |
| 57 | t.Fatalf("bezet %d + vrij %d != totaal %d", info.Used(), info.Free, info.Total) |
| 58 | } |
| 59 | if Percent(info.Used(), info.Total) < 0 || Percent(info.Used(), info.Total) > 100 { |
| 60 | t.Fatal(Percent(info.Used(), info.Total)) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestPercentAndUsed(t *testing.T) { |
| 65 | if Percent(0, 0) != 0 || Percent(1, 2) != 50 || Percent(2, 2) != 100 || Percent(1, 3) != 33 || Percent(5, 2) != 100 { |
| 66 | t.Fatalf("0=%d half=%d vol=%d derde=%d over=%d", Percent(0, 0), Percent(1, 2), Percent(2, 2), Percent(1, 3), Percent(5, 2)) |
| 67 | } |
| 68 | if (Info{Free: 30, Total: 10}).Used() != 0 || (Info{Free: 4, Total: 10}).Used() != 6 || (Info{Free: 4, Reserved: 1, Total: 10}).Used() != 5 || (Info{Free: 8, Reserved: 5, Total: 10}).Used() != 0 { |
| 69 | t.Fatal("used") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestDirSizeCountsTreeAndStops(t *testing.T) { |
| 74 | root := t.TempDir() |
| 75 | if err := os.WriteFile(filepath.Join(root, "a"), bytes.Repeat([]byte("x"), 1000), 0o644); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | sub := filepath.Join(root, "sub") |
| 79 | if err := os.Mkdir(sub, 0o755); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if err := os.WriteFile(filepath.Join(sub, "b"), bytes.Repeat([]byte("y"), 500), 0o644); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | outside := filepath.Join(t.TempDir(), "groot") |
| 86 | if err := os.WriteFile(outside, bytes.Repeat([]byte("z"), 2<<20), 0o644); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | n, err := DirSize(context.Background(), root) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if n < 1500 || n >= 2<<20 { |
| 97 | t.Fatalf("grootte %d volgt de symlink of mist bestanden", n) |
| 98 | } |
| 99 | again, err := DirSize(context.Background(), root) |
| 100 | if err != nil || again != n { |
| 101 | t.Fatalf("opnieuw %d %v", again, err) |
| 102 | } |
| 103 | ctx, cancel := context.WithCancel(context.Background()) |
| 104 | cancel() |
| 105 | stopped, err := DirSize(ctx, root) |
| 106 | if !errors.Is(err, context.Canceled) || stopped != 0 { |
| 107 | t.Fatalf("stop: %d %v", stopped, err) |
| 108 | } |
| 109 | } |