2022-10-26 09:57:00 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
package cloudcmd
import (
2023-08-24 10:38:02 -04:00
"bytes"
2022-10-26 09:57:00 -04:00
"context"
"errors"
"testing"
2023-04-14 08:15:07 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-10-26 09:57:00 -04:00
"github.com/stretchr/testify/assert"
)
func TestRollbackTerraform ( t * testing . T ) {
someErr := errors . New ( "failed" )
testCases := map [ string ] struct {
2023-08-24 10:38:02 -04:00
tfClient * stubTerraformClient
wantCleanupErr bool
wantDestroyErr bool
2022-10-26 09:57:00 -04:00
} {
"success" : {
tfClient : & stubTerraformClient { } ,
} ,
"destroy cluster error" : {
2023-08-24 10:38:02 -04:00
tfClient : & stubTerraformClient { destroyErr : someErr } ,
wantDestroyErr : true ,
2022-10-26 09:57:00 -04:00
} ,
"clean up workspace error" : {
2023-08-24 10:38:02 -04:00
tfClient : & stubTerraformClient { cleanUpWorkspaceErr : someErr } ,
wantCleanupErr : true ,
2022-10-26 09:57:00 -04:00
} ,
}
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
assert := assert . New ( t )
rollbacker := & rollbackerTerraform {
client : tc . tfClient ,
}
2023-08-24 10:38:02 -04:00
destroyClusterErrOutput := & bytes . Buffer { }
err := rollbacker . rollback ( context . Background ( ) , destroyClusterErrOutput , terraform . LogLevelNone )
if tc . wantCleanupErr {
2022-10-26 09:57:00 -04:00
assert . Error ( err )
if tc . tfClient . cleanUpWorkspaceErr == nil {
assert . False ( tc . tfClient . cleanUpWorkspaceCalled )
}
return
}
2023-08-24 10:38:02 -04:00
if tc . wantDestroyErr {
assert . Error ( err )
assert . Equal ( "Could not destroy the resources. Please delete the \"constellation-terraform\" directory manually if no resources were created\n" , destroyClusterErrOutput . String ( ) )
return
}
2022-10-26 09:57:00 -04:00
assert . NoError ( err )
2023-02-13 02:42:54 -05:00
assert . True ( tc . tfClient . destroyCalled )
2022-10-26 09:57:00 -04:00
assert . True ( tc . tfClient . cleanUpWorkspaceCalled )
} )
}
}
func TestRollbackQEMU ( t * testing . T ) {
someErr := errors . New ( "failed" )
testCases := map [ string ] struct {
2023-10-23 04:20:25 -04:00
libvirt * stubLibvirtRunner
tfClient * stubTerraformClient
wantDestroyErr bool
wantErr bool
2022-10-26 09:57:00 -04:00
} {
"success" : {
2023-10-23 04:20:25 -04:00
libvirt : & stubLibvirtRunner { } ,
tfClient : & stubTerraformClient { } ,
2022-10-26 09:57:00 -04:00
} ,
"stop libvirt error" : {
libvirt : & stubLibvirtRunner { stopErr : someErr } ,
tfClient : & stubTerraformClient { } ,
wantErr : true ,
} ,
"destroy cluster error" : {
2023-08-24 10:38:02 -04:00
libvirt : & stubLibvirtRunner { stopErr : someErr } ,
tfClient : & stubTerraformClient { destroyErr : someErr } ,
wantDestroyErr : true ,
2022-10-26 09:57:00 -04:00
} ,
"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 {
2023-10-23 04:20:25 -04:00
libvirt : tc . libvirt ,
client : tc . tfClient ,
2022-10-26 09:57:00 -04:00
}
2023-08-24 10:38:02 -04:00
destroyClusterErrOutput := & bytes . Buffer { }
err := rollbacker . rollback ( context . Background ( ) , destroyClusterErrOutput , terraform . LogLevelNone )
2022-10-26 09:57:00 -04:00
if tc . wantErr {
assert . Error ( err )
if tc . tfClient . cleanUpWorkspaceErr == nil {
assert . False ( tc . tfClient . cleanUpWorkspaceCalled )
}
return
}
2023-08-24 10:38:02 -04:00
if tc . wantDestroyErr {
assert . Error ( err )
assert . Equal ( "Could not destroy the resources. Please delete the \"constellation-terraform\" directory manually if no resources were created\n" , destroyClusterErrOutput . String ( ) )
return
}
2022-10-26 09:57:00 -04:00
assert . NoError ( err )
assert . True ( tc . libvirt . stopCalled )
2023-10-23 04:20:25 -04:00
assert . True ( tc . tfClient . destroyCalled )
2022-10-26 09:57:00 -04:00
assert . True ( tc . tfClient . cleanUpWorkspaceCalled )
} )
}
}