From 2a3a5309b8eec9d61e9e020aefc7b5d130b9ecc0 Mon Sep 17 00:00:00 2001 From: JASWINDER BHAMRA Date: Thu, 13 Jul 2023 20:19:49 +0530 Subject: [PATCH] Changes for the review comments --- dgraphtest/cluster.go | 4 +- go.mod | 2 +- go.sum | 3 +- systest/license/integration_test.go | 11 +-- systest/license/license_test.go | 106 +++++++++++----------------- systest/license/upgrade_test.go | 12 ++-- 6 files changed, 53 insertions(+), 85 deletions(-) diff --git a/dgraphtest/cluster.go b/dgraphtest/cluster.go index 8e962de0c27..2fda34a215a 100644 --- a/dgraphtest/cluster.go +++ b/dgraphtest/cluster.go @@ -77,7 +77,7 @@ 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"` + Extensions map[string]interface{} `json:"license,omitempty"` } type Location struct { @@ -555,7 +555,7 @@ func (hc *HTTPClient) PostPersistentQuery(query, sha string) ([]byte, error) { // Apply license using http endpoint func (hc *HTTPClient) ApplyLicenseHTTP(licenseKey []byte) (*GraphQLResponse, error) { - respBody, err := hc.doPost(licenseKey, hc.licenseURL, "application/json") + respBody, err := hc.doPost(licenseKey, hc.licenseURL, "application/text") if err != nil { return nil, errors.Wrap(err, "error applying license") } diff --git a/go.mod b/go.mod index 120bee62ab1..9f4ce6fbc35 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index eab5a0747da..6e9bfb54d7b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/systest/license/integration_test.go b/systest/license/integration_test.go index 979eac644b1..b8c1258e8f7 100644 --- a/systest/license/integration_test.go +++ b/systest/license/integration_test.go @@ -29,7 +29,6 @@ import ( type LicenseTestSuite struct { suite.Suite dc dgraphtest.Cluster - testData TestInp } func (lsuite *LicenseTestSuite) SetupTest() { @@ -44,12 +43,8 @@ func (lsuite *LicenseTestSuite) Upgrade() { } func TestLicenseTestSuite(t *testing.T) { - var tsuite LicenseTestSuite - for _, tt := range tests { - tsuite.testData = tt - suite.Run(t, &tsuite) - if t.Failed() { - t.Fatal("TestLicenseTestSuite tests failed") - } + suite.Run(t, new(LicenseTestSuite)) + if t.Failed() { + t.Fatal("TestLicenseTestSuite tests failed") } } diff --git a/systest/license/license_test.go b/systest/license/license_test.go index 06ff0b84c32..e7e789a3957 100644 --- a/systest/license/license_test.go +++ b/systest/license/license_test.go @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/dgraph-io/dgraph/dgraphtest" + "github.com/dgraph-io/dgraph/x" ) var expiredKey = []byte(`-----BEGIN PGP MESSAGE----- @@ -68,28 +69,7 @@ I8jcj3NZtGWFoxKq4laK/ruoeoHnWMznJyMm7mUd/5nzcU5QZU9yEEI= -----END PGP MESSAGE----- `) -type Location struct { - Line int `json:"line,omitempty"` - Column int `json:"column,omitempty"` -} - -type GqlError struct { - Message string `json:"message"` - Locations []Location `json:"locations,omitempty"` - Path []interface{} `json:"path,omitempty"` - Extensions map[string]interface{} `json:"extensions,omitempty"` -} - -type GqlErrorList []*GqlError - -type responseStruct struct { - Errors GqlErrorList `json:"errors"` - Code string `json:"code"` - Message string `json:"message"` - License map[string]interface{} `json:"license"` -} - -type TestInp struct { +type testInp struct { name string licenseKey []byte code string @@ -97,7 +77,7 @@ type TestInp struct { message string } -var tests = []TestInp{ +var tests = []testInp{ { "Using expired entrerprise license key, should be able to extract user information", expiredKey, @@ -128,26 +108,24 @@ func (lsuite *LicenseTestSuite) TestEnterpriseLicenseWithHttpEndPoint() { hcli, err := lsuite.dc.HTTPClient() require.NoError(t, err) - tt := lsuite.testData - enterpriseResponse, err := hcli.ApplyLicenseHTTP(tt.licenseKey) - require.NoError(t, err) - - // Check if the license is applied - require.Equal(t, enterpriseResponse.Code, tt.code) - - if enterpriseResponse.Code == `Success` { - // Upgrade - lsuite.Upgrade() - - hcli, err := lsuite.dc.HTTPClient() - require.NoError(t, err) - - // check the user information in case the license is applied - // Expired license should not be enabled even after it is applied - assertLicenseNotEnabled(t, hcli, tt.user) - } else { - // check the error message in case the license is not applied - require.Equal(t, enterpriseResponse.Errors[0].Message, tt.message) + err = hcli.LoginIntoNamespace(dgraphtest.DefaultUser, dgraphtest.DefaultPassword, x.GalaxyNamespace) + require.NoError(t, err, "login with namespace failed") + require.NotNil(t, hcli.AccessJwt, "galaxy token is nil") + + for _, tt := range tests { + enterpriseResponse, err := hcli.ApplyLicenseHTTP(tt.licenseKey) + + if err == nil && enterpriseResponse.Code == `Success` { + // Check if the license is applied + require.Equal(t, enterpriseResponse.Code, tt.code) + + // check the user information in case the license is applied + // Expired license should not be enabled even after it is applied + assertLicenseNotEnabled(t, hcli, tt.user) + } else { + // check the error message in case the license is not applied + require.Nil(t, enterpriseResponse) + } } } @@ -157,28 +135,26 @@ func (lsuite *LicenseTestSuite) TestEnterpriseLicenseWithGraphqlEndPoint() { t := lsuite.T() hcli, err := lsuite.dc.HTTPClient() require.NoError(t, err) - - tt := lsuite.testData - 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)) - - // Upgrade - lsuite.Upgrade() - - hcli, err := lsuite.dc.HTTPClient() - require.NoError(t, err) - - // check the user information in case the license is applied - // Expired license should not be enabled even after it is applied - assertLicenseNotEnabled(t, hcli, tt.user) - } else { - dgraphtest.CompareJSON(`{"enterpriseLicense":null}`, string(resp)) - // check the error message in case the license is not applied - require.Contains(t, err, tt.message) + err = hcli.LoginIntoNamespace(dgraphtest.DefaultUser, dgraphtest.DefaultPassword, x.GalaxyNamespace) + require.NoError(t, err, "login with namespace failed") + require.NotNil(t, hcli.AccessJwt, "galaxy token is nil") + + for _, tt := range tests { + resp, err := hcli.ApplyLicenseGraphQL(tt.licenseKey) + + if tt.code == `Success` { + require.NoError(t, err) + // Check if the license is applied + dgraphtest.CompareJSON(`{"enterpriseLicense":{"response":{"code":"Success"}}}`, string(resp)) + + // check the user information in case the license is applied + // Expired license should not be enabled even after it is applied + assertLicenseNotEnabled(t, hcli, tt.user) + } else { + dgraphtest.CompareJSON(`{"enterpriseLicense":null}`, string(resp)) + // check the error message in case the license is not applied + require.Contains(t, err.Error(), tt.message) + } } } diff --git a/systest/license/upgrade_test.go b/systest/license/upgrade_test.go index a4b5e6283c7..8cd4a02f810 100644 --- a/systest/license/upgrade_test.go +++ b/systest/license/upgrade_test.go @@ -34,7 +34,6 @@ type LicenseTestSuite struct { dc dgraphtest.Cluster lc *dgraphtest.LocalCluster uc dgraphtest.UpgradeCombo - testData TestInp } func (lsuite *LicenseTestSuite) SetupTest() { @@ -64,16 +63,13 @@ func (lsuite *LicenseTestSuite) Upgrade() { } func TestLicenseTestSuite(t *testing.T) { - for _, uc := range dgraphtest.AllUpgradeCombos { + for _, uc := range dgraphtest.AllUpgradeCombos() { log.Printf("running: backup in [%v], restore in [%v]", uc.Before, uc.After) var tsuite LicenseTestSuite tsuite.uc = uc - for _, tt := range tests { - tsuite.testData = tt - suite.Run(t, &tsuite) - if t.Failed() { - t.Fatal("TestLicenseTestSuite tests failed") - } + suite.Run(t, &tsuite) + if t.Failed() { + t.Fatal("TestLicenseTestSuite tests failed") } } }