Skip to content

Commit

Permalink
make sonarcloud happy
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Jan 8, 2024
1 parent b3da43a commit 9a690da
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,27 +540,7 @@ func (r *PlacementRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
clusterPred := getClusterPreds()

// Watch changes for AddonDeploymentConfig
AddonDeploymentPred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetName() == defaultAddonDeploymentConfig.Name &&
e.ObjectNew.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is updated")
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
if e.Object.GetName() == defaultAddonDeploymentConfig.Name &&
e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is deleted")
return true
}
return false
},
}
AddonDeploymentPred := GetAddOnDeploymentPredicates()

obsAddonPred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ func getClusterPreds() predicate.Funcs {
DeleteFunc: deleteFunc,
}
}

func GetAddOnDeploymentPredicates() predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetName() == defaultAddonDeploymentConfig.Name &&
e.ObjectNew.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is updated")
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
if e.Object.GetName() == defaultAddonDeploymentConfig.Name &&
e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is deleted")
return true
}
return false
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package placementrule

import (
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
"testing"
"time"

Expand Down Expand Up @@ -146,3 +147,68 @@ func TestClusterPred(t *testing.T) {
})
}
}

func TestAddonDeploymentPredicate(t *testing.T) {
name := "test-obj"
caseList := []struct {
caseName string
namespace string
expectedCreate bool
expectedUpdate bool
expectedDelete bool
}{
{
caseName: "Create AddonDeploymentConfig",
namespace: testNamespace,
expectedCreate: true,
expectedDelete: true,
expectedUpdate: true,
},
}

defaultAddonDeploymentConfig = &addonv1alpha1.AddOnDeploymentConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: addonv1alpha1.AddOnDeploymentConfigSpec{
ProxyConfig: addonv1alpha1.ProxyConfig{
HTTPProxy: "http://foo.com",
HTTPSProxy: "https://foo.com",
NoProxy: "bar.com",
},
},
}
for _, c := range caseList {
t.Run(c.caseName, func(t *testing.T) {
pred := GetAddOnDeploymentPredicates()
createEvent := event.CreateEvent{
Object: defaultAddonDeploymentConfig,
}

if c.expectedCreate {
if !pred.CreateFunc(createEvent) {
t.Fatalf("pre func return false on applied createevent in case: (%v)", c.caseName)
}
}

updateEvent := event.UpdateEvent{
ObjectNew: defaultAddonDeploymentConfig,
}
if c.expectedUpdate {
if !pred.UpdateFunc(updateEvent) {
t.Fatalf("pre func return false on applied update event in case: (%v)", c.caseName)
}
}

deleteEvent := event.DeleteEvent{
Object: defaultAddonDeploymentConfig,
}
if c.expectedDelete {
if !pred.DeleteFunc(deleteEvent) {
t.Fatalf("pre func return false on applied delete event in case: (%v)", c.caseName)
}
}
})
}
}

0 comments on commit 9a690da

Please sign in to comment.