cli: remove unnecessary check from QEMU rollbacker (#2489)

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße 2023-10-23 10:20:25 +02:00 committed by GitHub
parent 9c89b75a53
commit d154703c9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 29 deletions

View File

@ -232,7 +232,7 @@ type qemuCreateOptions struct {
}
func (c *Creator) createQEMU(ctx context.Context, cl tfResourceClient, lv libvirtRunner, opts qemuCreateOptions) (tfOutput state.Infrastructure, retErr error) {
qemuRollbacker := &rollbackerQEMU{client: cl, libvirt: lv, createdWorkspace: false}
qemuRollbacker := &rollbackerQEMU{client: cl, libvirt: lv}
defer rollbackOnError(c.out, &retErr, qemuRollbacker, opts.TFLogLevel)
// TODO(malt3): render progress bar
@ -288,9 +288,6 @@ func (c *Creator) createQEMU(ctx context.Context, cl tfResourceClient, lv libvir
return state.Infrastructure{}, fmt.Errorf("prepare workspace: %w", err)
}
// Allow rollback of QEMU Terraform workspace from this point on
qemuRollbacker.createdWorkspace = true
tfOutput, err = cl.ApplyCluster(ctx, opts.Provider, opts.TFLogLevel)
if err != nil {
return state.Infrastructure{}, fmt.Errorf("create cluster: %w", err)

View File

@ -50,19 +50,17 @@ func (r *rollbackerTerraform) rollback(ctx context.Context, w io.Writer, logLeve
}
type rollbackerQEMU struct {
client tfResourceClient
libvirt libvirtRunner
createdWorkspace bool
client tfResourceClient
libvirt libvirtRunner
}
func (r *rollbackerQEMU) rollback(ctx context.Context, w io.Writer, logLevel terraform.LogLevel) (retErr error) {
if r.createdWorkspace {
retErr = r.client.Destroy(ctx, logLevel)
}
if retErr := errors.Join(retErr, r.libvirt.Stop(ctx)); retErr != nil {
func (r *rollbackerQEMU) rollback(ctx context.Context, w io.Writer, logLevel terraform.LogLevel) error {
tfErr := r.client.Destroy(ctx, logLevel)
libvirtErr := r.libvirt.Stop(ctx)
if err := errors.Join(tfErr, libvirtErr); err != nil {
fmt.Fprintf(w, "Could not destroy the resources. Please delete the %q directory manually if no resources were created\n",
constants.TerraformWorkingDir)
return retErr
return err
}
return r.client.CleanUpWorkspace()
}

View File

@ -70,16 +70,14 @@ func TestRollbackQEMU(t *testing.T) {
someErr := errors.New("failed")
testCases := map[string]struct {
libvirt *stubLibvirtRunner
tfClient *stubTerraformClient
createdWorkspace bool
wantDestroyErr bool
wantErr bool
libvirt *stubLibvirtRunner
tfClient *stubTerraformClient
wantDestroyErr bool
wantErr bool
}{
"success": {
libvirt: &stubLibvirtRunner{},
tfClient: &stubTerraformClient{},
createdWorkspace: true,
libvirt: &stubLibvirtRunner{},
tfClient: &stubTerraformClient{},
},
"stop libvirt error": {
libvirt: &stubLibvirtRunner{stopErr: someErr},
@ -103,9 +101,8 @@ func TestRollbackQEMU(t *testing.T) {
assert := assert.New(t)
rollbacker := &rollbackerQEMU{
libvirt: tc.libvirt,
client: tc.tfClient,
createdWorkspace: tc.createdWorkspace,
libvirt: tc.libvirt,
client: tc.tfClient,
}
destroyClusterErrOutput := &bytes.Buffer{}
@ -125,11 +122,7 @@ func TestRollbackQEMU(t *testing.T) {
}
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.destroyCalled)
assert.True(tc.tfClient.cleanUpWorkspaceCalled)
})
}