From d154703c9a3d75eb4ee7e164964f0c58ac93ff02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= <66256922+daniel-weisse@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:20:25 +0200 Subject: [PATCH] cli: remove unnecessary check from QEMU rollbacker (#2489) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- cli/internal/cloudcmd/create.go | 5 +---- cli/internal/cloudcmd/rollback.go | 16 +++++++--------- cli/internal/cloudcmd/rollback_test.go | 25 +++++++++---------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/cli/internal/cloudcmd/create.go b/cli/internal/cloudcmd/create.go index 0c9a43d5e2..ec10340c44 100644 --- a/cli/internal/cloudcmd/create.go +++ b/cli/internal/cloudcmd/create.go @@ -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) diff --git a/cli/internal/cloudcmd/rollback.go b/cli/internal/cloudcmd/rollback.go index 5c2a41d78f..fe7cfe4750 100644 --- a/cli/internal/cloudcmd/rollback.go +++ b/cli/internal/cloudcmd/rollback.go @@ -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() } diff --git a/cli/internal/cloudcmd/rollback_test.go b/cli/internal/cloudcmd/rollback_test.go index fa64331bc6..320dd1745c 100644 --- a/cli/internal/cloudcmd/rollback_test.go +++ b/cli/internal/cloudcmd/rollback_test.go @@ -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) }) }