2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-13 07:01:38 -04:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2023-09-25 10:19:43 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/state"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-10-07 03:38:43 -04:00
|
|
|
"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"
|
2023-01-04 04:46:29 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-07-25 08:20:25 -04:00
|
|
|
consemver "github.com/edgelesssys/constellation/v2/internal/semver"
|
2022-03-22 11:03:15 -04:00
|
|
|
"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) {
|
2023-10-16 14:18:59 -04:00
|
|
|
fsWithDefaultConfigAndState := 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)))
|
|
|
|
stateFile := state.New()
|
|
|
|
switch provider {
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
stateFile.SetInfrastructure(state.Infrastructure{GCP: &state.GCP{}})
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
stateFile.SetInfrastructure(state.Infrastructure{Azure: &state.Azure{}})
|
|
|
|
}
|
|
|
|
require.NoError(stateFile.WriteToFile(file, constants.StateFilename))
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
fsWithoutState := func(require *require.Assertions, provider cloudprovider.Provider) afero.Fs {
|
2022-10-07 03:38:43 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
file := file.NewHandler(fs)
|
|
|
|
require.NoError(file.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), provider)))
|
|
|
|
return fs
|
|
|
|
}
|
2023-09-25 10:19:43 -04:00
|
|
|
infraState := state.Infrastructure{ClusterEndpoint: "192.0.2.1"}
|
2022-04-13 07:01:38 -04:00
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-10-07 03:38:43 -04:00
|
|
|
setupFs func(*require.Assertions, cloudprovider.Provider) afero.Fs
|
2022-05-04 02:50:50 -04:00
|
|
|
creator *stubCloudCreator
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
yesFlag bool
|
|
|
|
controllerCountFlag *int
|
|
|
|
workerCountFlag *int
|
|
|
|
stdin string
|
|
|
|
wantErr bool
|
2022-10-31 11:39:56 -04:00
|
|
|
wantAbort bool
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"create": {
|
2023-10-16 14:18:59 -04:00
|
|
|
setupFs: fsWithDefaultConfigAndState,
|
2023-09-25 10:19:43 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive": {
|
2023-10-16 14:18:59 -04:00
|
|
|
setupFs: fsWithDefaultConfigAndState,
|
2023-09-25 10:19:43 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
stdin: "yes\n",
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive abort": {
|
2023-10-16 14:18:59 -04:00
|
|
|
setupFs: fsWithDefaultConfigAndState,
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
stdin: "no\n",
|
|
|
|
wantAbort: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"interactive error": {
|
2023-10-16 14:18:59 -04:00
|
|
|
setupFs: fsWithDefaultConfigAndState,
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
stdin: "foo\nfoo\nfoo\n",
|
|
|
|
wantErr: true,
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
"old adminConf in directory": {
|
2022-10-07 03:38:43 -04:00
|
|
|
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))
|
2022-10-07 03:38:43 -04:00
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
|
2022-04-13 07:01:38 -04:00
|
|
|
return fs
|
|
|
|
},
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"old masterSecret in directory": {
|
2022-10-07 03:38:43 -04:00
|
|
|
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))
|
2022-10-07 03:38:43 -04:00
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), csp)))
|
2022-04-13 07:01:38 -04:00
|
|
|
return fs
|
|
|
|
},
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"config does not exist": {
|
2023-09-08 15:15:02 -04:00
|
|
|
setupFs: func(a *require.Assertions, p cloudprovider.Provider) afero.Fs { return afero.NewMemMapFs() },
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2023-10-16 14:18:59 -04:00
|
|
|
"state file does not exist": {
|
|
|
|
setupFs: fsWithoutState,
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-10-16 14:18:59 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
"create error": {
|
2023-10-16 14:18:59 -04:00
|
|
|
setupFs: fsWithDefaultConfigAndState,
|
2023-09-08 15:15:02 -04:00
|
|
|
creator: &stubCloudCreator{createErr: someErr},
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2023-10-17 08:37:09 -04:00
|
|
|
"write state file error": {
|
2022-10-07 03:38:43 -04:00
|
|
|
setupFs: func(require *require.Assertions, csp cloudprovider.Provider) afero.Fs {
|
2022-04-13 07:01:38 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
2022-10-07 03:38:43 -04:00
|
|
|
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)
|
|
|
|
},
|
2023-10-17 08:37:09 -04:00
|
|
|
creator: &stubCloudCreator{state: infraState},
|
2023-09-08 15:15:02 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
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-04 02:50:50 -04:00
|
|
|
|
2022-10-07 03:38:43 -04:00
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require, tc.provider))
|
2023-10-16 09:05:29 -04:00
|
|
|
c := &createCmd{log: logger.NewTest(t), flags: createFlags{yes: tc.yesFlag}}
|
2023-06-06 04:32:22 -04:00
|
|
|
err := c.create(cmd, tc.creator, fileHandler, &nopSpinner{}, stubAttestationFetcher{})
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-10-31 11:39:56 -04:00
|
|
|
if tc.wantAbort {
|
2022-04-13 07:01:38 -04:00
|
|
|
assert.False(tc.creator.createCalled)
|
|
|
|
} else {
|
|
|
|
assert.True(tc.creator.createCalled)
|
2023-09-25 10:19:43 -04:00
|
|
|
|
|
|
|
var gotState state.State
|
|
|
|
expectedState := state.Infrastructure{
|
|
|
|
ClusterEndpoint: "192.0.2.1",
|
|
|
|
APIServerCertSANs: []string{},
|
2023-10-09 07:04:29 -04:00
|
|
|
InitSecret: []byte{},
|
2023-09-25 10:19:43 -04:00
|
|
|
}
|
|
|
|
require.NoError(fileHandler.ReadYAML(constants.StateFilename, &gotState))
|
|
|
|
assert.Equal("v1", gotState.Version)
|
|
|
|
assert.Equal(expectedState, gotState.Infrastructure)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestCheckDirClean(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
existingFiles []string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2023-10-26 04:55:50 -04:00
|
|
|
"no file exists": {},
|
2022-03-22 11:03:15 -04:00
|
|
|
"adminconf exists": {
|
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": {
|
2022-04-06 04:36:58 -04:00
|
|
|
existingFiles: []string{constants.MasterSecretFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"multiple exist": {
|
2022-10-11 06:24:33 -04:00
|
|
|
existingFiles: []string{constants.AdminConfFilename, constants.MasterSecretFilename},
|
2022-03-22 11:03:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-10-26 04:55:50 -04:00
|
|
|
"terraform dir exists": {
|
|
|
|
existingFiles: []string{constants.TerraformWorkingDir},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-10-26 04:55:50 -04:00
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
2022-03-22 11:03:15 -04:00
|
|
|
for _, f := range tc.existingFiles {
|
2023-10-26 04:55:50 -04:00
|
|
|
require.NoError(fh.Write(f, []byte{1, 2, 3}, file.OptNone))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
c := &createCmd{log: logger.NewTest(t)}
|
2023-10-26 04:55:50 -04:00
|
|
|
err := c.checkDirClean(fh)
|
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
|
|
|
|
2023-06-27 12:24:35 -04:00
|
|
|
func TestValidateCLIandConstellationVersionCompatibility(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
imageVersion string
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion consemver.Semver
|
|
|
|
cliVersion consemver.Semver
|
2023-06-27 12:24:35 -04:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"empty": {
|
|
|
|
imageVersion: "",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.Semver{},
|
|
|
|
cliVersion: consemver.Semver{},
|
2023-06-27 12:24:35 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid when image < CLI": {
|
|
|
|
imageVersion: "v2.7.1",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 8, 0, ""),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 8, 0, ""),
|
2023-06-27 12:24:35 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid when microservice < CLI": {
|
|
|
|
imageVersion: "v2.8.0",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 7, 1, ""),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 8, 0, ""),
|
2023-06-27 12:24:35 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"valid release version": {
|
|
|
|
imageVersion: "v2.9.0",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 9, 0, ""),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 9, 0, ""),
|
2023-06-27 12:24:35 -04:00
|
|
|
},
|
|
|
|
"valid pre-version": {
|
|
|
|
imageVersion: "ref/main/stream/nightly/v2.9.0-pre.0.20230626150512-0a36ce61719f",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
2023-06-27 12:24:35 -04:00
|
|
|
},
|
|
|
|
"image version suffix need not be equal to CLI version": {
|
|
|
|
imageVersion: "ref/main/stream/nightly/v2.9.0-pre.0.19990626150512-9z36ce61799z",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
2023-06-27 12:24:35 -04:00
|
|
|
},
|
|
|
|
"image version can have different patch version": {
|
|
|
|
imageVersion: "ref/main/stream/nightly/v2.9.1-pre.0.19990626150512-9z36ce61799z",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
2023-06-27 12:24:35 -04:00
|
|
|
},
|
|
|
|
"microService version suffix must be equal to CLI version": {
|
|
|
|
imageVersion: "ref/main/stream/nightly/v2.9.0-pre.0.20230626150512-0a36ce61719f",
|
2023-07-25 08:20:25 -04:00
|
|
|
microServiceVersion: consemver.NewFromInt(2, 9, 0, "pre.0.19990626150512-9z36ce61799z"),
|
|
|
|
cliVersion: consemver.NewFromInt(2, 9, 0, "pre.0.20230626150512-0a36ce61719f"),
|
2023-06-27 12:24:35 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := validateCLIandConstellationVersionAreEqual(tc.cliVersion, tc.imageVersion, tc.microServiceVersion)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|