2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudtypes"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/state"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTerminator(t *testing.T) {
|
|
|
|
someAzureState := func() state.ConstellationState {
|
|
|
|
return state.ConstellationState{
|
|
|
|
CloudProvider: cloudprovider.Azure.String(),
|
2022-07-29 02:10:51 -04:00
|
|
|
AzureWorkerInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-07-29 02:10:51 -04:00
|
|
|
AzureControlPlaneInstances: cloudtypes.Instances{
|
2022-04-13 07:01:38 -04:00
|
|
|
"id-c": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
|
|
|
AzureADAppObjectID: "00000000-0000-0000-0000-000000000001",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient terraformClient
|
|
|
|
newTfClientErr error
|
2022-04-13 07:01:38 -04:00
|
|
|
azureclient azureclient
|
|
|
|
newAzureClientErr error
|
|
|
|
state state.ConstellationState
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"gcp": {
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient: &stubTerraformClient{},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.GCP.String()},
|
|
|
|
},
|
|
|
|
"gcp newTfClientErr": {
|
|
|
|
newTfClientErr: someErr,
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.GCP.String()},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"gcp destroy cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.GCP.String()},
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"gcp clean up workspace error": {
|
|
|
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.GCP.String()},
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"qemu": {
|
|
|
|
tfClient: &stubTerraformClient{},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.QEMU.String()},
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"qemu destroy cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.QEMU.String()},
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"qemu clean up workspace error": {
|
|
|
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
|
|
|
state: state.ConstellationState{CloudProvider: cloudprovider.QEMU.String()},
|
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
azureclient: &stubAzureClient{},
|
|
|
|
state: someAzureState(),
|
|
|
|
},
|
|
|
|
"azure newAzureClient error": {
|
|
|
|
newAzureClientErr: someErr,
|
|
|
|
state: someAzureState(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-25 09:12:08 -04:00
|
|
|
"azure terminateResourceGroupResources error": {
|
|
|
|
azureclient: &stubAzureClient{terminateResourceGroupResourcesErr: someErr},
|
2022-04-13 07:01:38 -04:00
|
|
|
state: someAzureState(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unknown cloud provider": {
|
|
|
|
state: state.ConstellationState{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
terminator := &Terminator{
|
2022-09-27 03:22:29 -04:00
|
|
|
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
|
|
|
|
return tc.tfClient, tc.newTfClientErr
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
|
|
|
|
return tc.azureclient, tc.newAzureClientErr
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := terminator.Terminate(context.Background(), tc.state)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
switch cloudprovider.FromString(tc.state.CloudProvider) {
|
|
|
|
case cloudprovider.GCP:
|
2022-09-27 03:22:29 -04:00
|
|
|
fallthrough
|
|
|
|
case cloudprovider.QEMU:
|
|
|
|
cl := tc.tfClient.(*stubTerraformClient)
|
|
|
|
assert.True(cl.destroyClusterCalled)
|
|
|
|
assert.True(cl.removeInstallerCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
case cloudprovider.Azure:
|
|
|
|
cl := tc.azureclient.(*stubAzureClient)
|
2022-08-25 09:12:08 -04:00
|
|
|
assert.True(cl.terminateResourceGroupResourcesCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|