From 44e3be2a3c954215305169d1ba544642d4f02e74 Mon Sep 17 00:00:00 2001 From: asabya Date: Tue, 10 Sep 2024 10:55:01 +0530 Subject: [PATCH] test: add test for persisting and removing entries from mantaray manifest --- pkg/manifest/mantaray/persist_test.go | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/pkg/manifest/mantaray/persist_test.go b/pkg/manifest/mantaray/persist_test.go index b6a6c47e699..1842b84e0e6 100644 --- a/pkg/manifest/mantaray/persist_test.go +++ b/pkg/manifest/mantaray/persist_test.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "crypto/sha256" + "errors" "sync" "testing" @@ -62,6 +63,96 @@ func TestPersistIdempotence(t *testing.T) { } } +func TestPersistRemove(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + toAdd []mantaray.NodeEntry + toRemove [][]byte + }{ + { + name: "simple", + toAdd: []mantaray.NodeEntry{ + { + Path: []byte("/"), + Metadata: map[string]string{ + "index-document": "index.html", + }, + }, + { + Path: []byte("index.html"), + }, + { + Path: []byte("img/1.png"), + }, + { + Path: []byte("img/2.png"), + }, + { + Path: []byte("robots.txt"), + }, + }, + toRemove: [][]byte{ + []byte("img/2.png"), + }, + }, + } { + ctx := context.Background() + var ls mantaray.LoadSaver = newMockLoadSaver() + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + // add and persist + n := mantaray.New() + for i := 0; i < len(tc.toAdd); i++ { + c := tc.toAdd[i].Path + e := tc.toAdd[i].Entry + if len(e) == 0 { + e = append(make([]byte, 32-len(c)), c...) + } + m := tc.toAdd[i].Metadata + err := n.Add(ctx, c, e, m, ls) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + } + err := n.Save(ctx, ls) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + ref := n.Reference() + + // reload and remove + nn := mantaray.NewNodeRef(ref) + for i := 0; i < len(tc.toRemove); i++ { + c := tc.toRemove[i] + err := nn.Remove(ctx, c, ls) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + } + err = nn.Save(ctx, ls) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + ref = nn.Reference() + + // reload and lookup removed node + nnn := mantaray.NewNodeRef(ref) + for i := 0; i < len(tc.toRemove); i++ { + c := tc.toRemove[i] + n, err = nnn.LookupNode(ctx, c, ls) + if !errors.Is(err, mantaray.ErrNotFound) { + t.Fatalf("expected not found error, got %v", err) + } + } + }) + } +} + type addr [32]byte type mockLoadSaver struct { mtx sync.Mutex