Skip to content

Commit 50e07fc

Browse files
committed
add policy cache test
1 parent d01bfa4 commit 50e07fc

File tree

4 files changed

+124
-15
lines changed

4 files changed

+124
-15
lines changed

pkg/authorization/cache/policy_cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ func (c *PolicyCache) ListPolicyBindings(ctx kapi.Context, labels, fields labels
123123
return nil, err
124124
}
125125

126-
ret := &authorizationapi.PolicyBindingList{}
126+
ret := &authorizationapi.PolicyBindingList{
127+
Items: make([]authorizationapi.PolicyBinding, 0, len(bindings)),
128+
}
127129
for i := range bindings {
128130
ret.Items = append(ret.Items, *bindings[i].(*authorizationapi.PolicyBinding))
129131
}

pkg/authorization/registry/test/policy.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ func (r *PolicyRegistry) ListPolicies(ctx kapi.Context, labels, fields klabels.S
3434
}
3535

3636
namespace := kapi.NamespaceValue(ctx)
37-
if len(namespace) == 0 {
38-
return nil, errors.New("invalid request. Namespace parameter required.")
39-
}
40-
4137
list := make([]authorizationapi.Policy, 0)
42-
if namespacedPolicies, ok := r.Policies[namespace]; ok {
43-
for _, curr := range namespacedPolicies {
44-
list = append(list, curr)
38+
39+
if namespace == kapi.NamespaceAll {
40+
for _, curr := range r.Policies {
41+
for _, policy := range curr {
42+
list = append(list, policy)
43+
}
4544
}
4645

46+
} else {
47+
if namespacedPolicies, ok := r.Policies[namespace]; ok {
48+
for _, curr := range namespacedPolicies {
49+
list = append(list, curr)
50+
}
51+
}
4752
}
4853

4954
return &authorizationapi.PolicyList{

pkg/authorization/registry/test/policybinding.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ func (r *PolicyBindingRegistry) ListPolicyBindings(ctx kapi.Context, labels, fie
3434
}
3535

3636
namespace := kapi.NamespaceValue(ctx)
37-
if len(namespace) == 0 {
38-
return nil, errors.New("invalid request. Namespace parameter required.")
39-
}
40-
4137
list := make([]authorizationapi.PolicyBinding, 0)
42-
if namespacedBindings, ok := r.PolicyBindings[namespace]; ok {
43-
for _, curr := range namespacedBindings {
44-
list = append(list, curr)
38+
39+
if namespace == kapi.NamespaceAll {
40+
for _, curr := range r.PolicyBindings {
41+
for _, binding := range curr {
42+
list = append(list, binding)
43+
}
4544
}
4645

46+
} else {
47+
if namespacedBindings, ok := r.PolicyBindings[namespace]; ok {
48+
for _, curr := range namespacedBindings {
49+
list = append(list, curr)
50+
}
51+
}
4752
}
4853

4954
return &authorizationapi.PolicyBindingList{

test/integration/policy_cache_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
8+
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
9+
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
10+
11+
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
12+
policycache "github.com/openshift/origin/pkg/authorization/cache"
13+
testregistry "github.com/openshift/origin/pkg/authorization/registry/test"
14+
)
15+
16+
func TestPolicyGet(t *testing.T) {
17+
policyRegistry := testregistry.NewPolicyRegistry(testPolicies(), nil)
18+
bindingRegistry := testregistry.NewPolicyBindingRegistry(testBindings(), nil)
19+
20+
policyCache := policycache.NewPolicyCache(bindingRegistry, policyRegistry)
21+
policyCache.Run(1 * time.Second)
22+
23+
time.Sleep(50 * time.Millisecond)
24+
25+
ctx := kapi.WithNamespace(kapi.NewContext(), "mallet")
26+
policy, err := policyCache.GetPolicy(ctx, authorizationapi.PolicyName)
27+
if err != nil {
28+
t.Errorf("Unexpected error: %v", err)
29+
}
30+
if policy == nil {
31+
t.Errorf("Missing policy")
32+
}
33+
34+
bindings, err := policyCache.ListPolicyBindings(ctx, labels.Everything(), labels.Everything())
35+
if err != nil {
36+
t.Errorf("Unexpected error: %v", err)
37+
}
38+
if len(bindings.Items) != 1 {
39+
t.Errorf("Unexpected bindings: %#v", bindings)
40+
}
41+
}
42+
43+
func testPolicies() []authorizationapi.Policy {
44+
return []authorizationapi.Policy{
45+
{
46+
ObjectMeta: kapi.ObjectMeta{
47+
Name: authorizationapi.PolicyName,
48+
Namespace: "mallet",
49+
},
50+
Roles: map[string]authorizationapi.Role{},
51+
}}
52+
}
53+
func testBindings() []authorizationapi.PolicyBinding {
54+
return []authorizationapi.PolicyBinding{
55+
{
56+
ObjectMeta: kapi.ObjectMeta{
57+
Name: "mallet",
58+
Namespace: "mallet",
59+
},
60+
RoleBindings: map[string]authorizationapi.RoleBinding{
61+
"projectAdmins": {
62+
ObjectMeta: kapi.ObjectMeta{
63+
Name: "projectAdmins",
64+
Namespace: "mallet",
65+
},
66+
RoleRef: kapi.ObjectReference{
67+
Name: "admin",
68+
Namespace: "mallet",
69+
},
70+
Users: util.NewStringSet("Matthew"),
71+
},
72+
"viewers": {
73+
ObjectMeta: kapi.ObjectMeta{
74+
Name: "viewers",
75+
Namespace: "mallet",
76+
},
77+
RoleRef: kapi.ObjectReference{
78+
Name: "view",
79+
Namespace: "mallet",
80+
},
81+
Users: util.NewStringSet("Victor"),
82+
},
83+
"editors": {
84+
ObjectMeta: kapi.ObjectMeta{
85+
Name: "editors",
86+
Namespace: "mallet",
87+
},
88+
RoleRef: kapi.ObjectReference{
89+
Name: "edit",
90+
Namespace: "mallet",
91+
},
92+
Users: util.NewStringSet("Edgar"),
93+
},
94+
},
95+
},
96+
}
97+
}

0 commit comments

Comments
 (0)