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"
|
2022-04-13 07:01:38 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTerminator(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-10-06 05:52:19 -04:00
|
|
|
tfClient terraformClient
|
|
|
|
newTfClientErr error
|
|
|
|
libvirt *stubLibvirtRunner
|
2022-10-11 06:24:33 -04:00
|
|
|
provider cloudprovider.Provider
|
2022-10-06 05:52:19 -04:00
|
|
|
wantErr bool
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient: &stubTerraformClient{},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.GCP,
|
2022-09-27 03:22:29 -04:00
|
|
|
},
|
|
|
|
"gcp newTfClientErr": {
|
|
|
|
newTfClientErr: someErr,
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.GCP,
|
2022-09-27 03:22:29 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"gcp destroy cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.GCP,
|
2022-09-27 03:22:29 -04:00
|
|
|
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},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.GCP,
|
2022-09-27 03:22:29 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"qemu": {
|
|
|
|
tfClient: &stubTerraformClient{},
|
2022-10-05 03:11:30 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.QEMU,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
"qemu destroy cluster error": {
|
|
|
|
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
|
2022-10-05 03:11:30 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.QEMU,
|
2022-09-27 03:22:29 -04:00
|
|
|
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},
|
2022-10-05 03:11:30 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.QEMU,
|
2022-10-05 03:11:30 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"qemu stop libvirt error": {
|
|
|
|
tfClient: &stubTerraformClient{},
|
|
|
|
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
2022-10-11 06:24:33 -04:00
|
|
|
provider: cloudprovider.QEMU,
|
2022-09-27 03:22:29 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
"unknown cloud provider": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
terminator := &Terminator{
|
2022-10-06 05:52:19 -04:00
|
|
|
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
2022-09-27 03:22:29 -04:00
|
|
|
return tc.tfClient, tc.newTfClientErr
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner: func() libvirtRunner {
|
|
|
|
return tc.libvirt
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
2022-10-11 06:24:33 -04:00
|
|
|
err := terminator.Terminate(context.Background(), tc.provider)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-10-06 05:52:19 -04:00
|
|
|
cl := tc.tfClient.(*stubTerraformClient)
|
|
|
|
assert.True(cl.destroyClusterCalled)
|
|
|
|
assert.True(cl.removeInstallerCalled)
|
2022-10-11 06:24:33 -04:00
|
|
|
if tc.provider == cloudprovider.QEMU {
|
2022-10-05 03:11:30 -04:00
|
|
|
assert.True(tc.libvirt.stopCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|