Provisioning: Cleanup tester interface (#110640)
* Provisioning: Cleanup tester interface * undo accidental change * cleanup * cleanup test
This commit is contained in:
@@ -18,21 +18,12 @@ type StatusPatcher interface {
|
||||
|
||||
// HealthChecker provides unified health checking for repositories
|
||||
type HealthChecker struct {
|
||||
tester RepositoryTester
|
||||
statusPatcher StatusPatcher
|
||||
}
|
||||
|
||||
// RepositoryTester defines the interface for testing repository connectivity
|
||||
//
|
||||
//go:generate mockery --name=RepositoryTester
|
||||
type RepositoryTester interface {
|
||||
TestRepository(ctx context.Context, repo repository.Repository) (*provisioning.TestResults, error)
|
||||
}
|
||||
|
||||
// NewHealthChecker creates a new health checker
|
||||
func NewHealthChecker(tester RepositoryTester, statusPatcher StatusPatcher) *HealthChecker {
|
||||
func NewHealthChecker(statusPatcher StatusPatcher) *HealthChecker {
|
||||
return &HealthChecker{
|
||||
tester: tester,
|
||||
statusPatcher: statusPatcher,
|
||||
}
|
||||
}
|
||||
@@ -172,7 +163,7 @@ func (hc *HealthChecker) RefreshTimestamp(ctx context.Context, repo *provisionin
|
||||
// refreshHealth performs a comprehensive health check
|
||||
// Returns test results, health status, and any error
|
||||
func (hc *HealthChecker) refreshHealth(ctx context.Context, repo repository.Repository, existingStatus provisioning.HealthStatus) (*provisioning.TestResults, provisioning.HealthStatus, error) {
|
||||
res, err := hc.tester.TestRepository(ctx, repo)
|
||||
res, err := repository.TestRepository(ctx, repo)
|
||||
if err != nil {
|
||||
return nil, existingStatus, fmt.Errorf("failed to test repository: %w", err)
|
||||
}
|
||||
|
||||
@@ -16,13 +16,11 @@ import (
|
||||
)
|
||||
|
||||
func TestNewHealthChecker(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
assert.NotNil(t, hc)
|
||||
assert.Equal(t, mockTester, hc.tester)
|
||||
assert.Equal(t, mockPatcher, hc.statusPatcher)
|
||||
}
|
||||
|
||||
@@ -136,9 +134,8 @@ func TestShouldCheckHealth(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
result := hc.ShouldCheckHealth(tt.repo)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
@@ -224,9 +221,8 @@ func TestHasRecentFailure(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
result := hc.HasRecentFailure(tt.healthStatus, tt.failureType)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
@@ -267,9 +263,8 @@ func TestRecordFailure(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
repo := &provisioning.Repository{
|
||||
Status: provisioning.RepositoryStatus{
|
||||
@@ -313,9 +308,8 @@ func TestRecordFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRecordFailureFunction(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
testErr := errors.New("test error")
|
||||
result := hc.recordFailure(provisioning.HealthFailureHook, testErr)
|
||||
@@ -437,23 +431,22 @@ func TestRefreshHealth(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
mockRepo := &mockRepository{
|
||||
config: &provisioning.Repository{
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Title: "Test Repository",
|
||||
Type: provisioning.LocalRepositoryType,
|
||||
},
|
||||
Status: provisioning.RepositoryStatus{
|
||||
Health: tt.existingStatus,
|
||||
},
|
||||
},
|
||||
testResult: tt.testResult,
|
||||
testError: tt.testError,
|
||||
}
|
||||
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
|
||||
if tt.testError != nil {
|
||||
mockTester.On("TestRepository", mock.Anything, mockRepo).Return(tt.testResult, tt.testError)
|
||||
} else {
|
||||
mockTester.On("TestRepository", mock.Anything, mockRepo).Return(tt.testResult, nil)
|
||||
}
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
if tt.expectPatch {
|
||||
if tt.patchError != nil {
|
||||
@@ -484,8 +477,6 @@ func TestRefreshHealth(t *testing.T) {
|
||||
assert.Equal(t, tt.testResult, testResult)
|
||||
}
|
||||
}
|
||||
|
||||
mockTester.AssertExpectations(t)
|
||||
if tt.expectPatch {
|
||||
mockPatcher.AssertExpectations(t)
|
||||
}
|
||||
@@ -564,9 +555,8 @@ func TestHasHealthStatusChanged(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockTester := mocks.NewRepositoryTester(t)
|
||||
mockPatcher := mocks.NewStatusPatcher(t)
|
||||
hc := NewHealthChecker(mockTester, mockPatcher)
|
||||
hc := NewHealthChecker(mockPatcher)
|
||||
|
||||
result := hc.hasHealthStatusChanged(tt.old, tt.new)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
@@ -576,7 +566,9 @@ func TestHasHealthStatusChanged(t *testing.T) {
|
||||
|
||||
// mockRepository implements repository.Repository interface for testing
|
||||
type mockRepository struct {
|
||||
config *provisioning.Repository
|
||||
config *provisioning.Repository
|
||||
testResult *provisioning.TestResults
|
||||
testError error
|
||||
}
|
||||
|
||||
func (m *mockRepository) Config() *provisioning.Repository {
|
||||
@@ -588,5 +580,11 @@ func (m *mockRepository) Validate() field.ErrorList {
|
||||
}
|
||||
|
||||
func (m *mockRepository) Test(ctx context.Context) (*provisioning.TestResults, error) {
|
||||
return &provisioning.TestResults{Success: true}, nil
|
||||
if m.testError != nil {
|
||||
return m.testResult, m.testError
|
||||
}
|
||||
if m.testResult != nil {
|
||||
return m.testResult, nil
|
||||
}
|
||||
return &provisioning.TestResults{Success: true, Code: 200}, nil
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
// Code generated by mockery v2.52.4. DO NOT EDIT.
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
|
||||
repository "github.com/grafana/grafana/apps/provisioning/pkg/repository"
|
||||
|
||||
v0alpha1 "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
|
||||
)
|
||||
|
||||
// RepositoryTester is an autogenerated mock type for the RepositoryTester type
|
||||
type RepositoryTester struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// TestRepository provides a mock function with given fields: ctx, repo
|
||||
func (_m *RepositoryTester) TestRepository(ctx context.Context, repo repository.Repository) (*v0alpha1.TestResults, error) {
|
||||
ret := _m.Called(ctx, repo)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for TestRepository")
|
||||
}
|
||||
|
||||
var r0 *v0alpha1.TestResults
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, repository.Repository) (*v0alpha1.TestResults, error)); ok {
|
||||
return rf(ctx, repo)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(context.Context, repository.Repository) *v0alpha1.TestResults); ok {
|
||||
r0 = rf(ctx, repo)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*v0alpha1.TestResults)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(context.Context, repository.Repository) error); ok {
|
||||
r1 = rf(ctx, repo)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// NewRepositoryTester creates a new instance of RepositoryTester. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewRepositoryTester(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *RepositoryTester {
|
||||
mock := &RepositoryTester{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
||||
@@ -68,7 +68,6 @@ func NewRepositoryController(
|
||||
repoFactory repository.Factory,
|
||||
resourceLister resources.ResourceLister,
|
||||
clients resources.ClientFactory,
|
||||
tester RepositoryTester,
|
||||
jobs jobs.Queue,
|
||||
dualwrite dualwrite.Service,
|
||||
healthChecker *HealthChecker,
|
||||
|
||||
Reference in New Issue
Block a user