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
ee23bb1b
Commit
ee23bb1b
authored
Mar 25, 2017
by
zhengjiajin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unit test for kubectl config set-context
parent
1e5fa8fe
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
0 deletions
+119
-0
BUILD
pkg/kubectl/cmd/config/BUILD
+1
-0
create_context_test.go
pkg/kubectl/cmd/config/create_context_test.go
+118
-0
No files found.
pkg/kubectl/cmd/config/BUILD
View file @
ee23bb1b
...
@@ -49,6 +49,7 @@ go_test(
...
@@ -49,6 +49,7 @@ go_test(
srcs = [
srcs = [
"config_test.go",
"config_test.go",
"create_authinfo_test.go",
"create_authinfo_test.go",
"create_context_test.go",
"current_context_test.go",
"current_context_test.go",
"delete_cluster_test.go",
"delete_cluster_test.go",
"delete_context_test.go",
"delete_context_test.go",
...
...
pkg/kubectl/cmd/config/create_context_test.go
0 → 100644
View file @
ee23bb1b
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
config
import
(
"bytes"
"io/ioutil"
"os"
"testing"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi
"k8s.io/client-go/tools/clientcmd/api"
)
type
createContextTest
struct
{
description
string
config
clientcmdapi
.
Config
//initiate kubectl config
args
[]
string
//kubectl set-context args
flags
[]
string
//kubectl set-context flags
expected
string
//expectd out
expectedConfig
clientcmdapi
.
Config
//expect kubectl config
}
func
TestCreateContext
(
t
*
testing
.
T
)
{
conf
:=
clientcmdapi
.
Config
{}
test
:=
createContextTest
{
description
:
"Testing for create a new context"
,
config
:
conf
,
args
:
[]
string
{
"shaker-context"
},
flags
:
[]
string
{
"--cluster=cluster_nickname"
,
"--user=user_nickname"
,
"--namespace=namespace"
,
},
expected
:
`Context "shaker-context" set.`
+
"
\n
"
,
expectedConfig
:
clientcmdapi
.
Config
{
Contexts
:
map
[
string
]
*
clientcmdapi
.
Context
{
"shaker-context"
:
{
AuthInfo
:
"user_nickname"
,
Cluster
:
"cluster_nickname"
,
Namespace
:
"namespace"
}},
},
}
test
.
run
(
t
)
}
func
TestModifyContext
(
t
*
testing
.
T
)
{
conf
:=
clientcmdapi
.
Config
{
Contexts
:
map
[
string
]
*
clientcmdapi
.
Context
{
"shaker-context"
:
{
AuthInfo
:
"blue-user"
,
Cluster
:
"big-cluster"
,
Namespace
:
"saw-ns"
},
"not-this"
:
{
AuthInfo
:
"blue-user"
,
Cluster
:
"big-cluster"
,
Namespace
:
"saw-ns"
}}}
test
:=
createContextTest
{
description
:
"Testing for modify a already exist context"
,
config
:
conf
,
args
:
[]
string
{
"shaker-context"
},
flags
:
[]
string
{
"--cluster=cluster_nickname"
,
"--user=user_nickname"
,
"--namespace=namespace"
,
},
expected
:
`Context "shaker-context" set.`
+
"
\n
"
,
expectedConfig
:
clientcmdapi
.
Config
{
Contexts
:
map
[
string
]
*
clientcmdapi
.
Context
{
"shaker-context"
:
{
AuthInfo
:
"user_nickname"
,
Cluster
:
"cluster_nickname"
,
Namespace
:
"namespace"
},
"not-this"
:
{
AuthInfo
:
"blue-user"
,
Cluster
:
"big-cluster"
,
Namespace
:
"saw-ns"
}}},
}
test
.
run
(
t
)
}
func
(
test
createContextTest
)
run
(
t
*
testing
.
T
)
{
fakeKubeFile
,
err
:=
ioutil
.
TempFile
(
os
.
TempDir
(),
""
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
defer
os
.
Remove
(
fakeKubeFile
.
Name
())
err
=
clientcmd
.
WriteToFile
(
test
.
config
,
fakeKubeFile
.
Name
())
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
pathOptions
:=
clientcmd
.
NewDefaultPathOptions
()
pathOptions
.
GlobalFile
=
fakeKubeFile
.
Name
()
pathOptions
.
EnvVar
=
""
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdConfigSetContext
(
buf
,
pathOptions
)
cmd
.
SetArgs
(
test
.
args
)
cmd
.
Flags
()
.
Parse
(
test
.
flags
)
if
err
:=
cmd
.
Execute
();
err
!=
nil
{
t
.
Fatalf
(
"unexpected error executing command: %v,kubectl set-context args: %v,flags: %v"
,
err
,
test
.
args
,
test
.
flags
)
}
config
,
err
:=
clientcmd
.
LoadFromFile
(
fakeKubeFile
.
Name
())
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error loading kubeconfig file: %v"
,
err
)
}
if
len
(
test
.
expected
)
!=
0
{
if
buf
.
String
()
!=
test
.
expected
{
t
.
Errorf
(
"Fail in %q:
\n
expected %v
\n
but got %v
\n
"
,
test
.
description
,
test
.
expected
,
buf
.
String
())
}
}
if
test
.
expectedConfig
.
Contexts
!=
nil
{
expectContext
:=
test
.
expectedConfig
.
Contexts
[
test
.
args
[
0
]]
actualContext
:=
config
.
Contexts
[
test
.
args
[
0
]]
if
expectContext
.
AuthInfo
!=
actualContext
.
AuthInfo
||
expectContext
.
Cluster
!=
actualContext
.
Cluster
||
expectContext
.
Namespace
!=
actualContext
.
Namespace
{
t
.
Errorf
(
"Fail in %q:
\n
expected Context %v
\n
but found %v in kubeconfig
\n
"
,
test
.
description
,
expectContext
,
actualContext
)
}
}
}
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