github.com/oam-dev/kubevela@v1.9.11/references/common/application_test.go (about) 1 /* 2 Copyright 2022 The KubeVela 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 package common 17 18 import ( 19 "context" 20 "testing" 21 22 terraformapi "github.com/oam-dev/terraform-controller/api/v1beta2" 23 "github.com/stretchr/testify/assert" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 "sigs.k8s.io/controller-runtime/pkg/client/fake" 29 30 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 31 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 32 "github.com/oam-dev/kubevela/apis/types" 33 ) 34 35 func TestPrepareToForceDeleteTerraformComponents(t *testing.T) { 36 ctx := context.Background() 37 s := runtime.NewScheme() 38 v1beta1.AddToScheme(s) 39 corev1.AddToScheme(s) 40 terraformapi.AddToScheme(s) 41 app1 := &v1beta1.Application{ 42 ObjectMeta: metav1.ObjectMeta{ 43 Name: "app1", 44 Namespace: "default", 45 }, 46 Spec: v1beta1.ApplicationSpec{ 47 Components: []common.ApplicationComponent{ 48 { 49 Name: "c1", 50 Type: "d1", 51 }, 52 }, 53 }, 54 } 55 def1 := &v1beta1.ComponentDefinition{ 56 TypeMeta: metav1.TypeMeta{ 57 Kind: "ComponentDefinition", 58 APIVersion: "core.oam.dev/v1beta2", 59 }, 60 ObjectMeta: metav1.ObjectMeta{ 61 Name: "d1", 62 Namespace: types.DefaultKubeVelaNS, 63 }, 64 Spec: v1beta1.ComponentDefinitionSpec{ 65 Schematic: &common.Schematic{ 66 Terraform: &common.Terraform{ 67 Configuration: "abc", 68 }, 69 }, 70 }, 71 } 72 conf1 := &terraformapi.Configuration{ 73 ObjectMeta: metav1.ObjectMeta{ 74 Name: "c1", 75 Namespace: "default", 76 }, 77 } 78 79 userNamespace := "another-namespace" 80 def2 := def1.DeepCopy() 81 def2.SetNamespace(userNamespace) 82 app2 := app1.DeepCopy() 83 app2.SetNamespace(userNamespace) 84 app2.SetName("app2") 85 conf2 := conf1.DeepCopy() 86 conf2.SetNamespace(userNamespace) 87 88 k8sClient1 := fake.NewClientBuilder().WithScheme(s).WithObjects(app1, def1, conf1).Build() 89 90 k8sClient2 := fake.NewClientBuilder().WithScheme(runtime.NewScheme()).Build() 91 92 k8sClient3 := fake.NewClientBuilder().WithScheme(s).WithObjects(app1).Build() 93 94 k8sClient4 := fake.NewClientBuilder().WithScheme(s).WithObjects(app1, def1).Build() 95 96 k8sClient5 := fake.NewClientBuilder().WithScheme(s).WithObjects(app2, def2, conf2).Build() 97 type args struct { 98 k8sClient client.Client 99 namespace string 100 name string 101 } 102 type want struct { 103 errMsg string 104 } 105 106 testcases := map[string]struct { 107 args args 108 want want 109 }{ 110 "valid": { 111 args: args{ 112 k8sClient1, 113 "default", 114 "app1", 115 }, 116 want: want{}, 117 }, 118 "application not found": { 119 args: args{ 120 k8sClient1, 121 "default", 122 "app99", 123 }, 124 want: want{ 125 errMsg: "already deleted or not exist", 126 }, 127 }, 128 "failed to get application": { 129 args: args{ 130 k8sClient2, 131 "default", 132 "app1", 133 }, 134 want: want{ 135 errMsg: "delete application err", 136 }, 137 }, 138 "definition is not available": { 139 args: args{ 140 k8sClient3, 141 "default", 142 "app1", 143 }, 144 want: want{ 145 errMsg: "componentdefinitions.core.oam.dev \"d1\" not found", 146 }, 147 }, 148 "Configuration is not available": { 149 args: args{ 150 k8sClient4, 151 "default", 152 "app1", 153 }, 154 want: want{ 155 errMsg: "configurations.terraform.core.oam.dev \"c1\" not found", 156 }, 157 }, 158 "can read definition from application namespace": { 159 args: args{ 160 k8sClient5, 161 userNamespace, 162 "app2", 163 }, 164 want: want{}, 165 }, 166 } 167 168 for name, tc := range testcases { 169 t.Run(name, func(t *testing.T) { 170 err := PrepareToForceDeleteTerraformComponents(ctx, tc.args.k8sClient, tc.args.namespace, tc.args.name) 171 if err != nil { 172 assert.NotEmpty(t, tc.want.errMsg) 173 assert.Contains(t, err.Error(), tc.want.errMsg) 174 } else { 175 assert.Empty(t, tc.want.errMsg) 176 } 177 }) 178 } 179 180 }