Files
Roberto Jiménez Sánchez 4f05b42275 Provisioning: initial unit tests push job (#103577)
* Add repository resources interface for export worker

* Add mocks for repository resources

* Add unit tests for ExportWorker's IsSupported method

* Add unit tests for ExportWorker's Process method, covering scenarios for missing export settings, write permissions, branch restrictions, and client creation failures.

* Fix unit tests

* Single function

* Add more unit tests

* Add test for failed folder

* Fail export folder errors

* Add another test

* Positive folder export

* Too many folder export errors

* Too many errors on folder export

* Partial folder errors

* Add test for nested folder

* Add test dashboard export

* More cases

* Ignore existing dashboards

* Fix folder tests

* Fix clonable test

* Add clone failure test

* Add test clean up without push

* Working tests

* Use mock clonable

* Add unit tests for IsWriteAllowed

* Add behaviour to cover ref equal to configured branch

* Fix worker test

* Fix linting

* Split clone and push

* Wrapper for clone and push
2025-04-08 12:44:11 +02:00

145 lines
4.1 KiB
Go

package repository
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type mockClonableRepo struct {
*MockClonableRepository
*MockClonedRepository
}
func Test_WrapWithCloneAndPushIfPossible_NonClonableRepository(t *testing.T) {
nonClonable := NewMockRepository(t)
var called bool
fn := func(repo Repository, cloned bool) error {
called = true
return errors.New("operation failed")
}
err := WrapWithCloneAndPushIfPossible(context.Background(), nonClonable, CloneOptions{}, PushOptions{}, fn)
require.EqualError(t, err, "operation failed")
require.True(t, called)
}
func TestWrapWithCloneAndPushIfPossible(t *testing.T) {
tests := []struct {
name string
setupMocks func(t *testing.T) *mockClonableRepo
operation func(repo Repository, cloned bool) error
expectedError string
}{
{
name: "successful clone, operation, and push",
setupMocks: func(t *testing.T) *mockClonableRepo {
mockRepo := NewMockClonableRepository(t)
mockCloned := NewMockClonedRepository(t)
mockRepo.EXPECT().Clone(mock.Anything, CloneOptions{}).Return(mockCloned, nil)
mockCloned.EXPECT().Push(mock.Anything, PushOptions{}).Return(nil)
mockCloned.EXPECT().Remove(mock.Anything).Return(nil)
return &mockClonableRepo{
MockClonableRepository: mockRepo,
MockClonedRepository: mockCloned,
}
},
operation: func(repo Repository, cloned bool) error {
require.True(t, cloned)
return nil
},
},
{
name: "clone failure",
setupMocks: func(t *testing.T) *mockClonableRepo {
mockRepo := NewMockClonableRepository(t)
mockRepo.EXPECT().Clone(mock.Anything, CloneOptions{}).Return(nil, errors.New("clone failed"))
return &mockClonableRepo{
MockClonableRepository: mockRepo,
}
},
operation: func(repo Repository, cloned bool) error {
return nil
},
expectedError: "clone repository: clone failed",
},
{
name: "operation failure",
setupMocks: func(t *testing.T) *mockClonableRepo {
mockRepo := NewMockClonableRepository(t)
mockCloned := NewMockClonedRepository(t)
mockRepo.EXPECT().Clone(mock.Anything, CloneOptions{}).Return(mockCloned, nil)
mockCloned.EXPECT().Remove(mock.Anything).Return(nil)
return &mockClonableRepo{
MockClonableRepository: mockRepo,
MockClonedRepository: mockCloned,
}
},
operation: func(repo Repository, cloned bool) error {
return errors.New("operation failed")
},
expectedError: "operation failed",
},
{
name: "push failure",
setupMocks: func(t *testing.T) *mockClonableRepo {
mockRepo := NewMockClonableRepository(t)
mockCloned := NewMockClonedRepository(t)
mockRepo.EXPECT().Clone(mock.Anything, CloneOptions{}).Return(mockCloned, nil)
mockCloned.EXPECT().Push(mock.Anything, PushOptions{}).Return(errors.New("push failed"))
mockCloned.EXPECT().Remove(mock.Anything).Return(nil)
return &mockClonableRepo{
MockClonableRepository: mockRepo,
MockClonedRepository: mockCloned,
}
},
operation: func(repo Repository, cloned bool) error {
return nil
},
expectedError: "push failed",
},
{
name: "remove failure should only log",
setupMocks: func(t *testing.T) *mockClonableRepo {
mockRepo := NewMockClonableRepository(t)
mockCloned := NewMockClonedRepository(t)
mockRepo.EXPECT().Clone(mock.Anything, CloneOptions{}).Return(mockCloned, nil)
mockCloned.EXPECT().Push(mock.Anything, PushOptions{}).Return(nil)
mockCloned.EXPECT().Remove(mock.Anything).Return(errors.New("remove failed"))
return &mockClonableRepo{
MockClonableRepository: mockRepo,
MockClonedRepository: mockCloned,
}
},
operation: func(repo Repository, cloned bool) error {
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo := tt.setupMocks(t)
err := WrapWithCloneAndPushIfPossible(context.Background(), repo, CloneOptions{}, PushOptions{}, tt.operation)
if tt.expectedError != "" {
require.EqualError(t, err, tt.expectedError)
} else {
require.NoError(t, err)
}
})
}
}