Skip to content

Commit

Permalink
Test claim scheduling randomness
Browse files Browse the repository at this point in the history
I tested this out manually and it appeared non random. Turns out it was not
broken, and just randomly selected the same class five times in a row.

Signed-off-by: Nic Cope <[email protected]>
  • Loading branch information
negz committed Oct 21, 2019
1 parent 6bd3d20 commit 1d9d376
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions pkg/resource/claim_scheduling_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package resource

import (
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 1d9d376

Please sign in to comment.