Skip to content

Commit

Permalink
adding lister tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robscott committed Sep 3, 2018
1 parent 154524d commit 0f9d5e1
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 4 deletions.
49 changes: 47 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ import:
version: 1.1.4
- package: github.com/spf13/cobra
version: 0.0.3
- package: github.com/stretchr/testify
version: ~1.2.2
2 changes: 1 addition & 1 deletion lookup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func List(args []string, outputFormat string) {

l := lister{
filter: filter,
clientset: *clientset,
clientset: clientset,
rbacSubjectsByScope: make(map[string]rbacSubject),
}

Expand Down
2 changes: 1 addition & 1 deletion lookup/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type simpleRoleSource struct {

type lister struct {
rbacSubjectsByScope map[string]rbacSubject
clientset kubernetes.Clientset
clientset kubernetes.Interface
filter string
}

Expand Down
189 changes: 189 additions & 0 deletions lookup/lister_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package lookup

import (
"testing"

"github.com/stretchr/testify/assert"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

testclient "k8s.io/client-go/kubernetes/fake"
)

func TestLoadRoleBindings(t *testing.T) {
l := genLister()

loadRoleBindings(t, l)

assert.Len(t, l.rbacSubjectsByScope, 0, "Expected no rbac subjects initially")

createRoleBindings(t, l)

loadRoleBindings(t, l)

assert.Len(t, l.rbacSubjectsByScope, 2, "Expected 2 rbac subjects")

expectedRbacSubject := rbacSubject{
Kind: "User",
RolesByScope: map[string][]simpleRole{
"foo": []simpleRole{{
Kind: "Role",
Name: "bar",
Source: simpleRoleSource{
Kind: "RoleBinding",
Name: "testing",
},
}},
},
}

assert.EqualValues(t, l.rbacSubjectsByScope["joe"], expectedRbacSubject)
assert.EqualValues(t, l.rbacSubjectsByScope["sue"], expectedRbacSubject)
}

func TestLoadClusterRoleBindings(t *testing.T) {
l := genLister()

loadClusterRoleBindings(t, l)

assert.Len(t, l.rbacSubjectsByScope, 0, "Expected no rbac subjects initially")

createClusterRoleBindings(t, l)

loadClusterRoleBindings(t, l)

assert.Len(t, l.rbacSubjectsByScope, 2, "Expected 2 rbac subjects")

expectedRbacSubject := rbacSubject{
Kind: "User",
RolesByScope: map[string][]simpleRole{
"cluster-wide": []simpleRole{{
Kind: "ClusterRole",
Name: "bar",
Source: simpleRoleSource{
Kind: "ClusterRoleBinding",
Name: "testing",
},
}},
},
}

assert.EqualValues(t, l.rbacSubjectsByScope["joe"], expectedRbacSubject)
assert.EqualValues(t, l.rbacSubjectsByScope["sue"], expectedRbacSubject)
}

func TestLoadAll(t *testing.T) {
l := genLister()

loadAll(t, l)

assert.Len(t, l.rbacSubjectsByScope, 0, "Expected no rbac subjects initially")

createRoleBindings(t, l)

createClusterRoleBindings(t, l)

loadAll(t, l)

assert.Len(t, l.rbacSubjectsByScope, 2, "Expected 2 rbac subjects")

expectedRbacSubject := rbacSubject{
Kind: "User",
RolesByScope: map[string][]simpleRole{
"cluster-wide": []simpleRole{{
Kind: "ClusterRole",
Name: "bar",
Source: simpleRoleSource{
Kind: "ClusterRoleBinding",
Name: "testing",
},
}},
"foo": []simpleRole{{
Kind: "Role",
Name: "bar",
Source: simpleRoleSource{
Kind: "RoleBinding",
Name: "testing",
},
}},
},
}

assert.EqualValues(t, l.rbacSubjectsByScope["joe"], expectedRbacSubject)
assert.EqualValues(t, l.rbacSubjectsByScope["sue"], expectedRbacSubject)
}

func genLister() lister {
return lister{
clientset: testclient.NewSimpleClientset(),
rbacSubjectsByScope: make(map[string]rbacSubject),
}
}

func loadAll(t *testing.T, l lister) {
err := l.loadAll()

assert.Nil(t, err, "Expected no error loading all rbac Bindings")
}

func loadRoleBindings(t *testing.T, l lister) {
err := l.loadRoleBindings()

assert.Nil(t, err, "Expected no error loading role bindings")
}

func createRoleBindings(t *testing.T, l lister) {
roleBindings := []rbacv1.RoleBinding{{
ObjectMeta: metav1.ObjectMeta{
Name: "testing",
Namespace: "foo",
},
Subjects: []rbacv1.Subject{{
Name: "joe",
Kind: "User",
}, {
Name: "sue",
Kind: "User",
}},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: "bar",
},
}}

for _, roleBinding := range roleBindings {
_, err := l.clientset.RbacV1().RoleBindings(roleBinding.Namespace).Create(&roleBinding)
assert.Nil(t, err, "Expected no error creating role bindings")
}
}

func loadClusterRoleBindings(t *testing.T, l lister) {
err := l.loadClusterRoleBindings()

assert.Nil(t, err, "Expected no error loading cluster role bindings")
}

func createClusterRoleBindings(t *testing.T, l lister) {
clusterRoleBindings := []rbacv1.ClusterRoleBinding{{
ObjectMeta: metav1.ObjectMeta{
Name: "testing",
},
Subjects: []rbacv1.Subject{{
Name: "joe",
Kind: "User",
}, {
Name: "sue",
Kind: "User",
}},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "bar",
},
}}

for _, clusterRoleBinding := range clusterRoleBindings {
_, err := l.clientset.RbacV1().ClusterRoleBindings().Create(&clusterRoleBinding)
assert.Nil(t, err, "Expected no error creating cluster role bindings")
}
}

0 comments on commit 0f9d5e1

Please sign in to comment.