github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/systest/loader/loader_test.go (about) 1 /* 2 * Copyright 2018 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "os" 24 "os/exec" 25 "path/filepath" 26 "strings" 27 "testing" 28 29 "github.com/stretchr/testify/require" 30 31 "github.com/dgraph-io/dgraph/testutil" 32 ) 33 34 func TestLoaderXidmap(t *testing.T) { 35 tmpDir, err := ioutil.TempDir("", "loader_test") 36 require.NoError(t, err) 37 defer os.RemoveAll(tmpDir) 38 39 data := os.ExpandEnv("$GOPATH/src/github.com/dgraph-io/dgraph/systest/data/first.rdf.gz") 40 liveCmd := exec.Command(os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 41 "--files", data, 42 "--alpha", testutil.SockAddr, 43 "--zero", testutil.SockAddrZero, 44 "-x", "x", 45 ) 46 liveCmd.Dir = tmpDir 47 require.NoError(t, liveCmd.Run()) 48 49 // Load another file, live should reuse the xidmap. 50 data = os.ExpandEnv("$GOPATH/src/github.com/dgraph-io/dgraph/systest/data/second.rdf.gz") 51 liveCmd = exec.Command(os.ExpandEnv("$GOPATH/bin/dgraph"), "live", 52 "--files", data, 53 "--alpha", testutil.SockAddr, 54 "--zero", testutil.SockAddrZero, 55 "-x", "x", 56 ) 57 liveCmd.Dir = tmpDir 58 liveCmd.Stdout = os.Stdout 59 liveCmd.Stderr = os.Stdout 60 require.NoError(t, liveCmd.Run()) 61 62 resp, err := http.Get(fmt.Sprintf("http://%s/admin/export", testutil.SockAddrHttp)) 63 require.NoError(t, err) 64 65 b, _ := ioutil.ReadAll(resp.Body) 66 expected := `{"code": "Success", "message": "Export completed."}` 67 require.Equal(t, expected, string(b)) 68 69 require.NoError(t, copyExportFiles(tmpDir)) 70 71 dataFile, err := findFile(filepath.Join(tmpDir, "export"), ".rdf.gz") 72 require.NoError(t, err) 73 74 cmd := fmt.Sprintf("gunzip -c %s | sort", dataFile) 75 out, err := exec.Command("sh", "-c", cmd).Output() 76 require.NoError(t, err) 77 78 expected = `<0x1> <age> "13" . 79 <0x1> <friend> <0x2711> . 80 <0x1> <location> "Wonderland" . 81 <0x1> <name> "Alice" . 82 <0x2711> <name> "Bob" . 83 ` 84 require.Equal(t, expected, string(out)) 85 } 86 87 func copyExportFiles(tmpDir string) error { 88 exportPath := filepath.Join(tmpDir, "export") 89 if err := os.MkdirAll(exportPath, 0755); err != nil { 90 return err 91 } 92 93 srcPath := "alpha1:/data/alpha1/export" 94 dstPath := filepath.Join(tmpDir, "export") 95 return testutil.DockerCp(srcPath, dstPath) 96 } 97 98 func findFile(dir string, ext string) (string, error) { 99 var fp string 100 err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 101 if err != nil { 102 return err 103 } 104 if strings.HasSuffix(path, ext) { 105 fp = path 106 return nil 107 } 108 return nil 109 }) 110 return fp, err 111 }