-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnamespacelister_test.go
195 lines (184 loc) · 5.43 KB
/
namespacelister_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main_test
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
namespacelister "github.com/konflux-ci/namespace-lister"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
var _ = Describe("Namespacelister", func() {
var ctx context.Context
BeforeEach(func(tctx context.Context) {
ctx = tctx //nolint:fatcontext
})
DescribeTable("when listing namespaces", func(
nn corev1.NamespaceList,
cr rbacv1.ClusterRoleList,
crb rbacv1.ClusterRoleBindingList,
r rbacv1.RoleList,
rb rbacv1.RoleBindingList,
expected corev1.NamespaceList,
) {
// given
reader := fake.NewClientBuilder().WithLists(&nn, &cr, &crb, &r, &rb).Build()
authorizer := namespacelister.NewAuthorizer(ctx, reader)
nsl := namespacelister.NewNamespaceLister(reader, authorizer)
// when
ann, err := nsl.ListNamespaces(ctx, "user")
// then
Expect(err).NotTo(HaveOccurred())
Expect(&expected).To(BeEquivalentTo(ann))
},
Entry("returns no namespaces if no namespaces exist",
corev1.NamespaceList{},
rbacv1.ClusterRoleList{},
rbacv1.ClusterRoleBindingList{},
rbacv1.RoleList{},
rbacv1.RoleBindingList{},
corev1.NamespaceList{Items: []corev1.Namespace{}},
),
Entry("returns no namespaces if no clusterroles or roles exist",
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "myns-2"}},
}},
rbacv1.ClusterRoleList{},
rbacv1.ClusterRoleBindingList{},
rbacv1.RoleList{},
rbacv1.RoleBindingList{},
corev1.NamespaceList{Items: []corev1.Namespace{}},
),
Entry("returns no namespace if a clusterrole exists but no clusterrolebinding",
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "myns-2"}},
}},
rbacv1.ClusterRoleList{Items: []rbacv1.ClusterRole{
{
ObjectMeta: metav1.ObjectMeta{Name: "ns-get"},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Verbs: []string{"get"},
Resources: []string{"namespaces"},
ResourceNames: []string{"myns-1"},
},
},
},
}},
rbacv1.ClusterRoleBindingList{},
rbacv1.RoleList{},
rbacv1.RoleBindingList{},
corev1.NamespaceList{Items: []corev1.Namespace{}},
),
Entry("returns the namespace if both the clusterrole and a valid clusterrolebinding exist",
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "myns-2"}},
}},
rbacv1.ClusterRoleList{Items: []rbacv1.ClusterRole{
{
ObjectMeta: metav1.ObjectMeta{Name: "ns-get"},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Verbs: []string{"get"},
Resources: []string{"namespaces"},
ResourceNames: []string{"myns-1"},
},
},
},
}},
rbacv1.ClusterRoleBindingList{Items: []rbacv1.ClusterRoleBinding{
{
ObjectMeta: metav1.ObjectMeta{Name: "ns-get:user"},
Subjects: []rbacv1.Subject{
{
APIGroup: rbacv1.GroupName,
Kind: "User",
Name: "user",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
APIGroup: rbacv1.GroupName,
Name: "ns-get",
},
},
}},
rbacv1.RoleList{},
rbacv1.RoleBindingList{},
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1", ResourceVersion: "999"}},
}},
),
Entry("returns no namespace if the role exists but no rolebinding",
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "myns-2"}},
}},
rbacv1.ClusterRoleList{},
rbacv1.ClusterRoleBindingList{},
rbacv1.RoleList{Items: []rbacv1.Role{
{
ObjectMeta: metav1.ObjectMeta{Name: "ns-get", Namespace: "myns-1"},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Verbs: []string{"get"},
Resources: []string{"namespaces"},
},
},
},
}},
rbacv1.RoleBindingList{},
corev1.NamespaceList{Items: []corev1.Namespace{}},
),
Entry("returns a namespace if both the role and the rolebinding exist",
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "myns-2"}},
}},
rbacv1.ClusterRoleList{},
rbacv1.ClusterRoleBindingList{},
rbacv1.RoleList{Items: []rbacv1.Role{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns-get",
Namespace: "myns-1",
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Verbs: []string{"get"},
Resources: []string{"namespaces"},
},
},
},
}},
rbacv1.RoleBindingList{Items: []rbacv1.RoleBinding{
{
ObjectMeta: metav1.ObjectMeta{Name: "ns-get:user", Namespace: "myns-1"},
Subjects: []rbacv1.Subject{
{
APIGroup: rbacv1.GroupName,
Kind: "User",
Name: "user",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
APIGroup: rbacv1.GroupName,
Name: "ns-get",
},
},
}},
corev1.NamespaceList{Items: []corev1.Namespace{
{ObjectMeta: metav1.ObjectMeta{Name: "myns-1", ResourceVersion: "999"}},
}},
),
)
})