constellation/cli/internal/cmd/terminate_test.go

172 lines
4.6 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
import (
"bytes"
"errors"
"testing"
2022-10-11 06:24:33 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
2022-04-13 07:01:38 -04:00
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
2022-04-13 07:01:38 -04:00
"github.com/stretchr/testify/require"
)
func TestTerminateCmdArgumentValidation(t *testing.T) {
testCases := map[string]struct {
args []string
wantErr bool
}{
"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()
err := cmd.ValidateArgs(tc.args)
2022-04-13 07:01:38 -04:00
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
}
})
}
}
2022-04-13 07:01:38 -04:00
func TestTerminate(t *testing.T) {
2022-10-11 06:24:33 -04:00
setupFs := func(require *require.Assertions, idFile clusterid.File) 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))
2022-10-11 06:24:33 -04:00
require.NoError(fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone))
2022-04-13 07:01:38 -04:00
return fs
}
someErr := errors.New("failed")
testCases := map[string]struct {
2022-10-11 06:24:33 -04:00
idFile clusterid.File
2022-10-31 11:58:15 -04:00
yesFlag bool
stdin string
2022-10-11 06:24:33 -04:00
setupFs func(*require.Assertions, clusterid.File) 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-04-13 07:01:38 -04:00
"success": {
2022-10-11 06:24:33 -04:00
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
2022-04-13 07:01:38 -04:00
setupFs: setupFs,
terminator: &stubCloudTerminator{},
2022-10-31 11:58:15 -04:00
yesFlag: true,
},
"interactive": {
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
setupFs: setupFs,
terminator: &stubCloudTerminator{},
stdin: "yes\n",
},
"interactive abort": {
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
setupFs: setupFs,
terminator: &stubCloudTerminator{},
stdin: "no\n",
wantAbort: true,
2022-04-13 07:01:38 -04:00
},
"files to remove do not exist": {
2022-10-11 06:24:33 -04:00
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
setupFs: func(require *require.Assertions, idFile clusterid.File) afero.Fs {
2022-04-13 07:01:38 -04:00
fs := afero.NewMemMapFs()
fileHandler := file.NewHandler(fs)
2022-10-11 06:24:33 -04:00
require.NoError(fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone))
2022-04-13 07:01:38 -04:00
return fs
},
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": {
2022-10-11 06:24:33 -04:00
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
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,
},
"missing id file does not error": {
2022-10-11 06:24:33 -04:00
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
setupFs: func(require *require.Assertions, idFile clusterid.File) 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-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": {
2022-10-11 06:24:33 -04:00
idFile: clusterid.File{CloudProvider: cloudprovider.GCP},
setupFs: func(require *require.Assertions, idFile clusterid.File) afero.Fs {
fs := setupFs(require, idFile)
2022-04-13 07:01:38 -04:00
return afero.NewReadOnlyFs(fs)
},
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,
},
}
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-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))
// register persistent flags manually
cmd.Flags().String("tf-log", "NONE", "")
2022-04-13 07:01:38 -04:00
require.NotNil(tc.setupFs)
2022-10-11 06:24:33 -04:00
fileHandler := file.NewHandler(tc.setupFs(require, tc.idFile))
2022-10-31 11:58:15 -04:00
if tc.yesFlag {
require.NoError(cmd.Flags().Set("yes", "true"))
}
err := terminate(cmd, tc.terminator, fileHandler, &nopSpinner{})
2022-04-13 07:01:38 -04:00
if tc.wantErr {
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)
_, err = fileHandler.Stat(constants.ClusterIDsFileName)
assert.Error(err)
}
}
})
}
}
2022-04-13 07:01:38 -04:00
type spyCloudTerminator interface {
cloudTerminator
Called() bool
}