Skip to content

Commit

Permalink
Merge pull request #687 from mskwon/20230707-cleanDir
Browse files Browse the repository at this point in the history
backup: Return nil error for delete of nonexistent directory
  • Loading branch information
Slach authored Jul 8, 2023
2 parents 34291f7 + e8e0b1a commit bb403fd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package backup
import (
"context"
"fmt"
"github.com/Altinity/clickhouse-backup/pkg/custom"
"github.com/Altinity/clickhouse-backup/pkg/status"
"github.com/Altinity/clickhouse-backup/pkg/storage/object_disk"
"github.com/Altinity/clickhouse-backup/pkg/utils"
"github.com/pkg/errors"
"io/fs"
"os"
"path"
Expand All @@ -16,9 +11,14 @@ import (
"time"

"github.com/Altinity/clickhouse-backup/pkg/clickhouse"
"github.com/Altinity/clickhouse-backup/pkg/custom"
"github.com/Altinity/clickhouse-backup/pkg/status"
"github.com/Altinity/clickhouse-backup/pkg/storage"
"github.com/Altinity/clickhouse-backup/pkg/storage/object_disk"
"github.com/Altinity/clickhouse-backup/pkg/utils"

apexLog "github.com/apex/log"
"github.com/pkg/errors"
)

// Clean - removed all data in shadow folder
Expand Down Expand Up @@ -48,6 +48,9 @@ func (b *Backuper) Clean(ctx context.Context) error {

func (b *Backuper) cleanDir(dirName string) error {
if items, err := os.ReadDir(dirName); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
} else {
for _, item := range items {
Expand Down
37 changes: 37 additions & 0 deletions pkg/backup/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package backup

import (
"os"
"path"
"testing"
)

func TestCleanDir(t *testing.T) {
t.Run("Test deletion of nonexistent directory",
func(t *testing.T) {
b := &Backuper{}

dir := path.Join(t.TempDir(), t.Name(), "does-not-exist")
if err := b.cleanDir(dir); err != nil {
t.Fatalf("unexpected error when deleting nonexistent dir: %v", err)
}
},
)

t.Run("Test deletion of existing directory",
func(t *testing.T) {
b := &Backuper{}

dir := t.TempDir()
if err := os.MkdirAll(dir, 0644); err != nil {
t.Fatalf("unexpected error while creating temporary directory: %v", err)
}
if err := b.cleanDir(dir); err != nil {
t.Fatalf("unexpected error while deleting existing dir: %v", err)
}
if err := b.cleanDir(dir); err != nil {
t.Fatalf("unexpected error during back to back invocation of delete: %v", err)
}
},
)
}

0 comments on commit bb403fd

Please sign in to comment.