Przeglądaj źródła

update validate func used by aggregator to not check for write and delete (#3330)

Ishaan Mittal 8 miesięcy temu
rodzic
commit
033e885ddc
2 zmienionych plików z 46 dodań i 13 usunięć
  1. 25 0
      core/pkg/storage/bucketstorage_test.go
  2. 21 13
      core/pkg/storage/storage.go

+ 25 - 0
core/pkg/storage/bucketstorage_test.go

@@ -92,3 +92,28 @@ func TestBucketStorage_Stat(t *testing.T) {
 
 	TestStorageStat(t, store)
 }
+
+// We should be able to call validate function with and without write and delete check without any errors
+func TestBucketStorage_Validate(t *testing.T) {
+	configPath := os.Getenv("TEST_BUCKET_CONFIG")
+	if configPath == "" {
+		t.Skip("skipping integration test, set environment variable TEST_BUCKET_CONFIG")
+	}
+	store, err := createStorage(configPath)
+	if err != nil {
+		t.Errorf("failed to create storage: %s", err.Error())
+		return
+	}
+
+	// Validate BucketStorage with write and delete check
+	err = Validate(store, true)
+	if err != nil {
+		t.Errorf("failed to validate storage: %s", err.Error())
+	}
+
+	// Validate BucketStorage without write and delete check (should not fail)
+	err = Validate(store, false)
+	if err != nil {
+		t.Errorf("failed to validate storage: %s", err.Error())
+	}
+}

+ 21 - 13
core/pkg/storage/storage.go

@@ -2,6 +2,7 @@ package storage
 
 import (
 	"os"
+	"strings"
 	"time"
 
 	"github.com/opencost/opencost/core/pkg/log"
@@ -60,7 +61,7 @@ type Storage interface {
 }
 
 // Validate uses the provided storage implementation to write a test file to the store, followed by a removal.
-func Validate(storage Storage) error {
+func Validate(storage Storage, validateWriteDelete bool) error {
 	const testPath = "tmp/test.txt"
 	const testContent = "test"
 
@@ -78,25 +79,32 @@ func Validate(storage Storage) error {
 		return errors.Wrap(err, "Failed to list path")
 	}
 
-	// attempt to write a path
-	err = storage.Write(testPath, []byte(testContent))
-	if err != nil {
-		return errors.Wrap(err, "Failed to write data to storage")
+	if validateWriteDelete {
+		// attempt to write a path
+		err = storage.Write(testPath, []byte(testContent))
+		if err != nil {
+			return errors.Wrap(err, "Failed to write data to storage")
+		}
 	}
 
 	// attempt to read the path
+	// If we are not validating write and delete, the file won't exist since we never wrote it.
+	// We only want to check read permissions, so ignore errors with "exist" and "404" in the error message to bypass the file not exist error.
 	data, err := storage.Read(testPath)
-	if err != nil {
+	if err != nil && !strings.Contains(err.Error(), "exist") && !strings.Contains(err.Error(), "404") {
 		return errors.Wrap(err, "Failed to read data from storage")
 	}
-	if string(data) != testContent {
-		return errors.New("Failed to read the expected data from storage")
-	}
 
-	// delete the path
-	err = storage.Remove(testPath)
-	if err != nil {
-		return errors.Wrap(err, "Failed to remove data from storage")
+	if validateWriteDelete {
+		if string(data) != testContent {
+			return errors.New("Failed to read the expected data from storage")
+		}
+
+		// delete the path
+		err = storage.Remove(testPath)
+		if err != nil {
+			return errors.Wrap(err, "Failed to remove data from storage")
+		}
 	}
 
 	return nil