github.com/oam-dev/kubevela@v1.9.11/references/cli/suite_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 cli 18 19 import ( 20 "context" 21 "math/rand" 22 "testing" 23 "time" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 corev1 "k8s.io/api/core/v1" 28 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "k8s.io/client-go/discovery" 30 "k8s.io/client-go/rest" 31 "k8s.io/utils/pointer" 32 "sigs.k8s.io/controller-runtime/pkg/client" 33 "sigs.k8s.io/controller-runtime/pkg/envtest" 34 35 "github.com/oam-dev/kubevela/apis/types" 36 "github.com/oam-dev/kubevela/pkg/utils/common" 37 ) 38 39 func TestCli(t *testing.T) { 40 RegisterFailHandler(Fail) 41 RunSpecs(t, "Cli Suite") 42 } 43 44 var cfg *rest.Config 45 var k8sClient client.Client 46 var testEnv *envtest.Environment 47 var dc *discovery.DiscoveryClient 48 49 var _ = BeforeSuite(func() { 50 rand.Seed(time.Now().UnixNano()) 51 By("bootstrapping test environment") 52 53 testEnv = &envtest.Environment{ 54 ControlPlaneStartTimeout: time.Minute * 3, 55 ControlPlaneStopTimeout: time.Minute, 56 UseExistingCluster: pointer.Bool(false), 57 CRDDirectoryPaths: []string{"../../charts/vela-core/crds"}, 58 } 59 60 By("start kube test env") 61 var err error 62 cfg, err = testEnv.Start() 63 Expect(err).ShouldNot(HaveOccurred()) 64 Expect(cfg).ToNot(BeNil()) 65 66 By("new kube client") 67 cfg.Timeout = time.Minute * 2 68 k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme}) 69 Expect(err).Should(BeNil()) 70 Expect(k8sClient).ToNot(BeNil()) 71 72 dc, err = discovery.NewDiscoveryClientForConfig(cfg) 73 Expect(err).ToNot(HaveOccurred()) 74 Expect(dc).ShouldNot(BeNil()) 75 76 By("new namespace") 77 err = k8sClient.Create(context.TODO(), &corev1.Namespace{ 78 ObjectMeta: v1.ObjectMeta{Name: types.DefaultKubeVelaNS}, 79 }) 80 Expect(err).Should(BeNil()) 81 }) 82 83 var _ = AfterSuite(func() { 84 if testEnv != nil { 85 err := testEnv.Stop() 86 Expect(err).Should(BeNil()) 87 } 88 })