-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Rogers
committed
Sep 7, 2017
1 parent
074acdc
commit 6bf7af4
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
pkg/project/registry/projectrequest/delegated/delegated_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,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 | ||
} |