Skip to content

Commit

Permalink
Fix bugs about karmada-data dir lost
Browse files Browse the repository at this point in the history
Signed-off-by: jwcesign <jiangwei115@huawei.com>
  • Loading branch information
jwcesign committed Sep 22, 2022
1 parent d81f0ff commit 8b0e9f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,19 @@ func (i *CommandInitOption) Complete() error {
}
}

// Determine whether KarmadaDataPath exists, if so, delete it
if utils.IsExist(i.KarmadaDataPath) {
if err := os.RemoveAll(i.KarmadaDataPath); err != nil {
return initializeDirectory(i.KarmadaDataPath)
}

// initializeDirectory initializes a directory and makes sure it's empty.
func initializeDirectory(path string) error {
if utils.IsExist(path) {
if err := os.RemoveAll(path); err != nil {
return err
}
}
if err := os.MkdirAll(path, os.FileMode(0755)); err != nil {
return fmt.Errorf("failed to create directory: %s, error: %v", path, err)
}

return nil
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/karmadactl/cmdinit/kubernetes/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kubernetes

import (
"os"
"testing"
)

func Test_initializeDirectory(t *testing.T) {
tests := []struct {
name string
createPathInAdvance bool
wantErr bool
}{
{
name: "Test when there is no dir exists",
createPathInAdvance: false,
wantErr: false,
},
{
name: "Test when there is dir exists",
createPathInAdvance: true,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.createPathInAdvance {
if err := os.MkdirAll("tmp", os.FileMode(0755)); err != nil {
t.Errorf("create test directory failed in advance:%v", err)
}
}
if err := initializeDirectory("tmp"); (err != nil) != tt.wantErr {
t.Errorf("initializeDirectory() error = %v, wantErr %v", err, tt.wantErr)
}
if err := os.RemoveAll("tmp"); err != nil {
t.Errorf("clean up test directory failed after ut case:%s, %v", tt.name, err)
}
})
}
}

0 comments on commit 8b0e9f3

Please sign in to comment.