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 (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2022-12-19 10:52:15 -05:00
|
|
|
"time"
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
|
|
|
"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"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpgradeExecute(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-11-29 05:39:07 -05:00
|
|
|
upgrader stubUpgrader
|
|
|
|
imageFetcher stubImageFetcher
|
|
|
|
wantErr bool
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-11-29 05:39:07 -05:00
|
|
|
imageFetcher: stubImageFetcher{
|
|
|
|
reference: "someReference",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"fetch error": {
|
|
|
|
imageFetcher: stubImageFetcher{
|
|
|
|
fetchReferenceErr: errors.New("error"),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"upgrade error": {
|
|
|
|
upgrader: stubUpgrader{err: errors.New("error")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
cmd := newUpgradeExecuteCmd()
|
|
|
|
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
|
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))
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
err := upgradeExecute(cmd, &tc.imageFetcher, tc.upgrader, handler)
|
2022-08-29 10:49:44 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubUpgrader struct {
|
2022-12-19 10:52:15 -05:00
|
|
|
err error
|
|
|
|
helmErr error
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
func (u stubUpgrader) Upgrade(context.Context, string, string, measurements.M) error {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.err
|
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
|
2023-01-04 07:55:10 -05:00
|
|
|
func (u stubUpgrader) UpgradeHelmServices(ctx context.Context, config *config.Config, timeout time.Duration, allowDestructive bool) error {
|
2022-12-19 10:52:15 -05:00
|
|
|
return u.helmErr
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
type stubImageFetcher struct {
|
|
|
|
reference string
|
|
|
|
fetchReferenceErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *stubImageFetcher) FetchReference(_ context.Context, _ *config.Config) (string, error) {
|
|
|
|
return f.reference, f.fetchReferenceErr
|
|
|
|
}
|