Skip to content

Commit 166c186

Browse files
committed
fix: cleanup azcopy jobs after job complete
1 parent 4df8f6e commit 166c186

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

pkg/azurefile/controllerserver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,9 @@ func (d *Driver) copyFileShareByAzcopy(srcFileShareName, dstFileShareName, srcPa
11671167
klog.Warningf("CopyFileShare(%s, %s, %s) failed with error: %v", accountOptions.ResourceGroup, dstAccountName, dstFileShareName, err)
11681168
} else {
11691169
klog.V(2).Infof("copied fileshare %s to %s successfully", srcFileShareName, dstFileShareName)
1170+
if out, err := d.azcopy.CleanJobs(); err != nil {
1171+
klog.Warningf("clean azcopy jobs failed with error: %v, output: %s", err, string(out))
1172+
}
11701173
}
11711174
return err
11721175
}

pkg/util/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,12 @@ func (ac *Azcopy) GetAzcopyJob(dstFileshare string, authAzcopyEnv []string) (Azc
143143
return jobState, percent, nil
144144
}
145145

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

155154
// parseAzcopyJobList parse command azcopy jobs list, get jobid and state from joblist

pkg/util/util_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ func TestGetAzcopyJob(t *testing.T) {
171171
}
172172
}
173173

174+
func TestCleanJobs(t *testing.T) {
175+
tests := []struct {
176+
desc string
177+
execStr string
178+
execErr error
179+
expectedErr error
180+
}{
181+
{
182+
desc: "run exec get error",
183+
execStr: "",
184+
execErr: fmt.Errorf("error"),
185+
expectedErr: fmt.Errorf("error"),
186+
},
187+
{
188+
desc: "run exec succeed",
189+
execStr: "cleaned",
190+
execErr: nil,
191+
expectedErr: nil,
192+
},
193+
}
194+
for _, test := range tests {
195+
ctrl := gomock.NewController(t)
196+
defer ctrl.Finish()
197+
198+
m := NewMockEXEC(ctrl)
199+
m.EXPECT().RunCommand(gomock.Eq("azcopy jobs clean --with-status=completed"), nil).Return(test.execStr, test.execErr)
200+
201+
azcopyFunc := &Azcopy{}
202+
azcopyFunc.ExecCmd = m
203+
_, err := azcopyFunc.CleanJobs()
204+
if !reflect.DeepEqual(err, test.expectedErr) {
205+
t.Errorf("test[%s]: unexpected err: %v, expected err: %v", test.desc, err, test.expectedErr)
206+
}
207+
}
208+
}
209+
174210
func TestParseAzcopyJobList(t *testing.T) {
175211
tests := []struct {
176212
desc string

0 commit comments

Comments
 (0)