github.com/oam-dev/kubevela@v1.9.11/test/e2e-addon-test/addon_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 controllers_test
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"math/rand"
    24  	"os/exec"
    25  	"strconv"
    26  	"time"
    27  
    28  	terraformv1beta1 "github.com/oam-dev/terraform-controller/api/v1beta1"
    29  	. "github.com/onsi/ginkgo/v2"
    30  	. "github.com/onsi/gomega"
    31  	corev1 "k8s.io/api/core/v1"
    32  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    33  	"sigs.k8s.io/controller-runtime/pkg/client"
    34  
    35  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    36  	"github.com/oam-dev/kubevela/pkg/oam/util"
    37  	"github.com/oam-dev/kubevela/pkg/utils/common"
    38  )
    39  
    40  var _ = Describe("Addon tests", func() {
    41  	ctx := context.Background()
    42  	var namespaceName string
    43  	var ns corev1.Namespace
    44  	var app v1beta1.Application
    45  
    46  	createNamespace := func() {
    47  		ns = corev1.Namespace{
    48  			ObjectMeta: metav1.ObjectMeta{
    49  				Name: namespaceName,
    50  			},
    51  		}
    52  		// delete the namespaceName with all its resources
    53  		Eventually(
    54  			func() error {
    55  				return k8sClient.Delete(ctx, &ns, client.PropagationPolicy(metav1.DeletePropagationForeground))
    56  			},
    57  			time.Second*120, time.Millisecond*500).Should(SatisfyAny(BeNil(), &util.NotFoundMatcher{}))
    58  		By("make sure all the resources are removed")
    59  		objectKey := client.ObjectKey{
    60  			Name: namespaceName,
    61  		}
    62  		res := &corev1.Namespace{}
    63  		Eventually(
    64  			func() error {
    65  				return k8sClient.Get(ctx, objectKey, res)
    66  			},
    67  			time.Second*120, time.Millisecond*500).Should(&util.NotFoundMatcher{})
    68  		Eventually(
    69  			func() error {
    70  				return k8sClient.Create(ctx, &ns)
    71  			},
    72  			time.Second*3, time.Millisecond*300).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
    73  	}
    74  
    75  	BeforeEach(func() {
    76  		By("Start to run a test, clean up previous resources")
    77  		namespaceName = "app-terraform" + "-" + strconv.FormatInt(rand.Int63(), 16)
    78  		createNamespace()
    79  	})
    80  
    81  	AfterEach(func() {
    82  		By("Clean up resources after a test")
    83  		k8sClient.Delete(ctx, &app)
    84  		By(fmt.Sprintf("Delete the entire namespaceName %s", ns.Name))
    85  		// delete the namespaceName with all its resources
    86  		Expect(k8sClient.Delete(ctx, &ns, client.PropagationPolicy(metav1.DeletePropagationBackground))).Should(BeNil())
    87  	})
    88  
    89  	It("Addon Terraform is successfully enabled and Terraform application works", func() {
    90  		By("Install Addon Terraform")
    91  		output, err := exec.Command("bash", "-c", "/tmp/vela addon enable terraform-alibaba").Output()
    92  		var ee *exec.ExitError
    93  		if errors.As(err, &ee) {
    94  			fmt.Println("exit code error:", string(ee.Stderr))
    95  		}
    96  		Expect(err).Should(BeNil())
    97  		Expect(string(output)).Should(ContainSubstring("enabled successfully"))
    98  
    99  		By("Checking Provider")
   100  		Eventually(func() error {
   101  			var provider terraformv1beta1.Provider
   102  			return k8sClient.Get(ctx, client.ObjectKey{Name: "default", Namespace: "default"}, &provider)
   103  		}, time.Second*120, time.Millisecond*500).Should(BeNil())
   104  
   105  		By("Apply an application with Terraform Component")
   106  		var terraformApp v1beta1.Application
   107  		Expect(common.ReadYamlToObject("testdata/app/app_terraform_oss.yaml", &terraformApp)).Should(BeNil())
   108  		terraformApp.Namespace = namespaceName
   109  		Eventually(func() error {
   110  			return k8sClient.Create(ctx, terraformApp.DeepCopy())
   111  		}, 10*time.Second, 500*time.Millisecond).Should(Succeed())
   112  
   113  		By("Check status.services of the application")
   114  		Eventually(
   115  			func() error {
   116  				k8sClient.Get(ctx, client.ObjectKey{Namespace: terraformApp.Namespace, Name: terraformApp.Name}, &app)
   117  				if len(app.Status.Services) == 1 {
   118  					return nil
   119  				}
   120  				return errors.New("expect 1 service")
   121  			},
   122  			time.Second*30, time.Millisecond*500).ShouldNot(BeNil())
   123  	})
   124  
   125  	PIt("Addon observability is successfully enabled", func() {
   126  		By("Install Addon Observability")
   127  		output, err := exec.Command("bash", "-c", "/tmp/vela addon enable observability domain=abc.com disk-size=20Gi").Output()
   128  		var ee *exec.ExitError
   129  		if errors.As(err, &ee) {
   130  			fmt.Println("exit code error:", string(ee.Stderr))
   131  		}
   132  		Expect(err).Should(BeNil())
   133  		Expect(string(output)).Should(ContainSubstring("enabled successfully"))
   134  	})
   135  })