Skip to content

Commit

Permalink
fix: cleanup azcopy jobs after job complete
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Feb 20, 2025
1 parent 4df8f6e commit 166c186
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/azurefile/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,9 @@ func (d *Driver) copyFileShareByAzcopy(srcFileShareName, dstFileShareName, srcPa
klog.Warningf("CopyFileShare(%s, %s, %s) failed with error: %v", accountOptions.ResourceGroup, dstAccountName, dstFileShareName, err)
} else {
klog.V(2).Infof("copied fileshare %s to %s successfully", srcFileShareName, dstFileShareName)
if out, err := d.azcopy.CleanJobs(); err != nil {
klog.Warningf("clean azcopy jobs failed with error: %v, output: %s", err, string(out))
}
}
return err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ func (ac *Azcopy) GetAzcopyJob(dstFileshare string, authAzcopyEnv []string) (Azc
return jobState, percent, nil
}

// TestListJobs test azcopy jobs list command with authAzcopyEnv
func (ac *Azcopy) TestListJobs(accountName, storageEndpointSuffix string, authAzcopyEnv []string) (string, error) {
cmdStr := fmt.Sprintf("azcopy list %s", fmt.Sprintf("https://%s.file.%s", accountName, storageEndpointSuffix))
func (ac *Azcopy) CleanJobs() (string, error) {
cmdStr := "azcopy jobs clean --with-status=completed"
if ac.ExecCmd == nil {
ac.ExecCmd = &ExecCommand{}
}
return ac.ExecCmd.RunCommand(cmdStr, authAzcopyEnv)
return ac.ExecCmd.RunCommand(cmdStr, nil)
}

// parseAzcopyJobList parse command azcopy jobs list, get jobid and state from joblist
Expand Down
36 changes: 36 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ func TestGetAzcopyJob(t *testing.T) {
}
}

func TestCleanJobs(t *testing.T) {
tests := []struct {
desc string
execStr string
execErr error
expectedErr error
}{
{
desc: "run exec get error",
execStr: "",
execErr: fmt.Errorf("error"),
expectedErr: fmt.Errorf("error"),
},
{
desc: "run exec succeed",
execStr: "cleaned",
execErr: nil,
expectedErr: nil,
},
}
for _, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

m := NewMockEXEC(ctrl)
m.EXPECT().RunCommand(gomock.Eq("azcopy jobs clean --with-status=completed"), nil).Return(test.execStr, test.execErr)

azcopyFunc := &Azcopy{}
azcopyFunc.ExecCmd = m
_, err := azcopyFunc.CleanJobs()
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("test[%s]: unexpected err: %v, expected err: %v", test.desc, err, test.expectedErr)
}
}
}

func TestParseAzcopyJobList(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 166c186

Please sign in to comment.