github.com/oam-dev/kubevela@v1.9.11/pkg/utils/helm/helm_helper_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 helm
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	"github.com/google/go-cmp/cmp"
    30  	v1 "k8s.io/api/core/v1"
    31  	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/apimachinery/pkg/types"
    33  	"sigs.k8s.io/yaml"
    34  
    35  	types2 "github.com/oam-dev/kubevela/apis/types"
    36  	util2 "github.com/oam-dev/kubevela/pkg/oam/util"
    37  	"github.com/oam-dev/kubevela/pkg/utils/util"
    38  )
    39  
    40  var _ = Describe("Test helm helper", func() {
    41  
    42  	ctx := context.Background()
    43  
    44  	It("Test LoadCharts ", func() {
    45  		helper := NewHelper()
    46  		chart, err := helper.LoadCharts("./testdata/autoscalertrait-0.1.0.tgz", nil)
    47  		Expect(err).Should(BeNil())
    48  		Expect(chart).ShouldNot(BeNil())
    49  		Expect(chart.Metadata).ShouldNot(BeNil())
    50  		Expect(cmp.Diff(chart.Metadata.Version, "0.1.0")).Should(BeEmpty())
    51  	})
    52  
    53  	It("Test UpgradeChart", func() {
    54  		helper := NewHelper()
    55  		chart, err := helper.LoadCharts("./testdata/autoscalertrait-0.1.0.tgz", nil)
    56  		Expect(err).Should(BeNil())
    57  		release, err := helper.UpgradeChart(chart, "autoscalertrait", "default", map[string]interface{}{
    58  			"replicaCount": 2,
    59  			"image.tag":    "0.1.0",
    60  		}, UpgradeChartOptions{
    61  			Config:  cfg,
    62  			Detail:  false,
    63  			Logging: util.IOStreams{Out: os.Stdout, ErrOut: os.Stderr},
    64  			Wait:    false,
    65  		})
    66  		crds := GetCRDFromChart(release.Chart)
    67  		Expect(cmp.Diff(len(crds), 1)).Should(BeEmpty())
    68  		Expect(err).Should(BeNil())
    69  		deployments := GetDeploymentsFromManifest(release.Manifest)
    70  		Expect(cmp.Diff(len(deployments), 1)).Should(BeEmpty())
    71  		Expect(cmp.Diff(*deployments[0].Spec.Replicas, int32(2))).Should(BeEmpty())
    72  		containers := deployments[0].Spec.Template.Spec.Containers
    73  		Expect(cmp.Diff(len(containers), 1)).Should(BeEmpty())
    74  
    75  		// add new default value
    76  		Expect(cmp.Diff(containers[0].Image, "ghcr.io/oam-dev/catalog/autoscalertrait:0.1.0")).Should(BeEmpty())
    77  
    78  		chartNew, err := helper.LoadCharts("./testdata/autoscalertrait-0.2.0.tgz", nil)
    79  		Expect(err).Should(BeNil())
    80  
    81  		// the new custom values should override the last release custom values
    82  		releaseNew, err := helper.UpgradeChart(chartNew, "autoscalertrait", "default", map[string]interface{}{
    83  			"image.tag": "0.2.0",
    84  		}, UpgradeChartOptions{
    85  			Config:      cfg,
    86  			Detail:      false,
    87  			ReuseValues: true,
    88  			Logging:     util.IOStreams{Out: os.Stdout, ErrOut: os.Stderr},
    89  			Wait:        false,
    90  		})
    91  		Expect(err).Should(BeNil())
    92  		deployments = GetDeploymentsFromManifest(releaseNew.Manifest)
    93  		Expect(cmp.Diff(len(deployments), 1)).Should(BeEmpty())
    94  		// keep the custom values
    95  		Expect(cmp.Diff(*deployments[0].Spec.Replicas, int32(2))).Should(BeEmpty())
    96  		containers = deployments[0].Spec.Template.Spec.Containers
    97  		Expect(cmp.Diff(len(containers), 1)).Should(BeEmpty())
    98  
    99  		// change the default value
   100  		Expect(cmp.Diff(containers[0].Image, "ghcr.io/oam-dev/catalog/autoscalertrait:0.2.0")).Should(BeEmpty())
   101  
   102  		// add new default value
   103  		Expect(cmp.Diff(len(containers[0].Env), 1)).Should(BeEmpty())
   104  		Expect(cmp.Diff(containers[0].Env[0].Name, "env1")).Should(BeEmpty())
   105  	})
   106  
   107  	It("Test UninstallRelease", func() {
   108  		helper := NewHelper()
   109  		err := helper.UninstallRelease("autoscalertrait", "default", cfg, false, util.IOStreams{Out: os.Stdout, ErrOut: os.Stderr})
   110  		Expect(err).Should(BeNil())
   111  	})
   112  
   113  	It("Test ListVersions ", func() {
   114  		helper := NewHelper()
   115  		versions, err := helper.ListVersions("./testdata", "autoscalertrait", true, nil)
   116  		Expect(err).Should(BeNil())
   117  		Expect(cmp.Diff(len(versions), 3)).Should(BeEmpty())
   118  	})
   119  
   120  	It("Test getValues from chart", func() {
   121  		helper := NewHelper()
   122  		values, err := helper.GetValuesFromChart("./testdata", "autoscalertrait", "0.2.0", true, "helm", nil)
   123  		Expect(err).Should(BeNil())
   124  		Expect(values).ShouldNot(BeNil())
   125  	})
   126  
   127  	It("Test getValues from chart with uncompleted index.yaml url", func() {
   128  		helper := NewHelper()
   129  		values, err := helper.GetValuesFromChart("./testdata", "autoscalertrait", "0.2.0_p1", true, "helm", nil)
   130  		Expect(err).Should(BeNil())
   131  		Expect(values).ShouldNot(BeNil())
   132  	})
   133  
   134  	It("Test validate helm repo", func() {
   135  		helper := NewHelper()
   136  		helmRepo := &Repository{
   137  			URL: "https://charts.kubevela.net/core",
   138  		}
   139  		ok, err := helper.ValidateRepo(ctx, helmRepo)
   140  		Expect(err).Should(BeNil())
   141  		Expect(ok).Should(BeTrue())
   142  	})
   143  
   144  	It("Test validate the corrupt helm repo", func() {
   145  		helper := NewHelper()
   146  		svr := httptest.NewTLSServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
   147  			_, _ = fmt.Fprintf(writer, "corrupted")
   148  		}))
   149  		defer svr.Close()
   150  		helmRepo := &Repository{URL: svr.URL}
   151  		ok, err := helper.ValidateRepo(ctx, helmRepo)
   152  		Expect(err).To(HaveOccurred())
   153  		Expect(ok).Should(BeFalse())
   154  	})
   155  })
   156  
   157  var _ = Describe("Test helm associated func", func() {
   158  	ctx := context.Background()
   159  	var aSec v1.Secret
   160  
   161  	BeforeEach(func() {
   162  		Expect(k8sClient.Create(ctx, &v1.Namespace{ObjectMeta: v12.ObjectMeta{Name: "vela-system"}})).Should(SatisfyAny(BeNil(), util2.AlreadyExistMatcher{}))
   163  		aSec = v1.Secret{}
   164  		Expect(yaml.Unmarshal([]byte(authSecret), &aSec)).Should(BeNil())
   165  		Expect(k8sClient.Create(ctx, &aSec)).Should(SatisfyAny(BeNil(), util2.AlreadyExistMatcher{}))
   166  
   167  		bSec := v1.Secret{}
   168  		Expect(yaml.Unmarshal([]byte(caFileSecret), &bSec)).Should(BeNil())
   169  		Expect(k8sClient.Create(ctx, &bSec)).Should(SatisfyAny(BeNil(), util2.AlreadyExistMatcher{}))
   170  	})
   171  
   172  	It("Test auth info secret func", func() {
   173  		opts, err := SetHTTPOption(context.Background(), k8sClient, types.NamespacedName{Namespace: types2.DefaultKubeVelaNS, Name: "auth-secret"})
   174  		Expect(err).Should(BeNil())
   175  		Expect(opts.Username).Should(BeEquivalentTo("admin"))
   176  		Expect(opts.Password).Should(BeEquivalentTo("admin"))
   177  	})
   178  
   179  	It("Test auth info secret func", func() {
   180  		_, err := SetHTTPOption(context.Background(), k8sClient, types.NamespacedName{Namespace: types2.DefaultKubeVelaNS, Name: "auth-secret-1"})
   181  		Expect(err).ShouldNot(BeNil())
   182  	})
   183  
   184  	It("Test ac secret func", func() {
   185  		opts, err := SetHTTPOption(context.Background(), k8sClient, types.NamespacedName{Namespace: types2.DefaultKubeVelaNS, Name: "ca-secret"})
   186  		Expect(err).Should(BeNil())
   187  		Expect(opts.CaFile).Should(BeEquivalentTo("testfile"))
   188  	})
   189  })
   190  
   191  var (
   192  	authSecret = `
   193  apiVersion: v1
   194  kind: Secret
   195  metadata:
   196    name: auth-secret
   197    namespace: vela-system
   198    labels:
   199      config.oam.dev/type: config-helm-repository
   200      config.oam.dev/project: my-project-1
   201  stringData:
   202    url: https://kedacore.github.io/charts
   203    username: admin
   204    password: admin
   205  type: Opaque
   206  `
   207  	caFileSecret = `
   208  apiVersion: v1
   209  kind: Secret
   210  metadata:
   211    name: ca-secret
   212    namespace: vela-system
   213    labels:
   214      config.oam.dev/type: config-helm-repository
   215      config.oam.dev/project: my-project-1
   216  stringData:
   217    url: https://kedacore.github.io/charts
   218    caFile: testfile
   219  type: Opaque
   220  `
   221  )