Stats: use dashboard stats rather than list (#99130)

This commit is contained in:
Stephanie Hingtgen
2025-01-16 22:19:56 -07:00
committed by GitHub
parent 5a930e0ec6
commit e019e34eb5
11 changed files with 226 additions and 100 deletions

View File

@@ -271,6 +271,11 @@ func (m *mockResourceIndexClient) Search(ctx context.Context, req *resource.Reso
return args.Get(0).(*resource.ResourceSearchResponse), args.Error(1)
}
func (m *mockResourceIndexClient) GetStats(ctx context.Context, in *resource.ResourceStatsRequest, opts ...grpc.CallOption) (*resource.ResourceStatsResponse, error) {
args := m.Called(in)
return args.Get(0).(*resource.ResourceStatsResponse), args.Error(1)
}
type mockResourceInterface struct {
mock.Mock
dynamic.ResourceInterface
@@ -1779,44 +1784,17 @@ func TestQuotaCount(t *testing.T) {
},
}
dashboardUnstructuredOrg1 := unstructured.UnstructuredList{
Items: []unstructured.Unstructured{{
Object: map[string]any{
"metadata": map[string]any{
"name": "uid",
},
"spec": map[string]any{
"test": "test",
"version": int64(1),
"title": "testing slugify",
},
},
}},
}
dashboardUnstructuredOrg2 := unstructured.UnstructuredList{
Items: []unstructured.Unstructured{{
Object: map[string]any{
"metadata": map[string]any{
"name": "uid",
},
"spec": map[string]any{
"test": "test",
"version": int64(1),
"title": "testing slugify",
},
countOrg1 := resource.ResourceStatsResponse{
Stats: []*resource.ResourceStatsResponse_Stats{
{
Count: 1,
},
},
}
countOrg2 := resource.ResourceStatsResponse{
Stats: []*resource.ResourceStatsResponse_Stats{
{
Object: map[string]any{
"metadata": map[string]any{
"name": "uid2",
},
"spec": map[string]any{
"test": "test2",
"version": int64(1),
"title": "testing slugify2",
},
},
Count: 2,
},
},
}
@@ -1833,13 +1811,11 @@ func TestQuotaCount(t *testing.T) {
})
t.Run("Should use Kubernetes client if feature flags are enabled", func(t *testing.T) {
ctx, k8sClientMock, k8sResourceMock := setupK8sDashboardTests(service)
ctx, k8sClientMock, _ := setupK8sDashboardTests(service)
orgSvc := orgtest.FakeOrgService{ExpectedOrgs: orgs}
service.orgService = &orgSvc
k8sClientMock.On("getClient", mock.Anything, int64(1)).Return(k8sResourceMock, true).Once()
k8sClientMock.On("getClient", mock.Anything, int64(2)).Return(k8sResourceMock, true).Once()
k8sResourceMock.On("List", mock.Anything, mock.Anything).Return(&dashboardUnstructuredOrg2, nil).Once()
k8sResourceMock.On("List", mock.Anything, mock.Anything).Return(&dashboardUnstructuredOrg1, nil).Once()
k8sClientMock.searcher.On("GetStats", mock.Anything).Return(&countOrg2, nil).Once()
k8sClientMock.searcher.On("GetStats", mock.Anything).Return(&countOrg1, nil).Once()
result, err := service.Count(ctx, query)
require.NoError(t, err)
@@ -1858,6 +1834,38 @@ func TestQuotaCount(t *testing.T) {
})
}
func TestCountDashboardsInOrg(t *testing.T) {
fakeStore := dashboards.FakeDashboardStore{}
defer fakeStore.AssertExpectations(t)
service := &DashboardServiceImpl{
cfg: setting.NewCfg(),
dashboardStore: &fakeStore,
}
count := resource.ResourceStatsResponse{
Stats: []*resource.ResourceStatsResponse_Stats{
{
Count: 3,
},
},
}
t.Run("Should fallback to dashboard store if Kubernetes feature flags are not enabled", func(t *testing.T) {
service.features = featuremgmt.WithFeatures()
fakeStore.On("CountInOrg", mock.Anything, mock.Anything).Return(nil, nil).Once()
_, err := service.CountDashboardsInOrg(context.Background(), 1)
require.NoError(t, err)
fakeStore.AssertExpectations(t)
})
t.Run("Should use Kubernetes client if feature flags are enabled", func(t *testing.T) {
ctx, k8sClientMock, _ := setupK8sDashboardTests(service)
k8sClientMock.searcher.On("GetStats", mock.Anything).Return(&count, nil).Once()
result, err := service.CountDashboardsInOrg(ctx, 1)
require.NoError(t, err)
require.Equal(t, result, int64(3))
})
}
func TestLegacySaveCommandToUnstructured(t *testing.T) {
namespace := "test-namespace"
t.Run("successfully converts save command to unstructured", func(t *testing.T) {