constellation/cli/internal/cloudcmd/rollback_test.go
Moritz Sanft 7410cf8038
cli: fix iam rollback (#1148)
* AB#2897 rename DestroyCluster

* #AB2897 error if terraform dir exists

* AB#2897 reword DestroyResources
2023-02-13 08:42:54 +01:00

120 lines
2.6 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cloudcmd
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRollbackTerraform(t *testing.T) {
someErr := errors.New("failed")
testCases := map[string]struct {
tfClient *stubTerraformClient
wantErr bool
}{
"success": {
tfClient: &stubTerraformClient{},
},
"destroy cluster error": {
tfClient: &stubTerraformClient{destroyErr: someErr},
wantErr: true,
},
"clean up workspace error": {
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
rollbacker := &rollbackerTerraform{
client: tc.tfClient,
}
err := rollbacker.rollback(context.Background())
if tc.wantErr {
assert.Error(err)
if tc.tfClient.cleanUpWorkspaceErr == nil {
assert.False(tc.tfClient.cleanUpWorkspaceCalled)
}
return
}
assert.NoError(err)
assert.True(tc.tfClient.destroyCalled)
assert.True(tc.tfClient.cleanUpWorkspaceCalled)
})
}
}
func TestRollbackQEMU(t *testing.T) {
someErr := errors.New("failed")
testCases := map[string]struct {
libvirt *stubLibvirtRunner
tfClient *stubTerraformClient
createdWorkspace bool
wantErr bool
}{
"success": {
libvirt: &stubLibvirtRunner{},
tfClient: &stubTerraformClient{},
createdWorkspace: true,
},
"stop libvirt error": {
libvirt: &stubLibvirtRunner{stopErr: someErr},
tfClient: &stubTerraformClient{},
wantErr: true,
},
"destroy cluster error": {
libvirt: &stubLibvirtRunner{stopErr: someErr},
tfClient: &stubTerraformClient{destroyErr: someErr},
wantErr: true,
},
"clean up workspace error": {
libvirt: &stubLibvirtRunner{},
tfClient: &stubTerraformClient{cleanUpWorkspaceErr: someErr},
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
rollbacker := &rollbackerQEMU{
libvirt: tc.libvirt,
client: tc.tfClient,
createdWorkspace: tc.createdWorkspace,
}
err := rollbacker.rollback(context.Background())
if tc.wantErr {
assert.Error(err)
if tc.tfClient.cleanUpWorkspaceErr == nil {
assert.False(tc.tfClient.cleanUpWorkspaceCalled)
}
return
}
assert.NoError(err)
assert.True(tc.libvirt.stopCalled)
if tc.createdWorkspace {
assert.True(tc.tfClient.destroyCalled)
} else {
assert.False(tc.tfClient.destroyCalled)
}
assert.True(tc.tfClient.cleanUpWorkspaceCalled)
})
}
}