Skip to content

Commit

Permalink
release: v0.1.3 (#16)
Browse files Browse the repository at this point in the history
* fix(java-runner): Remove warning lines from tests output (#13)

* fix(java-runner): Update execution time limit (#15)

* fix(java-runner): Increase tests execution time limit

* fix(java-runner): Handle empty error message
  • Loading branch information
PedroChaparro authored Jan 23, 2024
1 parent 90a559e commit 72375f3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## [0.1.3](https://github.com/upb-code-labs/tests-microservice/compare/v0.1.2...v0.1.3) (2024-01-23)


### Bug Fixes

* **java-runner:** Update execution time limit ([#15](https://github.com/upb-code-labs/tests-microservice/issues/15)) ([571b296](https://github.com/upb-code-labs/tests-microservice/commit/571b296f860cb11b732b384a2ce54e6a4426dae0))



## [0.1.2](https://github.com/upb-code-labs/tests-microservice/compare/v0.1.1...v0.1.2) (2024-01-23)


### Bug Fixes

* **java-runner:** Remove warning lines from tests output ([#13](https://github.com/upb-code-labs/tests-microservice/issues/13)) ([a76c5fb](https://github.com/upb-code-labs/tests-microservice/commit/a76c5fbd1b47fe9a41e2c26b1c69e3abe1ed902b))



## [0.1.1](https://github.com/upb-code-labs/tests-microservice/compare/v0.1.0...v0.1.1) (2024-01-06)


Expand Down
62 changes: 48 additions & 14 deletions src/infrastructure/implementations/java-tests-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (

type JavaTestsRunner struct{}

var templateArchivePathTemplate = "%s/template.zip"
var testsArchivePathTemplate = "%s/tests.zip"
var submissionArchivePathTemplate = "%s/submission.zip"

var defaultErrorLineOnEmptyErrorLines = "[ERROR] We had an error while running the tests. It's possible that the tests execution time exceeded our limit. Please try again or report the issue if it persists."

// SaveArchivesInFS saves the archives needed to run the tests in the file system
func (javaTestsRunner *JavaTestsRunner) SaveArchivesInFS(dto *dtos.TestArchivesDTO) error {
// Ensure the directory doesn't exist
path := fmt.Sprintf(
Expand All @@ -38,23 +45,23 @@ func (javaTestsRunner *JavaTestsRunner) SaveArchivesInFS(dto *dtos.TestArchivesD
// Save the archives
err = javaTestsRunner.saveArchiveInFS(
dto.LanguageTemplateArchive,
fmt.Sprintf("%s/template.zip", path),
fmt.Sprintf(templateArchivePathTemplate, path),
)
if err != nil {
return err
}

err = javaTestsRunner.saveArchiveInFS(
dto.TestsArchive,
fmt.Sprintf("%s/tests.zip", path),
fmt.Sprintf(testsArchivePathTemplate, path),
)
if err != nil {
return err
}

err = javaTestsRunner.saveArchiveInFS(
dto.SubmissionArchive,
fmt.Sprintf("%s/submission.zip", path),
fmt.Sprintf(submissionArchivePathTemplate, path),
)
if err != nil {
return err
Expand All @@ -69,6 +76,8 @@ func (javaTestsRunner *JavaTestsRunner) saveArchiveInFS(fileBytes *[]byte, path
return err
}

// MergeArchives merges the content of the template archive, teacher's tests archive and student's
// submission archive into a single directory that will be used to run the tests
func (javaTestsRunner *JavaTestsRunner) MergeArchives(submissionUUID string) error {
// Unzip the archives
err := javaTestsRunner.unzipArchives(submissionUUID)
Expand Down Expand Up @@ -114,7 +123,7 @@ func (javaTestsRunner *JavaTestsRunner) unzipArchives(submissionUUID string) err
archivesManager := ArchivesManagerImplementation{}

err := archivesManager.ExtractArchive(
fmt.Sprintf("%s/template.zip", submissionPathPrefix),
fmt.Sprintf(templateArchivePathTemplate, submissionPathPrefix),
fmt.Sprintf("%s/template", submissionPathPrefix),
)
if err != nil {
Expand All @@ -123,7 +132,7 @@ func (javaTestsRunner *JavaTestsRunner) unzipArchives(submissionUUID string) err
}

err = archivesManager.ExtractArchive(
fmt.Sprintf("%s/tests.zip", submissionPathPrefix),
fmt.Sprintf(testsArchivePathTemplate, submissionPathPrefix),
fmt.Sprintf("%s/tests", submissionPathPrefix),
)
if err != nil {
Expand All @@ -132,7 +141,7 @@ func (javaTestsRunner *JavaTestsRunner) unzipArchives(submissionUUID string) err
}

err = archivesManager.ExtractArchive(
fmt.Sprintf("%s/submission.zip", submissionPathPrefix),
fmt.Sprintf(submissionArchivePathTemplate, submissionPathPrefix),
fmt.Sprintf("%s/submission", submissionPathPrefix),
)
if err != nil {
Expand All @@ -153,23 +162,23 @@ func (javaTestsRunner *JavaTestsRunner) deleteArchives(submissionUUID string) er
archivesManager := ArchivesManagerImplementation{}

err := archivesManager.DeleteArchive(
fmt.Sprintf("%s/template.zip", submissionPathPrefix),
fmt.Sprintf(templateArchivePathTemplate, submissionPathPrefix),
)
if err != nil {
log.Println("Error while deleting template archive", err)
return err
}

err = archivesManager.DeleteArchive(
fmt.Sprintf("%s/tests.zip", submissionPathPrefix),
fmt.Sprintf(testsArchivePathTemplate, submissionPathPrefix),
)
if err != nil {
log.Println("Error while deleting tests archive", err)
return err
}

err = archivesManager.DeleteArchive(
fmt.Sprintf("%s/submission.zip", submissionPathPrefix),
fmt.Sprintf(submissionArchivePathTemplate, submissionPathPrefix),
)
if err != nil {
log.Println("Error while deleting submission archive", err)
Expand All @@ -179,8 +188,15 @@ func (javaTestsRunner *JavaTestsRunner) deleteArchives(submissionUUID string) er
return nil
}

// RunTests runs the tests and returns the result
func (javaTestsRunner *JavaTestsRunner) RunTests(submissionUUID string) (dto *dtos.TestResultDTO, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
timeLimitInMinutes := 2.0
timeLimitInSeconds := int(timeLimitInMinutes * 60)

ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeLimitInSeconds)*time.Second,
)
defer cancel()

// Delete the submission directory at the end
Expand All @@ -195,8 +211,9 @@ func (javaTestsRunner *JavaTestsRunner) RunTests(submissionUUID string) (dto *dt

// Prepare the command
testAndBuildCommand := fmt.Sprintf(
"cd %s && timeout 1m mvn clean test",
"cd %s && timeout %dm mvn clean test",
submissionPath,
timeLimitInSeconds,
)

cmd := exec.CommandContext(
Expand All @@ -214,6 +231,10 @@ func (javaTestsRunner *JavaTestsRunner) RunTests(submissionUUID string) (dto *dt
errorLines := javaTestsRunner.getErrorLinesFromOutput(string(out))
errorLines = javaTestsRunner.sanitizeConsoleTextLines(errorLines)

if len(errorLines) == 0 {
errorLines = []string{defaultErrorLineOnEmptyErrorLines}
}

return &dtos.TestResultDTO{
SubmissionUUID: submissionUUID,
TestsPassed: false,
Expand All @@ -222,7 +243,7 @@ func (javaTestsRunner *JavaTestsRunner) RunTests(submissionUUID string) (dto *dt
}

// Parse success lines
successLines := javaTestsRunner.getSuccessLinesFromOutput(string(out))
successLines := javaTestsRunner.getResultLinesFromSuccessOutput(string(out))
successLines = javaTestsRunner.sanitizeConsoleTextLines(successLines)

return &dtos.TestResultDTO{
Expand All @@ -232,34 +253,47 @@ func (javaTestsRunner *JavaTestsRunner) RunTests(submissionUUID string) (dto *dt
}, nil
}

// getErrorLinesFromOutput returns the lines starting with the `[ERROR]` prefix from the output
func (javaTestsRunner *JavaTestsRunner) getErrorLinesFromOutput(output string) []string {
errorRegex := regexp.MustCompile(`(?m)^\[ERROR\].*$`)
errorLines := errorRegex.FindAllString(output, -1)
return errorLines
}

// sanitizeConsoleTextLines removes the lines that are not relevant for the user or can contain
// sensitive information
func (javaTestsRunner *JavaTestsRunner) sanitizeConsoleTextLines(textLines []string) []string {
sanitizedTextLines := []string{}

regExpToReplace := []dtos.ReplaceRegexDTO{
// Remove possible path to the tests execution directory
{
Regexp: *regexp.MustCompile(`\/[a-zA-Z0-9_\-]+(?:\/[a-zA-Z0-9_\-]+)*\/template`),
Replacement: "****/****",
},
// Remove lines starting with [WARNING]
{
Regexp: *regexp.MustCompile(`(?m)^\[WARNING\].*$`),
Replacement: "",
},
}

for _, errorLine := range textLines {
for _, regExp := range regExpToReplace {
errorLine = regExp.Regexp.ReplaceAllString(errorLine, regExp.Replacement)
}

sanitizedTextLines = append(sanitizedTextLines, errorLine)
if len(errorLine) > 0 {
sanitizedTextLines = append(sanitizedTextLines, errorLine)
}
}

return sanitizedTextLines
}

func (javaTestsRunner *JavaTestsRunner) getSuccessLinesFromOutput(output string) []string {
// getResultLinesFromSuccessOutput returns the lines of the output starting from the tests results
// summary / header line until the end
func (javaTestsRunner *JavaTestsRunner) getResultLinesFromSuccessOutput(output string) []string {
// Header line
successRegex := regexp.MustCompile(`\[INFO\] Tests run: \d+, Failures: \d+, Errors: \d+, Skipped: \d+`)

Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.1.1"
"version": "0.1.3"
}

0 comments on commit 72375f3

Please sign in to comment.