Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Feb 10, 2025
1 parent 1089d36 commit a330c18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
14 changes: 6 additions & 8 deletions pkg/controller/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,12 @@ func (r *Reconciler) apply(ctx context.Context, res common.PlatformObject) error
}

Check warning on line 276 in pkg/controller/reconciler/reconciler.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/reconciler/reconciler.go#L268-L276

Added lines #L268 - L276 were not covered by tests

if provisionErr != nil {
if r.Recorder != nil {
r.Recorder.Event(
res,
corev1.EventTypeWarning,
"ProvisioningError",
provisionErr.Error(),
)
}
r.Recorder.Event(
res,
corev1.EventTypeWarning,
"ProvisioningError",
provisionErr.Error(),
)

return fmt.Errorf("provisioning failed: %w", provisionErr)
}
Expand Down
51 changes: 33 additions & 18 deletions pkg/controller/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"path/filepath"
"testing"
"time"

gomegaTypes "github.com/onsi/gomega/types"
"github.com/rs/xid"
Expand All @@ -16,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
Expand Down Expand Up @@ -67,11 +69,12 @@ func createEnvTest(s *runtime.Scheme) (*envtest.Environment, error) {

func createReconciler(cli *odhClient.Client) *Reconciler {
return &Reconciler{
Client: cli,
Scheme: cli.Scheme(),
Log: ctrl.Log.WithName("controllers").WithName("test"),
Release: cluster.GetRelease(),
name: "test",
Client: cli,
Scheme: cli.Scheme(),
Log: ctrl.Log.WithName("controllers").WithName("test"),
Release: cluster.GetRelease(),
Recorder: record.NewFakeRecorder(100),
name: "test",
instanceFactory: func() (common.PlatformObject, error) {
i := &componentApi.Dashboard{
TypeMeta: ctrl.TypeMeta{
Expand Down Expand Up @@ -117,19 +120,6 @@ func TestConditions(t *testing.T) {
err = cli.Create(ctx, dsci)
g.Expect(err).NotTo(HaveOccurred())

dash := resources.GvkToUnstructured(gvk.Dashboard)
dash.SetName(componentApi.DashboardInstanceName)
dash.SetGeneration(1)

err = cli.Create(ctx, dash)
g.Expect(err).NotTo(HaveOccurred())

req := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: componentApi.DashboardInstanceName,
},
}

tests := []struct {
name string
err error
Expand Down Expand Up @@ -163,6 +153,19 @@ func TestConditions(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dash := resources.GvkToUnstructured(gvk.Dashboard)
dash.SetName(componentApi.DashboardInstanceName)
dash.SetGeneration(1)

err = cli.Create(ctx, dash)
g.Expect(err).NotTo(HaveOccurred())

req := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: componentApi.DashboardInstanceName,
},
}

cc := createReconciler(cli)
cc.AddAction(func(ctx context.Context, rr *odhtype.ReconciliationRequest) error {
return tt.err
Expand All @@ -181,6 +184,18 @@ func TestConditions(t *testing.T) {
err = cli.Get(ctx, client.ObjectKeyFromObject(di), di)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(di).Should(tt.matcher)

err = cli.Delete(ctx, di, client.PropagationPolicy(metav1.DeletePropagationBackground))
g.Expect(err).ShouldNot(HaveOccurred())

g.Eventually(func() ([]componentApi.Dashboard, error) {
l := componentApi.DashboardList{}
if err := cli.List(ctx, &l, client.InNamespace("")); err != nil {
return nil, err
}

return l.Items, nil
}).WithTimeout(10 * time.Second).Should(BeEmpty())
})
}
}

0 comments on commit a330c18

Please sign in to comment.