2022-12-19 10:52:15 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2023-01-04 07:55:10 -05:00
|
|
|
"context"
|
2022-12-19 10:52:15 -05:00
|
|
|
"testing"
|
2023-01-04 07:55:10 -05:00
|
|
|
"time"
|
2022-12-19 10:52:15 -05:00
|
|
|
|
2023-07-24 04:30:53 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-02-28 04:23:09 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
2023-02-14 12:04:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2023-01-04 07:55:10 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-07-25 08:20:25 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/semver"
|
2022-12-19 10:52:15 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-03-20 09:49:04 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-04 07:55:10 -05:00
|
|
|
"helm.sh/helm/v3/pkg/chart"
|
|
|
|
"helm.sh/helm/v3/pkg/release"
|
2022-12-19 10:52:15 -05:00
|
|
|
)
|
|
|
|
|
2023-03-20 09:49:04 -04:00
|
|
|
func TestShouldUpgrade(t *testing.T) {
|
2023-01-04 07:55:10 -05:00
|
|
|
testCases := map[string]struct {
|
2023-02-28 04:23:09 -05:00
|
|
|
version string
|
|
|
|
assertCorrectError func(t *testing.T, err error) bool
|
|
|
|
wantError bool
|
2023-01-04 07:55:10 -05:00
|
|
|
}{
|
2023-03-20 09:49:04 -04:00
|
|
|
"valid upgrade": {
|
|
|
|
version: "1.9.0",
|
2023-02-28 04:23:09 -05:00
|
|
|
},
|
|
|
|
"not a valid upgrade": {
|
2023-03-20 09:49:04 -04:00
|
|
|
version: "1.0.0",
|
2023-02-28 04:23:09 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-06-30 10:46:05 -04:00
|
|
|
var target *compatibility.InvalidUpgradeError
|
2023-02-28 04:23:09 -05:00
|
|
|
return assert.ErrorAs(t, err, &target)
|
|
|
|
},
|
|
|
|
wantError: true,
|
2023-01-04 07:55:10 -05:00
|
|
|
},
|
2023-03-20 09:49:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-08-02 09:49:40 -04:00
|
|
|
client := UpgradeClient{kubectl: nil, actions: &stubActionWrapper{version: tc.version}, log: logger.NewTest(t)}
|
2023-03-20 09:49:04 -04:00
|
|
|
|
|
|
|
chart, err := loadChartsDir(helmFS, certManagerInfo.path)
|
|
|
|
require.NoError(err)
|
2023-07-25 08:20:25 -04:00
|
|
|
chartVersion, err := semver.New(chart.Metadata.Version)
|
|
|
|
require.NoError(err)
|
|
|
|
err = client.shouldUpgrade(certManagerInfo.releaseName, chartVersion, false)
|
2023-03-20 09:49:04 -04:00
|
|
|
if tc.wantError {
|
|
|
|
tc.assertCorrectError(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeRelease(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2023-07-03 09:13:36 -04:00
|
|
|
version string
|
|
|
|
wantError bool
|
2023-03-20 09:49:04 -04:00
|
|
|
}{
|
|
|
|
"allow": {
|
2023-07-03 09:13:36 -04:00
|
|
|
version: "1.9.0",
|
2023-01-04 07:55:10 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2023-03-20 09:49:04 -04:00
|
|
|
require := require.New(t)
|
2023-01-04 07:55:10 -05:00
|
|
|
|
2023-08-02 09:49:40 -04:00
|
|
|
client := UpgradeClient{kubectl: nil, actions: &stubActionWrapper{version: tc.version}, log: logger.NewTest(t)}
|
2023-03-20 09:49:04 -04:00
|
|
|
|
|
|
|
chart, err := loadChartsDir(helmFS, certManagerInfo.path)
|
|
|
|
require.NoError(err)
|
2023-07-24 04:30:53 -04:00
|
|
|
err = client.upgradeRelease(context.Background(), 0, config.Default(), clusterid.File{UID: "test"}, chart)
|
2023-01-04 07:55:10 -05:00
|
|
|
if tc.wantError {
|
2023-07-03 09:13:36 -04:00
|
|
|
assert.Error(err)
|
2023-01-04 07:55:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 04:23:09 -05:00
|
|
|
type stubActionWrapper struct {
|
|
|
|
version string
|
|
|
|
}
|
2023-01-04 07:55:10 -05:00
|
|
|
|
|
|
|
// listAction returns a list of len 1 with a release that has only it's version set.
|
|
|
|
func (a *stubActionWrapper) listAction(_ string) ([]*release.Release, error) {
|
2023-02-28 04:23:09 -05:00
|
|
|
return []*release.Release{{Chart: &chart.Chart{Metadata: &chart.Metadata{Version: a.version}}}}, nil
|
2023-01-04 07:55:10 -05:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (a *stubActionWrapper) getValues(_ string) (map[string]any, error) {
|
2023-01-04 07:55:10 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 07:43:23 -04:00
|
|
|
func (a *stubActionWrapper) installAction(_ context.Context, _ string, _ *chart.Chart, _ map[string]any, _ time.Duration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (a *stubActionWrapper) upgradeAction(_ context.Context, _ string, _ *chart.Chart, _ map[string]any, _ time.Duration) error {
|
2023-01-04 07:55:10 -05:00
|
|
|
return nil
|
|
|
|
}
|