Jelajahi Sumber

Fix listing directories in root path(empty string path) (#3499)

Ishaan Mittal 4 bulan lalu
induk
melakukan
2c004e2ef4
3 mengubah file dengan 13 tambahan dan 10 penghapusan
  1. 4 9
      core/pkg/storage/gcsstorage.go
  2. 1 1
      core/pkg/storage/s3storage.go
  3. 8 0
      core/pkg/storage/test.go

+ 4 - 9
core/pkg/storage/gcsstorage.go

@@ -239,7 +239,7 @@ func (gs *GCSStorage) List(path string) ([]*StorageInfo, error) {
 func (gs *GCSStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 	path = trimLeading(path)
 
-	log.Debugf("GCSStorage::List(%s)", path)
+	log.Debugf("GCSStorage::ListDirectories(%s)", path)
 	ctx := context.Background()
 
 	// Ensure the object name actually ends with a dir suffix. Otherwise we'll just iterate the
@@ -261,17 +261,12 @@ func (gs *GCSStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 			break
 		}
 		if err != nil {
-			return nil, errors.Wrap(err, "list gcs objects")
-		}
-
-		// ignore the root path directory
-		if attrs.Name == path {
-			continue
+			return nil, errors.Wrap(err, "list gcs prefixes")
 		}
 
-		// We filter directories using DirDelim, so a nameless entry is a dir
+		// We filter directories using DirDelim, so a non empty prefix entry is a prefix(directory)
 		// See gcs.ObjectAttrs Prefix property
-		if attrs.Name == "" {
+		if attrs.Prefix != "" {
 			stats = append(stats, &StorageInfo{
 				Name:    attrs.Prefix,
 				Size:    attrs.Size,

+ 1 - 1
core/pkg/storage/s3storage.go

@@ -419,7 +419,7 @@ func (s3 *S3Storage) List(path string) ([]*StorageInfo, error) {
 func (s3 *S3Storage) ListDirectories(path string) ([]*StorageInfo, error) {
 	path = trimLeading(path)
 
-	log.Tracef("S3Storage::List(%s)", path)
+	log.Tracef("S3Storage::ListDirectories(%s)", path)
 	ctx := context.Background()
 
 	if path != "" {

+ 8 - 0
core/pkg/storage/test.go

@@ -3,6 +3,7 @@ package storage
 import (
 	"fmt"
 	"path"
+	"strings"
 	"testing"
 
 	"github.com/opencost/opencost/core/pkg/util/json"
@@ -166,6 +167,13 @@ func TestStorageListDirectories(t *testing.T, store Storage) {
 		expected  []string
 		expectErr bool
 	}{
+		"root dir dir": {
+			path: "",
+			expected: []string{
+				strings.Split(testpath, "/")[0] + "/",
+			},
+			expectErr: false,
+		},
 		"base dir dir": {
 			path: path.Join(testpath, testName),
 			expected: []string{