delete_cluster_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2016 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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "k8s.io/client-go/tools/clientcmd"
  22. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  23. )
  24. type deleteClusterTest struct {
  25. config clientcmdapi.Config
  26. clusterToDelete string
  27. expectedClusters []string
  28. expectedOut string
  29. }
  30. func TestDeleteCluster(t *testing.T) {
  31. conf := clientcmdapi.Config{
  32. Clusters: map[string]*clientcmdapi.Cluster{
  33. "minikube": {Server: "https://192.168.0.99"},
  34. "otherkube": {Server: "https://192.168.0.100"},
  35. },
  36. }
  37. test := deleteClusterTest{
  38. config: conf,
  39. clusterToDelete: "minikube",
  40. expectedClusters: []string{"otherkube"},
  41. expectedOut: "deleted cluster minikube from %s\n",
  42. }
  43. test.run(t)
  44. }
  45. func (test deleteClusterTest) run(t *testing.T) {
  46. fakeKubeFile, err := ioutil.TempFile("", "")
  47. if err != nil {
  48. t.Fatalf("unexpected error: %v", err)
  49. }
  50. defer os.Remove(fakeKubeFile.Name())
  51. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  52. if err != nil {
  53. t.Fatalf("unexpected error: %v", err)
  54. }
  55. pathOptions := clientcmd.NewDefaultPathOptions()
  56. pathOptions.GlobalFile = fakeKubeFile.Name()
  57. pathOptions.EnvVar = ""
  58. buf := bytes.NewBuffer([]byte{})
  59. cmd := NewCmdConfigDeleteCluster(buf, pathOptions)
  60. cmd.SetArgs([]string{test.clusterToDelete})
  61. if err := cmd.Execute(); err != nil {
  62. t.Fatalf("unexpected error executing command: %v", err)
  63. }
  64. expectedOutWithFile := fmt.Sprintf(test.expectedOut, fakeKubeFile.Name())
  65. if expectedOutWithFile != buf.String() {
  66. t.Errorf("expected output %s, but got %s", expectedOutWithFile, buf.String())
  67. return
  68. }
  69. // Verify cluster was removed from kubeconfig file
  70. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  71. if err != nil {
  72. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  73. }
  74. clusters := make([]string, 0, len(config.Clusters))
  75. for k := range config.Clusters {
  76. clusters = append(clusters, k)
  77. }
  78. if !reflect.DeepEqual(test.expectedClusters, clusters) {
  79. t.Errorf("expected clusters %v, but found %v in kubeconfig", test.expectedClusters, clusters)
  80. }
  81. }