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"
|
2022-12-19 10:52:15 -05:00
|
|
|
"time"
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-08-11 09:18:59 -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-08-11 09:18:59 -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"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
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-08-21 10:15:32 -04:00
|
|
|
helmUpgrader *stubHelmUpgrader
|
|
|
|
kubeUpgrader *stubKubernetesUpgrader
|
|
|
|
terraformUpgrader *stubTerraformUpgrader
|
2023-08-23 04:35:42 -04:00
|
|
|
flags upgradeApplyFlags
|
2023-08-21 10:15:32 -04:00
|
|
|
wantErr bool
|
|
|
|
stdin string
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-08-16 03:59:32 -04:00
|
|
|
kubeUpgrader: &stubKubernetesUpgrader{currentConfig: config.DefaultForAzureSEVSNP()},
|
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
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-16 03:59:32 -04:00
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
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-16 03:59:32 -04:00
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
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-16 03:59:32 -04:00
|
|
|
helmUpgrader: &stubHelmUpgrader{err: assert.AnError},
|
|
|
|
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-16 03:59:32 -04:00
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
terraformUpgrader: &stubTerraformUpgrader{terraformDiff: true},
|
|
|
|
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-16 03:59:32 -04:00
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
helmUpgrader: &stubHelmUpgrader{},
|
|
|
|
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
|
|
|
},
|
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-08-09 09:11:14 -04:00
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
require.NoError(handler.WriteYAML(constants.ConfigFilename, cfg))
|
2023-08-04 07:53:51 -04:00
|
|
|
require.NoError(handler.WriteJSON(constants.ClusterIDsFilename, clusterid.File{}))
|
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,
|
|
|
|
helmUpgrader: tc.helmUpgrader,
|
|
|
|
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)
|
2022-08-29 10:49:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
type stubHelmUpgrader struct {
|
|
|
|
err error
|
2023-08-11 09:18:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
func (u stubHelmUpgrader) Upgrade(
|
|
|
|
_ context.Context, _ *config.Config, _ clusterid.File, _ time.Duration, _, _ bool, _ string, _ bool,
|
|
|
|
_ helm.WaitMode, _ uri.MasterSecret, _ string, _ versions.ValidK8sVersion, _ terraform.ApplyOutput,
|
|
|
|
) error {
|
|
|
|
return u.err
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
type stubKubernetesUpgrader struct {
|
2023-08-21 10:15:32 -04:00
|
|
|
nodeVersionErr error
|
|
|
|
currentConfig config.AttestationCfg
|
2023-08-11 09:18:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
func (u stubKubernetesUpgrader) UpgradeNodeVersion(_ context.Context, _ *config.Config, _ bool) error {
|
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-08-23 02:14:39 -04:00
|
|
|
func (u stubKubernetesUpgrader) ApplyJoinConfig(_ context.Context, _ config.AttestationCfg, _ []byte) error {
|
2023-08-16 03:59:32 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u stubKubernetesUpgrader) GetClusterAttestationConfig(_ context.Context, _ variant.Variant) (config.AttestationCfg, error) {
|
|
|
|
return u.currentConfig, nil
|
2022-12-19 10:52:15 -05:00
|
|
|
}
|
2023-03-14 13:34:58 -04:00
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
func (u stubKubernetesUpgrader) ExtendClusterConfigCertSANs(_ context.Context, _ []string) error {
|
2023-03-14 13:34:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:14:39 -04:00
|
|
|
// TODO(v2.11): Remove this function.
|
|
|
|
func (u stubKubernetesUpgrader) RemoveAttestationConfigHelmManagement(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(v2.12): Remove this function.
|
|
|
|
func (u stubKubernetesUpgrader) RemoveHelmKeepAnnotation(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
type stubTerraformUpgrader struct {
|
|
|
|
terraformDiff bool
|
|
|
|
planTerraformErr error
|
|
|
|
applyTerraformErr 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
|
|
|
}
|