github.com/oam-dev/kubevela@v1.9.11/test/e2e-multicluster-test/multicluster_adopt_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  	"fmt"
    22  	"os/exec"
    23  	"time"
    24  
    25  	"github.com/kubevela/pkg/util/k8s"
    26  	"github.com/kubevela/pkg/util/rand"
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  	corev1 "k8s.io/api/core/v1"
    30  	"k8s.io/apimachinery/pkg/api/errors"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/apimachinery/pkg/types"
    33  
    34  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    35  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    36  )
    37  
    38  var _ = Describe("Test adopt commands", func() {
    39  
    40  	Context("Test vela adopt commands", func() {
    41  
    42  		var ns string
    43  
    44  		BeforeEach(func() {
    45  			ns = "test-adopt-" + rand.RandomString(4)
    46  			Expect(k8s.EnsureNamespace(context.Background(), k8sClient, ns)).Should(Succeed())
    47  		})
    48  
    49  		AfterEach(func() {
    50  			Expect(k8s.ClearNamespace(context.Background(), k8sClient, ns)).Should(Succeed())
    51  		})
    52  
    53  		It("Test vela adopt native resources with read-only mode", func() {
    54  			ctx := context.Background()
    55  			Expect(k8sClient.Create(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "adopt-cm", Namespace: ns}})).Should(Succeed())
    56  			Expect(k8sClient.Create(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "adopt-secret", Namespace: ns}})).Should(Succeed())
    57  			_, err := execCommand("adopt", "configmap/adopt-cm", "secret/adopt-secret", "--app-name=adopt-test", "-n="+ns, "--apply")
    58  			Expect(err).Should(Succeed())
    59  			app := &v1beta1.Application{}
    60  			Eventually(func(g Gomega) {
    61  				g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "adopt-test", Namespace: ns}, app)).Should(Succeed())
    62  				g.Expect(app.Status.Phase).Should(Equal(common.ApplicationRunning))
    63  			}).WithTimeout(20 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
    64  			Expect(k8sClient.Delete(ctx, app)).Should(Succeed())
    65  			Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "adopt-cm", Namespace: ns}, &corev1.ConfigMap{})).Should(Succeed())
    66  			Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "adopt-cm", Namespace: ns}, &corev1.ConfigMap{})).Should(Succeed())
    67  		})
    68  
    69  		It("Test vela adopt helm chart with take-over mode", func() {
    70  			ctx, fn := context.WithTimeout(context.Background(), time.Second*60)
    71  			defer fn()
    72  			bs, err := exec.CommandContext(ctx, "helm", "install", "vela-test", "./testdata/chart/test", "-n", ns).CombinedOutput()
    73  			_, _ = fmt.Fprintf(GinkgoWriter, "%s\n", string(bs))
    74  			Expect(err).Should(Succeed())
    75  			_, err = execCommand("adopt", "vela-test", "--mode=take-over", "--type=helm", "-n="+ns, "--apply", "--recycle")
    76  			Expect(err).Should(Succeed())
    77  			app := &v1beta1.Application{}
    78  			Eventually(func(g Gomega) {
    79  				g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "vela-test", Namespace: ns}, app)).Should(Succeed())
    80  				g.Expect(app.Status.Phase).Should(Equal(common.ApplicationRunning))
    81  				g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "vela-test", Namespace: ns}, &corev1.ConfigMap{})).Should(Succeed())
    82  			}).WithTimeout(20 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
    83  			Expect(k8sClient.Delete(ctx, app)).Should(Succeed())
    84  			Eventually(func(g Gomega) {
    85  				g.Expect(errors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: "vela-test", Namespace: ns}, &corev1.ConfigMap{}))).Should(BeTrue())
    86  			}).WithTimeout(10 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
    87  		})
    88  
    89  		It("Test vela adopt resources from multiple cluster", func() {
    90  			hubCtx, workerCtx, _ns := initializeContextAndNamespace()
    91  			Expect(k8sClient.Create(hubCtx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "adopt-cm", Namespace: _ns}})).Should(Succeed())
    92  			Expect(k8sClient.Create(workerCtx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "adopt-secret", Namespace: _ns}})).Should(Succeed())
    93  			_, err := execCommand("adopt", fmt.Sprintf("configmap/local/%s/adopt-cm", _ns), fmt.Sprintf("secret/%s/%s/adopt-secret", WorkerClusterName, _ns), "--app-name=adopt-test", "-n="+_ns, "--apply")
    94  			Expect(err).Should(Succeed())
    95  			app := &v1beta1.Application{}
    96  			Eventually(func(g Gomega) {
    97  				g.Expect(k8sClient.Get(hubCtx, types.NamespacedName{Name: "adopt-test", Namespace: _ns}, app)).Should(Succeed())
    98  				g.Expect(app.Status.Phase).Should(Equal(common.ApplicationRunning))
    99  			}).WithTimeout(20 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
   100  			Expect(k8sClient.Delete(hubCtx, app)).Should(Succeed())
   101  		})
   102  	})
   103  
   104  })