Update the configuration pattern to create a new node for the tests

This commit is contained in:
Arnaud 2025-10-24 15:12:42 +02:00
parent 597b98dacc
commit 35c06ba719
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
5 changed files with 60 additions and 68 deletions

View File

@ -3,7 +3,11 @@ package codex
import "testing"
func TestCodexVersion(t *testing.T) {
node := newCodexNode(t, withNoStart())
config := defaultConfigHelper(t)
node, err := New(config)
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
version, err := node.Version()
if err != nil {
@ -17,7 +21,11 @@ func TestCodexVersion(t *testing.T) {
}
func TestCodexRevision(t *testing.T) {
node := newCodexNode(t, withNoStart())
config := defaultConfigHelper(t)
node, err := New(config)
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
revision, err := node.Revision()
if err != nil {

View File

@ -32,38 +32,19 @@ func TestUpdateLogLevel(t *testing.T) {
}
defer os.Remove(tmpFile.Name())
node, err := New(Config{
LogFile: tmpFile.Name(),
MetricsEnabled: false,
node := newCodexNode(t, Config{
LogLevel: "INFO",
LogFile: tmpFile.Name(),
LogFormat: LogFormatNoColors,
})
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
t.Cleanup(func() {
if err := node.Stop(); err != nil {
t.Logf("cleanup codex: %v", err)
}
if err := node.Destroy(); err != nil {
t.Logf("cleanup codex: %v", err)
}
})
if err := node.Start(); err != nil {
t.Fatalf("Failed to start Codex node: %v", err)
}
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
if !strings.Contains(string(content), "Started codex node") {
t.Errorf("Log file does not contain 'Started codex node' %s", string(content))
}
if err := node.Stop(); err != nil {
t.Fatalf("Failed to stop Codex node: %v", err)
if !strings.Contains(string(content), "INF") {
t.Errorf("Log file does not contain INFO statement %s", string(content))
}
err = node.UpdateLogLevel("ERROR")
@ -71,6 +52,11 @@ func TestUpdateLogLevel(t *testing.T) {
t.Fatalf("UpdateLogLevel call failed: %v", err)
}
if err := node.Stop(); err != nil {
t.Fatalf("Failed to stop Codex node: %v", err)
}
// Clear the file
if err := os.WriteFile(tmpFile.Name(), []byte{}, 0644); err != nil {
t.Fatalf("Failed to clear log file: %v", err)
}
@ -85,8 +71,8 @@ func TestUpdateLogLevel(t *testing.T) {
t.Fatalf("Failed to read log file: %v", err)
}
if strings.Contains(string(content), "Starting discovery node") {
t.Errorf("Log file contains 'Starting discovery node'")
if strings.Contains(string(content), "INF") {
t.Errorf("Log file contains INFO statement after log level update: %s", string(content))
}
}

View File

@ -87,7 +87,7 @@ func TestDownloadStreamWithAutosize(t *testing.T) {
}
func TestDownloadStreamWithNotExisting(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
codex := newCodexNode(t, Config{BlockRetries: 1})
opt := DownloadStreamOptions{}
if err := codex.DownloadStream(context.Background(), "bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", opt); err == nil {
@ -159,7 +159,7 @@ func TestDownloadManifest(t *testing.T) {
}
func TestDownloadManifestWithNotExistingCid(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
codex := newCodexNode(t, Config{BlockRetries: 1})
manifest, err := codex.DownloadManifest("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")
if err == nil {
@ -172,7 +172,7 @@ func TestDownloadManifestWithNotExistingCid(t *testing.T) {
}
func TestDownloadInitWithNotExistingCid(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
codex := newCodexNode(t, Config{BlockRetries: 1})
if err := codex.DownloadInit("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", DownloadInitOptions{}); err == nil {
t.Fatal("expected error when initializing download for non-existent cid")

View File

@ -84,7 +84,7 @@ func TestFetch(t *testing.T) {
}
func TestFetchCidDoesNotExist(t *testing.T) {
codex := newCodexNode(t, withBlockRetries(1))
codex := newCodexNode(t, Config{BlockRetries: 1})
_, err := codex.Fetch("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")
if err == nil {

View File

@ -6,52 +6,50 @@ import (
"testing"
)
type codexNodeTestOption func(*codexNodeTestOptions)
func defaultConfigHelper(t *testing.T) Config {
t.Helper()
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 := New(Config{
return Config{
DataDir: t.TempDir(),
LogFormat: LogFormatNoColors,
MetricsEnabled: false,
BlockRetries: o.blockRetries,
BlockRetries: 3000,
LogLevel: "ERROR",
})
}
}
func newCodexNode(t *testing.T, opts ...Config) *CodexNode {
config := defaultConfigHelper(t)
if len(opts) > 0 {
c := opts[0]
if c.BlockRetries > 0 {
config.BlockRetries = c.BlockRetries
}
if c.LogLevel != "" {
config.LogLevel = c.LogLevel
}
if c.LogFile != "" {
config.LogFile = c.LogFile
}
}
node, err := New(config)
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
if !o.noStart {
err = node.Start()
if err != nil {
t.Fatalf("Failed to start Codex node: %v", err)
}
err = node.Start()
if err != nil {
t.Fatalf("Failed to start Codex node: %v", err)
}
t.Cleanup(func() {
if !o.noStart {
if err := node.Stop(); err != nil {
t.Logf("cleanup codex: %v", err)
}
if err := node.Stop(); err != nil {
t.Logf("cleanup codex: %v", err)
}
if err := node.Destroy(); err != nil {