Skip to content

Commit

Permalink
test: add coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson committed Nov 16, 2023
1 parent da81af2 commit 514c74a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
15 changes: 14 additions & 1 deletion internal/test/controller_runtime_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
55 changes: 53 additions & 2 deletions pkg/validationresult/validation_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 514c74a

Please sign in to comment.