constellation/cli/internal/cmd/create_test.go

305 lines
9.8 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
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"
"testing"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/state"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2022-04-13 07:01:38 -04:00
func TestCreate(t *testing.T) {
fsWithDefaultConfig := func(require *require.Assertions, provider cloudprovider.Provider) afero.Fs {
fs := afero.NewMemMapFs()
file := file.NewHandler(fs)
require.NoError(file.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), provider)))
return fs
}
2022-08-01 10:51:34 -04:00
testState := state.ConstellationState{Name: "test", LoadBalancerIP: "192.0.2.1"}
2022-04-13 07:01:38 -04:00
someErr := errors.New("failed")
testCases := map[string]struct {
setupFs func(*require.Assertions, cloudprovider.Provider) afero.Fs
creator *stubCloudCreator
provider cloudprovider.Provider
yesFlag bool
controllerCountFlag *int
workerCountFlag *int
configFlag string
nameFlag string
stdin string
wantErr bool
wantAbbort bool
2022-04-13 07:01:38 -04:00
}{
"create": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
creator: &stubCloudCreator{state: testState},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(1),
workerCountFlag: intPtr(2),
yesFlag: true,
2022-04-13 07:01:38 -04:00
},
"interactive": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
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": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
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": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
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": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(1),
workerCountFlag: intPtr(1),
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
},
"flag control-plane-count invalid": {
setupFs: fsWithDefaultConfig,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(0),
workerCountFlag: intPtr(3),
wantErr: true,
},
"flag worker-count invalid": {
setupFs: fsWithDefaultConfig,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(3),
2022-05-04 12:41:24 -04:00
workerCountFlag: intPtr(0),
wantErr: true,
},
"flag control-plane-count missing": {
setupFs: fsWithDefaultConfig,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
workerCountFlag: intPtr(3),
wantErr: true,
},
"flag worker-count missing": {
setupFs: fsWithDefaultConfig,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(3),
wantErr: true,
},
2022-04-13 07:01:38 -04:00
"old state in directory": {
setupFs: func(require *require.Assertions, csp cloudprovider.Provider) afero.Fs {
2022-04-13 07:01:38 -04:00
fs := afero.NewMemMapFs()
fileHandler := file.NewHandler(fs)
require.NoError(fileHandler.Write(constants.StateFilename, []byte{1}, file.OptNone))
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
2022-04-13 07:01:38 -04:00
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, csp cloudprovider.Provider) afero.Fs {
2022-04-13 07:01:38 -04:00
fs := afero.NewMemMapFs()
fileHandler := file.NewHandler(fs)
require.NoError(fileHandler.Write(constants.AdminConfFilename, []byte{1}, file.OptNone))
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
2022-04-13 07:01:38 -04:00
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, csp cloudprovider.Provider) afero.Fs {
2022-04-13 07:01:38 -04:00
fs := afero.NewMemMapFs()
fileHandler := file.NewHandler(fs)
require.NoError(fileHandler.Write(constants.MasterSecretFilename, []byte{1}, file.OptNone))
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
2022-04-13 07:01:38 -04:00
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
},
"config does not exist": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
controllerCountFlag: intPtr(1),
workerCountFlag: intPtr(1),
yesFlag: true,
configFlag: "/does/not/exist",
2022-05-04 12:41:24 -04:00
wantErr: true,
2022-04-13 07:01:38 -04:00
},
"create error": {
setupFs: fsWithDefaultConfig,
2022-05-04 12:41:24 -04:00
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, csp cloudprovider.Provider) afero.Fs {
2022-04-13 07:01:38 -04:00
fs := afero.NewMemMapFs()
fileHandler := file.NewHandler(fs)
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
2022-04-13 07:01:38 -04:00
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))
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent 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))
}
if tc.configFlag != "" {
require.NoError(cmd.Flags().Set("config", tc.configFlag))
2022-04-13 07:01:38 -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)))
}
if tc.workerCountFlag != nil {
2022-05-04 12:41:24 -04:00
require.NoError(cmd.Flags().Set("worker-nodes", strconv.Itoa(*tc.workerCountFlag)))
}
fileHandler := file.NewHandler(tc.setupFs(require, tc.provider))
2022-04-13 07:01:38 -04:00
err := create(cmd, tc.creator, fileHandler)
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))
2022-07-29 04:01:10 -04:00
var idFile clusterIDsFile
require.NoError(fileHandler.ReadJSON(constants.ClusterIDsFileName, &idFile))
2022-04-13 07:01:38 -04:00
assert.Equal(state, testState)
}
}
})
}
}
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},
wantErr: true,
},
"master secret exists": {
fileHandler: file.NewHandler(afero.NewMemMapFs()),
2022-04-06 04:36:58 -04:00
existingFiles: []string{constants.MasterSecretFilename},
wantErr: true,
},
"state file exists": {
fileHandler: file.NewHandler(afero.NewMemMapFs()),
2022-04-06 04:36:58 -04:00
existingFiles: []string{constants.StateFilename},
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},
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 {
require.NoError(tc.fileHandler.Write(f, []byte{1, 2, 3}, file.OptNone))
}
2022-04-06 04:36:58 -04:00
err := checkDirClean(tc.fileHandler)
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
}
})
}
}
2022-04-13 07:01:38 -04:00
func intPtr(i int) *int {
return &i
}