Skip to content

Commit

Permalink
unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Feb 4, 2025
1 parent 128b55c commit daf6d7e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/actions/deploy/action_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (a *Action) run(ctx context.Context, rr *odhTypes.ReconciliationRequest) er
}

controllerName := strings.ToLower(kind)
igvk := rr.Instance.GetObjectKind().GroupVersionKind()

for i := range rr.Resources {
res := rr.Resources[i]
Expand All @@ -134,7 +135,7 @@ func (a *Action) run(ctx context.Context, rr *odhTypes.ReconciliationRequest) er
default:
// Remove the previous owner reference if set, This is required during the
// transition from the old to the new operator.
if err := resources.RemoveOwnerReferences(ctx, rr.Client, current, ownedTypeIsNot(rr.Manager.GetOwnerType())); err != nil {
if err := resources.RemoveOwnerReferences(ctx, rr.Client, current, ownedTypeIsNot(&igvk)); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/actions/deploy/action_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func TestDeployOwnerRef(t *testing.T) {
Version: version.OperatorVersion{Version: semver.Version{
Major: 1, Minor: 2, Patch: 3,
}}},
Manager: manager.New(nil, nil),
Manager: manager.New(nil),
}

rr.Manager.AddGVK(gvk.ConfigMap, true)
Expand Down
14 changes: 4 additions & 10 deletions pkg/controller/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ type gvkInfo struct {
owned bool
}

func New(manager ctrl.Manager, ownerType *schema.GroupVersionKind) *Manager {
func New(manager ctrl.Manager) *Manager {
return &Manager{
m: manager,
ownerType: ownerType,
gvks: map[schema.GroupVersionKind]gvkInfo{},
m: manager,
gvks: map[schema.GroupVersionKind]gvkInfo{},
}
}

type Manager struct {
m ctrl.Manager

ownerType *schema.GroupVersionKind
gvks map[schema.GroupVersionKind]gvkInfo
}

func (m *Manager) GetOwnerType() *schema.GroupVersionKind {
return m.ownerType
gvks map[schema.GroupVersionKind]gvkInfo
}

func (m *Manager) AddGVK(gvk schema.GroupVersionKind, owned bool) {
Expand Down
21 changes: 9 additions & 12 deletions pkg/controller/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,14 @@ func NewReconciler[T common.PlatformObject](mgr manager.Manager, name string, ob
return nil, err
}

gvk, err := resources.GetGroupVersionKindForObject(mgr.GetScheme(), object)
if err != nil {
return nil, err
}

cc := Reconciler{
Client: oc,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName(name),
Recorder: mgr.GetEventRecorderFor(name),
Release: cluster.GetRelease(),
name: name,
m: odhManager.New(mgr, &gvk),
m: odhManager.New(mgr),
instanceFactory: func() (common.PlatformObject, error) {
t := reflect.TypeOf(object).Elem()
res, ok := reflect.New(t).Interface().(T)
Expand Down Expand Up @@ -281,12 +276,14 @@ 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 {
r.Recorder.Event(
res,
corev1.EventTypeWarning,
"ProvisioningError",
provisionErr.Error(),
)
if r.Recorder != nil {
r.Recorder.Event(
res,
corev1.EventTypeWarning,
"ProvisioningError",
provisionErr.Error(),
)
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/controller/reconciler/reconciler.go#L280-L286

Added lines #L280 - L286 were not covered by tests

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

gomegaTypes "github.com/onsi/gomega/types"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
"github.com/rs/xid"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
odherrors "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/errors"
odhClient "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/client"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/conditions"
odhtype "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/utils/test/matchers/jq"
Expand Down Expand Up @@ -82,10 +82,13 @@ func createReconciler(cli *odhClient.Client) *Reconciler {

return i, nil
},
conditionsManagerFactory: func(accessor common.ConditionsAccessor) *conditions.Manager {
return conditions.NewManager(accessor, status.ConditionTypeReady)
},
}
}

func TestAvailableCondition(t *testing.T) {
func TestConditions(t *testing.T) {
ctx := context.Background()

g := NewWithT(t)
Expand Down Expand Up @@ -133,30 +136,27 @@ func TestAvailableCondition(t *testing.T) {
matcher gomegaTypes.GomegaMatcher
}{
{
name: "available",
name: "ready",
err: nil,
matcher: And(
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, conditionsv1.ConditionAvailable, metav1.ConditionTrue),
jq.Match(`.status.conditions[] | select(.type == "%s") | .reason == "%s"`, conditionsv1.ConditionAvailable, conditionsv1.ConditionAvailable),
jq.Match(`.status.conditions[] | select(.type == "%s") | .message == "%s"`, conditionsv1.ConditionAvailable, conditionsv1.ConditionAvailable),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeReady, metav1.ConditionTrue),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeProvisioningSucceeded, metav1.ConditionTrue),
),
},
{
name: "stop",
err: odherrors.NewStopError("stop"),
matcher: And(
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, conditionsv1.ConditionAvailable, metav1.ConditionFalse),
jq.Match(`.status.conditions[] | select(.type == "%s") | .reason == "%s"`, conditionsv1.ConditionAvailable, "Degraded"),
jq.Match(`.status.conditions[] | select(.type == "%s") | .message == "%s"`, conditionsv1.ConditionAvailable, "stop"),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeReady, metav1.ConditionFalse),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeProvisioningSucceeded, metav1.ConditionFalse),
),
},
{
name: "failure",
err: errors.New("failure"),
matcher: And(
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, conditionsv1.ConditionAvailable, metav1.ConditionFalse),
jq.Match(`.status.conditions[] | select(.type == "%s") | .reason == "%s"`, conditionsv1.ConditionAvailable, status.ReconcileFailed),
jq.Match(`.status.conditions[] | select(.type == "%s") | .message == "%s"`, conditionsv1.ConditionAvailable, "failure"),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeReady, metav1.ConditionFalse),
jq.Match(`.status.conditions[] | select(.type == "%s") | .status == "%s"`, status.ConditionTypeProvisioningSucceeded, metav1.ConditionFalse),
),
},
}
Expand Down

0 comments on commit daf6d7e

Please sign in to comment.