From 3a2c9a44a86db6763a2d87bef6988174a9b7415c Mon Sep 17 00:00:00 2001 From: shivaji-dgraph Date: Mon, 25 Nov 2024 11:44:38 +0530 Subject: [PATCH] integrate trunk --- .github/workflows/ci-dgraph-core-tests.yml | 8 +++ t/t.go | 79 ++++++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-dgraph-core-tests.yml b/.github/workflows/ci-dgraph-core-tests.yml index b2362a873dd..06bf54f5da4 100644 --- a/.github/workflows/ci-dgraph-core-tests.yml +++ b/.github/workflows/ci-dgraph-core-tests.yml @@ -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 }} diff --git a/t/t.go b/t/t.go index fd815ca8405..59ecd969501 100644 --- a/t/t.go +++ b/t/t.go @@ -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(``) + finalFile.WriteString(``) + + 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 element from the file and append it + parts := strings.SplitN(string(content), " 1 { + finalFile.WriteString("`) + 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. @@ -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() @@ -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 } } @@ -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) @@ -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 }