diff --git a/dgraph/cmd/live/run.go b/dgraph/cmd/live/run.go index 4fcd3d1e17d..0398783c6ab 100644 --- a/dgraph/cmd/live/run.go +++ b/dgraph/cmd/live/run.go @@ -186,9 +186,8 @@ func init() { "be used to store blank nodes as an xid") flag.String("tmp", "t", "Directory to store temporary buffers.") flag.Int64("force-namespace", 0, "Namespace onto which to load the data."+ - "This flag will be ignored when not logging into galaxy namespace."+ - "Only guardian of galaxy should use this for loading data into multiple namespaces."+ - "Setting it to negative value will preserve the namespace.") + "Only guardian of galaxy should use this for loading data into multiple namespaces or some"+ + "specific namespace. Setting it to negative value will preserve the namespace.") } func getSchema(ctx context.Context, dgraphClient *dgo.Dgraph, ns uint64) (*schema, error) { @@ -714,16 +713,19 @@ func run() error { tmpDir: Live.Conf.GetString("tmp"), } + forceNs := Live.Conf.GetInt64("force-namespace") switch creds.GetUint64("namespace") { case x.GalaxyNamespace: - ns := Live.Conf.GetInt64("force-namespace") - if ns < 0 { + if forceNs < 0 { opt.preserveNs = true opt.namespaceToLoad = math.MaxUint64 } else { - opt.namespaceToLoad = uint64(ns) + opt.namespaceToLoad = uint64(forceNs) } default: + if Live.Conf.IsSet("force-namespace") && uint64(forceNs) != creds.GetUint64("namespace") { + return errors.Errorf("cannot load into namespace %#x", forceNs) + } opt.namespaceToLoad = creds.GetUint64("namespace") } diff --git a/systest/multi-tenancy/basic_test.go b/systest/multi-tenancy/basic_test.go index 473bf727af0..4437f0956f6 100644 --- a/systest/multi-tenancy/basic_test.go +++ b/systest/multi-tenancy/basic_test.go @@ -278,8 +278,7 @@ func TestLiveLoadMulti(t *testing.T) { schema: ` name: string @index(term) . `, - creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", - Namespace: x.GalaxyNamespace}, + creds: galaxyCreds, forceNs: int64(ns), })) @@ -301,26 +300,46 @@ func TestLiveLoadMulti(t *testing.T) { // Try loading into a multiple namespaces. err = liveLoadData(t, &liveOpts{ - rdfs: fmt.Sprintf(`_:c "ns eon" <%#x> .`, ns), - schema: `[0x123456] name: string @index(term) .`, - creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", - Namespace: x.GalaxyNamespace}, + rdfs: fmt.Sprintf(`_:c "ns eon" <%#x> .`, ns), + schema: `[0x123456] name: string @index(term) .`, + creds: galaxyCreds, forceNs: -1, }) require.Error(t, err) require.Contains(t, err.Error(), "Namespace 0x123456 doesn't exist for pred") err = liveLoadData(t, &liveOpts{ - rdfs: fmt.Sprintf(`_:c "ns eon" <0x123456> .`), - schema: `name: string @index(term) .`, - creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", - Namespace: x.GalaxyNamespace}, + rdfs: fmt.Sprintf(`_:c "ns eon" <0x123456> .`), + schema: `name: string @index(term) .`, + creds: galaxyCreds, forceNs: -1, }) require.Error(t, err) require.Contains(t, err.Error(), "Cannot load nquad") // Load data by non-galaxy user. + err = liveLoadData(t, &liveOpts{ + rdfs: `_:c "ns hola" .`, + schema: ` + name: string @index(term) . +`, + creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns}, + forceNs: -1, + }) + require.Error(t, err) + require.Contains(t, err.Error(), "cannot load into namespace") + + err = liveLoadData(t, &liveOpts{ + rdfs: `_:c "ns hola" .`, + schema: ` + name: string @index(term) . +`, + creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns}, + forceNs: 10, + }) + require.Error(t, err) + require.Contains(t, err.Error(), "cannot load into namespace") + require.NoError(t, liveLoadData(t, &liveOpts{ rdfs: fmt.Sprintf(` _:a "ns free" . @@ -330,10 +349,8 @@ func TestLiveLoadMulti(t *testing.T) { schema: ` name: string @index(term) . `, - creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns}, - forceNs: -1, // this will be ignored. + creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns}, })) - resp = testutil.QueryData(t, dc1, query3) testutil.CompareJSON(t, `{"me": [{"name":"ns alice"}, {"name": "ns bob"},{"name":"ns chew"}, diff --git a/testutil/bulk.go b/testutil/bulk.go index c27979143fe..ff8f3a1f0e4 100644 --- a/testutil/bulk.go +++ b/testutil/bulk.go @@ -47,7 +47,9 @@ func LiveLoad(opts LiveOpts) error { "--schema", opts.SchemaFile, "--alpha", opts.Alpha, "--zero", opts.Zero, - "--force-namespace", strconv.FormatInt(opts.ForceNs, 10), + } + if opts.ForceNs != 0 { + args = append(args, "--force-namespace", strconv.FormatInt(opts.ForceNs, 10)) } if opts.Ludicrous { args = append(args, "--ludicrous")