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 (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-12-08 10:27:04 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constellation/state"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-10-16 09:05:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTerminateCmdArgumentValidation(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
args []string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"no args": {[]string{}, false},
|
|
|
|
"some args": {[]string{"hello", "test"}, true},
|
|
|
|
"some other args": {[]string{"12", "2"}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewTerminateCmd()
|
2022-03-22 11:03:15 -04:00
|
|
|
err := cmd.ValidateArgs(tc.args)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
func TestTerminate(t *testing.T) {
|
2023-10-09 07:04:29 -04:00
|
|
|
setupFs := func(require *require.Assertions, stateFile *state.State) 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, 2}, file.OptNone))
|
2023-10-09 07:04:29 -04:00
|
|
|
require.NoError(stateFile.WriteToFile(fileHandler, constants.StateFilename))
|
2022-04-13 07:01:38 -04:00
|
|
|
return fs
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile *state.State
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag bool
|
|
|
|
stdin string
|
2023-10-09 07:04:29 -04:00
|
|
|
setupFs func(*require.Assertions, *state.State) afero.Fs
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator spyCloudTerminator
|
|
|
|
wantErr bool
|
2022-10-31 11:58:15 -04:00
|
|
|
wantAbort bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-04-13 07:01:38 -04:00
|
|
|
"success": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
2022-04-13 07:01:38 -04:00
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{},
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag: true,
|
|
|
|
},
|
|
|
|
"interactive": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
2022-10-31 11:58:15 -04:00
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{},
|
|
|
|
stdin: "yes\n",
|
|
|
|
},
|
|
|
|
"interactive abort": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
2022-10-31 11:58:15 -04:00
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{},
|
|
|
|
stdin: "no\n",
|
|
|
|
wantAbort: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"files to remove do not exist": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
|
|
|
setupFs: func(require *require.Assertions, stateFile *state.State) afero.Fs {
|
2022-04-13 07:01:38 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
2023-10-09 07:04:29 -04:00
|
|
|
require.NoError(stateFile.WriteToFile(fileHandler, constants.StateFilename))
|
2022-04-13 07:01:38 -04:00
|
|
|
return fs
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator: &stubCloudTerminator{},
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"terminate error": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
2022-04-13 07:01:38 -04:00
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{terminateErr: someErr},
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-10-26 09:57:00 -04:00
|
|
|
"missing id file does not error": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
2024-02-21 07:30:31 -05:00
|
|
|
setupFs: func(require *require.Assertions, _ *state.State) 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, 2}, file.OptNone))
|
|
|
|
return fs
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator: &stubCloudTerminator{},
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"remove file fails": {
|
2023-10-09 07:04:29 -04:00
|
|
|
stateFile: state.New(),
|
|
|
|
setupFs: func(require *require.Assertions, stateFile *state.State) afero.Fs {
|
|
|
|
fs := setupFs(require, stateFile)
|
2022-04-13 07:01:38 -04:00
|
|
|
return afero.NewReadOnlyFs(fs)
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator: &stubCloudTerminator{},
|
2022-10-31 11:58:15 -04:00
|
|
|
yesFlag: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
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)
|
2022-04-13 07:01:38 -04:00
|
|
|
require := require.New(t)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewTerminateCmd()
|
2022-04-13 07:01:38 -04:00
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
2022-10-31 11:58:15 -04:00
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
require.NotNil(tc.setupFs)
|
2023-10-09 07:04:29 -04:00
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require, tc.stateFile))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
tCmd := &terminateCmd{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
fileHandler: fileHandler,
|
|
|
|
flags: terminateFlags{
|
|
|
|
yes: tc.yesFlag,
|
|
|
|
},
|
2022-10-31 11:58:15 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
err := tCmd.terminate(cmd, tc.terminator, &nopSpinner{})
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-10-31 11:58:15 -04:00
|
|
|
if tc.wantAbort {
|
|
|
|
assert.False(tc.terminator.Called())
|
|
|
|
} else {
|
|
|
|
assert.True(tc.terminator.Called())
|
|
|
|
_, err = fileHandler.Stat(constants.AdminConfFilename)
|
|
|
|
assert.Error(err)
|
2023-09-25 12:06:44 -04:00
|
|
|
_, err = fileHandler.Stat(constants.StateFilename)
|
|
|
|
assert.Error(err)
|
2022-10-31 11:58:15 -04:00
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
type spyCloudTerminator interface {
|
|
|
|
cloudTerminator
|
|
|
|
Called() bool
|
|
|
|
}
|