2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-13 07:01:38 -04:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-05-04 12:41:24 -04:00
|
|
|
"strconv"
|
2022-04-13 07:01:38 -04:00
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-06-07 10:30:41 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/azure"
|
2022-06-07 08:52:47 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/gcp"
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-04-06 04:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
func TestCreateArgumentValidation(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2022-05-04 02:50:50 -04:00
|
|
|
"gcp": {[]string{"gcp"}, false},
|
|
|
|
"azure": {[]string{"azure"}, false},
|
|
|
|
"aws waring": {[]string{"aws"}, true},
|
|
|
|
"too many args": {[]string{"gcp", "1", "2"}, true},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
err := NewCreateCmd().ValidateArgs(tc.args)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
testState := state.ConstellationState{Name: "test"}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs func(*require.Assertions) afero.Fs
|
|
|
|
creator *stubCloudCreator
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
yesFlag bool
|
|
|
|
controllerCountFlag *int
|
|
|
|
workerCountFlag *int
|
|
|
|
insTypeFlag string
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag string
|
2022-05-04 02:50:50 -04:00
|
|
|
nameFlag string
|
|
|
|
stdin string
|
|
|
|
wantErr bool
|
|
|
|
wantAbbort bool
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"create": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{state: testState},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(2),
|
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{state: testState},
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
controllerCountFlag: intPtr(2),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
stdin: "yes\n",
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive abort": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
stdin: "no\n",
|
|
|
|
wantAbbort: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive error": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
stdin: "foo\nfoo\nfoo\n",
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"flag name to long": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
2022-04-12 10:07:17 -04:00
|
|
|
nameFlag: strings.Repeat("a", constants.ConstellationNameLength+1),
|
2022-05-04 12:41:24 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-05-04 02:50:50 -04:00
|
|
|
"flag control-plane-count invalid": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(0),
|
|
|
|
workerCountFlag: intPtr(3),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"flag worker-count invalid": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(3),
|
2022-05-04 12:41:24 -04:00
|
|
|
workerCountFlag: intPtr(0),
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"flag control-plane-count missing": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
workerCountFlag: intPtr(3),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"flag worker-count missing": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(3),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"flag invalid instance-type": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
insTypeFlag: "invalid",
|
|
|
|
wantErr: true,
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
"old state in directory": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.Write(constants.StateFilename, []byte{1}, file.OptNone))
|
|
|
|
return fs
|
|
|
|
},
|
2022-05-04 12:41:24 -04:00
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"old adminConf in directory": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.Write(constants.AdminConfFilename, []byte{1}, file.OptNone))
|
|
|
|
return fs
|
|
|
|
},
|
2022-05-04 12:41:24 -04:00
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"old masterSecret in directory": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.Write(constants.MasterSecretFilename, []byte{1}, file.OptNone))
|
|
|
|
return fs
|
|
|
|
},
|
2022-05-04 12:41:24 -04:00
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"config does not exist": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag: constants.ConfigFilename,
|
2022-05-04 12:41:24 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"create error": {
|
2022-05-04 12:41:24 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
creator: &stubCloudCreator{createErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"write state error": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
return afero.NewReadOnlyFs(fs)
|
|
|
|
},
|
2022-05-04 12:41:24 -04:00
|
|
|
creator: &stubCloudCreator{},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
controllerCountFlag: intPtr(1),
|
|
|
|
workerCountFlag: intPtr(1),
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewCreateCmd()
|
2022-04-13 07:01:38 -04:00
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
2022-05-13 05:56:43 -04:00
|
|
|
cmd.Flags().String("config", "", "") // register persisten flag manually
|
2022-04-13 07:01:38 -04:00
|
|
|
if tc.yesFlag {
|
|
|
|
require.NoError(cmd.Flags().Set("yes", "true"))
|
|
|
|
}
|
|
|
|
if tc.nameFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("name", tc.nameFlag))
|
|
|
|
}
|
2022-05-13 05:56:43 -04:00
|
|
|
if tc.configFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("config", tc.configFlag))
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
2022-05-04 02:50:50 -04:00
|
|
|
if tc.controllerCountFlag != nil {
|
2022-05-04 12:41:24 -04:00
|
|
|
require.NoError(cmd.Flags().Set("control-plane-nodes", strconv.Itoa(*tc.controllerCountFlag)))
|
2022-05-04 02:50:50 -04:00
|
|
|
}
|
|
|
|
if tc.workerCountFlag != nil {
|
2022-05-04 12:41:24 -04:00
|
|
|
require.NoError(cmd.Flags().Set("worker-nodes", strconv.Itoa(*tc.workerCountFlag)))
|
2022-05-04 02:50:50 -04:00
|
|
|
}
|
|
|
|
if tc.insTypeFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("instance-type", tc.insTypeFlag))
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require))
|
|
|
|
|
2022-05-04 02:50:50 -04:00
|
|
|
err := create(cmd, tc.creator, fileHandler, tc.provider)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantAbbort {
|
|
|
|
assert.False(tc.creator.createCalled)
|
|
|
|
} else {
|
|
|
|
assert.True(tc.creator.createCalled)
|
|
|
|
var state state.ConstellationState
|
|
|
|
require.NoError(fileHandler.ReadJSON(constants.StateFilename, &state))
|
|
|
|
assert.Equal(state, testState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestCheckDirClean(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
fileHandler file.Handler
|
|
|
|
existingFiles []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"no file exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
|
|
|
},
|
|
|
|
"adminconf exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.AdminConfFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"master secret exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.MasterSecretFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"state file exists": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.StateFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"multiple exist": {
|
|
|
|
fileHandler: file.NewHandler(afero.NewMemMapFs()),
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.AdminConfFilename, constants.MasterSecretFilename, constants.StateFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
for _, f := range tc.existingFiles {
|
2022-04-13 03:15:27 -04:00
|
|
|
require.NoError(tc.fileHandler.Write(f, []byte{1, 2, 3}, file.OptNone))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:36:58 -04:00
|
|
|
err := checkDirClean(tc.fileHandler)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
func TestCreateCompletion(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantResult []string
|
|
|
|
wantShellCD cobra.ShellCompDirective
|
|
|
|
}{
|
|
|
|
"first arg": {
|
|
|
|
args: []string{},
|
|
|
|
wantResult: []string{"aws", "gcp", "azure"},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
|
|
|
},
|
|
|
|
"second arg": {
|
2022-05-04 02:50:50 -04:00
|
|
|
args: []string{"gcp", "foo"},
|
2022-04-13 07:01:38 -04:00
|
|
|
wantResult: []string{},
|
2022-05-04 02:50:50 -04:00
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-05-04 02:50:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
result, shellCD := createCompletion(cmd, tc.args, "")
|
|
|
|
assert.Equal(tc.wantResult, result)
|
|
|
|
assert.Equal(tc.wantShellCD, shellCD)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInstanceTypeCompletion(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
wantResult []string
|
|
|
|
wantShellCD cobra.ShellCompDirective
|
|
|
|
}{
|
|
|
|
"azure": {
|
|
|
|
args: []string{"azure"},
|
|
|
|
wantResult: azure.InstanceTypes,
|
2022-04-13 07:01:38 -04:00
|
|
|
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
|
|
|
},
|
2022-05-04 02:50:50 -04:00
|
|
|
"gcp": {
|
|
|
|
args: []string{"gcp"},
|
2022-04-13 07:01:38 -04:00
|
|
|
wantResult: gcp.InstanceTypes,
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
|
|
|
},
|
2022-05-04 02:50:50 -04:00
|
|
|
"empty args": {
|
|
|
|
args: []string{},
|
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-05-04 02:50:50 -04:00
|
|
|
"unknown provider": {
|
|
|
|
args: []string{"foo"},
|
2022-04-13 07:01:38 -04:00
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := &cobra.Command{}
|
2022-05-04 02:50:50 -04:00
|
|
|
result, shellCD := instanceTypeCompletion(cmd, tc.args, "")
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
assert.Equal(tc.wantResult, result)
|
|
|
|
assert.Equal(tc.wantShellCD, shellCD)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 02:50:50 -04:00
|
|
|
|
|
|
|
func intPtr(i int) *int {
|
|
|
|
return &i
|
|
|
|
}
|