Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
aec3d55c
Commit
aec3d55c
authored
Feb 02, 2018
by
WanLinghao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add UT test to PolicyRuleBuilder in file
./pkg/apis/rbac/helpers_test.go
parent
62b58e62
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
0 deletions
+129
-0
helpers_test.go
pkg/apis/rbac/helpers_test.go
+129
-0
No files found.
pkg/apis/rbac/helpers_test.go
View file @
aec3d55c
...
@@ -175,3 +175,132 @@ func TestResourceMatches(t *testing.T) {
...
@@ -175,3 +175,132 @@ func TestResourceMatches(t *testing.T) {
})
})
}
}
}
}
func
TestPolicyRuleBuilder
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
testName
string
verbs
[]
string
groups
[]
string
resources
[]
string
names
[]
string
urls
[]
string
expected
bool
policyRule
rbac
.
PolicyRule
}{
{
testName
:
"all empty"
,
verbs
:
nil
,
groups
:
nil
,
resources
:
nil
,
names
:
nil
,
urls
:
nil
,
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"normal resource case"
,
verbs
:
[]
string
{
"get"
},
groups
:
[]
string
{
""
},
resources
:
[]
string
{
"pod"
},
names
:
[]
string
{
"gakki"
},
urls
:
nil
,
expected
:
true
,
policyRule
:
rbac
.
PolicyRule
{
Verbs
:
[]
string
{
"get"
},
APIGroups
:
[]
string
{
""
},
Resources
:
[]
string
{
"pod"
},
ResourceNames
:
[]
string
{
"gakki"
},
NonResourceURLs
:
[]
string
{},
},
},
{
testName
:
"normal noResourceURLs case"
,
verbs
:
[]
string
{
"get"
},
groups
:
nil
,
resources
:
nil
,
names
:
nil
,
urls
:
[]
string
{
"/api/registry/healthz"
},
expected
:
true
,
policyRule
:
rbac
.
PolicyRule
{
Verbs
:
[]
string
{
"get"
},
APIGroups
:
[]
string
{},
Resources
:
[]
string
{},
ResourceNames
:
[]
string
{},
NonResourceURLs
:
[]
string
{
"/api/registry/healthz"
},
},
},
{
testName
:
"nonResourceURLs with no-empty groups"
,
verbs
:
[]
string
{
"get"
},
groups
:
[]
string
{
""
},
resources
:
nil
,
names
:
nil
,
urls
:
[]
string
{
"/api/registry/healthz"
},
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"nonResourceURLs with no-empty resources"
,
verbs
:
[]
string
{
"get"
},
groups
:
nil
,
resources
:
[]
string
{
"deployments"
,
"secrets"
},
names
:
nil
,
urls
:
[]
string
{
"/api/registry/healthz"
},
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"nonResourceURLs with no-empty resourceNames"
,
verbs
:
[]
string
{
"get"
},
groups
:
nil
,
resources
:
nil
,
names
:
[]
string
{
"gakki"
},
urls
:
[]
string
{
"/api/registry/healthz"
},
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"resource without apiGroups"
,
verbs
:
[]
string
{
"get"
},
groups
:
nil
,
resources
:
[]
string
{
"pod"
},
names
:
[]
string
{
""
},
urls
:
nil
,
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"resourceNames with illegal verb"
,
verbs
:
[]
string
{
"list"
,
"watch"
,
"create"
,
"deletecollection"
},
groups
:
[]
string
{
""
},
resources
:
[]
string
{
"pod"
},
names
:
[]
string
{
"gakki"
},
urls
:
nil
,
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
{
testName
:
"no nonResourceURLs nor resources"
,
verbs
:
[]
string
{
"get"
},
groups
:
[]
string
{
"rbac.authorization.k8s.io"
},
resources
:
nil
,
names
:
[]
string
{
"gakki"
},
urls
:
nil
,
expected
:
false
,
policyRule
:
rbac
.
PolicyRule
{},
},
}
for
_
,
tc
:=
range
tests
{
actual
,
err
:=
rbac
.
NewRule
(
tc
.
verbs
...
)
.
Groups
(
tc
.
groups
...
)
.
Resources
(
tc
.
resources
...
)
.
Names
(
tc
.
names
...
)
.
URLs
(
tc
.
urls
...
)
.
Rule
()
if
err
!=
nil
{
if
tc
.
expected
{
t
.
Error
(
err
)
}
else
{
continue
}
}
if
!
reflect
.
DeepEqual
(
actual
,
tc
.
policyRule
)
{
t
.
Errorf
(
"Expected %s got %s."
,
tc
.
policyRule
,
actual
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment