diff --git a/dgraphtest/cluster.go b/dgraphtest/cluster.go index 38de68f501b..6858f8f7cfe 100644 --- a/dgraphtest/cluster.go +++ b/dgraphtest/cluster.go @@ -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"` @@ -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}) { @@ -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") } diff --git a/dgraphtest/local_cluster.go b/dgraphtest/local_cluster.go index c977139fd34..7f8f1f5fcab 100644 --- a/dgraphtest/local_cluster.go +++ b/dgraphtest/local_cluster.go @@ -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 } diff --git a/systest/license/integration_test.go b/systest/license/integration_test.go index 8277c7d4281..979eac644b1 100644 --- a/systest/license/integration_test.go +++ b/systest/license/integration_test.go @@ -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") + } } } diff --git a/systest/license/license_test.go b/systest/license/license_test.go index 01357761703..b836f40cf3b 100644 --- a/systest/license/license_test.go +++ b/systest/license/license_test.go @@ -19,7 +19,6 @@ package main import ( - "encoding/json" "testing" "github.com/stretchr/testify/require" @@ -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) @@ -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() diff --git a/systest/license/upgrade_test.go b/systest/license/upgrade_test.go index da2bc215545..a4b5e6283c7 100644 --- a/systest/license/upgrade_test.go +++ b/systest/license/upgrade_test.go @@ -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") + } } } }