Skip to content

Commit

Permalink
Add a test that after snapshot is applied, GraphQL schema is refreshed (
Browse files Browse the repository at this point in the history
#8619)

After snapshot stream is received from other running alpha,
ensure that the new graphql schema is loaded into memory .

Sequence of steps in the test:
 * Stop one alpha container 
 * Add data into cluster + apply new graphql schema 
 * Start alpha container
 * Check whether the newly started alpha got graphql schema in snapshot.
  • Loading branch information
shivaji-kharse authored and all-seeing-code committed Feb 8, 2023
1 parent 65eb7a7 commit 2ae33e2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
25 changes: 25 additions & 0 deletions testutil/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,28 @@ func DockerInspect(containerID string) (types.ContainerJSON, error) {
x.Check(err)
return cli.ContainerInspect(context.Background(), containerID)
}

// checkHealthContainer checks health of container and determines wheather container is ready to accept request
func CheckHealthContainer(socketAddrHttp string) error {
var err error
var resp *http.Response
url := "http://" + socketAddrHttp + "/health"
for i := 0; i < 30; i++ {
resp, err = http.Get(url)
if err == nil && resp.StatusCode == http.StatusOK {
return nil
}
var body []byte
if resp != nil && resp.Body != nil {
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
resp.Body.Close()
}
fmt.Printf("health check for container failed: %v. Response: %q. Retrying...\n", err, body)
time.Sleep(time.Second)

}
return err
}
40 changes: 40 additions & 0 deletions testutil/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package testutil

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -133,3 +136,40 @@ func VerifySchema(t *testing.T, dg *dgo.Dgraph, opts SchemaOptions) {

CompareJSON(t, GetFullSchemaJSON(opts), string(resp.GetJson()))
}

func UpdateGQLSchema(t *testing.T, sockAddrHttp, schema string) {
query := `mutation updateGQLSchema($sch: String!) {
updateGQLSchema(input: { set: { schema: $sch }}) {
gqlSchema {
schema
}
}
}`
adminUrl := "http://" + sockAddrHttp + "/admin"

params := GraphQLParams{
Query: query,
Variables: map[string]interface{}{
"sch": schema,
},
}
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()
}

func GetGQLSchema(t *testing.T, sockAddrHttp string) string {
query := `{getGQLSchema {schema}}`
params := GraphQLParams{Query: query}
b, err := json.Marshal(params)
adminUrl := "http://" + sockAddrHttp + "/admin"
require.NoError(t, err)
resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b))
require.NoError(t, err)
defer resp.Body.Close()
var data interface{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
return JsonGet(data, "data", "getGQLSchema", "schema").(string)
}
15 changes: 14 additions & 1 deletion worker/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func TestSnapshot(t *testing.T) {
require.NoError(t, err)

// Wait for the container to start.
time.Sleep(time.Second * 2)
err = testutil.CheckHealthContainer(testutil.ContainerAddr("alpha2", 8080))
if err != nil {
t.Fatalf("error while getting alpha container health: %v", err)
}
dg2, err := testutil.DgraphClient(testutil.ContainerAddr("alpha2", 9080))
if err != nil {
t.Fatalf("Error while getting a dgraph client: %v", err)
Expand All @@ -99,17 +102,27 @@ func TestSnapshot(t *testing.T) {
})
require.NoError(t, err)
}
const testSchema = "type Person { name: String }"
// uploading new schema while alpha2 is not running so we can test whether the stopped alpha gets new schema in snapshot
testutil.UpdateGQLSchema(t, testutil.SockAddrHttp, testSchema)
_ = waitForSnapshot(t, snapshotTs)

t.Logf("Starting alpha2.\n")
err = testutil.DockerRun("alpha2", testutil.Start)
require.NoError(t, err)
err = testutil.CheckHealthContainer(testutil.ContainerAddr("alpha2", 8080))
if err != nil {
t.Fatalf("error while getting alpha container health: %v", err)
}

dg2, err = testutil.DgraphClient(testutil.ContainerAddr("alpha2", 9080))
if err != nil {
t.Fatalf("Error while getting a dgraph client: %v", err)
}
verifySnapshot(t, dg2, 400)
resp := testutil.GetGQLSchema(t, testutil.ContainerAddr("alpha2", 8080))
// comparing uploaded graphql schema and schema acquired from stopped container
require.Equal(t, testSchema, resp)
}

func verifySnapshot(t *testing.T, dg *dgo.Dgraph, num int) {
Expand Down

0 comments on commit 2ae33e2

Please sign in to comment.