Skip to content

Commit

Permalink
Restore delegated_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Rogers committed Sep 7, 2017
1 parent 074acdc commit 6bf7af4
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions pkg/project/registry/projectrequest/delegated/delegated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package delegated

import (
"sync"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
rbac "k8s.io/kubernetes/pkg/apis/rbac"
rbaclisters "k8s.io/kubernetes/pkg/client/listers/rbac/internalversion"

"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
)

func TestDelegatedWait(t *testing.T) {
cache := &testRoleBindingLister{}
storage := &REST{roleBindings: cache}

cache.namespacelister = &testRoleBindingNamespaceLister{}
cache.namespacelister.bindings = map[string]*rbac.RoleBinding{}
cache.namespacelister.bindings["anything"] = nil

callReturnedCh := testWait(storage)

select {
case <-callReturnedCh:
t.Errorf("too fast, should have blocked")
case <-time.After(1 * time.Second):
}

func() {
cache.namespacelister.lock.Lock()
defer cache.namespacelister.lock.Unlock()
cache.namespacelister.bindings[bootstrappolicy.AdminRoleName] = &rbac.RoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: bootstrappolicy.AdminRoleName},
}
}()

select {
case <-callReturnedCh:
case <-time.After(1 * time.Second):
t.Errorf("too slow, should have returned")
}
}

func testWait(storage *REST) chan struct{} {
ret := make(chan struct{})

go func() {
storage.waitForRoleBinding("foo", bootstrappolicy.AdminRoleName)
close(ret)
}()

return ret
}

type testRoleBindingNamespaceLister struct {
bindings map[string]*rbac.RoleBinding
lock sync.Mutex
}

func (t *testRoleBindingNamespaceLister) List(selector labels.Selector) (ret []*rbac.RoleBinding, err error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.bindings[bootstrappolicy.AdminRoleName] == nil {
return ret, nil
}
ret = append(ret, t.bindings[bootstrappolicy.AdminRoleName])
return ret, nil
}

func (t *testRoleBindingNamespaceLister) Get(name string) (*rbac.RoleBinding, error) {
return nil, nil
}

type testRoleBindingLister struct {
namespacelister *testRoleBindingNamespaceLister
}

func (t *testRoleBindingLister) RoleBindings(namespace string) rbaclisters.RoleBindingNamespaceLister {
return t.namespacelister
}

func (t *testRoleBindingLister) List(selector labels.Selector) ([]*rbac.RoleBinding, error) {
return nil, nil
}

0 comments on commit 6bf7af4

Please sign in to comment.