Resolve the requested changes

- Add the logic to retry the test case that is failed.
- Accpet the number of how many times we want to retry
- Run the last retry in debug
dev
shubhamrasal 2023-02-24 13:42:19 +05:30
parent ed46e9560e
commit 4ad64eb357
2 changed files with 42 additions and 16 deletions

View File

@ -44,6 +44,7 @@ jobs:
- name: Integration Tests - name: Integration Tests
env: env:
GH_ACTION: true GH_ACTION: true
GH_ACTION_RETRIES: "3"
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: | run: |
chmod +x run.sh chmod +x run.sh

View File

@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
@ -14,6 +15,7 @@ import (
var ( var (
debug = os.Getenv("DEBUG") == "true" debug = os.Getenv("DEBUG") == "true"
githubAction = os.Getenv("GH_ACTION") == "true" githubAction = os.Getenv("GH_ACTION") == "true"
githubActionRetries = os.Getenv("GH_ACTION_RETRIES")
customTests = os.Getenv("TESTS") customTests = os.Getenv("TESTS")
success = aurora.Green("[✓]").String() success = aurora.Green("[✓]").String()
@ -61,16 +63,13 @@ func main() {
os.Exit(1) os.Exit(1)
} }
failedTestTemplatePaths := runTests(toMap(toSlice(customTests))) failedTestCases := runTests(toMap(toSlice(customTests)))
if len(failedTestTemplatePaths) > 0 {
if githubAction { if githubAction {
debug = true if retryOnFailure(failedTestCases) {
fmt.Println("::group::Failed integration tests in debug mode") os.Exit(1)
_ = runTests(failedTestTemplatePaths)
fmt.Println("::endgroup::")
} }
} else if len(failedTestCases) > 0 {
os.Exit(1) os.Exit(1)
} }
} }
@ -86,8 +85,8 @@ func debugTests() {
} }
} }
func runTests(customTemplatePaths map[string]struct{}) map[string]struct{} { func runTests(customTemplatePaths map[string]struct{}) map[string]testutils.TestCase {
failedTestTemplatePaths := map[string]struct{}{} failedTestCase := map[string]testutils.TestCase{}
for proto, testCases := range protocolTests { for proto, testCases := range protocolTests {
if len(customTemplatePaths) == 0 { if len(customTemplatePaths) == 0 {
@ -96,14 +95,14 @@ func runTests(customTemplatePaths map[string]struct{}) map[string]struct{} {
for templatePath, testCase := range testCases { for templatePath, testCase := range testCases {
if len(customTemplatePaths) == 0 || contains(customTemplatePaths, templatePath) { if len(customTemplatePaths) == 0 || contains(customTemplatePaths, templatePath) {
if failedTemplatePath, err := execute(testCase, templatePath); err != nil { if _, err := execute(testCase, templatePath); err != nil {
failedTestTemplatePaths[failedTemplatePath] = struct{}{} failedTestCase[templatePath] = testCase
} }
} }
} }
} }
return failedTestTemplatePaths return failedTestCase
} }
func execute(testCase testutils.TestCase, templatePath string) (string, error) { func execute(testCase testutils.TestCase, templatePath string) (string, error) {
@ -145,3 +144,29 @@ func contains(input map[string]struct{}, value string) bool {
_, ok := input[value] _, ok := input[value]
return ok return ok
} }
// retries failed test cases and run the last retry in debug mode
// if any case failed in retry retrun true
func retryOnFailure(failedTestCases map[string]testutils.TestCase) bool {
retries, _ := strconv.Atoi(githubActionRetries)
var failed bool
for templatePath, testCase := range failedTestCases {
var err error
debug = false
fmt.Println("::group::Failed integration test", templatePath)
for i := 0; i < retries; i++ {
if (retries - i) == 1 {
debug = true
}
_, err = execute(testCase, templatePath)
if err == nil {
break
}
}
fmt.Println("::endgroup::")
if err != nil {
failed = true
}
}
return failed
}