Skip to content

Commit

Permalink
chore: Modify test to remove flakiness caused by explicit uids (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
all-seeing-code authored Jul 30, 2020
1 parent de88851 commit ecca695
Showing 1 changed file with 23 additions and 50 deletions.
73 changes: 23 additions & 50 deletions systest/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@
package main

import (
"fmt"
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/dgraph-io/dgraph/testutil"
"github.com/dgraph-io/dgraph/x"
)

// TestLoaderXidmap checks that live loader re-uses xidmap on loading data from two different files
func TestLoaderXidmap(t *testing.T) {
dg, err := testutil.DgraphClient(testutil.SockAddr)
require.NoError(t, err)
ctx := context.Background()
testutil.DropAll(t, dg)
tmpDir, err := ioutil.TempDir("", "loader_test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
Expand Down Expand Up @@ -61,53 +66,21 @@ func TestLoaderXidmap(t *testing.T) {
liveCmd.Stderr = os.Stdout
require.NoError(t, liveCmd.Run())

resp, err := http.Get(fmt.Sprintf("http://%s/admin/export", testutil.SockAddrHttp))
require.NoError(t, err)

b, _ := ioutil.ReadAll(resp.Body)
expected := `{"code": "Success", "message": "Export completed."}`
require.Equal(t, expected, string(b))

require.NoError(t, copyExportFiles(tmpDir))

dataFile, err := findFile(filepath.Join(tmpDir, "export"), ".rdf.gz")
require.NoError(t, err)

cmd := fmt.Sprintf("gunzip -c %s | sort", dataFile)
out, err := exec.Command("sh", "-c", cmd).Output()
op := api.Operation{Schema: "name: string @index(exact) ."}
x.Check(dg.Alter(ctx, &op))
query := `
{
q(func: eq(name, "Alice")) {
age
location
friend{
name
}
}
}`
expected := `{"q":[{"age":"13","location":"Wonderland","friend":[{"name":"Bob"}]}]}`
resp, err := dg.NewReadOnlyTxn().Query(ctx, query)
require.NoError(t, err)
testutil.CompareJSON(t, expected, string(resp.GetJson()))

expected = `<0x1> <age> "13" .
<0x1> <friend> <0x2711> .
<0x1> <location> "Wonderland" .
<0x1> <name> "Alice" .
<0x2711> <name> "Bob" .
`
require.Equal(t, expected, string(out))
}

func copyExportFiles(tmpDir string) error {
exportPath := filepath.Join(tmpDir, "export")
if err := os.MkdirAll(exportPath, 0755); err != nil {
return err
}

srcPath := "alpha1:/data/alpha1/export"
dstPath := filepath.Join(tmpDir, "export")
return testutil.DockerCp(srcPath, dstPath)
}

func findFile(dir string, ext string) (string, error) {
var fp string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ext) {
fp = path
return nil
}
return nil
})
return fp, err
}

0 comments on commit ecca695

Please sign in to comment.