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"
|
|
|
|
|
2023-04-14 08:15:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
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
|
|
|
|
wantErr bool
|
2022-04-13 07:01:38 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
2022-10-26 09:57:00 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient: &stubTerraformClient{},
|
|
|
|
},
|
2022-10-26 09:57:00 -04:00
|
|
|
"newTfClientErr": {
|
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-09-27 03:22:29 -04:00
|
|
|
newTfClientErr: someErr,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-10-26 09:57:00 -04:00
|
|
|
"destroy cluster error": {
|
2023-02-13 02:42:54 -05:00
|
|
|
tfClient: &stubTerraformClient{destroyErr: someErr},
|
2022-10-05 03:11:30 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
2022-09-27 03:22:29 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-10-26 09:57:00 -04:00
|
|
|
"clean up workspace error": {
|
2022-09-27 03:22:29 -04:00
|
|
|
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
|
2022-10-05 03:11:30 -04:00
|
|
|
libvirt: &stubLibvirtRunner{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"qemu stop libvirt error": {
|
|
|
|
tfClient: &stubTerraformClient{},
|
|
|
|
libvirt: &stubLibvirtRunner{stopErr: someErr},
|
2022-09-27 03:22:29 -04:00
|
|
|
wantErr: true,
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
terminator := &Terminator{
|
2022-10-26 09:57:00 -04:00
|
|
|
newTerraformClient: func(ctx context.Context) (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
|
|
|
}
|
|
|
|
|
2023-04-14 08:15:07 -04:00
|
|
|
err := terminator.Terminate(context.Background(), terraform.LogLevelNone)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
2022-10-26 09:57:00 -04:00
|
|
|
return
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
2022-10-26 09:57:00 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
cl := tc.tfClient.(*stubTerraformClient)
|
2023-02-13 02:42:54 -05:00
|
|
|
assert.True(cl.destroyCalled)
|
2022-10-26 09:57:00 -04:00
|
|
|
assert.True(cl.removeInstallerCalled)
|
|
|
|
assert.True(tc.libvirt.stopCalled)
|
2022-04-13 07:01:38 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|