From 316ee601267aaac8ee5d482960185f00296e17a2 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Mon, 10 Feb 2020 18:32:30 +0530 Subject: [PATCH 1/8] Remove HTTP admin endpoints. Also modify tests in worker package to call the new export API. --- dgraph/cmd/alpha/admin.go | 114 ------------------------------- dgraph/cmd/alpha/admin_backup.go | 64 ----------------- dgraph/cmd/alpha/run.go | 5 -- graphql/admin/export.go | 3 + worker/export_test.go | 68 +++++++++++++++--- 5 files changed, 63 insertions(+), 191 deletions(-) delete mode 100644 dgraph/cmd/alpha/admin_backup.go diff --git a/dgraph/cmd/alpha/admin.go b/dgraph/cmd/alpha/admin.go index e608ff473a3..271d12d4769 100644 --- a/dgraph/cmd/alpha/admin.go +++ b/dgraph/cmd/alpha/admin.go @@ -18,17 +18,11 @@ package alpha import ( "bytes" - "context" "fmt" - "io/ioutil" "net" "net/http" - "strconv" - "github.com/dgraph-io/dgraph/posting" - "github.com/dgraph-io/dgraph/worker" "github.com/dgraph-io/dgraph/x" - "github.com/golang/glog" ) // handlerInit does some standard checks. Returns false if something is wrong. @@ -46,114 +40,6 @@ func handlerInit(w http.ResponseWriter, r *http.Request, allowedMethods map[stri return true } -func drainingHandler(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case http.MethodPut, http.MethodPost: - enableStr := r.URL.Query().Get("enable") - - enable, err := strconv.ParseBool(enableStr) - if err != nil { - x.SetStatus(w, x.ErrorInvalidRequest, - "Found invalid value for the enable parameter") - return - } - - x.UpdateDrainingMode(enable) - _, err = w.Write([]byte(fmt.Sprintf(`{"code": "Success",`+ - `"message": "draining mode has been set to %v"}`, enable))) - if err != nil { - glog.Errorf("Failed to write response: %v", err) - } - default: - w.WriteHeader(http.StatusMethodNotAllowed) - } -} - -func shutDownHandler(w http.ResponseWriter, r *http.Request) { - if !handlerInit(w, r, map[string]bool{ - http.MethodGet: true, - }) { - return - } - - close(worker.ShutdownCh) - w.Header().Set("Content-Type", "application/json") - x.Check2(w.Write([]byte(`{"code": "Success", "message": "Server is shutting down"}`))) -} - -func exportHandler(w http.ResponseWriter, r *http.Request) { - if !handlerInit(w, r, map[string]bool{ - http.MethodGet: true, - }) { - return - } - if err := r.ParseForm(); err != nil { - x.SetHttpStatus(w, http.StatusBadRequest, "Parse of export request failed.") - return - } - - format := worker.DefaultExportFormat - if vals, ok := r.Form["format"]; ok { - if len(vals) > 1 { - x.SetHttpStatus(w, http.StatusBadRequest, - "Only one export format may be specified.") - return - } - format = worker.NormalizeExportFormat(vals[0]) - if format == "" { - x.SetHttpStatus(w, http.StatusBadRequest, "Invalid export format.") - return - } - } - if err := worker.ExportOverNetwork(context.Background(), format); err != nil { - x.SetStatus(w, err.Error(), "Export failed.") - return - } - w.Header().Set("Content-Type", "application/json") - x.Check2(w.Write([]byte(`{"code": "Success", "message": "Export completed."}`))) -} - -func memoryLimitHandler(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case http.MethodGet: - memoryLimitGetHandler(w, r) - case http.MethodPut: - memoryLimitPutHandler(w, r) - default: - w.WriteHeader(http.StatusMethodNotAllowed) - } -} - -func memoryLimitPutHandler(w http.ResponseWriter, r *http.Request) { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - memoryMB, err := strconv.ParseFloat(string(body), 64) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - if err := worker.UpdateLruMb(memoryMB); err != nil { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprint(w, err.Error()) - return - } - w.WriteHeader(http.StatusOK) -} - -func memoryLimitGetHandler(w http.ResponseWriter, r *http.Request) { - posting.Config.Lock() - memoryMB := posting.Config.AllottedMemory - posting.Config.Unlock() - - if _, err := fmt.Fprintln(w, memoryMB); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } -} - func ipInIPWhitelistRanges(ipString string) bool { ip := net.ParseIP(ipString) diff --git a/dgraph/cmd/alpha/admin_backup.go b/dgraph/cmd/alpha/admin_backup.go deleted file mode 100644 index 8919e0940da..00000000000 --- a/dgraph/cmd/alpha/admin_backup.go +++ /dev/null @@ -1,64 +0,0 @@ -// +build !oss - -/* - * Copyright 2018 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package alpha - -import ( - "context" - "net/http" - - "github.com/dgraph-io/dgraph/protos/pb" - "github.com/dgraph-io/dgraph/worker" - "github.com/dgraph-io/dgraph/x" -) - -func init() { - http.HandleFunc("/admin/backup", backupHandler) -} - -// backupHandler handles backup requests coming from the HTTP endpoint. -func backupHandler(w http.ResponseWriter, r *http.Request) { - if !handlerInit(w, r, map[string]bool{ - http.MethodPost: true, - }) { - return - } - - destination := r.FormValue("destination") - accessKey := r.FormValue("access_key") - secretKey := r.FormValue("secret_key") - sessionToken := r.FormValue("session_token") - anonymous := r.FormValue("anonymous") == "true" - forceFull := r.FormValue("force_full") == "true" - - req := pb.BackupRequest{ - Destination: destination, - AccessKey: accessKey, - SecretKey: secretKey, - SessionToken: sessionToken, - Anonymous: anonymous, - } - - if err := worker.ProcessBackupRequest(context.Background(), &req, forceFull); err != nil { - x.SetStatus(w, err.Error(), "Backup failed.") - return - } - - w.Header().Set("Content-Type", "application/json") - x.Check2(w.Write([]byte(`{"code": "Success", "message": "Backup completed."}`))) -} diff --git a/dgraph/cmd/alpha/run.go b/dgraph/cmd/alpha/run.go index c285d525540..40f763057e3 100644 --- a/dgraph/cmd/alpha/run.go +++ b/dgraph/cmd/alpha/run.go @@ -447,11 +447,6 @@ func setupServer(closer *y.Closer) { // TODO: Figure out what this is for? http.HandleFunc("/debug/store", storeStatsHandler) - http.HandleFunc("/admin/shutdown", shutDownHandler) - http.HandleFunc("/admin/draining", drainingHandler) - http.HandleFunc("/admin/export", exportHandler) - http.HandleFunc("/admin/config/lru_mb", memoryLimitHandler) - introspection := Alpha.Conf.GetBool("graphql_introspection") mainServer, adminServer := admin.NewServers(introspection, closer) http.Handle("/graphql", mainServer.HTTPHandler()) diff --git a/graphql/admin/export.go b/graphql/admin/export.go index 57d725d89ee..91742d50b43 100644 --- a/graphql/admin/export.go +++ b/graphql/admin/export.go @@ -19,6 +19,7 @@ package admin import ( "context" "encoding/json" + "fmt" dgoapi "github.com/dgraph-io/dgo/v2/protos/api" "github.com/dgraph-io/dgraph/gql" @@ -47,8 +48,10 @@ func (er *exportResolver) Rewrite( } format := worker.DefaultExportFormat + fmt.Printf("input: %+v\n", input) if input.Format != "" { format = worker.NormalizeExportFormat(input.Format) + fmt.Println("format: ", format) if format == "" { return nil, nil, errors.Errorf("invalid export format: %v", input.Format) } diff --git a/worker/export_test.go b/worker/export_test.go index 47f061e68e9..9316f488651 100644 --- a/worker/export_test.go +++ b/worker/export_test.go @@ -21,6 +21,7 @@ import ( "bytes" "compress/gzip" "context" + "encoding/json" "io/ioutil" "math" "net/http" @@ -321,26 +322,77 @@ func TestExportJson(t *testing.T) { checkExportSchema(t, schemaFileList) } +const exportRequest = `mutation export($format: String!) { + export(input: {format: $format}) { + response { + code + } + } +}` + +type graphQLParams struct { + Query string `json:"query"` + Variables map[string]interface{} `json:"variables"` +} + +type graphQLError struct { + Message string +} + +type graphQLResponse struct { + Errors []graphQLError +} + +func requireNoErrors(t *testing.T, resp *http.Response) { + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + var result *graphQLResponse + err = json.Unmarshal(b, &result) + require.NoError(t, err) + require.Nil(t, result.Errors) +} + func TestExportFormat(t *testing.T) { tmpdir, err := ioutil.TempDir("", "export") require.NoError(t, err) defer os.RemoveAll(tmpdir) - resp, err := http.Get("http://" + testutil.SockAddrHttp + "/admin/export?format=json") + adminUrl := "http://" + testutil.SockAddrHttp + "/admin" + params := graphQLParams{ + Query: exportRequest, + Variables: map[string]interface{}{"format": "json"}, + } + b, err := json.Marshal(params) + require.NoError(t, err) + + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - require.Equal(t, resp.StatusCode, http.StatusOK) + requireNoErrors(t, resp) - resp, err = http.Get("http://" + testutil.SockAddrHttp + "/admin/export?format=rdf") + params.Variables["format"] = "rdf" + b, err = json.Marshal(params) + require.NoError(t, err) + + resp, err = http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) + require.NoError(t, err) + requireNoErrors(t, resp) + + params.Variables["format"] = "xml" + b, err = json.Marshal(params) + require.NoError(t, err) + resp, err = http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - require.Equal(t, resp.StatusCode, http.StatusOK) - resp, err = http.Get("http://" + testutil.SockAddrHttp + "/admin/export?format=xml") + defer resp.Body.Close() + b, err = ioutil.ReadAll(resp.Body) require.NoError(t, err) - require.NotEqual(t, resp.StatusCode, http.StatusOK) - resp, err = http.Get("http://" + testutil.SockAddrHttp + "/admin/export?output=rdf") + var result *graphQLResponse + err = json.Unmarshal(b, &result) require.NoError(t, err) - require.Equal(t, resp.StatusCode, http.StatusOK) + require.NotNil(t, result.Errors) } type skv struct { From 7129a8333acb11d6f85c3301b027041aca75e6ab Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Mon, 10 Feb 2020 19:07:20 +0530 Subject: [PATCH 2/8] Start fixing some tests. --- dgraph/cmd/bulk/systest/test-bulk-schema.sh | 2 +- systest/cluster_setup_test.go | 16 +++++- testutil/graphql.go | 58 +++++++++++++++++++++ worker/export_test.go | 32 ++---------- 4 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 testutil/graphql.go diff --git a/dgraph/cmd/bulk/systest/test-bulk-schema.sh b/dgraph/cmd/bulk/systest/test-bulk-schema.sh index 52ee675f7da..b3424b59605 100755 --- a/dgraph/cmd/bulk/systest/test-bulk-schema.sh +++ b/dgraph/cmd/bulk/systest/test-bulk-schema.sh @@ -118,7 +118,7 @@ function QuerySchema function DoExport { INFO "running export" - docker exec alpha1 curl -Ss localhost:$HTTP_PORT/admin/export &>/dev/null + docker exec alpha1 curl -Ss -H "Content-Type: application/json" localhost:$HTTP_PORT/admin -XPOST -d '{ "query": "mutation { export(input: {format: \"json\"}) { response { code message } }}" }' &>/dev/null sleep 2 docker cp alpha1:/data/alpha1/export . sleep 1 diff --git a/systest/cluster_setup_test.go b/systest/cluster_setup_test.go index 3a58f3348dd..5e65b9bf8e9 100644 --- a/systest/cluster_setup_test.go +++ b/systest/cluster_setup_test.go @@ -17,6 +17,8 @@ package main import ( + "bytes" + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -29,6 +31,7 @@ import ( "github.com/dgraph-io/dgo/v2" "github.com/dgraph-io/dgo/v2/protos/api" + "github.com/dgraph-io/dgraph/testutil" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" "github.com/pkg/errors" @@ -167,11 +170,20 @@ type matchExport struct { func matchExportCount(opts matchExport) error { // Now try and export data from second server. - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/admin/export", opts.port)) + adminUrl := "http://" + opts.port + "/admin" + params := testutil.GraphQLParams{ + Query: testutil.ExportRequest, + } + b, err := json.Marshal(params) if err != nil { return err } - b, err := ioutil.ReadAll(resp.Body) + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) + if err != nil { + return err + } + + b, err = ioutil.ReadAll(resp.Body) if err != nil { return err } diff --git a/testutil/graphql.go b/testutil/graphql.go new file mode 100644 index 00000000000..f5f3369f77e --- /dev/null +++ b/testutil/graphql.go @@ -0,0 +1,58 @@ +/* + * Copyright 2020 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package testutil + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +const ExportRequest = `mutation export { + export(input: {format: "json"}) { + response { + code + } + } +}` + +type GraphQLParams struct { + Query string `json:"query"` + Variables map[string]interface{} `json:"variables"` +} + +type GraphQLError struct { + Message string +} + +type GraphQLResponse struct { + Errors []graphQLError +} + +func RequireNoGraphQLErrors(t *testing.T, resp *http.Response) { + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + var result *GraphQLResponse + err = json.Unmarshal(b, &result) + require.NoError(t, err) + require.Nil(t, result.Errors) +} diff --git a/worker/export_test.go b/worker/export_test.go index 9316f488651..808ec6574ff 100644 --- a/worker/export_test.go +++ b/worker/export_test.go @@ -330,37 +330,13 @@ const exportRequest = `mutation export($format: String!) { } }` -type graphQLParams struct { - Query string `json:"query"` - Variables map[string]interface{} `json:"variables"` -} - -type graphQLError struct { - Message string -} - -type graphQLResponse struct { - Errors []graphQLError -} - -func requireNoErrors(t *testing.T, resp *http.Response) { - defer resp.Body.Close() - b, err := ioutil.ReadAll(resp.Body) - require.NoError(t, err) - - var result *graphQLResponse - err = json.Unmarshal(b, &result) - require.NoError(t, err) - require.Nil(t, result.Errors) -} - func TestExportFormat(t *testing.T) { tmpdir, err := ioutil.TempDir("", "export") require.NoError(t, err) defer os.RemoveAll(tmpdir) adminUrl := "http://" + testutil.SockAddrHttp + "/admin" - params := graphQLParams{ + params := testutil.GraphQLParams{ Query: exportRequest, Variables: map[string]interface{}{"format": "json"}, } @@ -369,7 +345,7 @@ func TestExportFormat(t *testing.T) { resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - requireNoErrors(t, resp) + testutil.RequireNoGraphQLErrors(t, resp) params.Variables["format"] = "rdf" b, err = json.Marshal(params) @@ -377,7 +353,7 @@ func TestExportFormat(t *testing.T) { resp, err = http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - requireNoErrors(t, resp) + testutil.RequireNoGraphQLErrors(t, resp) params.Variables["format"] = "xml" b, err = json.Marshal(params) @@ -389,7 +365,7 @@ func TestExportFormat(t *testing.T) { b, err = ioutil.ReadAll(resp.Body) require.NoError(t, err) - var result *graphQLResponse + var result *testutil.GraphQLResponse err = json.Unmarshal(b, &result) require.NoError(t, err) require.NotNil(t, result.Errors) From 9f03676c9dd21bd1c6cc9aeaa1a4329df6651987 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Mon, 10 Feb 2020 19:30:55 +0530 Subject: [PATCH 3/8] Fix backup tests to use the GraphQL endpoint. --- ee/backup/tests/filesystem/backup_test.go | 29 ++++++++++++++------ ee/backup/tests/minio-large/backup_test.go | 26 +++++++++++++++--- ee/backup/tests/minio/backup_test.go | 31 +++++++++++++++------- testutil/graphql.go | 4 +-- 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/ee/backup/tests/filesystem/backup_test.go b/ee/backup/tests/filesystem/backup_test.go index be3b37077a8..478a789ff27 100644 --- a/ee/backup/tests/filesystem/backup_test.go +++ b/ee/backup/tests/filesystem/backup_test.go @@ -16,12 +16,13 @@ package main import ( + "bytes" "context" + "encoding/json" "fmt" "io/ioutil" "math" "net/http" - "net/url" "os" "path/filepath" "strings" @@ -210,15 +211,27 @@ func runBackup(t *testing.T, numExpectedFiles, numExpectedDirs int) []string { func runBackupInternal(t *testing.T, forceFull bool, numExpectedFiles, numExpectedDirs int) []string { - forceFullStr := "false" - if forceFull { - forceFullStr = "true" + backupRequest := `mutation backup($dst: String!, $ff: Boolean!) { + backup(input: {destination: $dst, forceFull: $ff}) { + response { + code + message + } + } + }` + + adminUrl := "http://localhost:8180/admin" + params := testutil.GraphQLParams{ + Query: backupRequest, + Variables: map[string]interface{}{ + "dst": alphaBackupDir, + "ff": forceFull, + }, } + b, err := json.Marshal(params) + require.NoError(t, err) - resp, err := http.PostForm("http://localhost:8180/admin/backup", url.Values{ - "destination": []string{alphaBackupDir}, - "force_full": []string{forceFullStr}, - }) + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) defer resp.Body.Close() buf, err := ioutil.ReadAll(resp.Body) diff --git a/ee/backup/tests/minio-large/backup_test.go b/ee/backup/tests/minio-large/backup_test.go index 32e540039d5..0403c21a936 100644 --- a/ee/backup/tests/minio-large/backup_test.go +++ b/ee/backup/tests/minio-large/backup_test.go @@ -16,12 +16,13 @@ package main import ( + "bytes" "context" + "encoding/json" "fmt" "io/ioutil" "math" "net/http" - "net/url" "os" "strings" "testing" @@ -139,9 +140,26 @@ func addTriples(t *testing.T, dg *dgo.Dgraph, numTriples int) { } func runBackup(t *testing.T) { - resp, err := http.PostForm("http://localhost:8180/admin/backup", url.Values{ - "destination": []string{backupDestination}, - }) + backupRequest := `mutation backup($dst: String!) { + backup(input: {destination: $dst}) { + response { + code + message + } + } + }` + + adminUrl := "http://localhost:8180/admin" + params := testutil.GraphQLParams{ + Query: backupRequest, + Variables: map[string]interface{}{ + "dst": backupDestination, + }, + } + b, err := json.Marshal(params) + require.NoError(t, err) + + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) defer resp.Body.Close() buf, err := ioutil.ReadAll(resp.Body) diff --git a/ee/backup/tests/minio/backup_test.go b/ee/backup/tests/minio/backup_test.go index b8872299c09..7274ce292a0 100644 --- a/ee/backup/tests/minio/backup_test.go +++ b/ee/backup/tests/minio/backup_test.go @@ -16,12 +16,13 @@ package main import ( + "bytes" "context" + "encoding/json" "fmt" "io/ioutil" "math" "net/http" - "net/url" "os" "path/filepath" "strings" @@ -216,17 +217,29 @@ func runBackup(t *testing.T, numExpectedFiles, numExpectedDirs int) []string { func runBackupInternal(t *testing.T, forceFull bool, numExpectedFiles, numExpectedDirs int) []string { - forceFullStr := "false" - if forceFull { - forceFullStr = "true" + + backupRequest := `mutation backup($dst: String!, $ff: Boolean!) { + backup(input: {destination: $dst, forceFull: $ff}) { + response { + code + message + } + } + }` + + adminUrl := "http://localhost:8180/admin" + params := testutil.GraphQLParams{ + Query: backupRequest, + Variables: map[string]interface{}{ + "dst": backupDst, + "ff": forceFull, + }, } + b, err := json.Marshal(params) + require.NoError(t, err) - resp, err := http.PostForm("http://localhost:8180/admin/backup", url.Values{ - "destination": []string{backupDst}, - "force_full": []string{forceFullStr}, - }) + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - defer resp.Body.Close() buf, err := ioutil.ReadAll(resp.Body) require.NoError(t, err) require.Contains(t, string(buf), "Backup completed.") diff --git a/testutil/graphql.go b/testutil/graphql.go index f5f3369f77e..a5ce54e31c4 100644 --- a/testutil/graphql.go +++ b/testutil/graphql.go @@ -25,7 +25,7 @@ import ( "github.com/stretchr/testify/require" ) -const ExportRequest = `mutation export { +const ExportRequest = `mutation { export(input: {format: "json"}) { response { code @@ -43,7 +43,7 @@ type GraphQLError struct { } type GraphQLResponse struct { - Errors []graphQLError + Errors []GraphQLError } func RequireNoGraphQLErrors(t *testing.T, resp *http.Response) { From 80632e1da6d23fdbcd34407a33b02294a5691650 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Fri, 14 Feb 2020 17:28:44 +0530 Subject: [PATCH 4/8] Remove fmt.Print in export file. --- graphql/admin/export.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/graphql/admin/export.go b/graphql/admin/export.go index 91742d50b43..57d725d89ee 100644 --- a/graphql/admin/export.go +++ b/graphql/admin/export.go @@ -19,7 +19,6 @@ package admin import ( "context" "encoding/json" - "fmt" dgoapi "github.com/dgraph-io/dgo/v2/protos/api" "github.com/dgraph-io/dgraph/gql" @@ -48,10 +47,8 @@ func (er *exportResolver) Rewrite( } format := worker.DefaultExportFormat - fmt.Printf("input: %+v\n", input) if input.Format != "" { format = worker.NormalizeExportFormat(input.Format) - fmt.Println("format: ", format) if format == "" { return nil, nil, errors.Errorf("invalid export format: %v", input.Format) } From 521352486a390e200cb5d3f2deed0e47486db392 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Fri, 14 Feb 2020 17:40:41 +0530 Subject: [PATCH 5/8] Update setDraining mode test to use the new GraphQL API --- dgraph/cmd/alpha/http_test.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/dgraph/cmd/alpha/http_test.go b/dgraph/cmd/alpha/http_test.go index ff74c842e88..1befd09d203 100644 --- a/dgraph/cmd/alpha/http_test.go +++ b/dgraph/cmd/alpha/http_test.go @@ -736,14 +736,29 @@ func TestHealth(t *testing.T) { } func setDrainingMode(t *testing.T, enable bool) { - url := fmt.Sprintf("%s/admin/draining?enable=%v", addr, enable) - req, err := http.NewRequest("POST", url, nil) - require.NoError(t, err, "Error while creating post request for %s", url) - client := &http.Client{} - resp, err := client.Do(req) - require.NoError(t, err, "Error while sending post request to %s", url) - status := resp.StatusCode - require.Equal(t, http.StatusOK, status, "Unexpected status code: %v", status) + drainingRequest := `mutation drain($enable: Boolean) { + draining(input: {enable: $enable}) { + response { + code + } + } + }` + adminUrl := fmt.Sprintf("%s/admin", addr) + params := testutil.GraphQLParams{ + Query: drainingRequest, + Variables: map[string]interface{}{"enable": enable}, + } + b, err := json.Marshal(params) + require.NoError(t, err) + + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) + require.NoError(t, err) + + defer resp.Body.Close() + b, err = ioutil.ReadAll(resp.Body) + require.NoError(t, err) + require.JSONEq(t, `{"data":{"draining":{"response":{"code":"Success"}}}}`, + string(b)) } func TestDrainingMode(t *testing.T) { From 6d6c32c67d8295f32d025f616cf6a176b12bedd3 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Fri, 14 Feb 2020 18:18:00 +0530 Subject: [PATCH 6/8] Fix TestLoaderXidmap --- systest/loader/loader_test.go | 38 +++++++++++++++++++++++++++++++---- testutil/graphql.go | 1 + 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/systest/loader/loader_test.go b/systest/loader/loader_test.go index 75d9adc35c2..5a57f999385 100644 --- a/systest/loader/loader_test.go +++ b/systest/loader/loader_test.go @@ -17,6 +17,8 @@ package main import ( + "bytes" + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -61,12 +63,40 @@ 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)) + exportRequest := `mutation { + export(input: {format: "rdf"}) { + response { + code + message + } + } + }` + + adminUrl := "http://" + testutil.SockAddrHttp + "/admin" + params := testutil.GraphQLParams{ + Query: exportRequest, + Variables: map[string]interface{}{"format": "json"}, + } + b, err := json.Marshal(params) require.NoError(t, err) - b, _ := ioutil.ReadAll(resp.Body) - expected := `{"code": "Success", "message": "Export completed."}` - require.Equal(t, expected, string(b)) + resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) + require.NoError(t, err) + defer resp.Body.Close() + + b, err = ioutil.ReadAll(resp.Body) + require.NoError(t, err) + expected := `{ + "data": { + "export": { + "response": { + "code": "Success", + "message": "Export completed." + } + } + } + }` + require.JSONEq(t, expected, string(b)) require.NoError(t, copyExportFiles(tmpDir)) diff --git a/testutil/graphql.go b/testutil/graphql.go index a5ce54e31c4..55f2db695c1 100644 --- a/testutil/graphql.go +++ b/testutil/graphql.go @@ -29,6 +29,7 @@ const ExportRequest = `mutation { export(input: {format: "json"}) { response { code + message } } }` From d36452593b0490e565c4a8bc4712c7db2c0ad2b1 Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Fri, 14 Feb 2020 18:21:53 +0530 Subject: [PATCH 7/8] Fix test bulk schema test. --- dgraph/cmd/bulk/systest/test-bulk-schema.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dgraph/cmd/bulk/systest/test-bulk-schema.sh b/dgraph/cmd/bulk/systest/test-bulk-schema.sh index b3424b59605..12868deb5eb 100755 --- a/dgraph/cmd/bulk/systest/test-bulk-schema.sh +++ b/dgraph/cmd/bulk/systest/test-bulk-schema.sh @@ -118,7 +118,7 @@ function QuerySchema function DoExport { INFO "running export" - docker exec alpha1 curl -Ss -H "Content-Type: application/json" localhost:$HTTP_PORT/admin -XPOST -d '{ "query": "mutation { export(input: {format: \"json\"}) { response { code message } }}" }' &>/dev/null + docker exec alpha1 curl -Ss -H "Content-Type: application/json" localhost:$HTTP_PORT/admin -XPOST -d '{ "query": "mutation { export(input: {format: \"rdf\"}) { response { code message } }}" }' &>/dev/null sleep 2 docker cp alpha1:/data/alpha1/export . sleep 1 From 19f025c54a0b64501a29df368fe567736de7f00d Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Fri, 14 Feb 2020 19:00:29 +0530 Subject: [PATCH 8/8] Fix test compilation. --- systest/cluster_setup_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systest/cluster_setup_test.go b/systest/cluster_setup_test.go index 5e65b9bf8e9..532f0fa9601 100644 --- a/systest/cluster_setup_test.go +++ b/systest/cluster_setup_test.go @@ -170,7 +170,7 @@ type matchExport struct { func matchExportCount(opts matchExport) error { // Now try and export data from second server. - adminUrl := "http://" + opts.port + "/admin" + adminUrl := fmt.Sprintf("http://localhost:%d/admin", opts.port) params := testutil.GraphQLParams{ Query: testutil.ExportRequest, }