Skip to content

Commit

Permalink
test: add test for persisting and removing entries from mantaray mani…
Browse files Browse the repository at this point in the history
…fest
  • Loading branch information
asabya committed Jan 10, 2025
1 parent 57bc1a8 commit 44e3be2
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions pkg/manifest/mantaray/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"sync"
"testing"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 44e3be2

Please sign in to comment.