constellation/cli/internal/cloudcmd/terminate_test.go

79 lines
1.7 KiB
Go
Raw Normal View History

/*
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"
"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": {
libvirt: &stubLibvirtRunner{},
2022-09-27 03:22:29 -04:00
tfClient: &stubTerraformClient{},
},
"newTfClientErr": {
libvirt: &stubLibvirtRunner{},
2022-09-27 03:22:29 -04:00
newTfClientErr: someErr,
wantErr: true,
},
"destroy cluster error": {
2022-09-27 03:22:29 -04:00
tfClient: &stubTerraformClient{destroyClusterErr: someErr},
libvirt: &stubLibvirtRunner{},
2022-09-27 03:22:29 -04:00
wantErr: true,
2022-04-13 07:01:38 -04:00
},
"clean up workspace error": {
2022-09-27 03:22:29 -04:00
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
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{
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
},
newLibvirtRunner: func() libvirtRunner {
return tc.libvirt
},
2022-04-13 07:01:38 -04:00
}
err := terminator.Terminate(context.Background())
2022-04-13 07:01:38 -04:00
if tc.wantErr {
assert.Error(err)
return
2022-04-13 07:01:38 -04:00
}
assert.NoError(err)
cl := tc.tfClient.(*stubTerraformClient)
assert.True(cl.destroyClusterCalled)
assert.True(cl.removeInstallerCalled)
assert.True(tc.libvirt.stopCalled)
2022-04-13 07:01:38 -04:00
})
}
}