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
0a750599
Commit
0a750599
authored
Dec 22, 2016
by
Kubernetes Submit Queue
Committed by
GitHub
Dec 22, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #37188 from NickrenREN/context_test
Automatic merge from submit-queue (batch tested with PRs 39006, 39078, 37188, 39118) add test functions in context_test.go
parents
7f60e1dc
00123c34
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
8 deletions
+101
-8
BUILD
pkg/api/BUILD
+1
-0
context_test.go
pkg/api/context_test.go
+100
-8
No files found.
pkg/api/BUILD
View file @
0a750599
...
@@ -96,6 +96,7 @@ go_test(
...
@@ -96,6 +96,7 @@ go_test(
"//pkg/apis/extensions:go_default_library",
"//pkg/apis/extensions:go_default_library",
"//pkg/apis/extensions/v1beta1:go_default_library",
"//pkg/apis/extensions/v1beta1:go_default_library",
"//pkg/apis/meta/v1:go_default_library",
"//pkg/apis/meta/v1:go_default_library",
"//pkg/auth/user:go_default_library",
"//pkg/conversion:go_default_library",
"//pkg/conversion:go_default_library",
"//pkg/runtime:go_default_library",
"//pkg/runtime:go_default_library",
"//pkg/runtime/schema:go_default_library",
"//pkg/runtime/schema:go_default_library",
...
...
pkg/api/context_test.go
View file @
0a750599
...
@@ -20,6 +20,8 @@ import (
...
@@ -20,6 +20,8 @@ import (
"testing"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/auth/user"
"k8s.io/kubernetes/pkg/types"
)
)
// TestNamespaceContext validates that a namespace can be get/set on a context object
// TestNamespaceContext validates that a namespace can be get/set on a context object
...
@@ -27,16 +29,16 @@ func TestNamespaceContext(t *testing.T) {
...
@@ -27,16 +29,16 @@ func TestNamespaceContext(t *testing.T) {
ctx
:=
api
.
NewDefaultContext
()
ctx
:=
api
.
NewDefaultContext
()
result
,
ok
:=
api
.
NamespaceFrom
(
ctx
)
result
,
ok
:=
api
.
NamespaceFrom
(
ctx
)
if
!
ok
{
if
!
ok
{
t
.
Error
f
(
"Error getting namespace"
)
t
.
Fatal
f
(
"Error getting namespace"
)
}
}
if
api
.
NamespaceDefault
!=
result
{
if
api
.
NamespaceDefault
!=
result
{
t
.
Errorf
(
"Expected: %v, Actual: %v
"
,
api
.
NamespaceDefault
,
result
)
t
.
Fatalf
(
"Expected: %s, Actual: %s
"
,
api
.
NamespaceDefault
,
result
)
}
}
ctx
=
api
.
NewContext
()
ctx
=
api
.
NewContext
()
result
,
ok
=
api
.
NamespaceFrom
(
ctx
)
result
,
ok
=
api
.
NamespaceFrom
(
ctx
)
if
ok
{
if
ok
{
t
.
Error
f
(
"Should not be ok because there is no namespace on the context"
)
t
.
Fatal
f
(
"Should not be ok because there is no namespace on the context"
)
}
}
}
}
...
@@ -46,23 +48,113 @@ func TestValidNamespace(t *testing.T) {
...
@@ -46,23 +48,113 @@ func TestValidNamespace(t *testing.T) {
namespace
,
_
:=
api
.
NamespaceFrom
(
ctx
)
namespace
,
_
:=
api
.
NamespaceFrom
(
ctx
)
resource
:=
api
.
ReplicationController
{}
resource
:=
api
.
ReplicationController
{}
if
!
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
if
!
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
t
.
Error
f
(
"expected success"
)
t
.
Fatal
f
(
"expected success"
)
}
}
if
namespace
!=
resource
.
Namespace
{
if
namespace
!=
resource
.
Namespace
{
t
.
Error
f
(
"expected resource to have the default namespace assigned during validation"
)
t
.
Fatal
f
(
"expected resource to have the default namespace assigned during validation"
)
}
}
resource
=
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"other"
}}
resource
=
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"other"
}}
if
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
if
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
t
.
Error
f
(
"Expected error that resource and context errors do not match because resource has different namespace"
)
t
.
Fatal
f
(
"Expected error that resource and context errors do not match because resource has different namespace"
)
}
}
ctx
=
api
.
NewContext
()
ctx
=
api
.
NewContext
()
if
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
if
api
.
ValidNamespace
(
ctx
,
&
resource
.
ObjectMeta
)
{
t
.
Error
f
(
"Expected error that resource and context errors do not match since context has no namespace"
)
t
.
Fatal
f
(
"Expected error that resource and context errors do not match since context has no namespace"
)
}
}
ctx
=
api
.
NewContext
()
ctx
=
api
.
NewContext
()
ns
:=
api
.
NamespaceValue
(
ctx
)
ns
:=
api
.
NamespaceValue
(
ctx
)
if
ns
!=
""
{
if
ns
!=
""
{
t
.
Errorf
(
"Expected the empty string"
)
t
.
Fatalf
(
"Expected the empty string"
)
}
}
//TestUserContext validates that a userinfo can be get/set on a context object
func
TestUserContext
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewContext
()
_
,
ok
:=
api
.
UserFrom
(
ctx
)
if
ok
{
t
.
Fatalf
(
"Should not be ok because there is no user.Info on the context"
)
}
ctx
=
api
.
WithUser
(
ctx
,
&
user
.
DefaultInfo
{
Name
:
"bob"
,
UID
:
"123"
,
Groups
:
[]
string
{
"group1"
},
Extra
:
map
[
string
][]
string
{
"foo"
:
{
"bar"
}},
},
)
result
,
ok
:=
api
.
UserFrom
(
ctx
)
if
!
ok
{
t
.
Fatalf
(
"Error getting user info"
)
}
expectedName
:=
"bob"
if
result
.
GetName
()
!=
expectedName
{
t
.
Fatalf
(
"Get user name error, Expected: %s, Actual: %s"
,
expectedName
,
result
.
GetName
())
}
expectedUID
:=
"123"
if
result
.
GetUID
()
!=
expectedUID
{
t
.
Fatalf
(
"Get UID error, Expected: %s, Actual: %s"
,
expectedUID
,
result
.
GetName
())
}
expectedGroup
:=
"group1"
actualGroup
:=
result
.
GetGroups
()
if
len
(
actualGroup
)
!=
1
{
t
.
Fatalf
(
"Get user group number error, Expected: 1, Actual: %d"
,
len
(
actualGroup
))
}
else
if
actualGroup
[
0
]
!=
expectedGroup
{
t
.
Fatalf
(
"Get user group error, Expected: %s, Actual: %s"
,
expectedGroup
,
actualGroup
[
0
])
}
expectedExtraKey
:=
"foo"
expectedExtraValue
:=
"bar"
actualExtra
:=
result
.
GetExtra
()
if
len
(
actualExtra
[
expectedExtraKey
])
!=
1
{
t
.
Fatalf
(
"Get user extra map number error, Expected: 1, Actual: %d"
,
len
(
actualExtra
[
expectedExtraKey
]))
}
else
if
actualExtra
[
expectedExtraKey
][
0
]
!=
expectedExtraValue
{
t
.
Fatalf
(
"Get user extra map value error, Expected: %s, Actual: %s"
,
expectedExtraValue
,
actualExtra
[
expectedExtraKey
])
}
}
//TestUIDContext validates that a UID can be get/set on a context object
func
TestUIDContext
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewContext
()
_
,
ok
:=
api
.
UIDFrom
(
ctx
)
if
ok
{
t
.
Fatalf
(
"Should not be ok because there is no UID on the context"
)
}
ctx
=
api
.
WithUID
(
ctx
,
types
.
UID
(
"testUID"
),
)
_
,
ok
=
api
.
UIDFrom
(
ctx
)
if
!
ok
{
t
.
Fatalf
(
"Error getting UID"
)
}
}
//TestUserAgentContext validates that a useragent can be get/set on a context object
func
TestUserAgentContext
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewContext
()
_
,
ok
:=
api
.
UserAgentFrom
(
ctx
)
if
ok
{
t
.
Fatalf
(
"Should not be ok because there is no UserAgent on the context"
)
}
ctx
=
api
.
WithUserAgent
(
ctx
,
"TestUserAgent"
,
)
result
,
ok
:=
api
.
UserAgentFrom
(
ctx
)
if
!
ok
{
t
.
Fatalf
(
"Error getting UserAgent"
)
}
expectedResult
:=
"TestUserAgent"
if
result
!=
expectedResult
{
t
.
Fatalf
(
"Get user agent error, Expected: %s, Actual: %s"
,
expectedResult
,
result
)
}
}
}
}
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