2022-09-05 09:06:08 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-29 16:49:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2022-12-19 16:52:15 +01:00
|
|
|
"time"
|
2022-08-29 16:49:44 +02:00
|
|
|
|
2023-05-03 11:11:53 +02:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-03-30 16:13:14 +02:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/kubernetes"
|
2022-11-15 15:40:49 +01:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-09-21 13:47:57 +02: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 15:54:12 +01:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-05-03 11:11:53 +02:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
2022-08-29 16:49:44 +02:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-14 18:34:58 +01:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-08-29 16:49:44 +02:00
|
|
|
)
|
|
|
|
|
2023-02-01 10:56:47 +01:00
|
|
|
func TestUpgradeApply(t *testing.T) {
|
2023-03-03 09:38:23 +01:00
|
|
|
someErr := errors.New("some error")
|
2022-08-29 16:49:44 +02:00
|
|
|
testCases := map[string]struct {
|
2023-03-03 09:38:23 +01:00
|
|
|
upgrader stubUpgrader
|
|
|
|
wantErr bool
|
2022-08-29 16:49:44 +02:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-05-03 11:11:53 +02:00
|
|
|
upgrader: stubUpgrader{currentConfig: config.DefaultForAzureSEVSNP()},
|
2022-11-29 11:39:07 +01:00
|
|
|
},
|
2023-03-03 09:38:23 +01:00
|
|
|
"nodeVersion some error": {
|
2023-05-03 11:11:53 +02:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
nodeVersionErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2023-03-03 09:38:23 +01:00
|
|
|
},
|
|
|
|
"nodeVersion in progress error": {
|
2023-05-03 11:11:53 +02:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
nodeVersionErr: kubernetes.ErrInProgress,
|
|
|
|
},
|
2022-08-29 16:49:44 +02:00
|
|
|
},
|
2023-03-03 09:38:23 +01:00
|
|
|
"helm other error": {
|
2023-05-03 11:11:53 +02:00
|
|
|
upgrader: stubUpgrader{
|
|
|
|
currentConfig: config.DefaultForAzureSEVSNP(),
|
|
|
|
helmErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-08-29 16:49:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
2023-02-01 10:56:47 +01:00
|
|
|
cmd := newUpgradeApplyCmd()
|
2022-08-29 16:49:44 +02:00
|
|
|
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent flag manually
|
2023-01-31 11:45:31 +01:00
|
|
|
cmd.Flags().Bool("force", true, "") // register persistent flag manually
|
2022-08-29 16:49:44 +02:00
|
|
|
|
2023-03-14 18:34:58 +01:00
|
|
|
err := cmd.Flags().Set("yes", "true")
|
|
|
|
require.NoError(err)
|
|
|
|
|
2022-08-29 16:49:44 +02:00
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
2022-11-15 15:40:49 +01:00
|
|
|
cfg := defaultConfigWithExpectedMeasurements(t, config.Default(), cloudprovider.Azure)
|
|
|
|
require.NoError(handler.WriteYAML(constants.ConfigFilename, cfg))
|
2023-05-03 11:11:53 +02:00
|
|
|
require.NoError(handler.WriteJSON(constants.ClusterIDsFileName, clusterid.File{}))
|
2022-08-29 16:49:44 +02:00
|
|
|
|
2023-02-09 15:54:12 +01:00
|
|
|
upgrader := upgradeApplyCmd{upgrader: tc.upgrader, log: logger.NewTest(t)}
|
2023-03-14 18:34:58 +01:00
|
|
|
err = upgrader.upgradeApply(cmd, handler)
|
2022-08-29 16:49:44 +02:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubUpgrader struct {
|
2023-05-03 11:11:53 +02:00
|
|
|
currentConfig config.AttestationCfg
|
2023-03-03 09:38:23 +01:00
|
|
|
nodeVersionErr error
|
|
|
|
helmErr error
|
2022-08-29 16:49:44 +02:00
|
|
|
}
|
|
|
|
|
2023-03-20 11:03:36 +01:00
|
|
|
func (u stubUpgrader) UpgradeNodeVersion(_ context.Context, _ *config.Config) error {
|
2023-03-03 09:38:23 +01:00
|
|
|
return u.nodeVersionErr
|
2022-08-29 16:49:44 +02:00
|
|
|
}
|
2022-11-29 11:39:07 +01:00
|
|
|
|
2023-03-20 11:03:36 +01:00
|
|
|
func (u stubUpgrader) UpgradeHelmServices(_ context.Context, _ *config.Config, _ time.Duration, _ bool) error {
|
2022-12-19 16:52:15 +01:00
|
|
|
return u.helmErr
|
|
|
|
}
|
2023-03-14 18:34:58 +01:00
|
|
|
|
2023-05-03 11:11:53 +02:00
|
|
|
func (u stubUpgrader) UpdateAttestationConfig(_ context.Context, _ config.AttestationCfg) error {
|
2023-03-14 18:34:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:11:53 +02:00
|
|
|
func (u stubUpgrader) GetClusterAttestationConfig(_ context.Context, _ variant.Variant) (config.AttestationCfg, *corev1.ConfigMap, error) {
|
|
|
|
return u.currentConfig, &corev1.ConfigMap{}, nil
|
2023-03-14 18:34:58 +01:00
|
|
|
}
|