Refactor testutil and add non existing cid tests

This commit is contained in:
Arnaud 2025-10-08 13:07:39 +02:00
parent 2bc0c76c8f
commit 696c6bfb2b
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
5 changed files with 98 additions and 44 deletions

View File

@ -3,8 +3,7 @@ package codex
import "testing" import "testing"
func TestCodexVersion(t *testing.T) { func TestCodexVersion(t *testing.T) {
start := false node := newCodexNode(t, withNoStart())
node := newCodexNode(t, start)
version, err := node.Version() version, err := node.Version()
if err != nil { if err != nil {
@ -18,8 +17,7 @@ func TestCodexVersion(t *testing.T) {
} }
func TestCodexRevision(t *testing.T) { func TestCodexRevision(t *testing.T) {
start := false node := newCodexNode(t, withNoStart())
node := newCodexNode(t, start)
revision, err := node.Revision() revision, err := node.Revision()
if err != nil { if err != nil {
@ -33,8 +31,7 @@ func TestCodexRevision(t *testing.T) {
} }
func TestCodexRepo(t *testing.T) { func TestCodexRepo(t *testing.T) {
start := true node := newCodexNode(t)
node := newCodexNode(t, start)
repo, err := node.Repo() repo, err := node.Repo()
if err != nil { if err != nil {
@ -48,8 +45,7 @@ func TestCodexRepo(t *testing.T) {
} }
func TestSpr(t *testing.T) { func TestSpr(t *testing.T) {
start := true node := newCodexNode(t)
node := newCodexNode(t, start)
spr, err := node.Spr() spr, err := node.Spr()
if err != nil { if err != nil {
@ -63,8 +59,7 @@ func TestSpr(t *testing.T) {
} }
func TestPeerId(t *testing.T) { func TestPeerId(t *testing.T) {
start := true node := newCodexNode(t)
node := newCodexNode(t, start)
peerId, err := node.PeerId() peerId, err := node.PeerId()
if err != nil { if err != nil {

View File

@ -8,8 +8,7 @@ import (
) )
func TestDebug(t *testing.T) { func TestDebug(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
info, err := codex.Debug() info, err := codex.Debug()
if err != nil { if err != nil {

View File

@ -1,28 +1,13 @@
package codex package codex
import ( import (
"bytes"
"os" "os"
"strings" "strings"
"testing" "testing"
) )
func uploadHelper(t *testing.T, codex *CodexNode) (string, int) {
t.Helper()
buf := bytes.NewBuffer([]byte("Hello World!"))
len := buf.Len()
cid, err := codex.UploadReader(UploadOptions{filepath: "hello.txt"}, buf)
if err != nil {
t.Fatalf("Error happened during upload: %v\n", err)
}
return cid, len
}
func TestDownloadStream(t *testing.T) { func TestDownloadStream(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
cid, len := uploadHelper(t, codex) cid, len := uploadHelper(t, codex)
f, err := os.Create("testdata/hello.downloaded.txt") f, err := os.Create("testdata/hello.downloaded.txt")
@ -59,7 +44,7 @@ func TestDownloadStream(t *testing.T) {
t.Fatalf("UploadReader progress callback total bytes %d but expected %d", totalBytes, len) t.Fatalf("UploadReader progress callback total bytes %d but expected %d", totalBytes, len)
} }
data, err := os.ReadFile("testdata/hello.writer.txt") data, err := os.ReadFile("testdata/hello.downloaded.writer.txt")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -70,8 +55,7 @@ func TestDownloadStream(t *testing.T) {
} }
func TestDownloadStreamWithAutosize(t *testing.T) { func TestDownloadStreamWithAutosize(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
cid, len := uploadHelper(t, codex) cid, len := uploadHelper(t, codex)
totalBytes := 0 totalBytes := 0
@ -101,9 +85,17 @@ func TestDownloadStreamWithAutosize(t *testing.T) {
} }
} }
func TestDownloadStreamWithNotExisting(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
opt := DownloadStreamOptions{}
if err := codex.DownloadStream("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", opt); err == nil {
t.Fatal("Error expected when downloading non-existing cid")
}
}
func TestDownloadManual(t *testing.T) { func TestDownloadManual(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
cid, _ := uploadHelper(t, codex) cid, _ := uploadHelper(t, codex)
if err := codex.DownloadInit(cid, DownloadInitOptions{}); err != nil { if err := codex.DownloadInit(cid, DownloadInitOptions{}); err != nil {
@ -126,3 +118,38 @@ func TestDownloadManual(t *testing.T) {
t.Fatalf("Error when cancelling the download %s", err) t.Fatalf("Error when cancelling the download %s", err)
} }
} }
func TestDownloadManifest(t *testing.T) {
codex := newCodexNode(t)
cid, _ := uploadHelper(t, codex)
manifest, err := codex.DownloadManifest(cid)
if err != nil {
t.Fatal("Error when downloading manifest:", err)
}
if manifest.Cid != cid {
t.Errorf("expected cid %q, got %q", cid, manifest.Cid)
}
}
func TestDownloadManifestWithNotExistingCid(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
manifest, err := codex.DownloadManifest("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")
if err == nil {
t.Fatal("Error when downloading manifest:", err)
}
if manifest.Cid != "" {
t.Errorf("expected empty cid, got %q", manifest.Cid)
}
}
func TestDownloadInitWithNotExistingCid(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
if err := codex.DownloadInit("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", DownloadInitOptions{}); err == nil {
t.Fatal("expected error when initializing download for non-existent cid")
}
}

View File

@ -1,20 +1,44 @@
package codex package codex
import ( import (
"bytes"
"testing" "testing"
) )
func newCodexNode(t *testing.T, start bool) *CodexNode { type codexNodeTestOption func(*codexNodeTestOptions)
type codexNodeTestOptions struct {
noStart bool
blockRetries int
}
func withNoStart() codexNodeTestOption {
return func(o *codexNodeTestOptions) { o.noStart = true }
}
func withBlockRetries(n int) codexNodeTestOption {
return func(o *codexNodeTestOptions) { o.blockRetries = n }
}
func newCodexNode(t *testing.T, opts ...codexNodeTestOption) *CodexNode {
o := codexNodeTestOptions{
blockRetries: 3000,
}
for _, opt := range opts {
opt(&o)
}
node, err := CodexNew(CodexConfig{ node, err := CodexNew(CodexConfig{
DataDir: t.TempDir(), DataDir: t.TempDir(),
LogFormat: LogFormatNoColors, LogFormat: LogFormatNoColors,
MetricsEnabled: false, MetricsEnabled: false,
BlockRetries: o.blockRetries,
}) })
if err != nil { if err != nil {
t.Fatalf("Failed to create Codex node: %v", err) t.Fatalf("Failed to create Codex node: %v", err)
} }
if start { if !o.noStart {
err = node.Start() err = node.Start()
if err != nil { if err != nil {
t.Fatalf("Failed to start Codex node: %v", err) t.Fatalf("Failed to start Codex node: %v", err)
@ -22,7 +46,7 @@ func newCodexNode(t *testing.T, start bool) *CodexNode {
} }
t.Cleanup(func() { t.Cleanup(func() {
if start { if !o.noStart {
if err := node.Stop(); err != nil { if err := node.Stop(); err != nil {
t.Logf("cleanup codex: %v", err) t.Logf("cleanup codex: %v", err)
} }
@ -35,3 +59,16 @@ func newCodexNode(t *testing.T, start bool) *CodexNode {
return node return node
} }
func uploadHelper(t *testing.T, codex *CodexNode) (string, int) {
t.Helper()
buf := bytes.NewBuffer([]byte("Hello World!"))
len := buf.Len()
cid, err := codex.UploadReader(UploadOptions{filepath: "hello.txt"}, buf)
if err != nil {
t.Fatalf("Error happened during upload: %v\n", err)
}
return cid, len
}

View File

@ -10,8 +10,7 @@ import (
const expectedCID = "zDvZRwzmAkhzDRPH5EW242gJBNZ2T7aoH2v1fVH66FxXL4kSbvyM" const expectedCID = "zDvZRwzmAkhzDRPH5EW242gJBNZ2T7aoH2v1fVH66FxXL4kSbvyM"
func TestUploadReader(t *testing.T) { func TestUploadReader(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
totalBytes := 0 totalBytes := 0
finalPercent := 0.0 finalPercent := 0.0
@ -44,8 +43,7 @@ func TestUploadReader(t *testing.T) {
} }
func TestUploadFile(t *testing.T) { func TestUploadFile(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
totalBytes := 0 totalBytes := 0
finalPercent := 0.0 finalPercent := 0.0
@ -82,8 +80,7 @@ func TestUploadFile(t *testing.T) {
} }
func TestUploadFileNoProgress(t *testing.T) { func TestUploadFileNoProgress(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
options := UploadOptions{filepath: "./testdata/doesnt_exist.txt"} options := UploadOptions{filepath: "./testdata/doesnt_exist.txt"}
@ -98,8 +95,7 @@ func TestUploadFileNoProgress(t *testing.T) {
} }
func TestManualUpload(t *testing.T) { func TestManualUpload(t *testing.T) {
start := true codex := newCodexNode(t)
codex := newCodexNode(t, start)
sessionId, err := codex.UploadInit(&UploadOptions{filepath: "hello.txt"}) sessionId, err := codex.UploadInit(&UploadOptions{filepath: "hello.txt"})
if err != nil { if err != nil {