|
|
|
@ -18,7 +18,6 @@ import ( |
|
|
|
|
|
|
|
|
|
func TestRunner_Run(t *testing.T) { |
|
|
|
|
t.Run("does not crash when error on list", func(t *testing.T) { |
|
|
|
|
mockCheckService := &MockCheckService{} |
|
|
|
|
mockClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
return nil, errors.New("list error") |
|
|
|
@ -28,9 +27,15 @@ func TestRunner_Run(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mockTypesClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
return &advisorv0alpha1.CheckTypeList{Items: []advisorv0alpha1.CheckType{}}, nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
runner := &Runner{ |
|
|
|
|
checkRegistry: mockCheckService, |
|
|
|
|
client: mockClient, |
|
|
|
|
typesClient: mockTypesClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
evaluationInterval: 1 * time.Hour, |
|
|
|
|
} |
|
|
|
@ -54,29 +59,71 @@ func TestRunner_checkLastCreated_ErrorOnList(t *testing.T) { |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lastCreated, err := runner.checkLastCreated(context.Background()) |
|
|
|
|
lastCreated, err := runner.checkLastCreated(context.Background(), logging.DefaultLogger) |
|
|
|
|
assert.Error(t, err) |
|
|
|
|
assert.True(t, lastCreated.IsZero()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRunner_createChecks_ErrorOnCreate(t *testing.T) { |
|
|
|
|
mockCheckService := &MockCheckService{ |
|
|
|
|
checks: []checks.Check{ |
|
|
|
|
&mockCheck{ |
|
|
|
|
id: "check-1", |
|
|
|
|
}, |
|
|
|
|
func TestRunner_checkLastCreated_UnprocessedCheck(t *testing.T) { |
|
|
|
|
patchOperation := resource.PatchOperation{} |
|
|
|
|
identifier := resource.Identifier{} |
|
|
|
|
|
|
|
|
|
mockClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
return &advisorv0alpha1.CheckList{ |
|
|
|
|
Items: []advisorv0alpha1.Check{ |
|
|
|
|
{ |
|
|
|
|
ObjectMeta: metav1.ObjectMeta{ |
|
|
|
|
Name: "check-1", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, nil |
|
|
|
|
}, |
|
|
|
|
patchFunc: func(ctx context.Context, id resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions, into resource.Object) error { |
|
|
|
|
patchOperation = patch.Operations[0] |
|
|
|
|
identifier = id |
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
runner := &Runner{ |
|
|
|
|
client: mockClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lastCreated, err := runner.checkLastCreated(context.Background(), logging.DefaultLogger) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.True(t, lastCreated.IsZero()) |
|
|
|
|
assert.Equal(t, "check-1", identifier.Name) |
|
|
|
|
assert.Equal(t, "/metadata/annotations", patchOperation.Path) |
|
|
|
|
expectedAnnotations := map[string]string{ |
|
|
|
|
checks.StatusAnnotation: "error", |
|
|
|
|
} |
|
|
|
|
assert.Equal(t, expectedAnnotations, patchOperation.Value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRunner_createChecks_ErrorOnCreate(t *testing.T) { |
|
|
|
|
mockClient := &MockClient{ |
|
|
|
|
createFunc: func(ctx context.Context, id resource.Identifier, obj resource.Object, opts resource.CreateOptions) (resource.Object, error) { |
|
|
|
|
return nil, errors.New("create error") |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mockTypesClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
checkType := &advisorv0alpha1.CheckType{} |
|
|
|
|
checkType.Spec.Name = "check-1" |
|
|
|
|
return &advisorv0alpha1.CheckTypeList{ |
|
|
|
|
Items: []advisorv0alpha1.CheckType{*checkType}, |
|
|
|
|
}, nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
runner := &Runner{ |
|
|
|
|
checkRegistry: mockCheckService, |
|
|
|
|
client: mockClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
client: mockClient, |
|
|
|
|
typesClient: mockTypesClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := runner.createChecks(context.Background()) |
|
|
|
@ -84,23 +131,26 @@ func TestRunner_createChecks_ErrorOnCreate(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRunner_createChecks_Success(t *testing.T) { |
|
|
|
|
mockCheckService := &MockCheckService{ |
|
|
|
|
checks: []checks.Check{ |
|
|
|
|
&mockCheck{ |
|
|
|
|
id: "check-1", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
mockClient := &MockClient{ |
|
|
|
|
createFunc: func(ctx context.Context, id resource.Identifier, obj resource.Object, opts resource.CreateOptions) (resource.Object, error) { |
|
|
|
|
return &advisorv0alpha1.Check{}, nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mockTypesClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
checkType := &advisorv0alpha1.CheckType{} |
|
|
|
|
checkType.Spec.Name = "check-1" |
|
|
|
|
return &advisorv0alpha1.CheckTypeList{ |
|
|
|
|
Items: []advisorv0alpha1.CheckType{*checkType}, |
|
|
|
|
}, nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
runner := &Runner{ |
|
|
|
|
checkRegistry: mockCheckService, |
|
|
|
|
client: mockClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
client: mockClient, |
|
|
|
|
typesClient: mockTypesClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := runner.createChecks(context.Background()) |
|
|
|
@ -250,41 +300,6 @@ func Test_getMaxHistory(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Test_markUnprocessedChecksAsErrored(t *testing.T) { |
|
|
|
|
checkList := &advisorv0alpha1.CheckList{ |
|
|
|
|
Items: []advisorv0alpha1.Check{ |
|
|
|
|
{ |
|
|
|
|
ObjectMeta: metav1.ObjectMeta{ |
|
|
|
|
Name: "check-1", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
patchOperation := resource.PatchOperation{} |
|
|
|
|
identifier := resource.Identifier{} |
|
|
|
|
mockClient := &MockClient{ |
|
|
|
|
listFunc: func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) { |
|
|
|
|
return checkList, nil |
|
|
|
|
}, |
|
|
|
|
patchFunc: func(ctx context.Context, id resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions, into resource.Object) error { |
|
|
|
|
patchOperation = patch.Operations[0] |
|
|
|
|
identifier = id |
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
runner := &Runner{ |
|
|
|
|
client: mockClient, |
|
|
|
|
log: logging.DefaultLogger, |
|
|
|
|
} |
|
|
|
|
runner.markUnprocessedChecksAsErrored(context.Background(), logging.DefaultLogger) |
|
|
|
|
assert.Equal(t, "check-1", identifier.Name) |
|
|
|
|
assert.Equal(t, "/metadata/annotations", patchOperation.Path) |
|
|
|
|
expectedAnnotations := map[string]string{ |
|
|
|
|
checks.StatusAnnotation: "error", |
|
|
|
|
} |
|
|
|
|
assert.Equal(t, expectedAnnotations, patchOperation.Value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Test_getNextSendInterval(t *testing.T) { |
|
|
|
|
lastCreated := time.Now().Add(-7 * 24 * time.Hour) |
|
|
|
|
evaluationInterval := 7 * 24 * time.Hour |
|
|
|
@ -296,14 +311,6 @@ func Test_getNextSendInterval(t *testing.T) { |
|
|
|
|
assert.NotEqual(t, nextSendInterval, nextSendInterval2) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type MockCheckService struct { |
|
|
|
|
checks []checks.Check |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *MockCheckService) Checks() []checks.Check { |
|
|
|
|
return m.checks |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type MockClient struct { |
|
|
|
|
resource.Client |
|
|
|
|
listFunc func(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) |
|
|
|
@ -327,18 +334,3 @@ func (m *MockClient) Delete(ctx context.Context, identifier resource.Identifier, |
|
|
|
|
func (m *MockClient) PatchInto(ctx context.Context, identifier resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions, into resource.Object) error { |
|
|
|
|
return m.patchFunc(ctx, identifier, patch, options, into) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type mockCheck struct { |
|
|
|
|
checks.Check |
|
|
|
|
|
|
|
|
|
id string |
|
|
|
|
steps []checks.Step |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *mockCheck) ID() string { |
|
|
|
|
return m.id |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *mockCheck) Steps() []checks.Step { |
|
|
|
|
return m.steps |
|
|
|
|
} |
|
|
|
|