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-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"
|
2022-12-19 10:52:15 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
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-01-04 07:55:10 -05:00
|
|
|
func TestUpgradeRelease(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2023-02-28 04:23:09 -05:00
|
|
|
allowDestructive bool
|
|
|
|
version string
|
|
|
|
assertCorrectError func(t *testing.T, err error) bool
|
|
|
|
wantError bool
|
2023-01-04 07:55:10 -05:00
|
|
|
}{
|
|
|
|
"allow": {
|
|
|
|
allowDestructive: true,
|
2023-02-28 04:23:09 -05:00
|
|
|
version: "1.9.0",
|
|
|
|
},
|
|
|
|
"not a valid upgrade": {
|
|
|
|
allowDestructive: true,
|
|
|
|
version: "1.0.0",
|
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
|
|
|
target := &compatibility.InvalidUpgradeError{}
|
|
|
|
return assert.ErrorAs(t, err, &target)
|
|
|
|
},
|
|
|
|
wantError: true,
|
2023-01-04 07:55:10 -05:00
|
|
|
},
|
|
|
|
"deny": {
|
|
|
|
allowDestructive: false,
|
2023-02-28 04:23:09 -05:00
|
|
|
version: "1.9.0",
|
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
|
|
|
return assert.ErrorIs(t, err, ErrConfirmationMissing)
|
|
|
|
},
|
|
|
|
wantError: true,
|
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-02-28 04:23:09 -05:00
|
|
|
client := Client{kubectl: nil, actions: &stubActionWrapper{version: tc.version}, log: logger.NewTest(t)}
|
2023-02-14 12:04:58 -05:00
|
|
|
err := client.upgradeRelease(context.Background(), 0, config.Default(), certManagerPath, certManagerReleaseName, false, tc.allowDestructive)
|
2023-01-04 07:55:10 -05:00
|
|
|
if tc.wantError {
|
2023-02-28 04:23:09 -05:00
|
|
|
tc.assertCorrectError(t, 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
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubActionWrapper) getValues(release string) (map[string]any, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubActionWrapper) upgradeAction(ctx context.Context, releaseName string, chart *chart.Chart, values map[string]any, timeout time.Duration) error {
|
|
|
|
return nil
|
|
|
|
}
|