github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/util/helm/helm_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package helm
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	"github.com/pkg/errors"
    26  	"helm.sh/helm/v3/pkg/action"
    27  	"helm.sh/helm/v3/pkg/chart"
    28  	"helm.sh/helm/v3/pkg/release"
    29  	"helm.sh/helm/v3/pkg/repo"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    32  	"github.com/1aal/kubeblocks/pkg/cli/types"
    33  	"github.com/1aal/kubeblocks/version"
    34  )
    35  
    36  var _ = Describe("helm util", func() {
    37  	It("add and remove repo", func() {
    38  		r := repo.Entry{
    39  			Name: "test-repo",
    40  			URL:  "https://test-kubebllcks.com/test-repo",
    41  		}
    42  		Expect(AddRepo(&r)).Should(HaveOccurred())
    43  		Expect(RemoveRepo(&r)).Should(Succeed())
    44  	})
    45  
    46  	It("Action Config", func() {
    47  		cfg := NewConfig("test", "config", "context", false)
    48  		actionCfg, err := NewActionConfig(cfg)
    49  		Expect(err).ShouldNot(HaveOccurred())
    50  		Expect(actionCfg).ShouldNot(BeNil())
    51  	})
    52  
    53  	Context("Install", func() {
    54  		var o *InstallOpts
    55  		var cfg *Config
    56  		var actionCfg *action.Configuration
    57  
    58  		BeforeEach(func() {
    59  			o = &InstallOpts{
    60  				Name:      testing.KubeBlocksChartName,
    61  				Chart:     testing.KubeBlocksChartURL,
    62  				Namespace: "default",
    63  				Version:   version.DefaultKubeBlocksVersion,
    64  			}
    65  			cfg = NewFakeConfig("default")
    66  			actionCfg, _ = NewActionConfig(cfg)
    67  			Expect(actionCfg).ShouldNot(BeNil())
    68  		})
    69  
    70  		It("Install", func() {
    71  			_, err := o.Install(cfg)
    72  			Expect(err).Should(HaveOccurred())
    73  			Expect(o.Uninstall(cfg)).Should(HaveOccurred()) // release not found
    74  		})
    75  
    76  		It("should ignore when chart is already deployed", func() {
    77  			err := actionCfg.Releases.Create(&release.Release{
    78  				Name:    o.Name,
    79  				Version: 1,
    80  				Info: &release.Info{
    81  					Status: release.StatusDeployed,
    82  				},
    83  			})
    84  			Expect(err).Should(BeNil())
    85  			_, err = o.tryInstall(actionCfg)
    86  			Expect(err).Should(BeNil())
    87  			Expect(o.tryUninstall(actionCfg)).Should(BeNil()) // release exists
    88  		})
    89  
    90  		It("should fail when chart is failed installed", func() {
    91  			err := actionCfg.Releases.Create(&release.Release{
    92  				Name:    o.Name,
    93  				Version: 1,
    94  				Info: &release.Info{
    95  					Status: release.StatusFailed,
    96  				},
    97  			})
    98  			Expect(err).Should(BeNil())
    99  			_, err = o.Install(cfg)
   100  			Expect(err).Should(HaveOccurred())
   101  		})
   102  	})
   103  
   104  	Context("Upgrade", func() {
   105  		var o *InstallOpts
   106  		var cfg *Config
   107  		var actionCfg *action.Configuration
   108  
   109  		BeforeEach(func() {
   110  			o = &InstallOpts{
   111  				Name:      types.KubeBlocksChartName,
   112  				Chart:     "kubeblocks-test-chart",
   113  				Namespace: "default",
   114  				Version:   version.DefaultKubeBlocksVersion,
   115  			}
   116  			cfg = NewFakeConfig("default")
   117  			actionCfg, _ = NewActionConfig(cfg)
   118  			Expect(actionCfg).ShouldNot(BeNil())
   119  		})
   120  
   121  		It("should fail when release is not found", func() {
   122  			Expect(ReleaseNotFound(o.Upgrade(cfg))).Should(BeTrue())
   123  			Expect(o.Uninstall(cfg)).Should(HaveOccurred()) // release not found
   124  		})
   125  
   126  		It("should fail at fetching charts when release is already deployed", func() {
   127  			err := actionCfg.Releases.Create(&release.Release{
   128  				Name:    o.Name,
   129  				Version: 1,
   130  				Info: &release.Info{
   131  					Status: release.StatusDeployed,
   132  				},
   133  				Chart: &chart.Chart{},
   134  			})
   135  			Expect(err).Should(BeNil())
   136  			_, err = o.tryUpgrade(actionCfg)
   137  			Expect(err).Should(HaveOccurred())                // failed at fetching charts
   138  			Expect(o.tryUninstall(actionCfg)).Should(BeNil()) // release exists
   139  		})
   140  
   141  		It("should fail when chart is already deployed", func() {
   142  			err := actionCfg.Releases.Create(&release.Release{
   143  				Name:    o.Name,
   144  				Version: 1,
   145  				Info: &release.Info{
   146  					Status: release.StatusFailed,
   147  				},
   148  				Chart: &chart.Chart{},
   149  			})
   150  			Expect(err).Should(BeNil())
   151  			_, err = o.tryUpgrade(actionCfg)
   152  			Expect(errors.Is(err, ErrReleaseNotDeployed)).Should(BeTrue())
   153  			Expect(o.tryUninstall(actionCfg)).Should(BeNil()) // release exists
   154  		})
   155  	})
   156  
   157  	It("get chart versions", func() {
   158  		versions, _ := GetChartVersions(testing.KubeBlocksChartName)
   159  		Expect(versions).Should(BeNil())
   160  	})
   161  })