Skip to content

Commit

Permalink
test(upgrade): add upgrade tests for systest/license package (#8902)
Browse files Browse the repository at this point in the history
Description: this PR adds upgrade tests for systest/license package
using dgraphtest package.
Closes: DGRAPHCORE-303
  • Loading branch information
jbhamra1 authored Jul 17, 2023
1 parent 95e8b34 commit a506d6d
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 111 deletions.
76 changes: 68 additions & 8 deletions dgraphtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"strings"
Expand Down Expand Up @@ -61,6 +62,8 @@ type HTTPClient struct {
*HttpToken
adminURL string
graphqlURL string
licenseURL string
stateURL string
}

// GraphQLParams are used for making graphql requests to dgraph
Expand All @@ -73,9 +76,17 @@ type GraphQLParams struct {
type GraphQLResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors x.GqlErrorList `json:"errors,omitempty"`
Code string `json:"code"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

type LicenseResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors x.GqlErrorList `json:"errors,omitempty"`
Code string `json:"code"`
Extensions map[string]interface{} `json:"license,omitempty"`
}

func (hc *HTTPClient) Login(user, password string, ns uint64) error {
login := `mutation login($userId: String, $password: String, $namespace: Int, $refreshToken: String) {
login(userId: $userId, password: $password, namespace: $namespace, refreshToken: $refreshToken) {
Expand Down Expand Up @@ -210,17 +221,13 @@ func (hc *HTTPClient) ResetPassword(userID, newPass string, nsID uint64) (string
return "", errors.New(result.ResetPassword.Message)
}

// doPost makes a post request to the graphql admin endpoint
func (hc *HTTPClient) doPost(body []byte, admin bool) ([]byte, error) {
url := hc.graphqlURL
if admin {
url = hc.adminURL
}
// doPost makes a post request to the 'url' endpoint
func (hc *HTTPClient) doPost(body []byte, url string, contentType string) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return nil, errors.Wrapf(err, "error building req for endpoint [%v]", url)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Type", contentType)

if hc.HttpToken != nil {
req.Header.Add("X-Dgraph-AccessToken", hc.AccessJwt)
Expand All @@ -236,7 +243,12 @@ func (hc *HTTPClient) RunGraphqlQuery(params GraphQLParams, admin bool) ([]byte,
return nil, errors.Wrap(err, "error while marshalling params")
}

respBody, err := hc.doPost(reqBody, admin)
url := hc.graphqlURL
if admin {
url = hc.adminURL
}

respBody, err := hc.doPost(reqBody, url, "application/json")
if err != nil {
return nil, errors.Wrap(err, "error while running graphql query")
}
Expand Down Expand Up @@ -525,6 +537,54 @@ func (hc *HTTPClient) PostPersistentQuery(query, sha string) ([]byte, error) {
return hc.RunGraphqlQuery(params, false)
}

// Apply license using http endpoint
func (hc *HTTPClient) ApplyLicenseHTTP(licenseKey []byte) (*LicenseResponse, error) {
respBody, err := hc.doPost(licenseKey, hc.licenseURL, "application/text")
if err != nil {
return nil, errors.Wrap(err, "error applying license")
}
var enterpriseResponse LicenseResponse
if err = json.Unmarshal(respBody, &enterpriseResponse); err != nil {
return nil, errors.Wrap(err, "error unmarshaling the license response")
}

return &enterpriseResponse, nil
}

// Apply license using graphql endpoint
func (hc *HTTPClient) ApplyLicenseGraphQL(license []byte) ([]byte, error) {
params := GraphQLParams{
Query: `mutation ($license: String!) {
enterpriseLicense(input: {license: $license}) {
response {
code
}
}
}`,
Variables: map[string]interface{}{
"license": string(license),
},
}
return hc.RunGraphqlQuery(params, true)
}

func (hc *HTTPClient) GetZeroState() (*LicenseResponse, error) {
response, err := http.Get(hc.stateURL)
if err != nil {
return nil, errors.Wrap(err, "error getting zero state http response")
}
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error reading zero state response body")
}
var stateResponse LicenseResponse
if err := json.Unmarshal(body, &stateResponse); err != nil {
return nil, errors.New("error unmarshaling zero state response")
}

return &stateResponse, nil
}

// SetupSchema sets up DQL schema
func (gc *GrpcClient) SetupSchema(dbSchema string) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
Expand Down
10 changes: 9 additions & 1 deletion dgraphtest/compose_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ func (c *ComposeCluster) Client() (*GrpcClient, func(), error) {
func (c *ComposeCluster) HTTPClient() (*HTTPClient, error) {
adminUrl := "http://" + testutil.SockAddrHttp + "/admin"
graphQLUrl := "http://" + testutil.SockAddrHttp + "/graphql"
return &HTTPClient{adminURL: adminUrl, graphqlURL: graphQLUrl, HttpToken: &HttpToken{}}, nil
licenseUrl := "http://" + testutil.SockAddrZeroHttp + "/enterpriseLicense"
stateUrl := "http://" + testutil.SockAddrZeroHttp + "/state"
return &HTTPClient{
adminURL: adminUrl,
graphqlURL: graphQLUrl,
licenseURL: licenseUrl,
stateURL: stateUrl,
HttpToken: &HttpToken{},
}, nil
}

func (c *ComposeCluster) AlphasHealth() ([]string, error) {
Expand Down
37 changes: 22 additions & 15 deletions dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,34 +619,41 @@ func (c *LocalCluster) Client() (*GrpcClient, func(), error) {

// HTTPClient creates an HTTP client
func (c *LocalCluster) HTTPClient() (*HTTPClient, error) {
adminURL, err := c.adminURL()
adminURL, err := c.serverURL("alpha", "/admin")
if err != nil {
return nil, err
}
graphqlURL, err := c.graphqlURL()
graphqlURL, err := c.serverURL("alpha", "/graphql")
if err != nil {
return nil, err
}
return &HTTPClient{adminURL: adminURL, graphqlURL: graphqlURL}, nil
}

// adminURL returns url to the graphql admin endpoint
func (c *LocalCluster) adminURL() (string, error) {
publicPort, err := publicPort(c.dcli, c.alphas[0], alphaHttpPort)
licenseURL, err := c.serverURL("zero", "/enterpriseLicense")
if err != nil {
return "", err
return nil, err
}
url := "http://localhost:" + publicPort + "/admin"
return url, nil
stateURL, err := c.serverURL("zero", "/state")
if err != nil {
return nil, err
}

return &HTTPClient{
adminURL: adminURL,
graphqlURL: graphqlURL,
licenseURL: licenseURL,
stateURL: stateURL,
}, nil
}

// graphqlURL returns url to the graphql endpoint
func (c *LocalCluster) graphqlURL() (string, error) {
publicPort, err := publicPort(c.dcli, c.alphas[0], alphaHttpPort)
// serverURL returns url to the 'server' 'endpoint'
func (c *LocalCluster) serverURL(server, endpoint string) (string, error) {
pubPort, err := publicPort(c.dcli, c.alphas[0], alphaHttpPort)
if server == "zero" {
pubPort, err = publicPort(c.dcli, c.zeros[0], zeroHttpPort)
}
if err != nil {
return "", err
}
url := "http://localhost:" + publicPort + "/graphql"
url := "http://localhost:" + pubPort + endpoint
return url, nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.4
github.com/twpayne/go-geom v1.0.5
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
go.etcd.io/etcd v0.5.0-alpha.5.0.20190108173120-83c051b701d3
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
Expand Down
50 changes: 50 additions & 0 deletions systest/license/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//go:build integration

/*
* Copyright 2023 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 (
"testing"

"github.com/stretchr/testify/suite"

"github.com/dgraph-io/dgraph/dgraphtest"
)

type LicenseTestSuite struct {
suite.Suite
dc dgraphtest.Cluster
}

func (lsuite *LicenseTestSuite) SetupTest() {
lsuite.dc = dgraphtest.NewComposeCluster()
}

func (lsuite *LicenseTestSuite) TearDownTest() {
}

func (lsuite *LicenseTestSuite) Upgrade() {
// Not implemented for integration tests
}

func TestLicenseTestSuite(t *testing.T) {
suite.Run(t, new(LicenseTestSuite))
if t.Failed() {
t.Fatal("TestLicenseTestSuite tests failed")
}
}
Loading

0 comments on commit a506d6d

Please sign in to comment.