Skip to content

Commit

Permalink
Changes for review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jbhamra1 committed Jul 12, 2023
1 parent 7bf88a4 commit 9cad5d5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 58 deletions.
26 changes: 14 additions & 12 deletions dgraphtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type gqlError struct {

type gqlErrorList []*gqlError

type LicenseResponse struct {
type ZeroResponse struct {
Errors gqlErrorList `json:"errors"`
Code string `json:"code"`
Message string `json:"message"`
Expand Down Expand Up @@ -535,18 +535,21 @@ func (hc *HTTPClient) PostPersistentQuery(query, sha string) ([]byte, error) {
}

// Apply license using http endpoint
func (hc *HTTPClient) ApplyLicenseWithHttpEP(licenseKey []byte) ([]byte, error) {
url := hc.licenseURL
respBody, err := hc.doPost(licenseKey, url, "application/json")
func (hc *HTTPClient) ApplyLicenseHTTP(licenseKey []byte) (*ZeroResponse, error) {
respBody, err := hc.doPost(licenseKey, hc.licenseURL, "application/json")
if err != nil {
return nil, errors.Wrap(err, "error applying license")
}

return respBody, nil
var enterpriseResponse ZeroResponse
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) ApplyLicenseWithGraphqlEP(license []byte) ([]byte, error) {
func (hc *HTTPClient) ApplyLicenseGraphQL(license []byte) ([]byte, error) {
params := GraphQLParams {
Query: `mutation ($license: String!) {
enterpriseLicense(input: {license: $license}) {
Expand All @@ -562,18 +565,17 @@ func (hc *HTTPClient) ApplyLicenseWithGraphqlEP(license []byte) ([]byte, error)
return hc.RunGraphqlQuery(params, true)
}

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

Expand Down
52 changes: 15 additions & 37 deletions dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,62 +615,40 @@ 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
}
licenseURL, err := c.licenseURL()
licenseURL, err := c.serverURL("zero", "/enterpriseLicense")
if err != nil {
return nil, err
}
stateURL, err := c.stateURL()
stateURL, err := c.serverURL("zero", "/state")
if err != nil {
return nil, err
}
return &HTTPClient{adminURL: adminURL, graphqlURL: graphqlURL, licenseURL: licenseURL, stateURL: stateURL}, nil
}

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

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

// licenseURL returns url to the enterprise license endpoint
func (c *LocalCluster) licenseURL() (string, error) {
publicPort, err := publicPort(c.dcli, c.zeros[0], zeroHttpPort)
if err != nil {
return "", err
// 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)
}
url := "http://localhost:" + publicPort + "/enterpriseLicense"
return url, nil
}

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

Expand Down
3 changes: 3 additions & 0 deletions systest/license/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ func TestLicenseTestSuite(t *testing.T) {
for _, tt := range tests {
tsuite.testData = tt
suite.Run(t, &tsuite)
if t.Failed() {
t.Fatal("TestLicenseTestSuite tests failed")
}
}
}
12 changes: 3 additions & 9 deletions systest/license/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package main

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -127,14 +126,11 @@ func (lsuite *LicenseTestSuite) TestEnterpriseLicenseWithHttpEndPoint() {

t := lsuite.T()

// Apply the license
hcli, err := lsuite.dc.HTTPClient()
require.NoError(t, err)
tt := lsuite.testData
responseBody, err := hcli.ApplyLicenseWithHttpEP(tt.licenseKey)
enterpriseResponse, err := hcli.ApplyLicenseHTTP(tt.licenseKey)
require.NoError(t, err)
var enterpriseResponse dgraphtest.LicenseResponse
require.NoError(t, json.Unmarshal(responseBody, &enterpriseResponse))

// Check if the license is applied
require.Equal(t, enterpriseResponse.Code, tt.code)
Expand All @@ -159,18 +155,16 @@ func (lsuite *LicenseTestSuite) TestEnterpriseLicenseWithGraphqlEndPoint() {
// this time, run them using the GraphQL admin endpoint

t := lsuite.T()
// Apply the license
hcli, err := lsuite.dc.HTTPClient()
require.NoError(t, err)

tt := lsuite.testData
resp, err := hcli.ApplyLicenseWithGraphqlEP(tt.licenseKey)
resp, err := hcli.ApplyLicenseGraphQL(tt.licenseKey)
require.NoError(t, err)

if tt.code == `Success` {
// Check if the license is applied
dgraphtest.CompareJSON(`{"enterpriseLicense":{"response":{"code":"Success"}}}`,
string(resp))
dgraphtest.CompareJSON(`{"enterpriseLicense":{"response":{"code":"Success"}}}`, string(resp))

// Upgrade
lsuite.Upgrade()
Expand Down
3 changes: 3 additions & 0 deletions systest/license/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func TestLicenseTestSuite(t *testing.T) {
for _, tt := range tests {
tsuite.testData = tt
suite.Run(t, &tsuite)
if t.Failed() {
t.Fatal("TestLicenseTestSuite tests failed")
}
}
}
}

0 comments on commit 9cad5d5

Please sign in to comment.