Files
open-cluster-management/pkg/helpers/testing/helpers.go
Yang Le 3d64c80b75 add scheduling controller
Signed-off-by: Yang Le <yangle@redhat.com>
2021-05-18 18:16:31 +08:00

69 lines
1.9 KiB
Go

package testing
import (
"testing"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clienttesting "k8s.io/client-go/testing"
"k8s.io/client-go/util/workqueue"
)
type FakeSyncContext struct {
queueKey string
queue workqueue.RateLimitingInterface
recorder events.Recorder
}
func (f FakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue }
func (f FakeSyncContext) QueueKey() string { return f.queueKey }
func (f FakeSyncContext) Recorder() events.Recorder { return f.recorder }
func NewFakeSyncContext(t *testing.T, queueKey string) *FakeSyncContext {
return &FakeSyncContext{
queueKey: queueKey,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
recorder: eventstesting.NewTestingEventRecorder(t),
}
}
// AssertActions asserts the actual actions have the expected action verb
func AssertActions(t *testing.T, actualActions []clienttesting.Action, expectedVerbs ...string) {
if len(actualActions) != len(expectedVerbs) {
t.Fatalf("expected %d call but got: %#v", len(expectedVerbs), actualActions)
}
for i, expected := range expectedVerbs {
if actualActions[i].GetVerb() != expected {
t.Errorf("expected %s action but got: %#v", expected, actualActions[i])
}
}
}
// AssertNoActions asserts no actions are happened
func AssertNoActions(t *testing.T, actualActions []clienttesting.Action) {
AssertActions(t, actualActions)
}
func HasCondition(conditions []metav1.Condition, expectedType, expectedReason string, expectedStatus metav1.ConditionStatus) bool {
found := false
for _, condition := range conditions {
if condition.Type != expectedType {
continue
}
found = true
if condition.Status != expectedStatus {
return false
}
if condition.Reason != expectedReason {
return false
}
return true
}
return found
}