Skip to content

Commit

Permalink
Fix backup test
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Nov 2, 2023
1 parent 0064c10 commit 5f5e84e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cli/internal/cloudcmd/tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ 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)
}
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)
}

Expand Down
33 changes: 22 additions & 11 deletions cli/internal/cloudcmd/tfplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cloudcmd
import (
"context"
"io"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
})
}
}
Expand Down

0 comments on commit 5f5e84e

Please sign in to comment.