| 1 | package mirror |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | |
| 7 | "gitlab.com/Timfa/bluegit/internal/backupserver/protocol" |
| 8 | ) |
| 9 | |
| 10 | func TestComputeProgress(t *testing.T) { |
| 11 | if p := ComputeProgress(nil, nil); p.Known { |
| 12 | t.Fatal("zonder manifest is de voortgang onbekend") |
| 13 | } |
| 14 | empty := ComputeProgress(nil, []protocol.Entry{}) |
| 15 | if !empty.Known || empty.Percent != 100 || empty.TotalFiles != 0 { |
| 16 | t.Fatalf("lege dataset: %+v", empty) |
| 17 | } |
| 18 | |
| 19 | files := []protocol.Entry{ |
| 20 | {Path: "state/bluegit.db", Size: 50, SHA256: "aaa"}, |
| 21 | {Path: "data/repos/tim/app.git/pack", Size: 150, MTime: 2}, |
| 22 | {Path: "state/avatars/x", Size: 0, MTime: 3}, |
| 23 | } |
| 24 | idx := map[string]protocol.Entry{ |
| 25 | "state/bluegit.db": {Path: "state/bluegit.db", Size: 50, SHA256: "aaa"}, |
| 26 | "data/repos/tim/app.git/pack": {Path: "data/repos/tim/app.git/pack", Size: 150, MTime: 2}, |
| 27 | } |
| 28 | p := ComputeProgress(idx, files) |
| 29 | if p.DoneFiles != 2 || p.TotalFiles != 3 || p.DoneBytes != 200 || p.TotalBytes != 200 { |
| 30 | t.Fatalf("tellers %+v", p) |
| 31 | } |
| 32 | if p.Percent != 99 { |
| 33 | t.Fatalf("ontbrekend leeg bestand mag geen 100%% geven: %+v", p) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestComputeProgressZeroByteGap(t *testing.T) { |
| 38 | files := []protocol.Entry{ |
| 39 | {Path: "data/repos/a", Size: 100, MTime: 1}, |
| 40 | {Path: "state/avatars/b", Size: 0, MTime: 1}, |
| 41 | } |
| 42 | idx := map[string]protocol.Entry{"data/repos/a": {Path: "data/repos/a", Size: 100, MTime: 1}} |
| 43 | p := ComputeProgress(idx, files) |
| 44 | if p.Percent != 99 { |
| 45 | t.Fatalf("ontbrekend leeg bestand: %+v", p) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestComputeProgressPartialAndFull(t *testing.T) { |
| 50 | files := []protocol.Entry{ |
| 51 | {Path: "data/repos/a", Size: 75, MTime: 1}, |
| 52 | {Path: "data/repos/b", Size: 25, MTime: 1}, |
| 53 | } |
| 54 | p := ComputeProgress(map[string]protocol.Entry{ |
| 55 | "data/repos/a": {Path: "data/repos/a", Size: 75, MTime: 1}, |
| 56 | }, files) |
| 57 | if !p.Known || p.Percent != 75 || p.DoneFiles != 1 { |
| 58 | t.Fatalf("75%% verwacht: %+v", p) |
| 59 | } |
| 60 | full := ComputeProgress(map[string]protocol.Entry{ |
| 61 | "data/repos/a": {Path: "data/repos/a", Size: 75, MTime: 1}, |
| 62 | "data/repos/b": {Path: "data/repos/b", Size: 25, MTime: 1}, |
| 63 | }, files) |
| 64 | if full.Percent != 100 || full.DoneFiles != 2 { |
| 65 | t.Fatalf("100%% verwacht: %+v", full) |
| 66 | } |
| 67 | // Verouderd bestand telt niet. |
| 68 | stale := ComputeProgress(map[string]protocol.Entry{ |
| 69 | "data/repos/a": {Path: "data/repos/a", Size: 75, MTime: 1}, |
| 70 | "data/repos/b": {Path: "data/repos/b", Size: 25, MTime: 99}, |
| 71 | }, files) |
| 72 | if stale.Percent != 75 { |
| 73 | t.Fatalf("verouderd moet ontbreken: %+v", stale) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestManifestRoundTripAndCurrentProgress(t *testing.T) { |
| 78 | dir := t.TempDir() |
| 79 | m := &Mirror{Dir: dir, Index: filepath.Join(dir, "mirror.index.json")} |
| 80 | if p := m.CurrentProgress(); p.Known { |
| 81 | t.Fatal("zonder manifest") |
| 82 | } |
| 83 | man := protocol.Manifest{Files: []protocol.Entry{{Path: "data/repos/x", Size: 10, MTime: 1}}} |
| 84 | if err := m.SaveManifest(man); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | p := m.CurrentProgress() |
| 88 | if !p.Known || p.Percent != 0 || p.TotalBytes != 10 { |
| 89 | t.Fatalf("leeg index: %+v", p) |
| 90 | } |
| 91 | if err := m.saveIndex(map[string]protocol.Entry{"data/repos/x": {Path: "data/repos/x", Size: 10, MTime: 1}}); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | p = m.CurrentProgress() |
| 95 | if p.Percent != 100 { |
| 96 | t.Fatalf("bij: %+v", p) |
| 97 | } |
| 98 | if err := m.ClearManifest(); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if p := m.CurrentProgress(); p.Known { |
| 102 | t.Fatal("na wissen nog bekend") |
| 103 | } |
| 104 | } |