Go: fix context (#18118)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-08-11 19:19:29 +08:00
committed by GitHub
parent 8bd5768ebc
commit c75edbfbe8
36 changed files with 492 additions and 352 deletions

View File

@@ -20,7 +20,6 @@ package storage
import (
"bytes"
"context"
"fmt"
"log"
"ragflow/internal/utility"
@@ -112,8 +111,9 @@ func TestNewMinioStorage_InvalidConfig(t *testing.T) {
func TestMinioStorage_Health(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
healthy := storage.Health(context.Background())
healthy := storage.Health(ctx)
// Health check should return true if connection is working
// Note: This depends on whether a default bucket is configured
t.Logf("Health check result: %v", healthy)
@@ -128,15 +128,16 @@ func TestMinioStorage_PutAndGet(t *testing.T) {
bucket := "test-bucket"
key := "test-file.txt"
content := []byte("Hello, MinIO Test!")
ctx := t.Context()
// Test Put
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
// Test Get
retrieved, err := storage.Get(context.Background(), bucket, key)
retrieved, err := storage.Get(ctx, bucket, key)
if err != nil {
t.Fatalf("Failed to get object: %v", err)
}
@@ -146,7 +147,7 @@ func TestMinioStorage_PutAndGet(t *testing.T) {
}
// Cleanup
err = storage.Remove(context.Background(), bucket, key)
err = storage.Remove(ctx, bucket, key)
if err != nil {
t.Logf("Warning: failed to cleanup test object: %v", err)
}
@@ -154,28 +155,30 @@ func TestMinioStorage_PutAndGet(t *testing.T) {
func TestMinioStorage_Put_EmptyData(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "empty-file.txt"
content := []byte{}
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put empty object: %v", err)
}
// Verify object exists
exists := storage.ObjExist(context.Background(), bucket, key)
exists := storage.ObjExist(ctx, bucket, key)
if !exists {
t.Error("Expected empty object to exist")
}
// Cleanup
storage.Remove(context.Background(), bucket, key)
storage.Remove(ctx, bucket, key)
}
func TestMinioStorage_Put_LargeData(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "large-file.bin"
@@ -185,12 +188,12 @@ func TestMinioStorage_Put_LargeData(t *testing.T) {
content[i] = byte(i % 256)
}
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put large object: %v", err)
}
retrieved, err := storage.Get(context.Background(), bucket, key)
retrieved, err := storage.Get(ctx, bucket, key)
if err != nil {
t.Fatalf("Failed to get large object: %v", err)
}
@@ -200,16 +203,17 @@ func TestMinioStorage_Put_LargeData(t *testing.T) {
}
// Cleanup
storage.Remove(context.Background(), bucket, key)
storage.Remove(ctx, bucket, key)
}
func TestMinioStorage_Get_NonExistent(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "non-existent-file.txt"
_, err := storage.Get(context.Background(), bucket, key)
_, err := storage.Get(ctx, bucket, key)
if err == nil {
t.Error("Expected error when getting non-existent object")
}
@@ -217,31 +221,32 @@ func TestMinioStorage_Get_NonExistent(t *testing.T) {
func TestMinioStorage_Remove(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "file-to-delete.txt"
content := []byte("Delete me")
// First, put an object
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
// Verify it exists
exists := storage.ObjExist(context.Background(), bucket, key)
exists := storage.ObjExist(ctx, bucket, key)
if !exists {
t.Fatal("Expected object to exist before removal")
}
// Remove it
err = storage.Remove(context.Background(), bucket, key)
err = storage.Remove(ctx, bucket, key)
if err != nil {
t.Fatalf("Failed to remove object: %v", err)
}
// Verify it's gone
exists = storage.ObjExist(context.Background(), bucket, key)
exists = storage.ObjExist(ctx, bucket, key)
if exists {
t.Error("Expected object to not exist after removal")
}
@@ -249,12 +254,13 @@ func TestMinioStorage_Remove(t *testing.T) {
func TestMinioStorage_Remove_NonExistent(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "non-existent-file.txt"
// Removing a non-existent object should not error
err := storage.Remove(context.Background(), bucket, key)
err := storage.Remove(ctx, bucket, key)
if err != nil {
t.Logf("Remove non-existent object returned error (may be acceptable): %v", err)
}
@@ -262,48 +268,50 @@ func TestMinioStorage_Remove_NonExistent(t *testing.T) {
func TestMinioStorage_ObjExist(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "existence-test.txt"
content := []byte("Test content")
// Check non-existent object
exists := storage.ObjExist(context.Background(), bucket, key)
exists := storage.ObjExist(ctx, bucket, key)
if exists {
t.Error("Expected non-existent object to return false")
}
// Create object
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
// Check existing object
exists = storage.ObjExist(context.Background(), bucket, key)
exists = storage.ObjExist(ctx, bucket, key)
if !exists {
t.Error("Expected existing object to return true")
}
// Cleanup
storage.Remove(context.Background(), bucket, key)
storage.Remove(ctx, bucket, key)
}
func TestMinioStorage_GetPresignedURL(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "presigned-test.txt"
content := []byte("Presigned URL test content")
// Create object first
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
// Get presigned URL
url, err := storage.GetPresignedURL(context.Background(), bucket, key, 5*time.Minute)
url, err := storage.GetPresignedURL(ctx, bucket, key, 5*time.Minute)
if err != nil {
t.Fatalf("Failed to get presigned URL: %v", err)
}
@@ -318,16 +326,17 @@ func TestMinioStorage_GetPresignedURL(t *testing.T) {
}
// Cleanup
storage.Remove(context.Background(), bucket, key)
storage.Remove(ctx, bucket, key)
}
func TestMinioStorage_GetPresignedURL_NonExistent(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "non-existent-presigned.txt"
_, err := storage.GetPresignedURL(context.Background(), bucket, key, 5*time.Minute)
_, err := storage.GetPresignedURL(ctx, bucket, key, 5*time.Minute)
if err == nil {
t.Log("Note: Some MinIO versions may allow presigned URLs for non-existent objects")
}
@@ -335,61 +344,63 @@ func TestMinioStorage_GetPresignedURL_NonExistent(t *testing.T) {
func TestMinioStorage_BucketExists(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := fmt.Sprintf("test-bucket-exists-%d", time.Now().Unix())
// Check non-existent bucket
exists := storage.BucketExists(context.Background(), bucket)
exists := storage.BucketExists(ctx, bucket)
if exists {
t.Error("Expected non-existent bucket to return false")
}
// Create bucket by putting an object
err := storage.Put(context.Background(), bucket, "test.txt", []byte("test"))
err := storage.Put(ctx, bucket, "test.txt", []byte("test"))
if err != nil {
t.Fatalf("Failed to create bucket: %v", err)
}
// Check existing bucket
exists = storage.BucketExists(context.Background(), bucket)
exists = storage.BucketExists(ctx, bucket)
if !exists {
t.Error("Expected existing bucket to return true")
}
// Cleanup
storage.RemoveBucket(context.Background(), bucket)
storage.RemoveBucket(ctx, bucket)
}
func TestMinioStorage_RemoveBucket(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := fmt.Sprintf("test-bucket-remove-%d", time.Now().Unix())
// Create bucket with some objects
err := storage.Put(context.Background(), bucket, "file1.txt", []byte("content1"))
err := storage.Put(ctx, bucket, "file1.txt", []byte("content1"))
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
err = storage.Put(context.Background(), bucket, "file2.txt", []byte("content2"))
err = storage.Put(ctx, bucket, "file2.txt", []byte("content2"))
if err != nil {
t.Fatalf("Failed to put object: %v", err)
}
// Verify bucket exists
exists := storage.BucketExists(context.Background(), bucket)
exists := storage.BucketExists(ctx, bucket)
if !exists {
t.Fatal("Expected bucket to exist before removal")
}
// Remove bucket
err = storage.RemoveBucket(context.Background(), bucket)
err = storage.RemoveBucket(ctx, bucket)
if err != nil {
t.Fatalf("Failed to remove bucket: %v", err)
}
// Verify bucket is gone
exists = storage.BucketExists(context.Background(), bucket)
exists = storage.BucketExists(ctx, bucket)
if exists {
t.Error("Expected bucket to not exist after removal")
}
@@ -397,6 +408,7 @@ func TestMinioStorage_RemoveBucket(t *testing.T) {
func TestMinioStorage_Copy(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
srcBucket := "test-bucket-src"
srcKey := "source-file.txt"
@@ -405,25 +417,25 @@ func TestMinioStorage_Copy(t *testing.T) {
content := []byte("Content to copy")
// Create source object
err := storage.Put(context.Background(), srcBucket, srcKey, content)
err := storage.Put(ctx, srcBucket, srcKey, content)
if err != nil {
t.Fatalf("Failed to put source object: %v", err)
}
// Copy object
success := storage.Copy(context.Background(), srcBucket, srcKey, destBucket, destKey)
success := storage.Copy(ctx, srcBucket, srcKey, destBucket, destKey)
if !success {
t.Fatal("Failed to copy object")
}
// Verify destination exists
exists := storage.ObjExist(context.Background(), destBucket, destKey)
exists := storage.ObjExist(ctx, destBucket, destKey)
if !exists {
t.Error("Expected copied object to exist")
}
// Verify content matches
retrieved, err := storage.Get(context.Background(), destBucket, destKey)
retrieved, err := storage.Get(ctx, destBucket, destKey)
if err != nil {
t.Fatalf("Failed to get copied object: %v", err)
}
@@ -433,33 +445,35 @@ func TestMinioStorage_Copy(t *testing.T) {
}
// Cleanup
storage.Remove(context.Background(), srcBucket, srcKey)
storage.Remove(context.Background(), destBucket, destKey)
storage.Remove(ctx, srcBucket, srcKey)
storage.Remove(ctx, destBucket, destKey)
}
func TestMinioStorage_Copy_NonExistentSource(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
srcBucket := "test-bucket-src"
srcKey := "non-existent-source.txt"
destBucket := "test-bucket-dest"
destKey := "should-not-exist.txt"
success := storage.Copy(context.Background(), srcBucket, srcKey, destBucket, destKey)
success := storage.Copy(ctx, srcBucket, srcKey, destBucket, destKey)
if success {
t.Error("Expected copy of non-existent object to fail")
}
// Verify destination does not exist
exists := storage.ObjExist(context.Background(), destBucket, destKey)
exists := storage.ObjExist(ctx, destBucket, destKey)
if exists {
t.Error("Expected destination object to not exist after failed copy")
storage.Remove(context.Background(), destBucket, destKey)
storage.Remove(ctx, destBucket, destKey)
}
}
func TestMinioStorage_Move(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
srcBucket := "test-bucket-src"
srcKey := "file-to-move.txt"
@@ -468,31 +482,31 @@ func TestMinioStorage_Move(t *testing.T) {
content := []byte("Content to move")
// Create source object
err := storage.Put(context.Background(), srcBucket, srcKey, content)
err := storage.Put(ctx, srcBucket, srcKey, content)
if err != nil {
t.Fatalf("Failed to put source object: %v", err)
}
// Move object
success := storage.Move(context.Background(), srcBucket, srcKey, destBucket, destKey)
success := storage.Move(ctx, srcBucket, srcKey, destBucket, destKey)
if !success {
t.Fatal("Failed to move object")
}
// Verify source is gone
exists := storage.ObjExist(context.Background(), srcBucket, srcKey)
exists := storage.ObjExist(ctx, srcBucket, srcKey)
if exists {
t.Error("Expected source object to not exist after move")
}
// Verify destination exists
exists = storage.ObjExist(context.Background(), destBucket, destKey)
exists = storage.ObjExist(ctx, destBucket, destKey)
if !exists {
t.Error("Expected moved object to exist")
}
// Verify content matches
retrieved, err := storage.Get(context.Background(), destBucket, destKey)
retrieved, err := storage.Get(ctx, destBucket, destKey)
if err != nil {
t.Fatalf("Failed to get moved object: %v", err)
}
@@ -502,18 +516,19 @@ func TestMinioStorage_Move(t *testing.T) {
}
// Cleanup
storage.Remove(context.Background(), destBucket, destKey)
storage.Remove(ctx, destBucket, destKey)
}
func TestMinioStorage_Move_NonExistentSource(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
srcBucket := "test-bucket-src"
srcKey := "non-existent-source.txt"
destBucket := "test-bucket-dest"
destKey := "should-not-exist.txt"
success := storage.Move(context.Background(), srcBucket, srcKey, destBucket, destKey)
success := storage.Move(ctx, srcBucket, srcKey, destBucket, destKey)
if success {
t.Error("Expected move of non-existent object to fail")
}
@@ -521,6 +536,7 @@ func TestMinioStorage_Move_NonExistentSource(t *testing.T) {
func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := fmt.Sprintf("test-multi-%d", time.Now().Unix())
numObjects := 10
@@ -529,7 +545,7 @@ func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
for i := 0; i < numObjects; i++ {
key := fmt.Sprintf("file-%d.txt", i)
content := []byte(fmt.Sprintf("Content %d", i))
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Fatalf("Failed to put object %d: %v", i, err)
}
@@ -538,7 +554,7 @@ func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
// Verify all objects exist
for i := 0; i < numObjects; i++ {
key := fmt.Sprintf("file-%d.txt", i)
exists := storage.ObjExist(context.Background(), bucket, key)
exists := storage.ObjExist(ctx, bucket, key)
if !exists {
t.Errorf("Expected object %s to exist", key)
}
@@ -548,7 +564,7 @@ func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
for i := 0; i < numObjects; i++ {
key := fmt.Sprintf("file-%d.txt", i)
expectedContent := []byte(fmt.Sprintf("Content %d", i))
retrieved, err := storage.Get(context.Background(), bucket, key)
retrieved, err := storage.Get(ctx, bucket, key)
if err != nil {
t.Errorf("Failed to get object %s: %v", key, err)
continue
@@ -559,7 +575,7 @@ func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
}
// Cleanup - remove bucket with all objects
err := storage.RemoveBucket(context.Background(), bucket)
err := storage.RemoveBucket(ctx, bucket)
if err != nil {
t.Logf("Warning: failed to cleanup bucket: %v", err)
}
@@ -567,6 +583,7 @@ func TestMinioStorage_MultipleObjectsInBucket(t *testing.T) {
func TestMinioStorage_SpecialCharactersInKey(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
specialKeys := []string{
@@ -581,13 +598,13 @@ func TestMinioStorage_SpecialCharactersInKey(t *testing.T) {
for _, key := range specialKeys {
content := []byte(fmt.Sprintf("Content for %s", key))
err := storage.Put(context.Background(), bucket, key, content)
err := storage.Put(ctx, bucket, key, content)
if err != nil {
t.Errorf("Failed to put object with key '%s': %v", key, err)
continue
}
retrieved, err := storage.Get(context.Background(), bucket, key)
retrieved, err := storage.Get(ctx, bucket, key)
if err != nil {
t.Errorf("Failed to get object with key '%s': %v", key, err)
continue
@@ -598,12 +615,13 @@ func TestMinioStorage_SpecialCharactersInKey(t *testing.T) {
}
// Cleanup
storage.Remove(context.Background(), bucket, key)
storage.Remove(ctx, bucket, key)
}
}
func TestMinioStorage_TenantID(t *testing.T) {
storage := newTestMinioStorage(t)
ctx := t.Context()
bucket := "test-bucket"
key := "tenant-test.txt"
@@ -611,13 +629,13 @@ func TestMinioStorage_TenantID(t *testing.T) {
tenantID := "tenant-123"
// Put with tenant ID
err := storage.Put(context.Background(), bucket, key, content, tenantID)
err := storage.Put(ctx, bucket, key, content, tenantID)
if err != nil {
t.Fatalf("Failed to put object with tenant ID: %v", err)
}
// Get with tenant ID
retrieved, err := storage.Get(context.Background(), bucket, key, tenantID)
retrieved, err := storage.Get(ctx, bucket, key, tenantID)
if err != nil {
t.Fatalf("Failed to get object with tenant ID: %v", err)
}
@@ -627,11 +645,11 @@ func TestMinioStorage_TenantID(t *testing.T) {
}
// Check existence with tenant ID
exists := storage.ObjExist(context.Background(), bucket, key, tenantID)
exists := storage.ObjExist(ctx, bucket, key, tenantID)
if !exists {
t.Error("Expected object to exist with tenant ID")
}
// Cleanup
storage.Remove(context.Background(), bucket, key, tenantID)
storage.Remove(ctx, bucket, key, tenantID)
}