diff --git a/pkg/resource/claim_scheduling_reconciler_test.go b/pkg/resource/claim_scheduling_reconciler_test.go index ea1ccf7b5..cef12f676 100644 --- a/pkg/resource/claim_scheduling_reconciler_test.go +++ b/pkg/resource/claim_scheduling_reconciler_test.go @@ -17,6 +17,7 @@ limitations under the License. package resource import ( + "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -31,7 +32,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1" "github.com/crossplaneio/crossplane-runtime/pkg/test" ) @@ -174,7 +174,6 @@ func TestClaimSchedulingReconciler(t *testing.T) { u.SetGroupVersionKind(MockGVK(&MockClass{})) u.SetName(name) u.SetUID(uid) - u.SetAnnotations(map[string]string{v1alpha1.AnnotationDefaultClassKey: v1alpha1.AnnotationDefaultClassValue}) l := o.(*unstructured.UnstructuredList) l.Items = []unstructured.Unstructured{*u} return nil @@ -218,3 +217,58 @@ func TestClaimSchedulingReconciler(t *testing.T) { }) } } + +func TestClaimSchedulingReconcilerRandomness(t *testing.T) { + classes := 10 + reconciles := 100 + refs := make([]*corev1.ObjectReference, 0) + + newClass := func(i int) unstructured.Unstructured { + u := &unstructured.Unstructured{} + u.SetUID(types.UID(strconv.Itoa(i))) + return *u + } + + m := &MockManager{ + c: &test.MockClient{ + MockGet: test.NewMockGetFn(nil, func(o runtime.Object) error { + c := o.(*MockClaim) + *c = MockClaim{MockClassSelector: MockClassSelector{Sel: &metav1.LabelSelector{}}} + return nil + }), + MockList: test.NewMockListFn(nil, func(o runtime.Object) error { + l := o.(*unstructured.UnstructuredList) + for i := 0; i < classes; i++ { + l.Items = append(l.Items, newClass(i)) + } + return nil + }), + MockUpdate: test.NewMockUpdateFn(nil, func(obj runtime.Object) error { + ls := obj.(ClassReferencer) + refs = append(refs, ls.GetClassReference()) + return nil + }), + }, + s: MockSchemeWith(&MockClaim{}), + } + + r := NewClaimSchedulingReconciler(m, + ClaimKind(MockGVK(&MockClaim{})), + ClassKind(MockGVK(&MockClass{})), + WithJitterer(func() {})) + + for i := 0; i < reconciles; i++ { + r.Reconcile(reconcile.Request{}) + } + + distribution := map[types.UID]int{} + for _, ref := range refs { + distribution[ref.UID]++ + } + + // The goal here is to test whether we're random-ish, i.e. that we're not + // picking the same class every time. + if len(distribution) < 2 { + t.Errorf("want > 1 resource classes selected, got %d", len(distribution)) + } +}