2025-10-03 14:28:14 +02:00
|
|
|
package codex
|
|
|
|
|
|
2025-10-07 15:15:21 +02:00
|
|
|
import (
|
2025-10-08 13:07:39 +02:00
|
|
|
"bytes"
|
2025-10-17 08:55:01 +02:00
|
|
|
"context"
|
2025-10-07 15:15:21 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
2025-10-03 14:28:14 +02:00
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
func defaultConfigHelper(t *testing.T) Config {
|
|
|
|
|
t.Helper()
|
2025-10-08 13:07:39 +02:00
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
return Config{
|
|
|
|
|
DataDir: t.TempDir(),
|
|
|
|
|
LogFormat: LogFormatNoColors,
|
|
|
|
|
MetricsEnabled: false,
|
|
|
|
|
BlockRetries: 3000,
|
2025-11-10 19:57:34 +11:00
|
|
|
Nat: "none",
|
2025-10-24 15:12:42 +02:00
|
|
|
}
|
2025-10-08 13:07:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
func newCodexNode(t *testing.T, opts ...Config) *CodexNode {
|
|
|
|
|
config := defaultConfigHelper(t)
|
2025-10-08 13:07:39 +02:00
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
if len(opts) > 0 {
|
|
|
|
|
c := opts[0]
|
2025-10-08 13:07:39 +02:00
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
if c.BlockRetries > 0 {
|
|
|
|
|
config.BlockRetries = c.BlockRetries
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.LogLevel != "" {
|
|
|
|
|
config.LogLevel = c.LogLevel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.LogFile != "" {
|
|
|
|
|
config.LogFile = c.LogFile
|
|
|
|
|
}
|
2025-10-27 10:56:32 +01:00
|
|
|
|
|
|
|
|
if len(c.BootstrapNodes) != 0 {
|
|
|
|
|
config.BootstrapNodes = c.BootstrapNodes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.DiscoveryPort != 0 {
|
|
|
|
|
config.DiscoveryPort = c.DiscoveryPort
|
|
|
|
|
}
|
2025-10-08 13:07:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
node, err := New(config)
|
2025-10-03 14:28:14 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to create Codex node: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 15:12:42 +02:00
|
|
|
err = node.Start()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to start Codex node: %v", err)
|
2025-10-03 14:28:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2025-10-24 15:12:42 +02:00
|
|
|
if err := node.Stop(); err != nil {
|
|
|
|
|
t.Logf("cleanup codex: %v", err)
|
2025-10-03 14:28:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := node.Destroy(); err != nil {
|
|
|
|
|
t.Logf("cleanup codex: %v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return node
|
|
|
|
|
}
|
2025-10-08 13:07:39 +02:00
|
|
|
|
|
|
|
|
func uploadHelper(t *testing.T, codex *CodexNode) (string, int) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte("Hello World!"))
|
|
|
|
|
len := buf.Len()
|
2025-10-17 08:55:01 +02:00
|
|
|
cid, err := codex.UploadReader(context.Background(), UploadOptions{Filepath: "hello.txt"}, buf)
|
2025-10-08 13:07:39 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error happened during upload: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cid, len
|
|
|
|
|
}
|
2025-10-17 13:18:36 +02:00
|
|
|
|
|
|
|
|
func uploadBigFileHelper(t *testing.T, codex *CodexNode) (string, int) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
len := 1024 * 1024 * 50
|
|
|
|
|
buf := bytes.NewBuffer(make([]byte, len))
|
|
|
|
|
|
|
|
|
|
cid, err := codex.UploadReader(context.Background(), UploadOptions{Filepath: "hello.txt"}, buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error happened during upload: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cid, len
|
|
|
|
|
}
|