AB#2309 constellation upgrade execute (#2)

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße 2022-08-29 16:49:44 +02:00 committed by GitHub
parent 7c5556864b
commit 7c832273fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 744 additions and 21 deletions

View file

@ -0,0 +1,19 @@
package cmd
import (
"github.com/spf13/cobra"
)
// NewUpgradeCmd returns a new cobra.Command for the upgrade command.
func NewUpgradeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade",
Short: "Plan and perform an upgrade of a Constellation cluster",
Long: "Plan and perform an upgrade of a Constellation cluster.",
Args: cobra.ExactArgs(0),
}
cmd.AddCommand(newUpgradeExecuteCmd())
return cmd
}

View file

@ -0,0 +1,53 @@
package cmd
import (
"context"
"github.com/edgelesssys/constellation/cli/internal/cloudcmd"
"github.com/edgelesssys/constellation/internal/config"
"github.com/edgelesssys/constellation/internal/file"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
func newUpgradeExecuteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "execute",
Short: "Execute an upgrade of a Constellation cluster",
Long: "Execute an upgrade of a Constellation cluster by applying the chosen configuration.",
Args: cobra.NoArgs,
RunE: runUpgradeExecute,
}
return cmd
}
func runUpgradeExecute(cmd *cobra.Command, args []string) error {
fileHandler := file.NewHandler(afero.NewOsFs())
upgrader, err := cloudcmd.NewUpgrader(cmd.OutOrStdout())
if err != nil {
return err
}
return upgradeExecute(cmd, upgrader, fileHandler)
}
func upgradeExecute(cmd *cobra.Command, upgrader cloudUpgrader, fileHandler file.Handler) error {
configPath, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
config, err := config.FromFile(fileHandler, configPath)
if err != nil {
return err
}
// TODO: validate upgrade config? Should be basic things like checking image is not an empty string
// More sophisticated validation, like making sure we don't downgrade the cluster, should be done by `constellation upgrade plan`
return upgrader.Upgrade(cmd.Context(), config.Upgrade.Image, config.Upgrade.Measurements)
}
type cloudUpgrader interface {
Upgrade(ctx context.Context, image string, measurements map[uint32][]byte) error
}

View file

@ -0,0 +1,56 @@
package cmd
import (
"context"
"errors"
"testing"
"github.com/edgelesssys/constellation/internal/config"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/file"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUpgradeExecute(t *testing.T) {
testCases := map[string]struct {
upgrader stubUpgrader
wantErr bool
}{
"success": {
upgrader: stubUpgrader{},
},
"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
handler := file.NewHandler(afero.NewMemMapFs())
require.NoError(handler.WriteYAML(constants.ConfigFilename, config.Default()))
err := upgradeExecute(cmd, tc.upgrader, handler)
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
}
})
}
}
type stubUpgrader struct {
err error
}
func (u stubUpgrader) Upgrade(context.Context, string, map[uint32][]byte) error {
return u.err
}