Merge pull request #2548 from jwcesign/fix-dir-lost
karmadactl: Fix karmada-data directory not inilization isssue
This commit is contained in:
commit
6102f41e81
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue