diff --git a/codex/codex_test.go b/codex/codex_test.go index d0cb91f..deb7cb7 100644 --- a/codex/codex_test.go +++ b/codex/codex_test.go @@ -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 { diff --git a/codex/debug_test.go b/codex/debug_test.go index a3be06c..ba4f7b4 100644 --- a/codex/debug_test.go +++ b/codex/debug_test.go @@ -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)) } } diff --git a/codex/download_test.go b/codex/download_test.go index c680498..edfc2a5 100644 --- a/codex/download_test.go +++ b/codex/download_test.go @@ -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") diff --git a/codex/storage_test.go b/codex/storage_test.go index f002bb2..bca6381 100644 --- a/codex/storage_test.go +++ b/codex/storage_test.go @@ -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 { diff --git a/codex/testutil.go b/codex/testutil.go index c02e54f..7521be4 100644 --- a/codex/testutil.go +++ b/codex/testutil.go @@ -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 {