2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/file"
|
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
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
|
|
|
|
|
|
|
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) {
|
|
|
|
setupFs := func(require *require.Assertions, state state.ConstellationState) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.Write(constants.AdminConfFilename, []byte{1, 2}, file.OptNone))
|
|
|
|
require.NoError(fileHandler.Write(constants.WGQuickConfigFilename, []byte{1, 2}, file.OptNone))
|
|
|
|
require.NoError(fileHandler.WriteJSON(constants.StateFilename, state, file.OptNone))
|
|
|
|
return fs
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-13 07:01:38 -04:00
|
|
|
state state.ConstellationState
|
|
|
|
setupFs func(*require.Assertions, state.ConstellationState) afero.Fs
|
|
|
|
terminator spyCloudTerminator
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-04-13 07:01:38 -04:00
|
|
|
"success": {
|
|
|
|
state: state.ConstellationState{CloudProvider: "gcp"},
|
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{},
|
|
|
|
},
|
|
|
|
"files to remove do not exist": {
|
|
|
|
state: state.ConstellationState{CloudProvider: "gcp"},
|
|
|
|
setupFs: func(require *require.Assertions, state state.ConstellationState) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.WriteJSON(constants.StateFilename, state, file.OptNone))
|
|
|
|
return fs
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator: &stubCloudTerminator{},
|
|
|
|
},
|
|
|
|
"terminate error": {
|
|
|
|
state: state.ConstellationState{CloudProvider: "gcp"},
|
|
|
|
setupFs: setupFs,
|
|
|
|
terminator: &stubCloudTerminator{terminateErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"missing state file": {
|
|
|
|
state: state.ConstellationState{CloudProvider: "gcp"},
|
|
|
|
setupFs: func(require *require.Assertions, state state.ConstellationState) afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.Write(constants.AdminConfFilename, []byte{1, 2}, file.OptNone))
|
|
|
|
require.NoError(fileHandler.Write(constants.WGQuickConfigFilename, []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{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"remove file fails": {
|
|
|
|
state: state.ConstellationState{CloudProvider: "gcp"},
|
|
|
|
setupFs: func(require *require.Assertions, state state.ConstellationState) afero.Fs {
|
|
|
|
fs := setupFs(require, state)
|
|
|
|
return afero.NewReadOnlyFs(fs)
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator: &stubCloudTerminator{},
|
|
|
|
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
|
|
|
|
|
|
|
cmd := newTerminateCmd()
|
2022-04-13 07:01:38 -04:00
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
require.NotNil(tc.setupFs)
|
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require, tc.state))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
err := terminate(cmd, tc.terminator, fileHandler)
|
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-04-13 07:01:38 -04:00
|
|
|
assert.True(tc.terminator.Called())
|
|
|
|
_, err := fileHandler.Stat(constants.StateFilename)
|
|
|
|
assert.Error(err)
|
|
|
|
_, err = fileHandler.Stat(constants.AdminConfFilename)
|
|
|
|
assert.Error(err)
|
|
|
|
_, err = fileHandler.Stat(constants.WGQuickConfigFilename)
|
|
|
|
assert.Error(err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
type spyCloudTerminator interface {
|
|
|
|
cloudTerminator
|
|
|
|
Called() bool
|
|
|
|
}
|