2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-22 07:31:20 -04:00
|
|
|
"bytes"
|
2022-08-29 10:49:44 -04:00
|
|
|
"context"
|
2023-08-23 04:35:42 -04:00
|
|
|
"io"
|
2022-08-29 10:49:44 -04:00
|
|
|
"testing"
|
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-09-08 08:55:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/helm"
|
2023-08-21 10:15:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/kubecmd"
|
2023-08-11 09:18:59 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-08-11 09:18:59 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
|
2023-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-09-19 07:50:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/semver"
|
2023-09-08 08:55:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-09-08 08:55:07 -04:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-08-24 10:40:47 -04:00
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
2022-08-29 10:49:44 -04:00
|
|
|
)
|
|
|
|
|
2023-02-01 04:56:47 -05:00
|
|
|
func TestUpgradeApply(t *testing.T) {
|
2022-08-29 10:49:44 -04:00
|
|
|
testCases := map[string]struct {
|
2023-09-08 08:55:07 -04:00
|
|
|
helmUpgrader helmApplier
|
2023-08-21 10:15:32 -04:00
|
|
|
kubeUpgrader *stubKubernetesUpgrader
|
2023-09-08 08:55:07 -04:00
|
|
|
terraformUpgrader clusterUpgrader
|
2023-08-21 10:15:32 -04:00
|
|
|
wantErr bool
|
2023-09-19 07:50:00 -04:00
|
|
|
customK8sVersion string
|
2023-08-24 10:40:47 -04:00
|
|
|
flags upgradeApplyFlags
|
2023-08-21 10:15:32 -04:00
|
|
|
stdin string
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{currentConfig: config.DefaultForAzureSEVSNP()},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2022-11-29 05:39:07 -05:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"nodeVersion some error": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
2023-05-03 05:11:53 -04:00
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
2023-08-16 03:59:32 -04:00
|
|
|
nodeVersionErr: assert.AnError,
|
2023-05-03 05:11:53 -04:00
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
|
|
|
wantErr: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2023-03-03 03:38:23 -05:00
|
|
|
},
|
|
|
|
"nodeVersion in progress error": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
2023-05-03 05:11:53 -04:00
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
2023-08-21 10:15:32 -04:00
|
|
|
nodeVersionErr: kubecmd.ErrInProgress,
|
2023-05-03 05:11:53 -04:00
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"helm other error": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
2023-05-03 05:11:53 -04:00
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{err: assert.AnError},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
|
|
|
wantErr: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"abort": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
2023-05-22 07:31:20 -04:00
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{terraformDiff: true},
|
|
|
|
wantErr: true,
|
|
|
|
stdin: "no\n",
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
2023-09-14 05:51:20 -04:00
|
|
|
"abort, restore terraform err": {
|
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
helmUpgrader: stubApplier{},
|
|
|
|
terraformUpgrader: &stubTerraformUpgrader{terraformDiff: true, rollbackWorkspaceErr: assert.AnError},
|
|
|
|
wantErr: true,
|
|
|
|
stdin: "no\n",
|
|
|
|
},
|
2023-05-22 07:31:20 -04:00
|
|
|
"plan terraform error": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{planTerraformErr: assert.AnError},
|
|
|
|
wantErr: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2023-05-22 07:31:20 -04:00
|
|
|
},
|
|
|
|
"apply terraform error": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
2023-08-24 10:40:47 -04:00
|
|
|
helmUpgrader: stubApplier{},
|
2023-08-16 03:59:32 -04:00
|
|
|
terraformUpgrader: &stubTerraformUpgrader{
|
|
|
|
applyTerraformErr: assert.AnError,
|
2023-05-22 07:31:20 -04:00
|
|
|
terraformDiff: true,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2023-08-23 04:35:42 -04:00
|
|
|
flags: upgradeApplyFlags{yes: true},
|
2023-08-11 09:18:59 -04:00
|
|
|
},
|
2023-09-19 07:50:00 -04:00
|
|
|
"outdated K8s patch version": {
|
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
helmUpgrader: stubApplier{},
|
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
|
|
|
customK8sVersion: func() string {
|
|
|
|
v, err := semver.New(versions.SupportedK8sVersions()[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
return semver.NewFromInt(v.Major(), v.Minor(), v.Patch()-1, "").String()
|
|
|
|
}(),
|
|
|
|
flags: upgradeApplyFlags{yes: true},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"outdated K8s version": {
|
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
helmUpgrader: stubApplier{},
|
|
|
|
terraformUpgrader: &stubTerraformUpgrader{},
|
|
|
|
customK8sVersion: "v1.20.0",
|
|
|
|
flags: upgradeApplyFlags{yes: true},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-09-08 08:55:07 -04:00
|
|
|
"skip all upgrade phases": {
|
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
helmUpgrader: &mockApplier{}, // mocks ensure that no methods are called
|
|
|
|
terraformUpgrader: &mockTerraformUpgrader{},
|
|
|
|
flags: upgradeApplyFlags{
|
|
|
|
skipPhases: []skipPhase{skipInfrastructurePhase, skipHelmPhase, skipK8sPhase, skipImagePhase},
|
|
|
|
yes: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"skip all phases except node upgrade": {
|
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
helmUpgrader: &mockApplier{}, // mocks ensure that no methods are called
|
|
|
|
terraformUpgrader: &mockTerraformUpgrader{},
|
|
|
|
flags: upgradeApplyFlags{
|
|
|
|
skipPhases: []skipPhase{skipInfrastructurePhase, skipHelmPhase, skipK8sPhase},
|
|
|
|
yes: true,
|
|
|
|
},
|
|
|
|
},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
2023-02-01 04:56:47 -05:00
|
|
|
cmd := newUpgradeApplyCmd()
|
2023-05-22 07:31:20 -04:00
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
2023-03-14 13:34:58 -04:00
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
2023-08-08 07:03:23 -04:00
|
|
|
|
2023-08-11 09:18:59 -04:00
|
|
|
cfg := defaultConfigWithExpectedMeasurements(t, config.Default(), cloudprovider.Azure)
|
2023-09-19 07:50:00 -04:00
|
|
|
if tc.customK8sVersion != "" {
|
|
|
|
cfg.KubernetesVersion = versions.ValidK8sVersion(tc.customK8sVersion)
|
|
|
|
}
|
2022-11-15 09:40:49 -05:00
|
|
|
require.NoError(handler.WriteYAML(constants.ConfigFilename, cfg))
|
2023-08-24 10:40:47 -04:00
|
|
|
require.NoError(handler.WriteJSON(constants.ClusterIDsFilename, clusterid.File{MeasurementSalt: []byte("measurementSalt")}))
|
2023-08-11 09:18:59 -04:00
|
|
|
require.NoError(handler.WriteJSON(constants.MasterSecretFilename, uri.MasterSecret{}))
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
upgrader := upgradeApplyCmd{
|
2023-08-23 04:35:42 -04:00
|
|
|
kubeUpgrader: tc.kubeUpgrader,
|
2023-08-24 10:40:47 -04:00
|
|
|
helmApplier: tc.helmUpgrader,
|
2023-08-23 04:35:42 -04:00
|
|
|
clusterUpgrader: tc.terraformUpgrader,
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
configFetcher: stubAttestationFetcher{},
|
|
|
|
clusterShower: &stubShowCluster{},
|
|
|
|
fileHandler: handler,
|
2023-08-16 03:59:32 -04:00
|
|
|
}
|
2023-07-24 04:30:53 -04:00
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
err := upgrader.upgradeApply(cmd, "test", tc.flags)
|
2022-08-29 10:49:44 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
2023-08-11 09:18:59 -04:00
|
|
|
return
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
2023-08-11 09:18:59 -04:00
|
|
|
assert.NoError(err)
|
2023-09-08 08:55:07 -04:00
|
|
|
assert.Equal(!tc.flags.skipPhases.contains(skipImagePhase), tc.kubeUpgrader.calledNodeUpgrade,
|
|
|
|
"incorrect node upgrade skipping behavior")
|
2022-08-29 10:49:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func TestUpgradeApplyFlagsForSkipPhases(t *testing.T) {
|
|
|
|
cmd := newUpgradeApplyCmd()
|
|
|
|
cmd.Flags().String("workspace", "", "") // register persistent flag manually
|
|
|
|
cmd.Flags().Bool("force", true, "") // register persistent flag manually
|
|
|
|
cmd.Flags().String("tf-log", "NONE", "") // register persistent flag manually
|
|
|
|
require.NoError(t, cmd.Flags().Set("skip-phases", "infrastructure,helm,k8s,image"))
|
|
|
|
result, err := parseUpgradeApplyFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error while parsing flags: %v", err)
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, []skipPhase{skipInfrastructurePhase, skipHelmPhase, skipK8sPhase, skipImagePhase}, result.skipPhases)
|
|
|
|
}
|
|
|
|
|
2023-08-24 10:40:47 -04:00
|
|
|
type stubKubernetesUpgrader struct {
|
2023-09-08 08:55:07 -04:00
|
|
|
nodeVersionErr error
|
|
|
|
currentConfig config.AttestationCfg
|
|
|
|
calledNodeUpgrade bool
|
2023-08-11 09:18:59 -04:00
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) BackupCRDs(_ context.Context, _ string) ([]apiextensionsv1.CustomResourceDefinition, error) {
|
2023-08-24 10:40:47 -04:00
|
|
|
return []apiextensionsv1.CustomResourceDefinition{}, nil
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) BackupCRs(_ context.Context, _ []apiextensionsv1.CustomResourceDefinition, _ string) error {
|
2023-08-24 10:40:47 -04:00
|
|
|
return nil
|
2023-08-11 09:18:59 -04:00
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) UpgradeNodeVersion(_ context.Context, _ *config.Config, _, _, _ bool) error {
|
|
|
|
u.calledNodeUpgrade = true
|
2023-03-03 03:38:23 -05:00
|
|
|
return u.nodeVersionErr
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) ApplyJoinConfig(_ context.Context, _ config.AttestationCfg, _ []byte) error {
|
2023-08-16 03:59:32 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) GetClusterAttestationConfig(_ context.Context, _ variant.Variant) (config.AttestationCfg, error) {
|
2023-08-16 03:59:32 -04:00
|
|
|
return u.currentConfig, nil
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2023-03-14 13:34:58 -04:00
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) ExtendClusterConfigCertSANs(_ context.Context, _ []string) error {
|
2023-03-14 13:34:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-08 15:15:02 -04:00
|
|
|
// TODO(v2.11): Remove this function after v2.11 is released.
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) RemoveAttestationConfigHelmManagement(_ context.Context) error {
|
2023-08-23 02:14:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(v2.12): Remove this function.
|
2023-09-08 08:55:07 -04:00
|
|
|
func (u *stubKubernetesUpgrader) RemoveHelmKeepAnnotation(_ context.Context) error {
|
2023-08-23 02:14:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
type stubTerraformUpgrader struct {
|
2023-09-14 05:51:20 -04:00
|
|
|
terraformDiff bool
|
|
|
|
planTerraformErr error
|
|
|
|
applyTerraformErr error
|
|
|
|
rollbackWorkspaceErr error
|
2023-03-14 13:34:58 -04:00
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
func (u stubTerraformUpgrader) PlanClusterUpgrade(_ context.Context, _ io.Writer, _ terraform.Variables, _ cloudprovider.Provider) (bool, error) {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.terraformDiff, u.planTerraformErr
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
func (u stubTerraformUpgrader) ApplyClusterUpgrade(_ context.Context, _ cloudprovider.Provider) (terraform.ApplyOutput, error) {
|
2023-08-11 09:18:59 -04:00
|
|
|
return terraform.ApplyOutput{}, u.applyTerraformErr
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|
2023-09-08 08:55:07 -04:00
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
func (u stubTerraformUpgrader) RestoreClusterWorkspace() error {
|
|
|
|
return u.rollbackWorkspaceErr
|
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
type mockTerraformUpgrader struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTerraformUpgrader) PlanClusterUpgrade(ctx context.Context, w io.Writer, variables terraform.Variables, provider cloudprovider.Provider) (bool, error) {
|
|
|
|
args := m.Called(ctx, w, variables, provider)
|
|
|
|
return args.Bool(0), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTerraformUpgrader) ApplyClusterUpgrade(ctx context.Context, provider cloudprovider.Provider) (terraform.ApplyOutput, error) {
|
|
|
|
args := m.Called(ctx, provider)
|
|
|
|
return args.Get(0).(terraform.ApplyOutput), args.Error(1)
|
|
|
|
}
|
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
func (m *mockTerraformUpgrader) RestoreClusterWorkspace() error {
|
|
|
|
args := m.Called()
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
type mockApplier struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2023-09-19 07:50:00 -04:00
|
|
|
func (m *mockApplier) PrepareApply(cfg *config.Config, clusterID clusterid.File, helmOpts helm.Options, terraformOut terraform.ApplyOutput, str string, masterSecret uri.MasterSecret) (helm.Applier, bool, error) {
|
|
|
|
args := m.Called(cfg, clusterID, helmOpts, terraformOut, str, masterSecret)
|
2023-09-08 08:55:07 -04:00
|
|
|
return args.Get(0).(helm.Applier), args.Bool(1), args.Error(2)
|
|
|
|
}
|