Skip to content

Commit

Permalink
fix(upgrade): fix failing upgrade tests (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-dgraph authored and mangalaman93 committed Mar 18, 2024
1 parent 1f6f149 commit 5dd52b1
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 32 deletions.
9 changes: 9 additions & 0 deletions dgraphtest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ func NewClusterConfig() ClusterConfig {
}
}

func newClusterConfigFrom(cc ClusterConfig) ClusterConfig {
prefix := fmt.Sprintf("dgraphtest-%d", rand.NewSource(time.Now().UnixNano()).Int63()%1000000)
defaultBackupVol := fmt.Sprintf("%v_backup", prefix)
defaultExportVol := fmt.Sprintf("%v_export", prefix)
cc.prefix = prefix
cc.volumes = map[string]string{DefaultBackupDir: defaultBackupVol, DefaultExportDir: defaultExportVol}
return cc
}

// WithNAlphas sets the number of alphas in the cluster
func (cc ClusterConfig) WithNumAlphas(n int) ClusterConfig {
cc.numAlphas = n
Expand Down
30 changes: 30 additions & 0 deletions dgraphtest/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"github.com/pkg/errors"
"golang.org/x/mod/modfile"
)

func (c *LocalCluster) dgraphImage() string {
Expand Down Expand Up @@ -151,6 +152,10 @@ func getHash(ref string) (string, error) {
func buildDgraphBinary(dir, binaryDir, version string) error {
log.Printf("[INFO] building dgraph binary for version [%v]", version)

if err := fixGoModIfNeeded(); err != nil {
return err
}

cmd := exec.Command("make", "dgraph")
cmd.Dir = filepath.Join(dir, "dgraph")
if out, err := cmd.CombinedOutput(); err != nil {
Expand Down Expand Up @@ -204,3 +209,28 @@ func copy(src, dst string) error {
_, err = io.Copy(destination, source)
return err
}

func fixGoModIfNeeded() error {
repoModFilePath := filepath.Join(repoDir, "go.mod")
repoModFile, err := modfile.Parse(repoModFilePath, nil, nil)
if err != nil {
return errors.Wrapf(err, "error parsing mod file in repoDir [%v]", repoDir)
}

modFile, err := modfile.Parse("go.mod", nil, nil)
if err != nil {
return errors.Wrapf(err, "error while parsing go.mod file")
}

if len(modFile.Replace) == len(repoModFile.Replace) {
return nil
}

repoModFile.Replace = modFile.Replace
if data, err := repoModFile.Format(); err != nil {
return errors.Wrapf(err, "error while formatting mod file")
} else if err := os.WriteFile(repoModFilePath, data, 0644); err != nil {
return errors.Wrapf(err, "error while writing to go.mod file")
}
return nil
}
31 changes: 18 additions & 13 deletions dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ func (c *LocalCluster) init() error {
}
}

c.zeros = c.zeros[:0]
for i := 0; i < c.conf.numZeros; i++ {
zo := &zero{id: i}
zo.containerName = fmt.Sprintf(zeroNameFmt, c.conf.prefix, zo.id)
zo.aliasName = fmt.Sprintf(zeroAliasNameFmt, zo.id)
c.zeros = append(c.zeros, zo)
}

c.alphas = c.alphas[:0]
for i := 0; i < c.conf.numAlphas; i++ {
aa := &alpha{id: i}
aa.containerName = fmt.Sprintf(alphaNameFmt, c.conf.prefix, aa.id)
Expand Down Expand Up @@ -334,26 +336,29 @@ func (c *LocalCluster) Start() error {
return c.HealthCheck(false)
}

var err error
// sometimes health check doesn't work due to unmapped ports. We dont know why this happens,
// but checking it 4 times before failing the test.
for i := 0; i < 4; i++ {
// sometimes health check doesn't work due to unmapped ports. We dont
// know why this happens, but checking it 3 times before failing the test.
retry := 0
for {
retry++

if err = startAll(); err == nil {
if err := startAll(); err == nil {
return nil
} else if retry == 3 {
return err
} else {
log.Printf("[WARNING] saw the err, trying again: %v", err)
}
log.Printf("[WARNING] Saw the error :%v, trying again", err)
if err1 := c.Stop(); err1 != nil {
log.Printf("[WARNING] error while stopping :%v", err)
}
c.Cleanup(false)

log.Printf("[INFO] cleaning up the cluster for retrying!")
c.Cleanup(true)

c.conf = newClusterConfigFrom(c.conf)
if err := c.init(); err != nil {
c.Cleanup(true)
log.Printf("[ERROR] error while init, returning: %v", err)
return err
}
}

return err
}

func (c *LocalCluster) StartZero(id int) error {
Expand Down
2 changes: 1 addition & 1 deletion ee/acl/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (asuite *AclTestSuite) Upgrade() {

func TestACLSuite(t *testing.T) {
for _, uc := range dgraphtest.AllUpgradeCombos(true) {
log.Printf("running upgrade tests for confg: %+v", uc)
log.Printf("running upgrade tests for config: %+v", uc)
aclSuite := AclTestSuite{uc: uc}
suite.Run(t, &aclSuite)
if t.Failed() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
go.opencensus.io v0.24.0
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.19.0
golang.org/x/mod v0.15.0
golang.org/x/net v0.21.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.17.0
Expand Down Expand Up @@ -142,7 +143,6 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/api v0.122.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
2 changes: 1 addition & 1 deletion query/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func TestMain(m *testing.M) {

dc = c
client = dg.Dgraph
populateCluster()
populateCluster(dc)
m.Run()
}
5 changes: 3 additions & 2 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgo/v230/protos/api"
"github.com/dgraph-io/dgraph/dgraphtest"
"github.com/dgraph-io/dgraph/x"
)

Expand Down Expand Up @@ -74,7 +75,7 @@ func processQuery(ctx context.Context, t *testing.T, query string) (string, erro
return string(jsonResponse), err
}

func processQueryRDF(ctx context.Context, t *testing.T, query string) (string, error) {
func processQueryRDF(ctx context.Context, query string) (string, error) {
txn := client.NewTxn()
defer func() { _ = txn.Discard(ctx) }()

Expand Down Expand Up @@ -346,7 +347,7 @@ name2 : string @index(term) .
age2 : int @index(int) .
`

func populateCluster() {
func populateCluster(dc dgraphtest.Cluster) {
x.Panic(client.Alter(context.Background(), &api.Operation{DropAll: true}))

// In the query package, we test using hard coded UIDs so that we know what results
Expand Down
2 changes: 1 addition & 1 deletion query/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func TestMain(m *testing.M) {
x.Panic(client.LoginIntoNamespace(context.Background(), dgraphtest.DefaultUser,
dgraphtest.DefaultPassword, x.GalaxyNamespace))

populateCluster()
populateCluster(dc)
m.Run()
}
22 changes: 11 additions & 11 deletions query/rdf_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRDFResult(t *testing.T) {
}
}`

rdf, err := processQueryRDF(context.Background(), t, query)
rdf, err := processQueryRDF(context.Background(), query)
require.NoError(t, err)
require.Equal(t, rdf, `<0x1> <name> "Michonne" .
<0x1> <friend> <0x17> .
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestRDFNormalize(t *testing.T) {
}
}
}`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Error(t, err, "normalize directive is not supported in the rdf output format")
}

Expand All @@ -80,7 +80,7 @@ func TestRDFGroupBy(t *testing.T) {
count(uid)
}
}`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Contains(t, err.Error(), "groupby is not supported in rdf output format")
}

Expand All @@ -91,7 +91,7 @@ func TestRDFUidCount(t *testing.T) {
count(uid)
}
}`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Contains(t, err.Error(), "uid count is not supported in the rdf output format")
}

Expand All @@ -108,7 +108,7 @@ func TestRDFIngoreReflex(t *testing.T) {
}
}
}`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Contains(t, err.Error(),
"ignorereflex directive is not supported in the rdf output format")
}
Expand All @@ -121,7 +121,7 @@ func TestRDFRecurse(t *testing.T) {
friend
}
}`
rdf, err := processQueryRDF(context.Background(), t, query)
rdf, err := processQueryRDF(context.Background(), query)
require.NoError(t, err)
require.Equal(t, rdf, `<0x1> <name> "Michonne" .
<0x17> <name> "Rick Grimes" .
Expand All @@ -137,7 +137,7 @@ func TestRDFIgnoreUid(t *testing.T) {
name
}
}`
rdf, err := processQueryRDF(context.Background(), t, query)
rdf, err := processQueryRDF(context.Background(), query)
require.NoError(t, err)
require.Equal(t, rdf, `<0x1> <name> "Michonne" .
<0x17> <name> "Rick Grimes" .
Expand All @@ -154,7 +154,7 @@ func TestRDFCheckPwd(t *testing.T) {
}
}
`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Contains(t, err.Error(),
"chkpwd function is not supported in the rdf output format")
}
Expand All @@ -172,7 +172,7 @@ func TestRDFPredicateCount(t *testing.T) {
}
`

rdf, err := processQueryRDF(context.Background(), t, query)
rdf, err := processQueryRDF(context.Background(), query)
require.NoError(t, err)
require.Equal(t, `<0x1> <name> "Michonne" .
<0x17> <name> "Rick Grimes" .
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestRDFFacets(t *testing.T) {
path @facets(weight)
}
}`
_, err := processQueryRDF(context.Background(), t, query)
_, err := processQueryRDF(context.Background(), query)
require.Contains(t, err.Error(),
"facets are not supported in the rdf output format")
}
Expand All @@ -219,7 +219,7 @@ func TestDateRDF(t *testing.T) {
}
}
`
rdf, err := processQueryRDF(context.Background(), t, query)
rdf, err := processQueryRDF(context.Background(), query)
require.NoError(t, err)
expected := `<0x1> <name> "Michonne" .
<0x1> <gender> "female" .
Expand Down
2 changes: 1 addition & 1 deletion query/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestMain(m *testing.M) {

client = dg
dc = c
populateCluster()
populateCluster(dc)
}

query := func(c dgraphtest.Cluster) int {
Expand Down
2 changes: 1 addition & 1 deletion systest/multi-tenancy/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (msuite *MultitenancyTestSuite) Upgrade() {

func TestMultitenancySuite(t *testing.T) {
for _, uc := range dgraphtest.AllUpgradeCombos(false) {
log.Printf("running upgrade tests for confg: %+v", uc)
log.Printf("running upgrade tests for config: %+v", uc)
var msuite MultitenancyTestSuite
msuite.uc = uc
suite.Run(t, &msuite)
Expand Down

0 comments on commit 5dd52b1

Please sign in to comment.