Skip to content

Commit

Permalink
support host static html resources of test report
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Nov 28, 2024
1 parent c518c5a commit 3c324ca
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 225 deletions.
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ func LocalChartTemplatePath(name string) string {
return LocalTemplatePath(name, setting.ChartTemplatesPath)
}

func LocalHtmlReportPath(project, workflowName string, taskID int64) string {
return filepath.Join(DataPath(), project, workflowName, fmt.Sprintf("%d", taskID), "html-report") + "/"
}

func MongoURI() string {
return viper.GetString(setting.ENVMongoDBConnectionString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (w *WorkflowV4) CalculateHash() [md5.Size]byte {
return md5.Sum(jsonBytes)
}

type ParameterSettingType string

const (
StringType ParameterSettingType = "string"
ChoiceType ParameterSettingType = "choice"
ImageType ParameterSettingType = "image"
Script ParameterSettingType = "script"
// Deprecated
ExternalType ParameterSettingType = "external"
)

type WorkflowStage struct {
Name string `bson:"name" yaml:"name" json:"name"`
Parallel bool `bson:"parallel" yaml:"parallel" json:"parallel"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/microservice/aslan/core/system/handler/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package handler

import (
"encoding/json"
"fmt"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -59,6 +60,9 @@ func UpdateStrategy(c *gin.Context) {
return
}

bs, _ := json.Marshal(args)
internalhandler.InsertOperationLog(c, ctx.UserName, "", "更新", "任务配置", "", string(bs), ctx.Logger)

if err := service.UpdateSysCapStrategy(args); err != nil {
ctx.RespErr = err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,51 +565,36 @@ func (j *TestingJob) toJobtask(jobSubTaskID int, testing *commonmodels.TestModul
StepType: config.StepDebugAfter,
}
jobTaskSpec.Steps = append(jobTaskSpec.Steps, debugAfterStep)
// init archive html step
if len(testingInfo.TestReportPath) > 0 {
ext := filepath.Ext(testingInfo.TestReportPath)
if ext != ".html" {
return jobTask, fmt.Errorf("test report path: %s is not a html file", testingInfo.TestReportPath)
}
outputPath := strings.TrimSuffix(testingInfo.TestReportPath, ext) + "-archive" + ext

archiveHtmlStep := &commonmodels.StepTask{
Name: config.TestJobHTMLReportArchiveStepName,
JobName: jobTask.Name,
StepType: config.StepArchiveHtml,
Onfailure: true,
Spec: step.StepArchiveHtmlSpec{
HtmlPath: testingInfo.TestReportPath,
OutputPath: outputPath,
},
}
jobTaskSpec.Steps = append(jobTaskSpec.Steps, archiveHtmlStep)
tarDestDir := "/tmp"
if testingInfo.ScriptType == types.ScriptTypeBatchFile {
tarDestDir = "%TMP%"
} else if testingInfo.ScriptType == types.ScriptTypePowerShell {
tarDestDir = "%TMP%"
}

uploads := []*step.Upload{
{
FilePath: outputPath,
DestinationPath: path.Join(j.workflow.Name, fmt.Sprint(taskID), jobTask.Name, "html"),
},
}
archiveStep := &commonmodels.StepTask{
// init archive html step
if len(testingInfo.TestReportPath) > 0 {
testReportDir := filepath.Dir(testingInfo.TestReportPath)
testReportName := filepath.Base(testingInfo.TestReportPath)
tarArchiveStep := &commonmodels.StepTask{
Name: config.TestJobHTMLReportStepName,
JobName: jobTask.Name,
StepType: config.StepArchive,
StepType: config.StepTarArchive,
Onfailure: true,
Spec: step.StepArchiveSpec{
UploadDetail: uploads,
S3: modelS3toS3(defaultS3),
Spec: &step.StepTarArchiveSpec{
FileName: setting.HtmlReportArchivedFileName,
AbsResultDir: true,
ResultDirs: []string{testReportName},
ChangeTarDir: true,
TarDir: "$WORKSPACE/" + testReportDir,
DestDir: tarDestDir,
S3DestDir: path.Join(j.workflow.Name, fmt.Sprint(taskID), jobTask.Name, "html-report"),
},
}
jobTaskSpec.Steps = append(jobTaskSpec.Steps, archiveStep)
jobTaskSpec.Steps = append(jobTaskSpec.Steps, tarArchiveStep)
}

destDir := "/tmp"
if testingInfo.ScriptType == types.ScriptTypeBatchFile {
destDir = "%TMP%"
} else if testingInfo.ScriptType == types.ScriptTypePowerShell {
destDir = "%TMP%"
}
// init test result storage step
if len(testingInfo.ArtifactPaths) > 0 {
tarArchiveStep := &commonmodels.StepTask{
Expand All @@ -621,7 +606,7 @@ func (j *TestingJob) toJobtask(jobSubTaskID int, testing *commonmodels.TestModul
ResultDirs: testingInfo.ArtifactPaths,
S3DestDir: path.Join(j.workflow.Name, fmt.Sprint(taskID), jobTask.Name, "test-result"),
FileName: setting.ArtifactResultOut,
DestDir: destDir,
DestDir: tarDestDir,
},
}
if len(testingInfo.ArtifactPaths) > 1 || testingInfo.ArtifactPaths[0] != "" {
Expand All @@ -644,7 +629,7 @@ func (j *TestingJob) toJobtask(jobSubTaskID int, testing *commonmodels.TestModul
S3DestDir: path.Join(j.workflow.Name, fmt.Sprint(taskID), jobTask.Name, "junit"),
TestName: testing.Name,
TestProject: testing.ProjectName,
DestDir: destDir,
DestDir: tarDestDir,
FileName: "merged.xml",
ServiceName: serviceName,
ServiceModule: serviceModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (*Router) Inject(router *gin.RouterGroup) {
// 查看html测试报告不做鉴权
testReport := router.Group("report")
{
testReport.GET("", GetHTMLTestReport)
testReport.GET("workflowv4/:workflowName/id/:id/job/:jobName", GetWorkflowV4HTMLTestReport)
testReport.GET("/html/testing/:projectName/:testName/:taskID/*path", GetTestTaskHtmlReportInfo)
testReport.GET("/html/workflowv4/:projectName/:workflowName/:taskID/*path", GetWorkflowV4HTMLTestReport)
}

// sse apis
Expand Down Expand Up @@ -103,7 +103,6 @@ func (*Router) Inject(router *gin.RouterGroup) {
testTask.DELETE("", CancelTestTaskV3)
testTask.GET("/detail", GetTestTaskInfo)
testTask.GET("/report", GetTestTaskJUnitReportInfo)
testTask.GET("/html_report", GetTestTaskHtmlReportInfo)
testTask.POST("/restart", RestartTestTaskV2)
testTask.GET("/artifact", GetTestingTaskArtifact)
// TODO: below is the deprecated apis, remove after 2.2.0
Expand Down
48 changes: 15 additions & 33 deletions pkg/microservice/aslan/core/workflow/testing/handler/test_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
"github.com/gin-gonic/gin/binding"
"k8s.io/apimachinery/pkg/util/wait"

commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/notify"
commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util"
workflowservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/workflow/service/workflow"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/workflow/testing/service"
"github.com/koderover/zadig/v2/pkg/setting"
Expand Down Expand Up @@ -263,47 +263,29 @@ func GetTestTaskJUnitReportInfo(c *gin.Context) {
}

func GetTestTaskHtmlReportInfo(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}

projectKey := c.Query("projectName")
testName := c.Query("testName")
taskIDStr := c.Query("taskID")

// authorization check
if !ctx.Resources.IsSystemAdmin {
if _, ok := ctx.Resources.ProjectAuthInfo[projectKey]; !ok {
ctx.UnAuthorized = true
return
}

if !ctx.Resources.ProjectAuthInfo[projectKey].IsProjectAdmin &&
!ctx.Resources.ProjectAuthInfo[projectKey].Test.View {
ctx.UnAuthorized = true
return
}
}
_ = c.Param("projectName")
testName := c.Param("testName")
taskIDStr := c.Param("taskID")

taskID, err := strconv.ParseInt(taskIDStr, 10, 64)
if err != nil {
ctx.RespErr = e.ErrInvalidParam.AddDesc(fmt.Sprintf("taskID args err :%s", err))
c.JSON(500, gin.H{"err": fmt.Sprintf("failed to parse taskID %s, err :%s", taskIDStr, err)})
return
}

content, err := service.GetTestTaskHTMLTestReport(testName, taskID, ginzap.WithContext(c).Sugar())
reportDir, path, err := service.GetTestTaskHTMLTestReport(testName, taskID, ginzap.WithContext(c).Sugar())
if err != nil {
c.JSON(500, gin.H{"err": err})
c.JSON(500, gin.H{"err": fmt.Sprintf("failed to get testing task html report, err :%s", err)})
return
}

c.Header("content-type", "text/html")
c.String(200, content)
if path == "" {
log.Debugf("path: %s, reportDir: %s", c.Param("path"), reportDir)
c.FileFromFS(c.Param("path"), gin.Dir(reportDir, false))
} else {
log.Debugf("path: %s, reportDir: %s", path, reportDir)
c.FileFromFS(path, gin.Dir(reportDir, false))
}
}

func RestartTestTaskV2(c *gin.Context) {
Expand Down Expand Up @@ -450,4 +432,4 @@ func GetTestingTaskSSE(c *gin.Context) {
ctx.Logger.Error(err)
}
}, ctx.Logger)
}
}
31 changes: 8 additions & 23 deletions pkg/microservice/aslan/core/workflow/testing/handler/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,35 +276,20 @@ func DeleteTestModule(c *gin.Context) {
ctx.RespErr = commonservice.DeleteTestModule(name, projectKey, ctx.RequestID, ctx.Logger)
}

func GetHTMLTestReport(c *gin.Context) {
content, err := service.GetHTMLTestReport(
c.Query("pipelineName"),
c.Query("pipelineType"),
c.Query("taskID"),
c.Query("testName"),
ginzap.WithContext(c).Sugar(),
)
if err != nil {
c.JSON(500, gin.H{"err": err})
return
}

c.Header("content-type", "text/html")
c.String(200, content)
}

func GetWorkflowV4HTMLTestReport(c *gin.Context) {
taskID, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(500, gin.H{"err": err})
c.JSON(500, gin.H{"err": fmt.Sprintf("invalid taskID %s", c.Param("id"))})
return
}
content, err := service.GetWorkflowV4HTMLTestReport(c.Param("workflowName"), c.Param("jobName"), taskID, ginzap.WithContext(c).Sugar())
reportDir, path, err := service.GetWorkflowV4HTMLTestReport(c.Param("workflowName"), c.Param("jobName"), taskID, ginzap.WithContext(c).Sugar())
if err != nil {
c.JSON(500, gin.H{"err": err})
c.JSON(500, gin.H{"err": fmt.Sprintf("get workflow html test report failed, err: %v", err)})
return
}

c.Header("content-type", "text/html")
c.String(200, content)
if path == "" {
c.FileFromFS(c.Param("path"), gin.Dir(reportDir, false))
} else {
c.FileFromFS(path, gin.Dir(reportDir, false))
}
}
Loading

0 comments on commit 3c324ca

Please sign in to comment.