github.com/oam-dev/kubevela@v1.9.11/test/e2e-multicluster-test/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 e2e_multicluster_test 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/client-go/kubernetes" 28 "k8s.io/utils/strings/slices" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 "sigs.k8s.io/controller-runtime/pkg/client/config" 31 32 "github.com/kubevela/pkg/multicluster" 33 34 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 35 "github.com/oam-dev/kubevela/apis/types" 36 multicluster2 "github.com/oam-dev/kubevela/pkg/multicluster" 37 oamutil "github.com/oam-dev/kubevela/pkg/oam/util" 38 "github.com/oam-dev/kubevela/pkg/utils/common" 39 "github.com/oam-dev/kubevela/pkg/utils/util" 40 "github.com/oam-dev/kubevela/references/cli" 41 ) 42 43 const ( 44 WorkerClusterName = "cluster-worker" 45 WorkerClusterKubeConfigPath = "/tmp/worker.kubeconfig" 46 ) 47 48 var ( 49 k8sClient client.Client 50 k8sCli kubernetes.Interface 51 ) 52 53 func execCommand(args ...string) (string, error) { 54 ioStream, buf := util.NewTestIOStreams() 55 command := cli.NewCommandWithIOStreams(ioStream) 56 command.SetArgs(args) 57 command.SetOut(buf) 58 command.SetErr(buf) 59 err := command.Execute() 60 return buf.String(), err 61 } 62 63 func TestMulticluster(t *testing.T) { 64 RegisterFailHandler(Fail) 65 RunSpecs(t, "KubeVela MultiCluster Test Suite") 66 } 67 68 var _ = BeforeSuite(func() { 69 var err error 70 71 // initialize clients 72 options := client.Options{Scheme: common.Scheme} 73 config := config.GetConfigOrDie() 74 config.Wrap(multicluster.NewTransportWrapper()) 75 k8sClient, err = multicluster.NewClient(config, multicluster.ClientOptions{Options: options}) 76 Expect(err).Should(Succeed()) 77 k8sCli, err = kubernetes.NewForConfig(config) 78 Expect(err).Should(Succeed()) 79 80 // join worker cluster 81 _, err = execCommand("cluster", "join", WorkerClusterKubeConfigPath, "--name", WorkerClusterName) 82 Expect(err).Should(Succeed()) 83 cv, err := multicluster2.GetVersionInfoFromCluster(context.Background(), WorkerClusterName, config) 84 Expect(err).Should(Succeed()) 85 Expect(cv.Minor).Should(Not(BeEquivalentTo(""))) 86 Expect(cv.Major).Should(BeEquivalentTo("1")) 87 ocv := multicluster2.GetVersionInfoFromObject(context.Background(), k8sClient, WorkerClusterName) 88 Expect(ocv).Should(BeEquivalentTo(cv)) 89 }) 90 91 var _ = AfterSuite(func() { 92 holdAddons := []string{"addon-terraform", "addon-fluxcd"} 93 Eventually(func(g Gomega) { 94 apps := &v1beta1.ApplicationList{} 95 g.Expect(k8sClient.List(context.Background(), apps)).Should(Succeed()) 96 for _, app := range apps.Items { 97 if slices.Contains(holdAddons, app.Name) { 98 continue 99 } 100 g.Expect(k8sClient.Delete(context.Background(), app.DeepCopy())).Should(Succeed()) 101 } 102 }, 3*time.Minute).Should(Succeed()) 103 Eventually(func(g Gomega) { 104 // Delete terraform and fluxcd in order 105 app := &v1beta1.Application{} 106 apps := &v1beta1.ApplicationList{} 107 for _, addon := range holdAddons { 108 g.Expect(k8sClient.Delete(context.Background(), &v1beta1.Application{ObjectMeta: v1.ObjectMeta{Name: addon, Namespace: types.DefaultKubeVelaNS}})).Should(SatisfyAny(Succeed(), oamutil.NotFoundMatcher{})) 109 g.Expect(k8sClient.Get(context.Background(), client.ObjectKey{Name: addon, Namespace: types.DefaultKubeVelaNS}, app)).Should(SatisfyAny(Succeed(), oamutil.NotFoundMatcher{})) 110 } 111 err := k8sClient.List(context.Background(), apps) 112 g.Expect(err, nil) 113 g.Expect(len(apps.Items)).Should(Equal(0)) 114 }, 5*time.Minute).Should(Succeed()) 115 Eventually(func(g Gomega) { 116 _, err := execCommand("cluster", "detach", WorkerClusterName) 117 g.Expect(err).Should(Succeed()) 118 }, time.Minute).Should(Succeed()) 119 })