github.com/oam-dev/kubevela@v1.9.11/pkg/appfile/dryrun/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 dryrun 18 19 import ( 20 "context" 21 "os" 22 "path/filepath" 23 "testing" 24 "time" 25 26 corev1 "k8s.io/api/core/v1" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 29 "sigs.k8s.io/yaml" 30 31 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 32 "github.com/oam-dev/kubevela/apis/types" 33 34 . "github.com/onsi/ginkgo/v2" 35 . "github.com/onsi/gomega" 36 37 crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 38 "k8s.io/apimachinery/pkg/runtime" 39 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 40 "k8s.io/client-go/rest" 41 "sigs.k8s.io/controller-runtime/pkg/client" 42 "sigs.k8s.io/controller-runtime/pkg/envtest" 43 logf "sigs.k8s.io/controller-runtime/pkg/log" 44 "sigs.k8s.io/controller-runtime/pkg/log/zap" 45 46 "github.com/kubevela/workflow/pkg/cue/packages" 47 48 coreoam "github.com/oam-dev/kubevela/apis/core.oam.dev" 49 "github.com/oam-dev/kubevela/pkg/appfile" 50 oamutil "github.com/oam-dev/kubevela/pkg/oam/util" 51 ) 52 53 var cfg *rest.Config 54 var scheme *runtime.Scheme 55 var k8sClient client.Client 56 var testEnv *envtest.Environment 57 var pd *packages.PackageDiscover 58 var dryrunOpt *Option 59 var diffOpt *LiveDiffOption 60 61 func TestDryRun(t *testing.T) { 62 RegisterFailHandler(Fail) 63 RunSpecs(t, "Cli Suite") 64 } 65 66 var _ = BeforeSuite(func() { 67 logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))) 68 By("bootstrapping test environment") 69 useExistCluster := false 70 testEnv = &envtest.Environment{ 71 ControlPlaneStartTimeout: time.Minute, 72 ControlPlaneStopTimeout: time.Minute, 73 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "charts", "vela-core", "crds")}, 74 UseExistingCluster: &useExistCluster, 75 } 76 77 var err error 78 cfg, err = testEnv.Start() 79 Expect(err).ToNot(HaveOccurred()) 80 Expect(cfg).ToNot(BeNil()) 81 scheme = runtime.NewScheme() 82 Expect(coreoam.AddToScheme(scheme)).NotTo(HaveOccurred()) 83 Expect(clientgoscheme.AddToScheme(scheme)).NotTo(HaveOccurred()) 84 Expect(crdv1.AddToScheme(scheme)).NotTo(HaveOccurred()) 85 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) 86 Expect(err).ToNot(HaveOccurred()) 87 Expect(k8sClient).ToNot(BeNil()) 88 pd, err = packages.NewPackageDiscover(cfg) 89 Expect(err).ToNot(HaveOccurred()) 90 Expect(pd).ToNot(BeNil()) 91 92 By("Prepare capability definitions") 93 myingressYAML := readDataFromFile("./testdata/td-myingress.yaml") 94 myworkerYAML := readDataFromFile("./testdata/cd-myworker.yaml") 95 96 myworkerDef, err := oamutil.UnMarshalStringToComponentDefinition(myworkerYAML) 97 Expect(err).Should(BeNil()) 98 myingressDef, err := oamutil.UnMarshalStringToTraitDefinition(myingressYAML) 99 Expect(err).Should(BeNil()) 100 101 cdMyWorker, err := oamutil.Object2Unstructured(myworkerDef) 102 Expect(err).Should(BeNil()) 103 tdMyIngress, err := oamutil.Object2Unstructured(myingressDef) 104 Expect(err).Should(BeNil()) 105 106 // create vela-system ns 107 Expect(k8sClient.Create(context.TODO(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: types.DefaultKubeVelaNS}})).Should(Succeed()) 108 // create deploy workflow step definition 109 deploy, err := os.ReadFile("./testdata/wd-deploy.yaml") 110 Expect(err).Should(BeNil()) 111 var wfsd v1beta1.WorkflowStepDefinition 112 Expect(yaml.Unmarshal([]byte(deploy), &wfsd)).Should(BeNil()) 113 wfsd.SetNamespace(types.DefaultKubeVelaNS) 114 Expect(k8sClient.Create(context.TODO(), &wfsd)).Should(BeNil()) 115 116 dryrunOpt = NewDryRunOption(k8sClient, cfg, pd, []*unstructured.Unstructured{cdMyWorker, tdMyIngress}, false) 117 diffOpt = &LiveDiffOption{DryRun: dryrunOpt, Parser: appfile.NewApplicationParser(k8sClient, pd)} 118 }) 119 120 var _ = AfterSuite(func() { 121 By("tearing down the test environment") 122 err := testEnv.Stop() 123 Expect(err).ToNot(HaveOccurred()) 124 }) 125 126 func readDataFromFile(path string) string { 127 b, _ := os.ReadFile(path) 128 return string(b) 129 }