-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into remove_pendo_common
- Loading branch information
Showing
3 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package toolchainclustercache | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
) | ||
|
||
// CreateAndUpdateOnlyPredicate will filter out all events out of the provided namespace | ||
type namespacePredicate struct { | ||
namespace string | ||
} | ||
|
||
// Update allows events only in the given namespace | ||
func (p namespacePredicate) Update(e event.UpdateEvent) bool { | ||
return e.ObjectNew.GetNamespace() == p.namespace | ||
} | ||
|
||
// Create allows events only in the given namespace | ||
func (p namespacePredicate) Create(e event.CreateEvent) bool { | ||
return e.Object.GetNamespace() == p.namespace | ||
} | ||
|
||
// Delete allows events only in the given namespace | ||
func (p namespacePredicate) Delete(e event.DeleteEvent) bool { | ||
return e.Object.GetNamespace() == p.namespace | ||
} | ||
|
||
// Generic allows events only in the given namespace | ||
func (p namespacePredicate) Generic(e event.GenericEvent) bool { | ||
return e.Object.GetNamespace() == p.namespace | ||
} |
101 changes: 101 additions & 0 deletions
101
controllers/toolchainclustercache/namespace_predicate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package toolchainclustercache | ||
|
||
import ( | ||
"testing" | ||
|
||
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
) | ||
|
||
func TestNamespacePredicate(t *testing.T) { | ||
// given | ||
predicate := namespacePredicate{ | ||
namespace: "matching-namespace", | ||
} | ||
|
||
tests := map[string]struct { | ||
namespace string | ||
expected bool | ||
}{ | ||
"with matching namespace": { | ||
namespace: "matching-namespace", | ||
expected: true, | ||
}, | ||
|
||
"without matching namespace": { | ||
namespace: "non-matching-namespace", | ||
expected: false, | ||
}, | ||
|
||
"without any namespace": { | ||
namespace: "", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for testName, data := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
tc := &toolchainv1alpha1.ToolchainCluster{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "tc-name", | ||
Namespace: data.namespace, | ||
}, | ||
} | ||
t.Run("update event", func(t *testing.T) { | ||
// given | ||
ev := event.UpdateEvent{ | ||
ObjectNew: tc, | ||
// we don't care about the old version of the object | ||
ObjectOld: nil, | ||
} | ||
|
||
// when | ||
shouldReconcile := predicate.Update(ev) | ||
|
||
// then | ||
assert.Equal(t, data.expected, shouldReconcile) | ||
}) | ||
|
||
t.Run("create event", func(t *testing.T) { | ||
// given | ||
ev := event.CreateEvent{ | ||
Object: tc, | ||
} | ||
|
||
// when | ||
shouldReconcile := predicate.Create(ev) | ||
|
||
// then | ||
assert.Equal(t, data.expected, shouldReconcile) | ||
}) | ||
|
||
t.Run("generic event", func(t *testing.T) { | ||
// given | ||
ev := event.GenericEvent{ | ||
Object: tc, | ||
} | ||
|
||
// when | ||
shouldReconcile := predicate.Generic(ev) | ||
|
||
// then | ||
assert.Equal(t, data.expected, shouldReconcile) | ||
}) | ||
|
||
t.Run("delete event", func(t *testing.T) { | ||
// given | ||
ev := event.DeleteEvent{ | ||
Object: tc, | ||
} | ||
|
||
// when | ||
shouldReconcile := predicate.Delete(ev) | ||
|
||
// then | ||
assert.Equal(t, data.expected, shouldReconcile) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters