K8s Dashboards: Fix Tags (#98417)

This commit is contained in:
Stephanie Hingtgen
2025-01-02 10:38:23 -07:00
committed by GitHub
parent 0e55e45f88
commit 01b3e56706
3 changed files with 128 additions and 12 deletions

View File

@@ -660,7 +660,10 @@ func TestSearchDashboards(t *testing.T) {
Type: "dash-db",
URI: "db/dashboard-1",
URL: "/d/uid1/dashboard-1",
Tags: []string{},
Tags: []string{
"tag1",
"tag2",
},
},
{
UID: "uid2",
@@ -683,6 +686,7 @@ func TestSearchDashboards(t *testing.T) {
Slug: "dashboard-1",
OrgID: 1,
Title: "Dashboard 1",
Tags: []string{"tag1", "tag2"},
},
{
UID: "uid2",
@@ -709,6 +713,9 @@ func TestSearchDashboards(t *testing.T) {
{
Name: "folder",
},
{
Name: "tags",
},
},
Rows: []*resource.ResourceTableRow{
{
@@ -719,6 +726,7 @@ func TestSearchDashboards(t *testing.T) {
Cells: [][]byte{
[]byte("Dashboard 1"),
[]byte(""),
[]byte("[\"tag1\", \"tag2\"]"),
},
},
{
@@ -729,6 +737,7 @@ func TestSearchDashboards(t *testing.T) {
Cells: [][]byte{
[]byte("Dashboard 2"),
[]byte(""),
[]byte(""),
},
},
},
@@ -958,6 +967,63 @@ func TestUnstructuredToLegacyDashboard(t *testing.T) {
})
}
func TestGetDashboardTags(t *testing.T) {
fakeStore := dashboards.FakeDashboardStore{}
defer fakeStore.AssertExpectations(t)
service := &DashboardServiceImpl{
cfg: setting.NewCfg(),
dashboardStore: &fakeStore,
}
expectedResult := []*dashboards.DashboardTagCloudItem{
{
Term: "tag1",
Count: 1,
},
{
Term: "tag2",
Count: 3,
},
}
query := &dashboards.GetDashboardTagsQuery{
OrgID: 1,
}
t.Run("Should fallback to dashboard store if Kubernetes feature flags are not enabled", func(t *testing.T) {
service.features = featuremgmt.WithFeatures()
fakeStore.On("GetDashboardTags", mock.Anything, query).Return(expectedResult, nil).Once()
result, err := service.GetDashboardTags(context.Background(), query)
require.NoError(t, err)
require.Equal(t, expectedResult, result)
fakeStore.AssertExpectations(t)
})
t.Run("Should use Kubernetes client if feature flags are enabled", func(t *testing.T) {
ctx, k8sClientMock, k8sResourceMock := setupK8sDashboardTests(service)
k8sClientMock.On("getClient", mock.Anything, int64(1)).Return(k8sResourceMock, true).Once()
k8sClientMock.searcher.On("Search", mock.Anything).Return(&resource.ResourceSearchResponse{
Facet: map[string]*resource.ResourceSearchResponse_Facet{
"tags": {
Terms: []*resource.ResourceSearchResponse_TermFacet{
{
Term: "tag1",
Count: 1,
},
{
Term: "tag2",
Count: 3,
},
},
},
},
}, nil)
result, err := service.GetDashboardTags(ctx, query)
require.NoError(t, err)
require.Equal(t, expectedResult, result)
fakeStore.AssertExpectations(t)
})
}
func TestLegacySaveCommandToUnstructured(t *testing.T) {
namespace := "test-namespace"
t.Run("successfully converts save command to unstructured", func(t *testing.T) {