Skip to content

Commit

Permalink
cli: remove unnecessary check from QEMU rollbacker (#2489)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse authored Oct 23, 2023
1 parent 9c89b75 commit d154703
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
5 changes: 1 addition & 4 deletions cli/internal/cloudcmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions cli/internal/cloudcmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
25 changes: 9 additions & 16 deletions cli/internal/cloudcmd/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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{}
Expand All @@ -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)
})
}
Expand Down

0 comments on commit d154703

Please sign in to comment.