Files
Roberto Jiménez Sánchez 47664a7d51 Provisioning: unit test sync job (#103636)
* Split in multiple files

* Refactor sync even further

* Move more things between RepositoryResources

* Add status patcher

* Interface for sync functions

* Interface for compare function

* Add syncer back

* Move interfaces

* Move execute complete

* Return currentRef in syncer

* Add repository status test

* Add initial sync tests

* Fix a couple of spots

* Make initial sync tests work

* Fix register.go

* Add initial tests for sync worker

* Finish tests for sync

* Add incremental tests

* Add TODO

* Finish incremental tests

* Move folder creation to full sync

* Move interfaces

* Add initial full sync tests

* Update tests

* Reshape things

* Add changes test

* Fix register

* Add some tests

* Add more tests

* Add test

* WIP

* WIP: delete test

* Add more full test

* More tests

* Add tests for folder creation

* Add folder tests full sync

* Full coverage full sync

* Clean up tests

* Add more tests for changes function

* Enhance tests for Changes function to cover error scenarios and empty paths

- Added test cases for handling empty file paths and folder resources.
- Updated error message formatting in the Compare function for clarity.

* Add unit tests

* Failed initial patch

* Add tests failed repository resources

* Add test failed getting client

* Test for successful and unsuccessful syncs

* Add final tests for worker

* Fix existing tests

* Add missing test

* Fix spelling mistake

* Fix flake in changes test
2025-04-10 13:55:49 +01:00

77 lines
2.4 KiB
Go

package dualwrite
import (
"context"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
dashboard "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1alpha1"
folders "github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
)
func TestIsReadingLegacyDashboardsAndFolders(t *testing.T) {
tests := []struct {
name string
setupMockSvc func(*MockService)
expectedResult bool
}{
{
name: "both folders and dashboards are read from unified storage",
setupMockSvc: func(svc *MockService) {
svc.On("ReadFromUnified", mock.Anything, folders.FolderResourceInfo.GroupResource()).Return(true, nil)
svc.On("ReadFromUnified", mock.Anything, schema.GroupResource{
Group: dashboard.GROUP,
Resource: dashboard.DASHBOARD_RESOURCE,
}).Return(true, nil)
},
expectedResult: false,
},
{
name: "only folders are read from unified storage",
setupMockSvc: func(svc *MockService) {
svc.On("ReadFromUnified", mock.Anything, folders.FolderResourceInfo.GroupResource()).Return(true, nil)
svc.On("ReadFromUnified", mock.Anything, schema.GroupResource{
Group: dashboard.GROUP,
Resource: dashboard.DASHBOARD_RESOURCE,
}).Return(false, nil)
},
expectedResult: true,
},
{
name: "only dashboards are read from unified storage",
setupMockSvc: func(svc *MockService) {
svc.On("ReadFromUnified", mock.Anything, folders.FolderResourceInfo.GroupResource()).Return(false, nil)
svc.On("ReadFromUnified", mock.Anything, schema.GroupResource{
Group: dashboard.GROUP,
Resource: dashboard.DASHBOARD_RESOURCE,
}).Return(true, nil)
},
expectedResult: true,
},
{
name: "neither folders nor dashboards are read from unified storage",
setupMockSvc: func(svc *MockService) {
svc.On("ReadFromUnified", mock.Anything, folders.FolderResourceInfo.GroupResource()).Return(false, nil)
svc.On("ReadFromUnified", mock.Anything, schema.GroupResource{
Group: dashboard.GROUP,
Resource: dashboard.DASHBOARD_RESOURCE,
}).Return(false, nil)
},
expectedResult: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockSvc := NewMockService(t)
tt.setupMockSvc(mockSvc)
result := IsReadingLegacyDashboardsAndFolders(context.Background(), mockSvc)
require.Equal(t, tt.expectedResult, result)
})
}
}