mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-03 23:04:53 -04:00
Split cmd package
This commit is contained in:
parent
63898c42bf
commit
de52bf14da
36 changed files with 1875 additions and 2302 deletions
|
@ -5,12 +5,12 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/cli/azure"
|
||||
"github.com/edgelesssys/constellation/cli/cloudprovider"
|
||||
"github.com/edgelesssys/constellation/cli/ec2"
|
||||
"github.com/edgelesssys/constellation/cli/gcp"
|
||||
"github.com/edgelesssys/constellation/cli/file"
|
||||
"github.com/edgelesssys/constellation/internal/constants"
|
||||
"github.com/edgelesssys/constellation/internal/state"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTerminateCmdArgumentValidation(t *testing.T) {
|
||||
|
@ -23,12 +23,13 @@ func TestTerminateCmdArgumentValidation(t *testing.T) {
|
|||
"some other args": {[]string{"12", "2"}, true},
|
||||
}
|
||||
|
||||
cmd := newTerminateCmd()
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := newTerminateCmd()
|
||||
err := cmd.ValidateArgs(tc.args)
|
||||
|
||||
if tc.expectErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
|
@ -38,251 +39,98 @@ func TestTerminateCmdArgumentValidation(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTerminateEC2(t *testing.T) {
|
||||
testState := state.ConstellationState{
|
||||
CloudProvider: cloudprovider.AWS.String(),
|
||||
EC2Instances: ec2.Instances{
|
||||
"id-0": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
"id-1": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
"id-3": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
},
|
||||
EC2SecurityGroup: "sg-test",
|
||||
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
|
||||
}
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
existingState state.ConstellationState
|
||||
client ec2client
|
||||
errExpected bool
|
||||
state state.ConstellationState
|
||||
setupFs func(*require.Assertions, state.ConstellationState) afero.Fs
|
||||
terminator spyCloudTerminator
|
||||
wantErr bool
|
||||
}{
|
||||
"terminate existing instances": {
|
||||
existingState: testState,
|
||||
client: &fakeEc2Client{},
|
||||
errExpected: false,
|
||||
"success": {
|
||||
state: state.ConstellationState{CloudProvider: "gcp"},
|
||||
setupFs: setupFs,
|
||||
terminator: &stubCloudTerminator{},
|
||||
},
|
||||
"state without instances": {
|
||||
existingState: state.ConstellationState{
|
||||
CloudProvider: cloudprovider.AWS.String(),
|
||||
EC2Instances: ec2.Instances{},
|
||||
"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
|
||||
},
|
||||
client: &fakeEc2Client{},
|
||||
errExpected: true,
|
||||
terminator: &stubCloudTerminator{},
|
||||
},
|
||||
"fail TerminateInstances": {
|
||||
existingState: testState,
|
||||
client: &stubEc2Client{terminateInstancesErr: someErr},
|
||||
errExpected: true,
|
||||
"terminate error": {
|
||||
state: state.ConstellationState{CloudProvider: "gcp"},
|
||||
setupFs: setupFs,
|
||||
terminator: &stubCloudTerminator{terminateErr: someErr},
|
||||
wantErr: true,
|
||||
},
|
||||
"fail DeleteSecurityGroup": {
|
||||
existingState: testState,
|
||||
client: &stubEc2Client{deleteSecurityGroupErr: someErr},
|
||||
errExpected: 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
|
||||
},
|
||||
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)
|
||||
},
|
||||
terminator: &stubCloudTerminator{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
cmd := newTerminateCmd()
|
||||
out := bytes.NewBufferString("")
|
||||
cmd.SetOut(out)
|
||||
errOut := bytes.NewBufferString("")
|
||||
cmd.SetErr(errOut)
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
err := terminateEC2(cmd, tc.client, tc.existingState)
|
||||
if tc.errExpected {
|
||||
require.NotNil(tc.setupFs)
|
||||
fileHandler := file.NewHandler(tc.setupFs(require, tc.state))
|
||||
|
||||
err := terminate(cmd, tc.terminator, fileHandler)
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminateGCP(t *testing.T) {
|
||||
testState := state.ConstellationState{
|
||||
GCPNodes: gcp.Instances{
|
||||
"id-0": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
"id-1": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
},
|
||||
GCPCoordinators: gcp.Instances{
|
||||
"id-c": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
},
|
||||
GCPNodeInstanceGroup: "nodes-group",
|
||||
GCPCoordinatorInstanceGroup: "coordinator-group",
|
||||
GCPNodeInstanceTemplate: "template",
|
||||
GCPCoordinatorInstanceTemplate: "template",
|
||||
GCPNetwork: "network",
|
||||
GCPFirewalls: []string{"coordinator", "wireguard", "ssh"},
|
||||
}
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
existingState state.ConstellationState
|
||||
client gcpclient
|
||||
errExpected bool
|
||||
}{
|
||||
"terminate existing instances": {
|
||||
existingState: testState,
|
||||
client: &fakeGcpClient{},
|
||||
},
|
||||
"state without instances": {
|
||||
existingState: state.ConstellationState{EC2Instances: ec2.Instances{}},
|
||||
client: &fakeGcpClient{},
|
||||
},
|
||||
"state not found": {
|
||||
existingState: testState,
|
||||
client: &fakeGcpClient{},
|
||||
},
|
||||
"fail setState": {
|
||||
existingState: testState,
|
||||
client: &stubGcpClient{setStateErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail terminateFirewall": {
|
||||
existingState: testState,
|
||||
client: &stubGcpClient{terminateFirewallErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail terminateVPC": {
|
||||
existingState: testState,
|
||||
client: &stubGcpClient{terminateVPCsErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail terminateInstances": {
|
||||
existingState: testState,
|
||||
client: &stubGcpClient{terminateInstancesErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail terminateServiceAccount": {
|
||||
existingState: testState,
|
||||
client: &stubGcpClient{terminateServiceAccountErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := newTerminateCmd()
|
||||
out := bytes.NewBufferString("")
|
||||
cmd.SetOut(out)
|
||||
errOut := bytes.NewBufferString("")
|
||||
cmd.SetErr(errOut)
|
||||
|
||||
err := terminateGCP(cmd, tc.client, tc.existingState)
|
||||
if tc.errExpected {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
stat, err := tc.client.GetState()
|
||||
assert.NoError(err)
|
||||
assert.Equal(state.ConstellationState{
|
||||
CloudProvider: cloudprovider.GCP.String(),
|
||||
}, stat)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminateAzure(t *testing.T) {
|
||||
testState := state.ConstellationState{
|
||||
CloudProvider: cloudprovider.Azure.String(),
|
||||
AzureNodes: azure.Instances{
|
||||
"id-0": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
"id-1": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
},
|
||||
AzureCoordinators: azure.Instances{
|
||||
"id-c": {
|
||||
PrivateIP: "192.0.2.1",
|
||||
PublicIP: "192.0.2.1",
|
||||
},
|
||||
},
|
||||
AzureResourceGroup: "test",
|
||||
}
|
||||
someErr := errors.New("failed")
|
||||
|
||||
testCases := map[string]struct {
|
||||
existingState state.ConstellationState
|
||||
client azureclient
|
||||
errExpected bool
|
||||
}{
|
||||
"terminate existing instances": {
|
||||
existingState: testState,
|
||||
client: &fakeAzureClient{},
|
||||
},
|
||||
"state resource group": {
|
||||
existingState: state.ConstellationState{AzureResourceGroup: ""},
|
||||
client: &fakeAzureClient{},
|
||||
},
|
||||
"state not found": {
|
||||
existingState: testState,
|
||||
client: &fakeAzureClient{},
|
||||
},
|
||||
"fail setState": {
|
||||
existingState: testState,
|
||||
client: &stubAzureClient{setStateErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail resource group termination": {
|
||||
existingState: testState,
|
||||
client: &stubAzureClient{terminateResourceGroupErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
"fail service principal termination": {
|
||||
existingState: testState,
|
||||
client: &stubAzureClient{terminateServicePrincipalErr: someErr},
|
||||
errExpected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := newTerminateCmd()
|
||||
out := bytes.NewBufferString("")
|
||||
cmd.SetOut(out)
|
||||
errOut := bytes.NewBufferString("")
|
||||
cmd.SetErr(errOut)
|
||||
|
||||
err := terminateAzure(cmd, tc.client, tc.existingState)
|
||||
if tc.errExpected {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
stat, err := tc.client.GetState()
|
||||
assert.NoError(err)
|
||||
assert.Equal(state.ConstellationState{
|
||||
CloudProvider: cloudprovider.Azure.String(),
|
||||
}, stat)
|
||||
}
|
||||
})
|
||||
}
|
||||
type spyCloudTerminator interface {
|
||||
cloudTerminator
|
||||
Called() bool
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue