Skip to content

Commit

Permalink
fix ns login after drop data
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Sep 9, 2024
1 parent 960a93a commit be9876e
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 11 deletions.
5 changes: 3 additions & 2 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er

// just reinsert the GraphQL schema, no need to alter dgraph schema as this was drop_data
_, err = UpdateGQLSchema(ctx, graphQLSchema, "")
// recreate the admin account after a drop data operation
InitializeAcl(nil)

// Since all data has been dropped, we need to recreate the admin account in the respective namespace.
upsertGuardianAndGroot(nil, namespace)
return empty, err
}

Expand Down
10 changes: 1 addition & 9 deletions systest/integration2/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"
"time"

"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/dgraphtest"
"github.com/dgraph-io/dgraph/v24/x"
Expand Down Expand Up @@ -60,14 +59,7 @@ func testDuplicateUserUpgradeStrat(t *testing.T, strat dgraphtest.UpgradeStrateg
require.NoError(t, err)
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser,
dgraphapi.DefaultPassword, x.GalaxyNamespace))
require.NoError(t, gc.SetupSchema(`name: string .`))

rdfs := `
_:a <name> "alice" .
_:b <name> "bob" .
_:c <name> "sagar" .
_:d <name> "ajay" .`
_, err = gc.Mutate(&api.Mutation{SetNquads: []byte(rdfs), CommitNow: true})
addData(t, gc)
require.NoError(t, c.Upgrade("local", strat))
gc, cleanup, err = c.Client()
require.NoError(t, err)
Expand Down
136 changes: 136 additions & 0 deletions systest/integration2/multitenancy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//go:build integration2

/*
* Copyright 2024 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 main

import (
"context"
"testing"
"time"

"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/dgraphtest"
"github.com/dgraph-io/dgraph/v24/x"
"github.com/stretchr/testify/require"
)

var (
gc *dgraphapi.GrpcClient
hc *dgraphapi.HTTPClient
)

func addData(t *testing.T, gc *dgraphapi.GrpcClient) {
require.NoError(t, gc.SetupSchema(`name: string .`))

rdfs := `
_:a <name> "alice" .
_:b <name> "bob" .
_:c <name> "sagar" .
_:d <name> "ajay" .`
_, err := gc.Mutate(&api.Mutation{SetNquads: []byte(rdfs), CommitNow: true})
require.NoError(t, err)
}

func TestRunAllMultiTenancy(t *testing.T) {
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
c, err := dgraphtest.NewLocalCluster(conf)
require.NoError(t, err)
defer func() { c.Cleanup(t.Failed()) }()
require.NoError(t, c.Start())
var cleanup func()
gc, cleanup, err = c.Client()
require.NoError(t, err)
defer cleanup()
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

hc, err = c.HTTPClient()
require.NoError(t, err)
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser,
dgraphapi.DefaultPassword, x.GalaxyNamespace))
addData(t, gc)

RunAll(t)
}

func RunAll(t *testing.T) {
t.Run("login into the ns after drop data opration from the ns", testDropDataNSLogin)
t.Run("login into the default ns after drop data opration from default ns", testDropDataNSLogin)
}

func testDropDataNSLogin(t *testing.T) {
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
require.NoError(t, gc.DropAll())
for i := 1; i < 5; i++ {
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser,
dgraphapi.DefaultPassword, x.GalaxyNamespace))
ns, err := hc.AddNamespace()
require.NoError(t, err)
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
require.NoError(t, gc.LoginIntoNamespace(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))

addData(t, gc)

// Drop data from the namespace
require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))

// Login into the namespace
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))

require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
}
}

func testDropDataNSFromDefaultNs(t *testing.T) {
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
require.NoError(t, gc.DropAll())
nss := []uint64{}
for i := 1; i < 2; i++ {
ns, err := hc.AddNamespace()
nss = append(nss, ns)
require.NoError(t, err)
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
require.NoError(t, gc.LoginIntoNamespace(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))

addData(t, gc)
}

// Drop data from default namespace
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))

// Login into the namespace
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

for _, ns := range nss {
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
query := `{
q(func: has(name)) {
count(uid)
}
}`
resp, err := gc.Query(query)
require.NoError(t, err)
require.Contains(t, string(resp.Json), `"count":4`)
}
}

0 comments on commit be9876e

Please sign in to comment.