get_clusters_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. "k8s.io/client-go/tools/clientcmd"
  20. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  21. )
  22. type getClustersTest struct {
  23. config clientcmdapi.Config
  24. expected string
  25. }
  26. func TestGetClusters(t *testing.T) {
  27. conf := clientcmdapi.Config{
  28. Clusters: map[string]*clientcmdapi.Cluster{
  29. "minikube": {Server: "https://192.168.0.99"},
  30. },
  31. }
  32. test := getClustersTest{
  33. config: conf,
  34. expected: `NAME
  35. minikube
  36. `,
  37. }
  38. test.run(t)
  39. }
  40. func TestGetClustersEmpty(t *testing.T) {
  41. test := getClustersTest{
  42. config: clientcmdapi.Config{},
  43. expected: "NAME\n",
  44. }
  45. test.run(t)
  46. }
  47. func (test getClustersTest) run(t *testing.T) {
  48. fakeKubeFile, err := ioutil.TempFile("", "")
  49. if err != nil {
  50. t.Fatalf("unexpected error: %v", err)
  51. }
  52. defer os.Remove(fakeKubeFile.Name())
  53. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  54. if err != nil {
  55. t.Fatalf("unexpected error: %v", err)
  56. }
  57. pathOptions := clientcmd.NewDefaultPathOptions()
  58. pathOptions.GlobalFile = fakeKubeFile.Name()
  59. pathOptions.EnvVar = ""
  60. buf := bytes.NewBuffer([]byte{})
  61. cmd := NewCmdConfigGetClusters(buf, pathOptions)
  62. if err := cmd.Execute(); err != nil {
  63. t.Fatalf("unexpected error executing command: %v", err)
  64. }
  65. if len(test.expected) != 0 {
  66. if buf.String() != test.expected {
  67. t.Errorf("expected %v, but got %v", test.expected, buf.String())
  68. }
  69. return
  70. }
  71. }