Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed jobs clean broken command #2850

Merged
merged 10 commits into from
Nov 19, 2024
8 changes: 6 additions & 2 deletions cmd/jobsClean.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/Azure/azure-storage-azcopy/v10/common"
)

var JobsCleanupSuccessMsg = "Successfully removed all jobs."

func init() {
type JobsCleanReq struct {
withStatus string
Expand Down Expand Up @@ -122,10 +124,12 @@ func blindDeleteAllJobFiles() (int, error) {
if err != nil {
return numPlanFilesRemoved, err
}

// get rid of the logs
numLogFilesRemoved, err := removeFilesWithPredicate(azcopyLogPathFolder, func(s string) bool {
if strings.HasSuffix(s, ".log") {
// Do not remove the current job's log file this will cause the cleanup job to fail.
if strings.Contains(s, azcopyCurrentJobID.String()) {
return false
} else if strings.HasSuffix(s, ".log") {
return true
}
return false
Expand Down
26 changes: 16 additions & 10 deletions e2etest/newe2e_task_runazcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package e2etest
import (
"bytes"
"fmt"
"github.com/Azure/azure-storage-azcopy/v10/common"
"github.com/google/uuid"
"io"
"io/fs"
"os"
Expand All @@ -13,6 +11,9 @@ import (
"reflect"
"runtime/debug"
"strings"

"github.com/Azure/azure-storage-azcopy/v10/common"
"github.com/google/uuid"
)

// AzCopyJobPlan todo probably load the job plan directly? WI#26418256
Expand Down Expand Up @@ -52,13 +53,13 @@ var _ AzCopyStdout = &AzCopyRawStdout{}
type AzCopyVerb string

const ( // initially supporting a limited set of verbs
AzCopyVerbCopy AzCopyVerb = "copy"
AzCopyVerbSync AzCopyVerb = "sync"
AzCopyVerbRemove AzCopyVerb = "remove"
AzCopyVerbList AzCopyVerb = "list"
AzCopyVerbLogin AzCopyVerb = "login"
AzCopyVerbLogout AzCopyVerb = "logout"
AzCopyVerbJobsList AzCopyVerb = "jobs"
AzCopyVerbCopy AzCopyVerb = "copy"
AzCopyVerbSync AzCopyVerb = "sync"
AzCopyVerbRemove AzCopyVerb = "remove"
AzCopyVerbList AzCopyVerb = "list"
AzCopyVerbLogin AzCopyVerb = "login"
AzCopyVerbLogout AzCopyVerb = "logout"
AzCopyVerbJobs AzCopyVerb = "jobs"
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
)

type AzCopyTarget struct {
Expand Down Expand Up @@ -318,7 +319,7 @@ func RunAzCopy(a ScenarioAsserter, commandSpec AzCopyCommand) (AzCopyStdout, *Az
}
case commandSpec.Verb == AzCopyVerbList:
out = &AzCopyParsedListStdout{}
case commandSpec.Verb == AzCopyVerbJobsList:
case commandSpec.Verb == AzCopyVerbJobs && len(commandSpec.PositionalArgs) != 0 && commandSpec.PositionalArgs[0] == "list":
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
out = &AzCopyParsedJobsListStdout{}
default: // We don't know how to parse this.
out = &AzCopyRawStdout{}
Expand Down Expand Up @@ -350,6 +351,11 @@ func RunAzCopy(a ScenarioAsserter, commandSpec AzCopyCommand) (AzCopyStdout, *Az
common.Iff[Assertion](commandSpec.ShouldFail, Not{Equal{}}, Equal{}),
0, command.ProcessState.ExitCode())

// don't upload and cleanup logs for clean jobs
if !a.Failed() && len(commandSpec.PositionalArgs) != 0 && commandSpec.PositionalArgs[0] == "clean" {
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
dphulkar-msft marked this conversation as resolved.
Show resolved Hide resolved
return out, &AzCopyJobPlan{}
}

a.Cleanup(func(a ScenarioAsserter) {
if !commandSpec.Environment.ManualLogin {
UploadLogs(a, out, stderr, DerefOrZero(commandSpec.Environment.LogLocation))
Expand Down
15 changes: 15 additions & 0 deletions e2etest/newe2e_task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"io"
"os"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -425,3 +426,17 @@ func ValidateJobsListOutput(a Asserter, stdout AzCopyStdout, expectedJobIDs int)
a.AssertNow("stdout must be AzCopyParsedJobsListStdout", Equal{}, ok, true)
a.Assert("No of jobs executed should be equivalent", Equal{}, expectedJobIDs, jobsListStdout.JobsCount)
}

func ValidateLogFileRetaintion(a Asserter, logsDir string, expectedLogFileToRetain int) {

files, err := os.ReadDir(logsDir)
a.NoError("Failed to read log dir", err)
cnt := 0

for _, file := range files { // first, find the job ID
if strings.HasSuffix(file.Name(), ".log") {
cnt++
}
}
a.AssertNow("Expected job log files to be retained", Equal{}, cnt, expectedLogFileToRetain)
}
49 changes: 49 additions & 0 deletions e2etest/zt_newe2e_jobs_clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package e2etest

import (
"fmt"
"os"
"path/filepath"

"github.com/Azure/azure-storage-azcopy/v10/cmd"
)

func init() {
suiteManager.RegisterSuite(&JobsCleanSuite{})
}

type JobsCleanSuite struct{}

func (s *JobsCleanSuite) Scenario_JobsCleanBasic(svm *ScenarioVariationManager) {

logsDir, err := os.MkdirTemp("", "testLogs")
if err != nil {
svm.t.Fatal("failed to create logs dir:", err)
}
defer os.RemoveAll(logsDir)

jobPlanDir, err := os.MkdirTemp("", "testPlans")
if err != nil {
svm.t.Fatal("failed to create plan file dir:", err)
}
defer os.RemoveAll(jobPlanDir)

_, err = os.Create(filepath.Join(logsDir + fmt.Sprintf("%s.log", "jobID1")))
_, err = os.Create(filepath.Join(logsDir + fmt.Sprintf("%s.log", "jobID2")))

jobsCleanOutput, _ := RunAzCopy(
svm,
AzCopyCommand{
Verb: AzCopyVerbJobs,
PositionalArgs: []string{"clean"},
ShouldFail: false,
Environment: &AzCopyEnvironment{
LogLocation: &logsDir,
JobPlanLocation: &jobPlanDir,
},
})

ValidateMessageOutput(svm, jobsCleanOutput, cmd.JobsCleanupSuccessMsg)
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
ValidateLogFileRetaintion(svm, logsDir, 1)

}
3 changes: 1 addition & 2 deletions e2etest/zt_newe2e_jobs_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ func (s *JobsListSuite) Scenario_JobsListBasic(svm *ScenarioVariationManager) {
jobsListOutput, _ := RunAzCopy(
svm,
AzCopyCommand{
Verb: AzCopyVerbJobsList,
Verb: AzCopyVerbJobs,
PositionalArgs: []string{"list"},
Stdout: &AzCopyParsedJobsListStdout{},
Flags: ListFlags{},
})
ValidateJobsListOutput(svm, jobsListOutput, 0)
}
Loading