Skip to content

Commit

Permalink
Resolved review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-dgraph committed Feb 22, 2023
1 parent 4d952e9 commit e920ba6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build integration

package main

import (
Expand Down
81 changes: 25 additions & 56 deletions t/t.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Dgraph Labs, Inc. and Contributors
* Copyright 2022 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand All @@ -39,7 +40,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/golang/glog"
"github.com/spf13/pflag"
"golang.org/x/tools/go/packages"

Expand All @@ -66,8 +66,6 @@ var (
"Only run tests for this package")
runTest = pflag.StringP("test", "t", "",
"Only run this test")
testType = pflag.StringP("test-type", "", "",
"Test type e.g unit,integration,upgrade")
runCustom = pflag.BoolP("custom-only", "o", false,
"Run only custom cluster tests.")
count = pflag.IntP("count", "c", 0,
Expand Down Expand Up @@ -170,13 +168,8 @@ func detectRace(prefix string) bool {
}

func outputLogs(prefix string) {
f, err := os.CreateTemp(".", prefix+"*.log")
f, err := ioutil.TempFile(".", prefix+"*.log")
x.Check(err)
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("error closing file: %v", err)
}
}()
printLogs := func(container string) {
in := testutil.GetContainerInstance(prefix, container)
c := in.GetContainer()
Expand All @@ -186,9 +179,7 @@ func outputLogs(prefix string) {
logCmd := exec.Command("docker", "logs", c.ID)
out, err := logCmd.CombinedOutput()
x.Check(err)
if _, err := f.Write(out); err != nil {
fmt.Printf("error writing container logs to file: %v", err)
}
f.Write(out)
fmt.Printf("Docker logs for %s is %s with error %+v ", c.ID, string(out), err)
}
for i := 0; i <= 3; i++ {
Expand All @@ -198,6 +189,8 @@ func outputLogs(prefix string) {
for i := 0; i <= 6; i++ {
printLogs("alpha" + strconv.Itoa(i))
}
f.Sync()
f.Close()
s := fmt.Sprintf("---> LOGS for %s written to %s .\n", prefix, f.Name())
_, err = oc.Write([]byte(s))
x.Check(err)
Expand All @@ -224,29 +217,24 @@ func stopCluster(composeFile, prefix string, wg *sync.WaitGroup, err error) {
tmp := fmt.Sprintf("%s.%s", tmpCoverageFile, c.ID)

containerInfo, err := testutil.DockerInspect(c.ID)
if err != nil {
fmt.Printf("error while inspecting container. Prefix: %s. Error: %v\n", prefix, err)
}

workDir := containerInfo.Config.WorkingDir

err = testutil.DockerCpFromContainer(c.ID, workDir+"/coverage.out", tmp)
if err != nil {
fmt.Printf("error bringing down cluster. Failed at copying coverage file. Prefix: %s. Error: %v\n",
fmt.Printf("Error while bringing down cluster. Failed at copying coverage file. Prefix: %s. Error: %v\n",
prefix, err)
}

if err = appendTestCoverageFile(tmp, coverageFile); err != nil {
fmt.Printf("error bringing down cluster. Failed at appending coverage file. Prefix: %s. Error: %v\n",
prefix, err,
)
fmt.Printf("Error while bringing down cluster. Failed at appending coverage file. Prefix: %s. Error: %v\n",
prefix, err)
}

_ = os.Remove(tmp)
os.Remove(tmp)

coverageBulk := strings.Replace(composeFile, "docker-compose.yml", "coverage_bulk.out", -1)
if err = appendTestCoverageFile(coverageBulk, coverageFile); err != nil {
fmt.Printf("Error bringing down cluster. Failed at appending coverage file. Prefix: %s. Error: %v\n",
fmt.Printf("Error while bringing down cluster. Failed at appending coverage file. Prefix: %s. Error: %v\n",
prefix, err)
}
}
Expand All @@ -265,7 +253,7 @@ 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"}
var args = []string{"go", "test", "-failfast", "-v", "-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 All @@ -277,9 +265,6 @@ func runTestsFor(ctx context.Context, pkg, prefix string) error {
if *count > 0 {
args = append(args, "-count="+strconv.Itoa(*count))
}
if len(*testType) > 0 {
args = append(args, "-tags="+*testType)
}
if len(*runTest) > 0 {
args = append(args, "-run="+*runTest)
}
Expand All @@ -295,7 +280,7 @@ func runTestsFor(ctx context.Context, pkg, prefix string) error {
cmd.Env = append(cmd.Env, "TEST_DOCKER_PREFIX="+prefix)
abs, err := filepath.Abs(*tmp)
if err != nil {
return fmt.Errorf("while getting absolute path of tmp directory: %v Error: %v", *tmp, err)
return fmt.Errorf("while getting absolute path of tmp directory: %v Error: %v\n", *tmp, err)
}
cmd.Env = append(cmd.Env, "TEST_DATA_DIRECTORY="+abs)
// Use failureCatcher.
Expand All @@ -308,12 +293,12 @@ func runTestsFor(ctx context.Context, pkg, prefix string) error {
time.Sleep(time.Second)
} else {
if err := cmd.Run(); err != nil {
return fmt.Errorf("while running command: %v, error: %v", args, err)
return fmt.Errorf("While running command: %v Error: %v", args, err)
}
}

dur := time.Since(start).Round(time.Second)
tid, _ := ctx.Value(_threadIdKey{}).(int32)
tid, _ := ctx.Value("threadId").(int32)
oc.Took(tid, pkg, dur)
fmt.Printf("Ran tests for package: %s in %s\n", pkg, dur)
if *runCoverage {
Expand All @@ -333,7 +318,7 @@ func hasTestFiles(pkg string) bool {
dir = filepath.Join(*baseDir, dir)

hasTests := false
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if hasTests {
return filepath.SkipDir
}
Expand All @@ -343,12 +328,9 @@ func hasTestFiles(pkg string) bool {
}
return nil
})
x.Check(err)
return hasTests
}

type _threadIdKey struct{}

var _threadId int32

func runTests(taskCh chan task, closer *z.Closer) error {
Expand Down Expand Up @@ -394,7 +376,7 @@ func runTests(taskCh chan task, closer *z.Closer) error {
defer stop()

ctx := closer.Ctx()
ctx = context.WithValue(ctx, _threadIdKey{}, threadId)
ctx = context.WithValue(ctx, "threadId", threadId)

for task := range taskCh {
if ctx.Err() != nil {
Expand All @@ -410,10 +392,7 @@ func runTests(taskCh chan task, closer *z.Closer) error {
// If we only need to run custom cluster tests, then skip this one.
continue
}
if strings.Contains(*testType, "integration") {
start()
}

start()
if err = runTestsFor(ctx, task.pkg.ID, prefix); err != nil {
// fmt.Printf("ERROR for package: %s. Err: %v\n", task.pkg.ID, err)
return err
Expand Down Expand Up @@ -451,10 +430,7 @@ func runCustomClusterTest(ctx context.Context, pkg string, wg *sync.WaitGroup) e
var err error
compose := composeFileFor(pkg)
prefix := getClusterPrefix()
if strings.Contains(*testType, "integration") {
err = startCluster(compose, prefix)
}

err = startCluster(compose, prefix)
if err != nil {
return err
}
Expand Down Expand Up @@ -516,8 +492,8 @@ func (o *outputCatcher) Write(p []byte) (n int, err error) {
o.Lock()
defer o.Unlock()

if bytes.Contains(p, []byte("FAIL")) ||
bytes.Contains(p, []byte("TODO")) {
if bytes.Index(p, []byte("FAIL")) >= 0 ||
bytes.Index(p, []byte("TODO")) >= 0 {
o.failure.Write(p)
}
return os.Stdout.Write(p)
Expand Down Expand Up @@ -850,18 +826,15 @@ func downloadLDBCFiles() {
}
wg.Wait()
fmt.Printf("Downloaded %d files in %s \n", len(ldbcDataFiles), time.Since(start))

}

func createTestCoverageFile(path string) error {
outFile, err := os.Create(path)
if err != nil {
return err
}
defer func() {
if err := outFile.Close(); err != nil {
glog.Warningf("error closing file: %v", err)
}
}()
defer outFile.Close()

cmd := command("echo", coverageFileHeader)
cmd.Stdout = outFile
Expand All @@ -887,11 +860,7 @@ func isTestCoverageEmpty(path string) (bool, error) {
if err != nil {
return true, err
}
defer func() {
if err := file.Close(); err != nil {
glog.Warningf("error closing file: %v", err)
}
}()
defer file.Close()

var l int
scanner := bufio.NewScanner(file)
Expand Down Expand Up @@ -970,7 +939,7 @@ func run() error {
oc.Took(0, "COMPILE", time.Since(start))
}

tmpDir, err := os.MkdirTemp("", "dgraph-test")
tmpDir, err := ioutil.TempDir("", "dgraph-test")
x.Check(err)
defer os.RemoveAll(tmpDir)

Expand Down

0 comments on commit e920ba6

Please sign in to comment.