|
| 1 | +/* |
| 2 | +Copyright 2023 The KubeStellar Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ctx |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/kubestellar/kubeflex/cmd/kflex/common" |
| 23 | + "github.com/kubestellar/kubeflex/pkg/certs" |
| 24 | + "github.com/kubestellar/kubeflex/pkg/kubeconfig" |
| 25 | +) |
| 26 | + |
| 27 | +// Test delete a context that exist and checks that it is removed |
| 28 | +// from the kubeconfig |
| 29 | +func TestDeleteOk(t *testing.T) { |
| 30 | + ctxName := "cptobedeleted" |
| 31 | + setupMockContext(kubeconfigPath, ctxName) |
| 32 | + defer teardown(kubeconfigPath) |
| 33 | + |
| 34 | + // Start test |
| 35 | + cp := common.NewCP(kubeconfigPath, common.WithName(ctxName)) |
| 36 | + err := ExecuteCtxDelete(cp, ctxName, false) |
| 37 | + if err != nil { |
| 38 | + t.Errorf("failed to run 'kflex ctx delete %s': %v", ctxName, err) |
| 39 | + } |
| 40 | + kconf, err := kubeconfig.LoadKubeconfig(kubeconfigPath) |
| 41 | + if err != nil { |
| 42 | + t.Errorf("error loading kubeconfig: %v", err) |
| 43 | + } |
| 44 | + clusterName := certs.GenerateClusterName(ctxName) |
| 45 | + authInfoName := certs.GenerateAuthInfoAdminName(ctxName) |
| 46 | + if c, ok := kconf.Contexts[ctxName]; ok { |
| 47 | + t.Errorf("context '%v' still present in kubeconfig", c) |
| 48 | + } |
| 49 | + if c, ok := kconf.Clusters[clusterName]; ok { |
| 50 | + t.Errorf("cluster '%v' still present in kubeconfig", c) |
| 51 | + } |
| 52 | + if c, ok := kconf.AuthInfos[authInfoName]; ok { |
| 53 | + t.Errorf("user '%v' still present in kubeconfig", c) |
| 54 | + } |
| 55 | + if kconf.CurrentContext == ctxName { |
| 56 | + t.Errorf("current context must not be set as the deleted context %s", ctxName) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Test delete on non-existent context and checks that the kubeconfig is unchanged |
| 61 | +func TestDeleteNonExistentContext(t *testing.T) { |
| 62 | + ctxName := "cptobedeleted" |
| 63 | + noneCtxName := "none" |
| 64 | + setupMockContext(kubeconfigPath, ctxName) |
| 65 | + defer teardown(kubeconfigPath) |
| 66 | + |
| 67 | + // Start test |
| 68 | + cp := common.NewCP(kubeconfigPath, common.WithName(ctxName)) |
| 69 | + kconf, err := kubeconfig.LoadKubeconfig(kubeconfigPath) |
| 70 | + if err != nil { |
| 71 | + t.Errorf("error loading kubeconfig: %v", err) |
| 72 | + } |
| 73 | + nCtx := len(kconf.Contexts) |
| 74 | + err = ExecuteCtxDelete(cp, noneCtxName, false) |
| 75 | + if err == nil { |
| 76 | + t.Errorf("expect ExecuteCtxDelete to fail but it succeeded") |
| 77 | + } |
| 78 | + if nCtx != len(kconf.Contexts) { |
| 79 | + t.Errorf("expect ExecuteCtxDelete to not delete any context but it did") |
| 80 | + } |
| 81 | +} |
0 commit comments