From 514c74a620ce170102c659b1d4e584a5cdf9cc5b Mon Sep 17 00:00:00 2001 From: Tyler Gillson Date: Thu, 16 Nov 2023 11:43:45 -0700 Subject: [PATCH] test: add coverage Signed-off-by: Tyler Gillson --- internal/test/controller_runtime_client.go | 15 ++++- .../validation_result_test.go | 55 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/internal/test/controller_runtime_client.go b/internal/test/controller_runtime_client.go index 7f9187a2..c0998f19 100644 --- a/internal/test/controller_runtime_client.go +++ b/internal/test/controller_runtime_client.go @@ -33,12 +33,23 @@ func (m SubResourceMock) Patch(ctx context.Context, obj client.Object, patch cli type ClientMock struct { CreateErrors []error + GetErrors []error UpdateErrors []error SubResourceMock } func (m ClientMock) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { - return nil + var err error + var errs []error + if m.GetErrors != nil { + err, errs = m.GetErrors[0], m.GetErrors[1:] + } + m = ClientMock{ + CreateErrors: m.CreateErrors, + GetErrors: errs, + UpdateErrors: m.UpdateErrors, + } + return err } func (m ClientMock) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { return nil @@ -51,6 +62,7 @@ func (m ClientMock) Create(ctx context.Context, obj client.Object, opts ...clien } m = ClientMock{ CreateErrors: errs, + GetErrors: m.GetErrors, UpdateErrors: m.UpdateErrors, } return err @@ -66,6 +78,7 @@ func (m ClientMock) Update(ctx context.Context, obj client.Object, opts ...clien } m = ClientMock{ CreateErrors: m.CreateErrors, + GetErrors: m.GetErrors, UpdateErrors: errs, } return err diff --git a/pkg/validationresult/validation_result_test.go b/pkg/validationresult/validation_result_test.go index cc4e3fb2..b9aa93e6 100644 --- a/pkg/validationresult/validation_result_test.go +++ b/pkg/validationresult/validation_result_test.go @@ -101,15 +101,24 @@ func TestHandleNewValidationResult(t *testing.T) { res: vr([]corev1.ConditionStatus{corev1.ConditionFalse}, v1alpha1.ValidationFailed, nil), expected: errors.New("creation failed"), }, + { + name: "Fail (get)", + client: test.ClientMock{ + GetErrors: []error{errors.New("get failed")}, + SubResourceMock: test.SubResourceMock{}, + }, + res: vr([]corev1.ConditionStatus{corev1.ConditionFalse}, v1alpha1.ValidationFailed, nil), + expected: errors.New("get failed"), + }, { name: "Fail (status update)", client: test.ClientMock{ SubResourceMock: test.SubResourceMock{ - UpdateErrors: []error{errors.New("update failed")}, + UpdateErrors: []error{errors.New("status update failed")}, }, }, res: vr(nil, v1alpha1.ValidationSucceeded, nil), - expected: errors.New("update failed"), + expected: errors.New("status update failed"), }, } for _, c := range cs { @@ -121,6 +130,48 @@ func TestHandleNewValidationResult(t *testing.T) { } } +func TestSafeUpdateValidationResult(t *testing.T) { + cs := []struct { + name string + client test.ClientMock + nn ktypes.NamespacedName + res *types.ValidationResult + resErr error + }{ + { + name: "Pass", + client: test.ClientMock{}, + nn: ktypes.NamespacedName{Name: "", Namespace: ""}, + res: res(corev1.ConditionTrue, v1alpha1.ValidationSucceeded), + resErr: nil, + }, + { + name: "Fail (get)", + client: test.ClientMock{ + GetErrors: []error{errors.New("get failed")}, + }, + nn: ktypes.NamespacedName{Name: "", Namespace: ""}, + res: res(corev1.ConditionTrue, v1alpha1.ValidationSucceeded), + resErr: errors.New("get failed"), + }, + { + name: "Fail (update)", + client: test.ClientMock{ + SubResourceMock: test.SubResourceMock{ + UpdateErrors: []error{errors.New("status update failed")}, + }, + }, + nn: ktypes.NamespacedName{Name: "", Namespace: ""}, + res: res(corev1.ConditionTrue, v1alpha1.ValidationSucceeded), + resErr: errors.New("status update failed"), + }, + } + for _, c := range cs { + t.Log(c.name) + SafeUpdateValidationResult(c.client, c.nn, c.res, c.resErr, logr.Logger{}) + } +} + func TestUpdateValidationResult(t *testing.T) { cs := []struct { name string