From 5f5e84e9f4e43bd7261d74c3a0db305107bebfc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= Date: Thu, 2 Nov 2023 11:27:26 +0100 Subject: [PATCH] Fix backup test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- cli/internal/cloudcmd/tfplan.go | 3 ++- cli/internal/cloudcmd/tfplan_test.go | 33 ++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cli/internal/cloudcmd/tfplan.go b/cli/internal/cloudcmd/tfplan.go index 7a6098caf17..bac11467803 100644 --- a/cli/internal/cloudcmd/tfplan.go +++ b/cli/internal/cloudcmd/tfplan.go @@ -67,6 +67,7 @@ func plan( } // restoreBackup replaces the existing Terraform workspace with the backup. +// If no backup exists, this function simply removes workingDir. func restoreBackup(fileHandler file.Handler, workingDir, backupDir string) error { if err := fileHandler.RemoveAll(workingDir); err != nil { return fmt.Errorf("removing existing workspace: %w", err) @@ -74,7 +75,7 @@ func restoreBackup(fileHandler file.Handler, workingDir, backupDir string) error if err := fileHandler.CopyDir( backupDir, workingDir, - ); err != nil && !errors.Is(err, os.ErrNotExist) { // ignore not found error because backup might not exist for new clusters + ); err != nil && !errors.Is(err, os.ErrNotExist) { // ignore not found error because backup does not exist for new clusters return fmt.Errorf("replacing terraform workspace with backup: %w", err) } diff --git a/cli/internal/cloudcmd/tfplan_test.go b/cli/internal/cloudcmd/tfplan_test.go index 2c899c6e4ad..5d7ad6a42aa 100644 --- a/cli/internal/cloudcmd/tfplan_test.go +++ b/cli/internal/cloudcmd/tfplan_test.go @@ -9,6 +9,7 @@ package cloudcmd import ( "context" "io" + "os" "path/filepath" "testing" @@ -123,40 +124,42 @@ func TestTFPlan(t *testing.T) { func TestRestoreBackup(t *testing.T) { existingWorkspace := "foo" backupDir := "bar" + testFile := "file" testCases := map[string]struct { - prepareFs func(require *require.Assertions) file.Handler - wantErr bool + prepareFs func(require *require.Assertions) file.Handler + wantRemoveWorkingDir bool + wantErr bool }{ "success": { prepareFs: func(require *require.Assertions) file.Handler { fs := file.NewHandler(afero.NewMemMapFs()) - require.NoError(fs.MkdirAll(existingWorkspace)) - require.NoError(fs.MkdirAll(backupDir)) + require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll)) + require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll)) return fs }, }, - "existing workspace does not exist": { + "only backup exists": { prepareFs: func(require *require.Assertions) file.Handler { fs := file.NewHandler(afero.NewMemMapFs()) - require.NoError(fs.MkdirAll(backupDir)) + require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll)) return fs }, }, - "backup dir does not exist": { + "only existingWorkspace exists": { prepareFs: func(require *require.Assertions) file.Handler { fs := file.NewHandler(afero.NewMemMapFs()) - require.NoError(fs.MkdirAll(existingWorkspace)) + require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll)) return fs }, - wantErr: true, + wantRemoveWorkingDir: true, }, "read only file system": { prepareFs: func(require *require.Assertions) file.Handler { memFS := afero.NewMemMapFs() fs := file.NewHandler(memFS) - require.NoError(fs.MkdirAll(existingWorkspace)) - require.NoError(fs.MkdirAll(backupDir)) + require.NoError(fs.Write(filepath.Join(existingWorkspace, testFile), []byte{}, file.OptMkdirAll)) + require.NoError(fs.Write(filepath.Join(backupDir, testFile), []byte{}, file.OptMkdirAll)) return file.NewHandler(afero.NewReadOnlyFs(memFS)) }, wantErr: true, @@ -174,6 +177,14 @@ func TestRestoreBackup(t *testing.T) { return } assert.NoError(err) + _, err = fs.Stat(filepath.Join(backupDir, testFile)) + assert.ErrorIs(err, os.ErrNotExist) + _, err = fs.Stat(filepath.Join(existingWorkspace, testFile)) + if tc.wantRemoveWorkingDir { + assert.ErrorIs(err, os.ErrNotExist) + } else { + assert.NoError(err) + } }) } }