Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore delegated_test.go #16180

Merged
merged 1 commit into from
Jan 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions pkg/project/registry/projectrequest/delegated/delegated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package delegated

import (
"sync"
"testing"
"time"

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

"github.com/go-openapi/errors"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
)

func TestDelegatedWait(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to split this to 2 functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. Unless I miss something. Do we really need to check 2 cases sequentially within one test method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, to test the unblock we want to ensure it blocked in the first place, so it makes sense to me to have this sequence in one test.

cache := &testRoleBindingLister{}
storage := &REST{roleBindings: cache}

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

waitReturnedCh := waitForResultChannel(storage)

select {
case <-waitReturnedCh:
t.Error("waitForRoleBinding() failed to block pending rolebinding creation")
case <-time.After(1 * time.Second):
}

cache.addAdminRolebinding()

select {
case <-waitReturnedCh:
case <-time.After(1 * time.Second):
t.Error("waitForRoleBinding() failed to unblock after rolebinding creation")
}
}

func waitForResultChannel(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) {
return ret, nil
}

func (t *testRoleBindingNamespaceLister) Get(name string) (*rbac.RoleBinding, error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.bindings[bootstrappolicy.AdminRoleName] != nil {
return t.bindings[bootstrappolicy.AdminRoleName], nil
}
return nil, errors.NotFound("could not find role " + bootstrappolicy.AdminRoleName)
}

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
}

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