|
|
@@ -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
|