Skip to content

Commit

Permalink
improve agent job log func
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed Jun 24, 2024
1 parent 2e4f771 commit 3031095
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
30 changes: 10 additions & 20 deletions pkg/cli/zadig-agent/helper/log/job_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ func (l *JobLogger) Println(args ...interface{}) {
}
}

func (l *JobLogger) Debugf(args ...interface{}) {
func (l *JobLogger) Debugf(template string, args ...interface{}) {
if l.logger == nil {
return
}

raw := fmt.Sprint(args...)

l.logger.Debug(raw)
l.logger.Debugf(template, args...)
}

func (l *JobLogger) Infof(template string, args ...interface{}) {
Expand All @@ -108,44 +106,36 @@ func (l *JobLogger) Infof(template string, args ...interface{}) {
l.logger.Infof(template, args...)
}

func (l *JobLogger) Warnf(args ...interface{}) {
func (l *JobLogger) Warnf(template string, args ...interface{}) {
if l.logger == nil {
return
}

raw := fmt.Sprint(args...)

l.logger.Warnf(raw)
l.logger.Warnf(template, args...)
}

func (l *JobLogger) Errorf(args ...interface{}) {
func (l *JobLogger) Errorf(template string, args ...interface{}) {
if l.logger == nil {
return
}

raw := fmt.Sprint(args...)

l.logger.Errorf(raw)
l.logger.Errorf(template, args...)
}

func (l *JobLogger) Fatalf(args ...interface{}) {
func (l *JobLogger) Fatalf(template string, args ...interface{}) {
if l.logger == nil {
return
}

raw := fmt.Sprint(args...)

l.logger.Fatalf(raw)
l.logger.Fatalf(template, args...)
}

func (l *JobLogger) Panicf(args ...interface{}) {
func (l *JobLogger) Panicf(template string, args ...interface{}) {
if l.logger == nil {
return
}

raw := fmt.Sprint(args...)

l.logger.Panicf(raw)
l.logger.Panicf(template, args...)
}

func (l *JobLogger) Write(p []byte) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/zadig-agent/internal/agent/job/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (e *JobExecutor) Execute() {
e.Logger.Printf("====================== Job Executor Start ======================\n")
if e.CheckZadigCancel() {
err = fmt.Errorf("user cancel job %s", e.Job.JobName)
e.Logger.Errorf(err)
e.Logger.Errorf(err.Error())
e.JobResult.SetError(err)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func (s *SonarCheckStep) Run(ctx context.Context) error {
}
analysisID, err := client.WaitForCETaskTobeDone(ceTaskID, time.Minute*10)
if err != nil {
s.Logger.Errorf(err)
s.Logger.Errorf(err.Error())
return err
}
gateInfo, err := client.GetQualityGateInfo(analysisID)
if err != nil {
s.Logger.Errorf(err)
s.Logger.Errorf(err.Error())
return err
}
s.Logger.Infof("Sonar quality gate status: %s", gateInfo.ProjectStatus.Status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type JunitReportStep struct {
secretEnvs []string
workspace string
dirs *types.AgentWorkDirs
Logger *log.JobLogger
}

func NewJunitReportStep(spec interface{}, dirs *types.AgentWorkDirs, envs, secretEnvs []string, logger *log.JobLogger) (*JunitReportStep, error) {
Expand All @@ -61,11 +62,12 @@ func NewJunitReportStep(spec interface{}, dirs *types.AgentWorkDirs, envs, secre
if err := yaml.Unmarshal(yamlBytes, &junitReportStep.spec); err != nil {
return junitReportStep, fmt.Errorf("unmarshal spec %s to shell spec failed", yamlBytes)
}
junitReportStep.Logger = logger
return junitReportStep, nil
}

func (s *JunitReportStep) Run(ctx context.Context) error {
log.Info("Start merge ginkgo test results.")
s.Logger.Infof("Start merge ginkgo test results.")
if err := os.MkdirAll(s.spec.DestDir, os.ModePerm); err != nil {
return fmt.Errorf("create dest dir: %s error: %s", s.spec.DestDir, err)
}
Expand All @@ -74,13 +76,13 @@ func (s *JunitReportStep) Run(ctx context.Context) error {
s.spec.ReportDir = helper.ReplaceEnvWithValue(s.spec.ReportDir, envMap)

reportDir := filepath.Join(s.workspace, s.spec.ReportDir)
failedCaseCount, err := mergeGinkgoTestResults(s.spec.FileName, reportDir, s.spec.DestDir, time.Now())
failedCaseCount, err := mergeGinkgoTestResults(s.spec.FileName, reportDir, s.spec.DestDir, time.Now(), s.Logger)
if err != nil {
return fmt.Errorf("failed to merge test result: %s", err)
}
log.Info("Finish merge ginkgo test results.")
s.Logger.Infof("Finish merge ginkgo test results.")

log.Infof("Start archive %s.", s.spec.FileName)
s.Logger.Infof("Start archive %s.", s.spec.FileName)
if s.spec.S3DestDir == "" || s.spec.FileName == "" {
return nil
}
Expand Down Expand Up @@ -116,14 +118,14 @@ func (s *JunitReportStep) Run(ctx context.Context) error {
return err
}
}
log.Infof("Finish archive %s.", s.spec.FileName)
s.Logger.Infof("Finish archive %s.", s.spec.FileName)
if failedCaseCount > 0 {
return fmt.Errorf("%d case(s) failed", failedCaseCount)
}
return nil
}

func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath string, startTime time.Time) (int, error) {
func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath string, startTime time.Time, logger *log.JobLogger) (int, error) {
var (
err error
newXMLBytes []byte
Expand All @@ -149,12 +151,12 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
for _, file := range files {
if filepath.Ext(file.Name()) == ".xml" {
filePath := filepath.Join(testResultPath, file.Name())
log.Infof("name %s mod time: %v", file.Name(), file.ModTime())
logger.Infof("name %s mod time: %v", file.Name(), file.ModTime())

// 1. read file
xmlBytes, err2 := os.ReadFile(filePath)
if err2 != nil {
log.Warningf("Read file [%s], error: %v", filePath, err2)
logger.Warnf("Read file [%s], error: %v", filePath, err2)
continue
}

Expand All @@ -164,7 +166,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
var results *meta.TestSuites
err2 = xml.Unmarshal(xmlBytes, &results)
if err2 != nil {
log.Warningf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
logger.Warnf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
continue
}
for _, testSuite := range results.TestSuites {
Expand All @@ -184,7 +186,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
var result *meta.TestSuite
err2 = xml.Unmarshal(xmlBytes, &result)
if err2 != nil {
log.Warningf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
logger.Warnf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
continue
}
// 4. process summary result attribute
Expand Down Expand Up @@ -226,7 +228,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
return failedCaseCount, err
}

log.Infof("merge test results files %s succeeded", filepath.Join(testUploadPath, testResultFile))
logger.Infof("merge test results files %s succeeded", filepath.Join(testUploadPath, testResultFile))
return summaryResult.Failures, nil
}

Expand Down

0 comments on commit 3031095

Please sign in to comment.