github.com/oam-dev/kubevela@v1.9.11/e2e/plugin/plugin_suit_test.go (about) 1 /* 2 Copyright 2021. 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 plugin 18 19 import ( 20 "context" 21 "os" 22 "path/filepath" 23 "testing" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 "k8s.io/apimachinery/pkg/runtime" 28 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 "sigs.k8s.io/controller-runtime/pkg/client/config" 31 logf "sigs.k8s.io/controller-runtime/pkg/log" 32 "sigs.k8s.io/yaml" 33 34 core "github.com/oam-dev/kubevela/apis/core.oam.dev" 35 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 36 "github.com/oam-dev/kubevela/pkg/oam/util" 37 ) 38 39 var k8sClient client.Client 40 var scheme = runtime.NewScheme() 41 var ctx = context.Background() 42 var app v1beta1.Application 43 var testShowCdDef v1beta1.ComponentDefinition 44 var testShowTdDef v1beta1.TraitDefinition 45 var testCdDef v1beta1.ComponentDefinition 46 var testCdWithDeepCue v1beta1.ComponentDefinition 47 var testTdDef v1beta1.TraitDefinition 48 var testTdDefWithKube v1beta1.TraitDefinition 49 50 func TestKubectlPlugin(t *testing.T) { 51 RegisterFailHandler(Fail) 52 RunSpecs(t, "Kubectl Plugin Suite") 53 } 54 55 var testRegistryPath string 56 var _ = BeforeSuite(func() { 57 err := clientgoscheme.AddToScheme(scheme) 58 Expect(err).Should(BeNil()) 59 err = core.AddToScheme(scheme) 60 Expect(err).Should(BeNil()) 61 62 By("Setting up kubernetes client") 63 k8sClient, err = client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme}) 64 if err != nil { 65 logf.Log.Error(err, "failed to create k8sClient") 66 Fail("setup failed") 67 } 68 69 err = os.MkdirAll("definitions", os.ModePerm) 70 Expect(err).NotTo(HaveOccurred()) 71 err = os.WriteFile("definitions/webservice.yaml", []byte(componentDef), 0644) 72 Expect(err).NotTo(HaveOccurred()) 73 err = os.WriteFile("definitions/ingress.yaml", []byte(traitDef), 0644) 74 Expect(err).NotTo(HaveOccurred()) 75 76 By("apply test definitions") 77 Expect(yaml.Unmarshal([]byte(componentDef), &testCdDef)).Should(BeNil()) 78 err = k8sClient.Create(ctx, &testCdDef) 79 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 80 81 Expect(yaml.Unmarshal([]byte(componentWithDeepCue), &testCdWithDeepCue)).Should(BeNil()) 82 err = k8sClient.Create(ctx, &testCdWithDeepCue) 83 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 84 85 Expect(yaml.Unmarshal([]byte(traitDef), &testTdDef)).Should(BeNil()) 86 err = k8sClient.Create(ctx, &testTdDef) 87 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 88 89 Expect(yaml.Unmarshal([]byte(traitDefWithKube), &testTdDefWithKube)).Should(BeNil()) 90 err = k8sClient.Create(ctx, &testTdDefWithKube) 91 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 92 93 Expect(yaml.Unmarshal([]byte(testShowComponentDef), &testShowCdDef)).Should(BeNil()) 94 err = k8sClient.Create(ctx, &testShowCdDef) 95 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 96 97 Expect(yaml.Unmarshal([]byte(testShowTraitDef), &testShowTdDef)).Should(BeNil()) 98 err = k8sClient.Create(ctx, &testShowTdDef) 99 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 100 101 By("apply test application") 102 Expect(yaml.Unmarshal([]byte(application), &app)).Should(BeNil()) 103 err = k8sClient.Create(ctx, &app) 104 Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 105 106 By("set test registry absolute path") 107 testRegistryPath, err = filepath.Abs("testdata") 108 testRegistryPath = "file://" + testRegistryPath 109 Expect(err).Should(BeNil()) 110 }) 111 112 var _ = AfterSuite(func() { 113 By("delete application and definitions") 114 115 Expect(k8sClient.Delete(ctx, &app)).Should(BeNil()) 116 Expect(k8sClient.Delete(ctx, &testCdDef)).Should(BeNil()) 117 Expect(k8sClient.Delete(ctx, &testCdWithDeepCue)).Should(BeNil()) 118 Expect(k8sClient.Delete(ctx, &testTdDef)).Should(BeNil()) 119 Expect(k8sClient.Delete(ctx, &testTdDefWithKube)).Should(BeNil()) 120 Expect(k8sClient.Delete(ctx, &testShowCdDef)).Should(BeNil()) 121 Expect(k8sClient.Delete(ctx, &testShowTdDef)).Should(BeNil()) 122 123 _ = os.RemoveAll("definitions/") 124 _ = os.Remove("dry-run-app.yaml") 125 _ = os.Remove("live-diff-app.yaml") 126 })