UniStore: Legacy List Folders must return all folders (#96062)

* UniStore: Legacy List Folders must return all folders

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>

---------

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
This commit is contained in:
maicon 2024-11-08 12:38:34 -03:00 committed by GitHub
parent 9525891455
commit b5e636b126
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 6 deletions

View File

@ -66,7 +66,6 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO
return nil, err
}
parentUID := ""
// // translate grafana.app/* label selectors into field requirements
// requirements, newSelector, err := entity.ReadLabelSelectors(options.LabelSelector)
// if err != nil {
@ -88,13 +87,13 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO
return nil, err
}
// When nested folders are not enabled, all folders are root folders
hits, err := s.service.GetChildren(ctx, &folder.GetChildrenQuery{
UID: parentUID, // NOTE! we should do a different query when nested folders are enabled!
// List must return all folders
hits, err := s.service.GetFolders(ctx, folder.GetFoldersQuery{
SignedInUser: user,
Limit: paging.page,
OrgID: orgId,
Page: paging.limit,
// TODO: enable pagination
// Limit: paging.page,
// Page: paging.limit,
})
if err != nil {
return nil, err

View File

@ -0,0 +1,59 @@
package folders
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/folder/foldertest"
"github.com/grafana/grafana/pkg/services/user"
)
func TestLegacyStorageList(t *testing.T) {
// Ensure List returns all folders existing in service folder, regardless of
// whether they are at root level or not
folderService := &foldertest.FakeService{}
folderService.ExpectedFolders = []*folder.Folder{
&folder.Folder{UID: "parent", Title: "Folder Parent", ParentUID: ""},
&folder.Folder{UID: "child", Title: "Folder Child", ParentUID: "parent"},
&folder.Folder{UID: "anotherparent1", Title: "Folder Another Parent 1", ParentUID: ""},
&folder.Folder{UID: "anotherparent1", Title: "Folder Another Parent 2", ParentUID: ""},
}
usr := &user.SignedInUser{UserID: 1}
ctx := identity.WithRequester(context.Background(), usr)
ls := legacyStorage{
service: folderService,
namespacer: func(_ int64) string { return "1" },
}
ll, err := ls.List(ctx, &metainternalversion.ListOptions{})
require.Nil(t, err)
require.NotNil(t, ll)
list, err := meta.ExtractList(ll)
require.Nil(t, err)
require.NotNil(t, list)
require.Equal(t, len(list), 4)
uidsFromServiceFolder := []string{}
for _, f := range folderService.ExpectedFolders {
uidsFromServiceFolder = append(uidsFromServiceFolder, f.UID)
}
uidsReturnedByList := []string{}
for _, obj := range list {
f, ok := obj.(*v0alpha1.Folder)
require.Equal(t, true, ok)
uidsReturnedByList = append(uidsReturnedByList, f.ObjectMeta.Name)
}
require.ElementsMatch(t, uidsFromServiceFolder, uidsReturnedByList)
}