github.com/oam-dev/kubevela@v1.9.11/references/cli/top/model/suit_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 17 package model 18 19 import ( 20 "context" 21 "encoding/json" 22 "fmt" 23 "testing" 24 "time" 25 26 "github.com/kubevela/workflow/api/v1alpha1" 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 appsv1 "k8s.io/api/apps/v1" 30 corev1 "k8s.io/api/core/v1" 31 "k8s.io/apimachinery/pkg/api/resource" 32 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 33 "k8s.io/apimachinery/pkg/runtime" 34 "k8s.io/client-go/rest" 35 "k8s.io/utils/pointer" 36 "sigs.k8s.io/controller-runtime/pkg/client" 37 "sigs.k8s.io/controller-runtime/pkg/envtest" 38 39 common2 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 40 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 41 "github.com/oam-dev/kubevela/apis/types" 42 helmapi "github.com/oam-dev/kubevela/pkg/appfile/helm/flux2apis" 43 "github.com/oam-dev/kubevela/pkg/oam" 44 "github.com/oam-dev/kubevela/pkg/utils/common" 45 ) 46 47 var cfg *rest.Config 48 var k8sClient client.Client 49 var testEnv *envtest.Environment 50 51 var _ = BeforeSuite(func() { 52 // env init 53 By("bootstrapping test environment") 54 testEnv = &envtest.Environment{ 55 ControlPlaneStartTimeout: time.Minute * 3, 56 ControlPlaneStopTimeout: time.Minute, 57 UseExistingCluster: pointer.Bool(false), 58 CRDDirectoryPaths: []string{ 59 "../../../../charts/vela-core/crds", 60 }, 61 } 62 // env start 63 By("start kube test env") 64 var err error 65 cfg, err = testEnv.Start() 66 Expect(err).ShouldNot(HaveOccurred()) 67 Expect(cfg).ToNot(BeNil()) 68 // client start 69 By("new kube client") 70 cfg.Timeout = time.Minute * 2 71 k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme}) 72 Expect(err).Should(BeNil()) 73 Expect(k8sClient).ToNot(BeNil()) 74 75 // create namespace 76 By("create namespace") 77 _ = k8sClient.Create(context.Background(), &corev1.Namespace{ 78 ObjectMeta: metav1.ObjectMeta{ 79 Name: "default", 80 }, 81 }) 82 // create app 83 name, namespace := "first-vela-app", "default" 84 testApp := &v1beta1.Application{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: name, 87 Namespace: namespace, 88 }, 89 Spec: v1beta1.ApplicationSpec{ 90 Components: []common2.ApplicationComponent{ 91 { 92 Name: "webservice-test", 93 Type: "webservice", 94 }, 95 }, 96 }, 97 } 98 err = k8sClient.Create(context.Background(), testApp) 99 Expect(err).Should(BeNil()) 100 testApp.Status = common2.AppStatus{ 101 Phase: common2.ApplicationRunning, 102 Workflow: &common2.WorkflowStatus{ 103 AppRevision: "", 104 Mode: "DAG", 105 Phase: "", 106 Message: "", 107 Suspend: false, 108 SuspendState: "", 109 Terminated: false, 110 Finished: false, 111 ContextBackend: nil, 112 Steps: []v1alpha1.WorkflowStepStatus{}, 113 StartTime: metav1.Time{}, 114 EndTime: metav1.Time{}, 115 }, 116 AppliedResources: []common2.ClusterObjectReference{ 117 { 118 Cluster: "", 119 ObjectReference: corev1.ObjectReference{ 120 Kind: "Ingress", 121 Namespace: "default", 122 Name: "ingress-http", 123 APIVersion: "networking.k8s.io/v1beta1", 124 }, 125 }, 126 { 127 Cluster: "", 128 ObjectReference: corev1.ObjectReference{ 129 Kind: "Ingress", 130 Namespace: "default", 131 Name: "ingress-https", 132 APIVersion: "networking.k8s.io/v1", 133 }, 134 }, 135 { 136 Cluster: "", 137 ObjectReference: corev1.ObjectReference{ 138 Kind: "Ingress", 139 Namespace: "default", 140 Name: "ingress-paths", 141 APIVersion: "networking.k8s.io/v1", 142 }, 143 }, 144 { 145 Cluster: "", 146 ObjectReference: corev1.ObjectReference{ 147 Kind: "Service", 148 Namespace: "default", 149 Name: "nodeport", 150 APIVersion: "v1", 151 }, 152 }, 153 { 154 Cluster: "", 155 ObjectReference: corev1.ObjectReference{ 156 Kind: "Service", 157 Namespace: "default", 158 Name: "loadbalancer", 159 APIVersion: "v1", 160 }, 161 }, 162 { 163 Cluster: "", 164 ObjectReference: corev1.ObjectReference{ 165 Kind: helmapi.HelmReleaseGVK.Kind, 166 Namespace: "default", 167 Name: "helmRelease", 168 }, 169 }, 170 }, 171 } 172 err = k8sClient.Status().Update(context.Background(), testApp) 173 Expect(err).Should(BeNil()) 174 // create service 175 svc := &corev1.Service{ 176 TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Service"}, 177 ObjectMeta: metav1.ObjectMeta{ 178 Name: "service1", 179 Namespace: namespace, 180 }, 181 Spec: corev1.ServiceSpec{ 182 Ports: []corev1.ServicePort{{Port: 2002}}, 183 }, 184 } 185 svcRaw, err := json.Marshal(svc) 186 Expect(err).Should(Succeed()) 187 Expect(k8sClient.Create(context.Background(), svc)).Should(BeNil()) 188 // create deploy 189 dply := &appsv1.Deployment{ 190 TypeMeta: metav1.TypeMeta{ 191 Kind: "Deployment", 192 APIVersion: "apps/v1", 193 }, 194 ObjectMeta: metav1.ObjectMeta{ 195 Name: "deploy1", 196 Namespace: namespace, 197 }, 198 Spec: appsv1.DeploymentSpec{ 199 Selector: &metav1.LabelSelector{ 200 MatchLabels: map[string]string{"app": "test"}, 201 }, 202 Template: corev1.PodTemplateSpec{ 203 ObjectMeta: metav1.ObjectMeta{Name: "pod", Namespace: namespace, Labels: map[string]string{"app": "test"}}, 204 Spec: corev1.PodSpec{Containers: []corev1.Container{ 205 { 206 Name: "vela-core-1", 207 Image: "vela", 208 }, 209 }}, 210 }, 211 }, 212 } 213 dplyRaw, err := json.Marshal(dply) 214 Expect(err).Should(Succeed()) 215 Expect(k8sClient.Create(context.Background(), dply)).Should(BeNil()) 216 //create replicaSet 217 var rsNum int32 = 2 218 rs := &appsv1.ReplicaSet{ 219 TypeMeta: metav1.TypeMeta{ 220 Kind: "ReplicaSet", 221 APIVersion: "apps/v1", 222 }, 223 ObjectMeta: metav1.ObjectMeta{ 224 Name: "rs1", 225 Namespace: namespace, 226 Labels: map[string]string{"app": "test"}, 227 }, 228 Spec: appsv1.ReplicaSetSpec{ 229 Replicas: &rsNum, 230 Selector: &metav1.LabelSelector{ 231 MatchLabels: map[string]string{"app": "test"}, 232 }, 233 Template: corev1.PodTemplateSpec{ 234 ObjectMeta: metav1.ObjectMeta{Name: "pod", Namespace: namespace, Labels: map[string]string{"app": "test"}}, 235 Spec: corev1.PodSpec{Containers: []corev1.Container{ 236 { 237 Name: "vela-core-1", 238 Image: "vela", 239 }, 240 }}, 241 }, 242 }, 243 } 244 rsRaw, err := json.Marshal(rs) 245 Expect(err).Should(Succeed()) 246 Expect(k8sClient.Create(context.Background(), rs)).Should(BeNil()) 247 // create pod 248 pod := &corev1.Pod{ 249 TypeMeta: metav1.TypeMeta{ 250 Kind: "Pod", 251 APIVersion: "v1", 252 }, 253 ObjectMeta: metav1.ObjectMeta{ 254 Name: "pod1", 255 Namespace: namespace, 256 Labels: map[string]string{"app": "test"}, 257 }, 258 Spec: corev1.PodSpec{Containers: []corev1.Container{ 259 { 260 Name: "vela-core-1", 261 Image: "vela", 262 }, 263 }}, 264 } 265 podRaw, err := json.Marshal(pod) 266 Expect(err).Should(Succeed()) 267 Expect(k8sClient.Create(context.Background(), pod)).Should(BeNil()) 268 // create resourceTracker 269 rt := &v1beta1.ResourceTracker{ 270 ObjectMeta: metav1.ObjectMeta{ 271 Name: fmt.Sprintf("%s-v1-%s", name, namespace), 272 Labels: map[string]string{ 273 oam.LabelAppName: name, 274 oam.LabelAppNamespace: namespace, 275 }, 276 Annotations: map[string]string{ 277 oam.AnnotationPublishVersion: "v1", 278 }, 279 }, 280 Spec: v1beta1.ResourceTrackerSpec{ 281 ManagedResources: []v1beta1.ManagedResource{ 282 { 283 ClusterObjectReference: common2.ClusterObjectReference{ 284 Cluster: "", 285 ObjectReference: corev1.ObjectReference{ 286 APIVersion: "v1", 287 Kind: "Service", 288 Namespace: namespace, 289 Name: "service1", 290 }, 291 }, 292 OAMObjectReference: common2.OAMObjectReference{ 293 Component: "service1", 294 }, 295 Data: &runtime.RawExtension{Raw: svcRaw}, 296 }, 297 { 298 ClusterObjectReference: common2.ClusterObjectReference{ 299 Cluster: "", 300 ObjectReference: corev1.ObjectReference{ 301 APIVersion: "apps/v1", 302 Kind: "Deployment", 303 Namespace: namespace, 304 Name: "deploy1", 305 }, 306 }, 307 OAMObjectReference: common2.OAMObjectReference{ 308 Component: "deploy1", 309 }, 310 Data: &runtime.RawExtension{Raw: dplyRaw}, 311 }, 312 { 313 ClusterObjectReference: common2.ClusterObjectReference{ 314 Cluster: "", 315 ObjectReference: corev1.ObjectReference{ 316 APIVersion: "apps/v1", 317 Kind: "ReplicaSet", 318 Namespace: namespace, 319 Name: "rs1", 320 }, 321 }, 322 OAMObjectReference: common2.OAMObjectReference{ 323 Component: "rs1", 324 }, 325 Data: &runtime.RawExtension{Raw: rsRaw}, 326 }, 327 { 328 ClusterObjectReference: common2.ClusterObjectReference{ 329 Cluster: "", 330 ObjectReference: corev1.ObjectReference{ 331 APIVersion: "v1", 332 Kind: "Pod", 333 Namespace: namespace, 334 Name: "pod1", 335 }, 336 }, 337 OAMObjectReference: common2.OAMObjectReference{ 338 Component: "pod1", 339 }, 340 Data: &runtime.RawExtension{Raw: podRaw}, 341 }, 342 }, 343 Type: v1beta1.ResourceTrackerTypeVersioned, 344 }, 345 } 346 Expect(k8sClient.Create(context.Background(), rt)).Should(BeNil()) 347 348 err = k8sClient.Create(context.Background(), &corev1.Namespace{ 349 ObjectMeta: metav1.ObjectMeta{ 350 Name: types.DefaultKubeVelaNS, 351 }, 352 }) 353 Expect(err).Should(BeNil()) 354 355 quantityLimitsCPU, _ := resource.ParseQuantity("10m") 356 quantityLimitsMemory, _ := resource.ParseQuantity("10Mi") 357 quantityRequestsCPU, _ := resource.ParseQuantity("10m") 358 quantityRequestsMemory, _ := resource.ParseQuantity("10Mi") 359 360 pod1 := &corev1.Pod{ 361 ObjectMeta: metav1.ObjectMeta{Name: "vela-core", Namespace: "vela-system", Labels: map[string]string{"app.kubernetes.io/name": "vela-core"}}, 362 Spec: corev1.PodSpec{Containers: []corev1.Container{ 363 { 364 Name: "vela-core-1", 365 Image: "vela", 366 Resources: corev1.ResourceRequirements{ 367 Requests: map[corev1.ResourceName]resource.Quantity{"memory": quantityRequestsMemory, "cpu": quantityRequestsCPU}, 368 Limits: map[corev1.ResourceName]resource.Quantity{"memory": quantityLimitsMemory, "cpu": quantityLimitsCPU}, 369 }, 370 }, 371 }}, 372 } 373 Expect(k8sClient.Create(context.Background(), pod1)).Should(BeNil()) 374 pod2 := &corev1.Pod{ 375 ObjectMeta: metav1.ObjectMeta{Name: "vela-core-cluster-gateway", Namespace: "vela-system", Labels: map[string]string{"app.kubernetes.io/name": "vela-core-cluster-gateway"}}, 376 Spec: corev1.PodSpec{Containers: []corev1.Container{ 377 { 378 Name: "vela-core-cluster-gateway-1", 379 Image: "vela", 380 Resources: corev1.ResourceRequirements{ 381 Requests: map[corev1.ResourceName]resource.Quantity{"memory": quantityRequestsMemory, "cpu": quantityRequestsCPU}, 382 Limits: map[corev1.ResourceName]resource.Quantity{"memory": quantityLimitsMemory, "cpu": quantityLimitsCPU}, 383 }, 384 }, 385 }}, 386 } 387 Expect(k8sClient.Create(context.Background(), pod2)).Should(BeNil()) 388 }) 389 390 var _ = AfterSuite(func() { 391 By("tearing down the test environment") 392 err := testEnv.Stop() 393 Expect(err).ToNot(HaveOccurred()) 394 }) 395 396 func TestModel(t *testing.T) { 397 RegisterFailHandler(Fail) 398 RunSpecs(t, "Model Suite") 399 }