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"
|
|
|
|
"errors"
|
|
|
|
"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-03-30 10:13:14 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/kubernetes"
|
2023-06-27 07:12:50 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2023-05-22 07:31:20 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/upgrade"
|
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-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/spf13/afero"
|
2023-07-24 04:30:53 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-14 13:34:58 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-08-29 10:49:44 -04:00
|
|
|
)
|
|
|
|
|
2023-02-01 04:56:47 -05:00
|
|
|
func TestUpgradeApply(t *testing.T) {
|
2023-03-03 03:38:23 -05:00
|
|
|
someErr := errors.New("some error")
|
2022-08-29 10:49:44 -04:00
|
|
|
testCases := map[string]struct {
|
2023-03-03 03:38:23 -05:00
|
|
|
upgrader stubUpgrader
|
2023-05-22 07:31:20 -04:00
|
|
|
fetcher stubImageFetcher
|
2023-03-03 03:38:23 -05:00
|
|
|
wantErr bool
|
2023-05-22 07:31:20 -04:00
|
|
|
yesFlag bool
|
|
|
|
stdin string
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-05-03 05:11:53 -04:00
|
|
|
upgrader: stubUpgrader{currentConfig: config.DefaultForAzureSEVSNP()},
|
2023-05-22 07:31:20 -04:00
|
|
|
yesFlag: true,
|
2022-11-29 05:39:07 -05:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"nodeVersion some error": {
|
2023-05-03 05:11:53 -04:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
nodeVersionErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
yesFlag: true,
|
2023-03-03 03:38:23 -05:00
|
|
|
},
|
|
|
|
"nodeVersion in progress error": {
|
2023-05-03 05:11:53 -04:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
nodeVersionErr: kubernetes.ErrInProgress,
|
|
|
|
},
|
2023-05-22 07:31:20 -04:00
|
|
|
yesFlag: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"helm other error": {
|
2023-05-03 05:11:53 -04:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
helmErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2023-05-22 07:31:20 -04:00
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
|
|
|
"check terraform error": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
checkTerraformErr: someErr,
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
wantErr: true,
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
|
|
|
"abort": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
terraformDiff: true,
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
wantErr: true,
|
|
|
|
stdin: "no\n",
|
|
|
|
},
|
|
|
|
"clean terraform error": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
cleanTerraformErr: someErr,
|
|
|
|
terraformDiff: true,
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
wantErr: true,
|
|
|
|
stdin: "no\n",
|
|
|
|
},
|
|
|
|
"plan terraform error": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
planTerraformErr: someErr,
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
wantErr: true,
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
|
|
|
"apply terraform error": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
applyTerraformErr: someErr,
|
|
|
|
terraformDiff: true,
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{},
|
|
|
|
wantErr: true,
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
|
|
|
"fetch reference error": {
|
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
},
|
|
|
|
fetcher: stubImageFetcher{fetchReferenceErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
yesFlag: 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))
|
2022-08-29 10:49:44 -04:00
|
|
|
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent flag manually
|
2023-01-31 05:45:31 -05:00
|
|
|
cmd.Flags().Bool("force", true, "") // register persistent flag manually
|
2023-05-22 07:31:20 -04:00
|
|
|
cmd.Flags().String("tf-log", "DEBUG", "") // register persistent flag manually
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2023-05-22 07:31:20 -04:00
|
|
|
if tc.yesFlag {
|
|
|
|
err := cmd.Flags().Set("yes", "true")
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
2023-03-14 13:34:58 -04:00
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
2022-11-15 09:40:49 -05:00
|
|
|
cfg := defaultConfigWithExpectedMeasurements(t, config.Default(), cloudprovider.Azure)
|
|
|
|
require.NoError(handler.WriteYAML(constants.ConfigFilename, cfg))
|
2023-05-03 05:11:53 -04:00
|
|
|
require.NoError(handler.WriteJSON(constants.ClusterIDsFileName, clusterid.File{}))
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2023-07-24 04:30:53 -04:00
|
|
|
upgrader := upgradeApplyCmd{upgrader: tc.upgrader, log: logger.NewTest(t), imageFetcher: tc.fetcher, configFetcher: stubAttestationFetcher{}, migrationExecutor: &migrationExecutorPlaceholder{}}
|
|
|
|
|
2023-05-22 07:31:20 -04:00
|
|
|
err := upgrader.upgradeApply(cmd, handler)
|
2022-08-29 10:49:44 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 04:30:53 -04:00
|
|
|
type migrationExecutorPlaceholder struct{}
|
|
|
|
|
|
|
|
func (d *migrationExecutorPlaceholder) applyMigration(_ *cobra.Command, _ file.Handler, _ upgrade.TfMigrationCmd, _ upgradeApplyFlags) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
type stubUpgrader struct {
|
2023-05-22 07:31:20 -04:00
|
|
|
currentConfig config.AttestationCfg
|
|
|
|
nodeVersionErr error
|
|
|
|
helmErr error
|
|
|
|
terraformDiff bool
|
|
|
|
planTerraformErr error
|
|
|
|
checkTerraformErr error
|
|
|
|
applyTerraformErr error
|
|
|
|
cleanTerraformErr error
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-07-24 04:30:53 -04:00
|
|
|
func (u stubUpgrader) GetUpgradeID() string {
|
|
|
|
return "test-upgrade"
|
|
|
|
}
|
|
|
|
|
2023-06-21 09:49:42 -04:00
|
|
|
func (u stubUpgrader) 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-07-24 04:30:53 -04:00
|
|
|
func (u stubUpgrader) UpgradeHelmServices(_ context.Context, _ *config.Config, _ clusterid.File, _ time.Duration, _, _ bool) error {
|
2022-12-19 10:52:15 -05:00
|
|
|
return u.helmErr
|
|
|
|
}
|
2023-03-14 13:34:58 -04:00
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
func (u stubUpgrader) UpdateAttestationConfig(_ context.Context, _ config.AttestationCfg) error {
|
2023-03-14 13:34:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
func (u stubUpgrader) GetClusterAttestationConfig(_ context.Context, _ variant.Variant) (config.AttestationCfg, *corev1.ConfigMap, error) {
|
|
|
|
return u.currentConfig, &corev1.ConfigMap{}, nil
|
2023-03-14 13:34:58 -04:00
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
func (u stubUpgrader) CheckTerraformMigrations() error {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.checkTerraformErr
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
func (u stubUpgrader) CleanUpTerraformMigrations() error {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.cleanTerraformErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u stubUpgrader) PlanTerraformMigrations(context.Context, upgrade.TerraformUpgradeOptions) (bool, error) {
|
|
|
|
return u.terraformDiff, u.planTerraformErr
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:33:42 -04:00
|
|
|
func (u stubUpgrader) ApplyTerraformMigrations(context.Context, upgrade.TerraformUpgradeOptions) error {
|
2023-05-22 07:31:20 -04:00
|
|
|
return u.applyTerraformErr
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:43:51 -04:00
|
|
|
func (u stubUpgrader) ExtendClusterConfigCertSANs(_ context.Context, _ []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:12:50 -04:00
|
|
|
// AddManualStateMigration is not used in this test.
|
|
|
|
// TODO(AB#3248): remove this method together with the definition in the interfaces.
|
|
|
|
func (u stubUpgrader) AddManualStateMigration(_ terraform.StateMigration) {
|
|
|
|
panic("unused")
|
|
|
|
}
|
|
|
|
|
2023-05-22 07:31:20 -04:00
|
|
|
type stubImageFetcher struct {
|
|
|
|
fetchReferenceErr error
|
|
|
|
}
|
|
|
|
|
2023-05-23 03:17:27 -04:00
|
|
|
func (f stubImageFetcher) FetchReference(_ context.Context,
|
|
|
|
_ cloudprovider.Provider, _ variant.Variant,
|
|
|
|
_, _ string,
|
|
|
|
) (string, error) {
|
|
|
|
return "", f.fetchReferenceErr
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|