Skip to content

Commit

Permalink
CrossNamespace: Testing Code Style (knative#7918)
Browse files Browse the repository at this point in the history
* split parameters

* pointer change for roles

* rolebinding pointer change

* pointer change

* unit test fix
  • Loading branch information
yijie-04 authored May 13, 2024
1 parent 4951b74 commit 4358574
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 24 deletions.
18 changes: 16 additions & 2 deletions pkg/reconciler/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,23 @@ func TestAllCases(t *testing.T) {
}}),
),
// Role
CreateRole(channelNS, "test-role", "knsubscribe", "InMemoryChannel", "messaging.knative.dev"),
CreateRole("test-role", channelNS,
WithRoleRules(
WithPolicyRule(
WithAPIGroups([]string{"messaging.knative.dev"}),
WithResources("InMemoryChannel"),
WithVerbs("knsubscribe")))),
// Rolebinding
CreateRoleBinding(channelNS, "test-role", NewServiceAccount("test-user")),
CreateRoleBinding("test-role", channelNS,
WithRoleBindingSubjects(
WithSubjects(
WithSubjectKind("ServiceAccount"),
WithSubjectName("test-user"))),
WithRoleBindingRoleRef(
WithRoleRef(
WithRoleRefAPIGroup("rbac.authorization.k8s.io"),
WithRoleRefKind("Role"),
WithRoleRefName("test-role")))),
},
Key: testNS + "/" + subscriptionName,
SkipNamespaceValidation: true,
Expand Down
89 changes: 75 additions & 14 deletions pkg/reconciler/testing/v1/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,88 @@ limitations under the License.
package testing

import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func CreateRoleBinding(namespace, roleName string, sa *corev1.ServiceAccount) *v1.RoleBinding {
return &v1.RoleBinding{
// RoleBindingOption enables further configuration of a RoleBinding.
type RoleBindingOption func(*v1.RoleBinding)
type SubjectOption func(*v1.Subject)
type RoleRefOption func(*v1.RoleRef)

func CreateRoleBinding(roleName, namespace string, o ...RoleBindingOption) *v1.RoleBinding {
rb := &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: roleName + "-binding",
Namespace: namespace,
},
Subjects: []v1.Subject{
{
Kind: "ServiceAccount",
Name: sa.Name,
},
},
RoleRef: v1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: roleName,
},
Subjects: []v1.Subject{},
RoleRef: v1.RoleRef{},
}
for _, opt := range o {
opt(rb)
}
return rb
}

func WithRoleBindingSubjects(subjects ...*v1.Subject) RoleBindingOption {
s := make([]v1.Subject, 0, len(subjects))
for _, subject := range subjects {
s = append(s, *subject)
}
return func(rb *v1.RoleBinding) {
rb.Subjects = s
}
}

func WithSubjects(o ...SubjectOption) *v1.Subject {
s := &v1.Subject{}
for _, opt := range o {
opt(s)
}
return s
}

func WithSubjectKind(kind string) SubjectOption {
return func(s *v1.Subject) {
s.Kind = kind
}
}

func WithSubjectName(name string) SubjectOption {
return func(s *v1.Subject) {
s.Name = name
}
}

func WithRoleBindingRoleRef(roleRef *v1.RoleRef) RoleBindingOption {
return func(rb *v1.RoleBinding) {
rb.RoleRef = *roleRef
}
}

func WithRoleRef(o ...RoleRefOption) *v1.RoleRef {
rr := &v1.RoleRef{}
for _, opt := range o {
opt(rr)
}
return rr
}

func WithRoleRefAPIGroup(apiGroup string) RoleRefOption {
return func(rr *v1.RoleRef) {
rr.APIGroup = apiGroup
}
}

func WithRoleRefKind(kind string) RoleRefOption {
return func(rr *v1.RoleRef) {
rr.Kind = kind
}
}

func WithRoleRefName(name string) RoleRefOption {
return func(rr *v1.RoleRef) {
rr.Name = name
}
}
56 changes: 48 additions & 8 deletions pkg/reconciler/testing/v1/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,57 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// RoleOption enables further configuration of a Role.
type RoleOption func(*v1.Role)
type PolicyRuleOption func(*v1.PolicyRule)

// createRole creates a Role in the Kubernetes cluster.
func CreateRole(namespace, roleName, verbs, resources, apiGroup string) *v1.Role {
return &v1.Role{
func CreateRole(name, namespace string, o ...RoleOption) *v1.Role {
r := &v1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: roleName,
Name: name,
Namespace: namespace,
},
Rules: []v1.PolicyRule{{
APIGroups: []string{apiGroup},
Resources: []string{resources},
Verbs: []string{verbs},
}},
Rules: []v1.PolicyRule{},
}
for _, opt := range o {
opt(r)
}
return r
}

func WithRoleRules(rules ...*v1.PolicyRule) RoleOption {
roleRules := make([]v1.PolicyRule, 0, len(rules))
for _, rule := range rules {
roleRules = append(roleRules, *rule)
}
return func(r *v1.Role) {
r.Rules = roleRules
}
}

func WithPolicyRule(o ...PolicyRuleOption) *v1.PolicyRule {
pr := &v1.PolicyRule{}
for _, opt := range o {
opt(pr)
}
return pr
}

func WithAPIGroups(apiGroups []string) PolicyRuleOption {
return func(r *v1.PolicyRule) {
r.APIGroups = apiGroups
}
}

func WithResources(resources ...string) PolicyRuleOption {
return func(r *v1.PolicyRule) {
r.Resources = resources
}
}

func WithVerbs(verbs ...string) PolicyRuleOption {
return func(r *v1.PolicyRule) {
r.Verbs = verbs
}
}

0 comments on commit 4358574

Please sign in to comment.