-
Notifications
You must be signed in to change notification settings - Fork 46
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
Showing
5 changed files
with
240 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,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") | ||
} | ||
} |