1 Star 0 Fork 0

zhuchance/kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
Godeps
api
build
cluster
cmd
docs
hack
logo
pkg
api
apis
auth
capabilities
client
cloudprovider
controller
credentialprovider
features
fieldpath
generated
kubeapiserver
kubectl
apply
apps
cmd
auth
config
BUILD
config.go
config_test.go
create_authinfo.go
create_authinfo_test.go
create_cluster.go
create_cluster_test.go
create_context.go
create_context_test.go
current_context.go
current_context_test.go
delete_cluster.go
delete_cluster_test.go
delete_context.go
delete_context_test.go
get_clusters.go
get_clusters_test.go
get_contexts.go
get_contexts_test.go
navigation_step_parser.go
navigation_step_parser_test.go
rename_context.go
rename_context_test.go
set.go
set_test.go
unset.go
unset_test.go
use_context.go
use_context_test.go
view.go
view_test.go
create
get
rollout
scalejob
set
templates
testdata
testing
util
wait
BUILD
alpha.go
annotate.go
annotate_test.go
apiresources.go
apiversions.go
apply.go
apply_edit_last_applied.go
apply_set_last_applied.go
apply_test.go
apply_view_last_applied.go
attach.go
attach_test.go
autoscale.go
certificates.go
clusterinfo.go
clusterinfo_dump.go
clusterinfo_dump_test.go
cmd.go
cmd_printing_test.go
cmd_test.go
completion.go
convert.go
convert_test.go
cp.go
cp_test.go
delete.go
delete_flags.go
delete_test.go
describe.go
describe_test.go
diff.go
diff_test.go
drain.go
drain_test.go
edit.go
edit_test.go
exec.go
exec_test.go
explain.go
expose.go
expose_test.go
help.go
label.go
label_test.go
logs.go
logs_test.go
options.go
patch.go
patch_test.go
plugin.go
portforward.go
portforward_test.go
proxy.go
replace.go
replace_test.go
rollingupdate.go
rollingupdate_test.go
run.go
run_test.go
scale.go
taint.go
taint_test.go
top.go
top_node.go
top_node_test.go
top_pod.go
top_pod_test.go
top_test.go
version.go
explain
metricsutil
polymorphichelpers
proxy
scheme
util
validation
.import-restrictions
BUILD
OWNERS
apply.go
autoscale.go
autoscale_test.go
clusterrolebinding.go
clusterrolebinding_test.go
conditions.go
configmap.go
configmap_test.go
deployment.go
deployment_test.go
doc.go
env_file.go
env_file_test.go
generate.go
generate_test.go
history.go
history_test.go
interfaces.go
namespace.go
namespace_test.go
pdb.go
pdb_test.go
priorityclass.go
priorityclass_test.go
quota.go
quota_test.go
rolebinding.go
rolebinding_test.go
rollback.go
rollback_test.go
rolling_updater.go
rolling_updater_test.go
rollout_status.go
rollout_status_test.go
run.go
run_test.go
scale.go
scale_test.go
secret.go
secret_for_docker_registry.go
secret_for_docker_registry_test.go
secret_for_tls.go
secret_for_tls_test.go
secret_test.go
service.go
service_basic.go
service_basic_test.go
service_test.go
serviceaccount.go
serviceaccount_test.go
sorter.go
sorter_test.go
kubelet
kubemark
master
printers
probe
proxy
quota
registry
routes
scheduler
security
securitycontext
serviceaccount
ssh
util
version
volume
watch/json
windows/service
.import-restrictions
BUILD
OWNERS
plugin
staging
test
third_party
translations
vendor
.bazelrc
.generated_files
.gitattributes
.gitignore
.kazelcfg.json
BUILD.bazel
CHANGELOG-1.12.md
CHANGELOG.md
CONTRIBUTING.md
LICENSE
Makefile
Makefile.generated_files
OWNERS
OWNERS_ALIASES
README.md
SECURITY_CONTACTS
SUPPORT.md
WORKSPACE
code-of-conduct.md
克隆/下载
view_test.go 5.22 KB
一键复制 编辑 原始数据 按行查看 历史
/*
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 (
"io/ioutil"
"os"
"testing"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
type viewClusterTest struct {
description string
config clientcmdapi.Config //initiate kubectl config
flags []string //kubectl config viw flags
expected string //expect out
}
func TestViewCluster(t *testing.T) {
conf := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: map[string]*clientcmdapi.Cluster{
"minikube": {Server: "https://192.168.99.100:8443"},
"my-cluster": {Server: "https://192.168.0.1:3434"},
},
Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
},
CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"},
"mu-cluster": {Token: "minikube-token"},
},
}
test := viewClusterTest{
description: "Testing for kubectl config view",
config: conf,
expected: `apiVersion: v1
clusters:
- cluster:
server: https://192.168.99.100:8443
name: minikube
- cluster:
server: https://192.168.0.1:3434
name: my-cluster
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
- context:
cluster: my-cluster
user: mu-cluster
name: my-cluster
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
token: minikube-token
- name: mu-cluster
user:
token: minikube-token` + "\n",
}
test.run(t)
}
func TestViewClusterMinify(t *testing.T) {
conf := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: map[string]*clientcmdapi.Cluster{
"minikube": {Server: "https://192.168.99.100:8443"},
"my-cluster": {Server: "https://192.168.0.1:3434"},
},
Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
},
CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"},
"mu-cluster": {Token: "minikube-token"},
},
}
testCases := []struct {
description string
config clientcmdapi.Config
flags []string
expected string
}{
{
description: "Testing for kubectl config view --minify=true",
config: conf,
flags: []string{"--minify=true"},
expected: `apiVersion: v1
clusters:
- cluster:
server: https://192.168.99.100:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
token: minikube-token` + "\n",
},
{
description: "Testing for kubectl config view --minify=true --context=my-cluster",
config: conf,
flags: []string{"--minify=true", "--context=my-cluster"},
expected: `apiVersion: v1
clusters:
- cluster:
server: https://192.168.0.1:3434
name: my-cluster
contexts:
- context:
cluster: my-cluster
user: mu-cluster
name: my-cluster
current-context: my-cluster
kind: Config
preferences: {}
users:
- name: mu-cluster
user:
token: minikube-token` + "\n",
},
}
for _, test := range testCases {
cmdTest := viewClusterTest{
description: test.description,
config: test.config,
flags: test.flags,
expected: test.expected,
}
cmdTest.run(t)
}
}
func (test viewClusterTest) 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 = ""
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdConfigView(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), streams, pathOptions)
// "context" is a global flag, inherited from base kubectl command in the real world
cmd.Flags().String("context", "", "The name of the kubeconfig context to use")
cmd.Flags().Parse(test.flags)
if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags)
}
if len(test.expected) != 0 {
if buf.String() != test.expected {
t.Errorf("Failed in %q\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.12.2

搜索帮助