Skip to content

Commit

Permalink
integrate trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Dec 19, 2024
1 parent 6207892 commit 3a2c9a4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci-dgraph-core-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ jobs:
./t -r
# sleep
sleep 5
- name: Upload Test Results
if: !cancelled() # Upload the results even if the tests fail
continue-on-error: true # don't fail this job if the upload fails
uses: trunk-io/analytics-uploader@main
with:
junit-paths: "./t/test-results.xml"
org-slug: hypermode
token: ${{ secrets.TRUNK_TOKEN }}
79 changes: 72 additions & 7 deletions t/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,50 @@ func stopCluster(composeFile, prefix string, wg *sync.WaitGroup, err error) {
}()
}

func runTestsFor(ctx context.Context, pkg, prefix string) error {
var args = []string{"go", "test", "-failfast", "-v", "-tags=integration"}
func mergeXMLFiles(outputFile string, files []string) error {
if len(files) == 0 {
return fmt.Errorf("no files to merge")
}

finalFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer finalFile.Close()

finalFile.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
finalFile.WriteString(`<testsuites>`)

for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Printf("Skipping missing file: %s\n", file)
continue
}

content, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", file, err)
}

// Extract the <testsuite> element from the file and append it
parts := strings.SplitN(string(content), "<testsuite", 2)
if len(parts) > 1 {
finalFile.WriteString("<testsuite" + parts[1])
}
}

finalFile.WriteString(`</testsuites>`)
return nil
}

// Helper function to sanitize filenames
func sanitizeFilename(pkg string) string {
return strings.ReplaceAll(pkg, "/", "_")
}

func runTestsFor(ctx context.Context, pkg, prefix string, xmlFile string) error {
args := []string{"gotestsum", "--junitfile", xmlFile, "--format", "standard-verbose", "--max-fails", "1", "--",
"-v", "-failfast", "-tags=integration"}
if *race {
args = append(args, "-timeout", "180m")
// Todo: There are few race errors in tests itself. Enable this once that is fixed.
Expand Down Expand Up @@ -394,6 +436,27 @@ func runTests(taskCh chan task, closer *z.Closer) error {
ctx := closer.Ctx()
ctx = context.WithValue(ctx, _threadIdKey{}, threadId)

tmpDir, err := os.MkdirTemp("", "dgraph-test-xml")
if err != nil {
return fmt.Errorf("failed to create temp directory: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("Failed to remove temporary directory %s: %v", tmpDir, err)
}
}()

var xmlFiles []string

defer func() {
finalXMLFile := filepath.Join("./", "test-results.xml")
if err := mergeXMLFiles(finalXMLFile, xmlFiles); err != nil {
log.Printf("Error merging XML files: %v\n", err)
} else {
fmt.Printf("Merged test results into %s\n", finalXMLFile)
}
}()

for task := range taskCh {
if ctx.Err() != nil {
err = ctx.Err()
Expand All @@ -403,20 +466,22 @@ func runTests(taskCh chan task, closer *z.Closer) error {
continue
}

xmlFile := filepath.Join(tmpDir, sanitizeFilename(task.pkg.ID))
xmlFiles = append(xmlFiles, xmlFile) // Add XML file path regardless of success or failure
if task.isCommon {
if *runCustom {
// If we only need to run custom cluster tests, then skip this one.
continue
}
start()
if err = runTestsFor(ctx, task.pkg.ID, prefix); err != nil {
// fmt.Printf("ERROR for package: %s. Err: %v\n", task.pkg.ID, err)
if err = runTestsFor(ctx, task.pkg.ID, prefix, xmlFile); err != nil {
fmt.Printf("ERROR for package: %s. Err: %v\n", task.pkg.ID, err)
return err
}
} else {
// we are not using err variable here because we dont want to
// print logs of default cluster in case of custom test fail.
if cerr := runCustomClusterTest(ctx, task.pkg.ID, wg); cerr != nil {
if cerr := runCustomClusterTest(ctx, task.pkg.ID, wg, xmlFile); cerr != nil {
return cerr
}
}
Expand All @@ -441,7 +506,7 @@ func getClusterPrefix() string {
}

// for tests that require custom docker-compose file (located in test directory)
func runCustomClusterTest(ctx context.Context, pkg string, wg *sync.WaitGroup) error {
func runCustomClusterTest(ctx context.Context, pkg string, wg *sync.WaitGroup, xmlFile string) error {
fmt.Printf("Bringing up cluster for package: %s\n", pkg)
var err error
compose := composeFileFor(pkg)
Expand All @@ -455,7 +520,7 @@ func runCustomClusterTest(ctx context.Context, pkg string, wg *sync.WaitGroup) e
defer stopCluster(compose, prefix, wg, err)
}

err = runTestsFor(ctx, pkg, prefix)
err = runTestsFor(ctx, pkg, prefix, xmlFile)
return err
}

Expand Down

0 comments on commit 3a2c9a4

Please sign in to comment.