Skip to content

Commit 7f5dcf5

Browse files
Refactor K8s tests to use imroc/req library for all the requests (#261)
1 parent 5423c9e commit 7f5dcf5

File tree

8 files changed

+99
-47
lines changed

8 files changed

+99
-47
lines changed

test/e2e/env_param_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/tls"
55
"fmt"
66
"io"
7-
"net/http"
87
"os"
98
"path/filepath"
109
"strconv"
@@ -15,13 +14,12 @@ import (
1514
"github.com/gruntwork-io/terratest/modules/helm"
1615
"github.com/gruntwork-io/terratest/modules/k8s"
1716
"github.com/gruntwork-io/terratest/modules/random"
17+
"github.com/imroc/req/v3"
1818
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
1919
"github.com/stretchr/testify/assert"
20-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2120
)
2221

2322
func TestEnableConvertersAndLicense(t *testing.T) {
24-
var resp *http.Response
2523
var body []byte
2624
var err error
2725
// Path to the helm chart we will test
@@ -123,12 +121,19 @@ func TestEnableConvertersAndLicense(t *testing.T) {
123121
endpoint := fmt.Sprintf("http://%s/admin/v1/timestamp", tunnel.Endpoint())
124122
t.Logf(`Endpoint: %s`, endpoint)
125123

124+
client := req.C().
125+
SetCommonDigestAuth(username, password).
126+
SetCommonRetryCount(10).
127+
SetCommonRetryFixedInterval(10 * time.Second)
128+
126129
// Make request to server as soon as it is ready
127-
timestamp := digestAuth.NewRequest(username, password, "GET", endpoint, "")
130+
resp, err := client.R().
131+
Get(endpoint)
128132

129-
if resp, err = timestamp.Execute(); err != nil {
133+
if err != nil {
130134
t.Fatalf(err.Error())
131135
}
136+
defer resp.Body.Close()
132137
if body, err = io.ReadAll(resp.Body); err != nil {
133138
t.Fatalf(err.Error())
134139
}

test/e2e/group_cfg_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
1818
"github.com/stretchr/testify/assert"
1919
"github.com/tidwall/gjson"
20-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2120
)
2221

2322
func VerifyGrpNameChng(t *testing.T, groupEndpoint string, newGroupName string) (int, error) {
@@ -192,9 +191,12 @@ func TestMultiGroupCfgChng(t *testing.T) {
192191
hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint())
193192
t.Logf(`Endpoint: %s`, hostsEndpoint)
194193

195-
getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
196-
197-
resp, err := getHostsDR.Execute()
194+
client := req.C().
195+
SetCommonDigestAuth(username, password).
196+
SetCommonRetryCount(10).
197+
SetCommonRetryFixedInterval(10 * time.Second)
198+
resp, err := client.R().
199+
Get(hostsEndpoint)
198200
if err != nil {
199201
t.Fatalf(err.Error())
200202
}

test/e2e/install_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package e2e
33
import (
44
"crypto/tls"
55
"fmt"
6-
"io/ioutil"
7-
"net/http"
6+
"io"
87
"os"
98
"path/filepath"
109
"strconv"
@@ -15,14 +14,13 @@ import (
1514
"github.com/gruntwork-io/terratest/modules/helm"
1615
"github.com/gruntwork-io/terratest/modules/k8s"
1716
"github.com/gruntwork-io/terratest/modules/random"
17+
"github.com/imroc/req/v3"
1818
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
1919
"github.com/stretchr/testify/assert"
2020
"github.com/tidwall/gjson"
21-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2221
)
2322

2423
func TestHelmInstall(t *testing.T) {
25-
var resp *http.Response
2624
var body []byte
2725
var err error
2826
var podZeroName string
@@ -136,26 +134,31 @@ func TestHelmInstall(t *testing.T) {
136134
tunnel8002.ForwardPort(t)
137135
endpointManage := fmt.Sprintf("http://%s/manage/v2", tunnel8002.Endpoint())
138136

139-
request := digestAuth.NewRequest(username, password, "GET", endpointManage, "")
140-
response, err := request.Execute()
137+
client := req.C().
138+
SetCommonDigestAuth(username, password).
139+
SetCommonRetryCount(10).
140+
SetCommonRetryFixedInterval(10 * time.Second)
141+
142+
resp, err := client.R().
143+
Get(endpointManage)
141144
if err != nil {
142145
t.Fatalf(err.Error())
143146
}
144-
defer response.Body.Close()
147+
defer resp.Body.Close()
145148
// the generated password should be able to access the manage endpoint
146-
assert.Equal(t, 200, response.StatusCode)
149+
assert.Equal(t, 200, resp.StatusCode)
147150

148151
t.Log("====Verify xdqp-ssl-enabled is set to true by default")
149152
endpoint := fmt.Sprintf("http://%s/manage/v2/groups/Default/properties?format=json", tunnel8002.Endpoint())
150153
t.Logf(`Endpoint for group properties: %s`, endpoint)
151154

152-
request = digestAuth.NewRequest(username, password, "GET", endpoint, "")
153-
resp, err = request.Execute()
155+
resp, err = client.R().
156+
Get(endpoint)
154157
if err != nil {
155158
t.Fatalf(err.Error())
156159
}
157160
defer resp.Body.Close()
158-
body, err = ioutil.ReadAll(resp.Body)
161+
body, err = io.ReadAll(resp.Body)
159162
if err != nil {
160163
t.Fatalf(err.Error())
161164
}
@@ -166,12 +169,16 @@ func TestHelmInstall(t *testing.T) {
166169

167170
t.Log("====Verify no groups beyond default were created/modified====")
168171
groupStatusEndpoint := fmt.Sprintf("http://%s/manage/v2/groups?format=json", tunnel8002.Endpoint())
169-
groupStatus := digestAuth.NewRequest(username, password, "GET", groupStatusEndpoint, "")
170172
t.Logf(`groupStatusEndpoint: %s`, groupStatusEndpoint)
171-
if resp, err = groupStatus.Execute(); err != nil {
173+
resp, err = client.R().
174+
Get(groupStatusEndpoint)
175+
176+
if err != nil {
172177
t.Fatalf(err.Error())
173178
}
174-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
179+
defer resp.Body.Close()
180+
181+
if body, err = io.ReadAll(resp.Body); err != nil {
175182
t.Fatalf(err.Error())
176183
}
177184
groupQuantityJSON := gjson.Get(string(body), "group-default-list.list-items.list-count.value")

test/e2e/ready_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package e2e
33
import (
44
"crypto/tls"
55
"fmt"
6-
"io/ioutil"
7-
"net/http"
6+
"io"
87
"os"
98
"path/filepath"
109
"strconv"
@@ -15,8 +14,8 @@ import (
1514
"github.com/gruntwork-io/terratest/modules/helm"
1615
"github.com/gruntwork-io/terratest/modules/k8s"
1716
"github.com/gruntwork-io/terratest/modules/random"
17+
"github.com/imroc/req/v3"
1818
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
19-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2019
)
2120

2221
func TestMarklogicReady(t *testing.T) {
@@ -25,7 +24,6 @@ func TestMarklogicReady(t *testing.T) {
2524
if e != nil {
2625
t.Fatalf(e.Error())
2726
}
28-
var resp *http.Response
2927
var body []byte
3028
var err error
3129
var initialChartVersion string
@@ -120,12 +118,21 @@ func TestMarklogicReady(t *testing.T) {
120118
t.Logf(`Endpoint: %s`, endpoint)
121119

122120
// Make request to server as soon as it is ready
123-
timestamp := digestAuth.NewRequest(username, password, "GET", endpoint, "")
121+
client := req.C().
122+
SetCommonDigestAuth(username, password).
123+
SetCommonRetryCount(10).
124+
SetCommonRetryFixedInterval(10 * time.Second)
124125

125-
if resp, err = timestamp.Execute(); err != nil {
126+
// Make request to server as soon as it is ready
127+
resp, err := client.R().
128+
Get(endpoint)
129+
130+
if err != nil {
126131
t.Fatalf(err.Error())
127132
}
128-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
133+
defer resp.Body.Close()
134+
135+
if body, err = io.ReadAll(resp.Body); err != nil {
129136
t.Fatalf(err.Error())
130137
}
131138

test/e2e/separate_nodes_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
2020
"github.com/stretchr/testify/assert"
2121
"github.com/tidwall/gjson"
22-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2322
)
2423

2524
var username = "admin"
@@ -406,15 +405,20 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
406405
hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint())
407406
t.Logf(`Endpoint: %s`, hostsEndpoint)
408407

409-
getHostsRequest := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
410-
resp, err := getHostsRequest.Execute()
408+
client := req.C().
409+
SetCommonDigestAuth(username, password).
410+
SetCommonRetryCount(10).
411+
SetCommonRetryFixedInterval(10 * time.Second)
412+
413+
resp, err := client.R().
414+
Get(hostsEndpoint)
411415
if err != nil {
412416
t.Fatalf(err.Error())
413417
}
414418

415419
defer resp.Body.Close()
416420

417-
body, err := ioutil.ReadAll(resp.Body)
421+
body, err := io.ReadAll(resp.Body)
418422
if err != nil {
419423
t.Fatalf(err.Error())
420424
}
@@ -452,9 +456,9 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
452456

453457
// Verify clustering failed given incorrect hostname
454458
clusterStatusEndpoint := fmt.Sprintf("http://%s/manage/v2?view=status", tunnel.Endpoint())
455-
clusterStatus := digestAuth.NewRequest(username, password, "GET", clusterStatusEndpoint, "")
456459
t.Logf(`clusterStatusEndpoint: %s`, clusterStatusEndpoint)
457-
resp, err = clusterStatus.Execute()
460+
resp, err = client.R().
461+
Get(clusterStatusEndpoint)
458462
if err != nil {
459463
t.Fatalf(err.Error())
460464
}
@@ -465,9 +469,15 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
465469

466470
// Verify enode group creation failed given incorrect hostname
467471
enodeGroupStatusEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/enode", tunnel.Endpoint())
468-
groupStatus := digestAuth.NewRequest(username, password, "GET", enodeGroupStatusEndpoint, "")
469472
t.Logf(`enodeGroupStatusEndpoint: %s`, enodeGroupStatusEndpoint)
470-
resp, err = groupStatus.Execute()
473+
resp, err = client.R().
474+
Get(enodeGroupStatusEndpoint)
475+
if err != nil {
476+
t.Fatalf(err.Error())
477+
}
478+
defer resp.Body.Close()
479+
480+
_, err = io.ReadAll(resp.Body)
471481
if err != nil {
472482
t.Fatalf(err.Error())
473483
}

test/e2e/tls_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ func TestTLSEnabledWithNamedCert(t *testing.T) {
332332

333333
resp, err = client.R().
334334
Get("https://localhost:8002/manage/v2/certificate-templates/defaultTemplate?format=json")
335+
if err != nil {
336+
t.Fatalf(err.Error())
337+
}
335338
defer resp.Body.Close()
336339

337340
body, err := io.ReadAll(resp.Body)
@@ -342,6 +345,9 @@ func TestTLSEnabledWithNamedCert(t *testing.T) {
342345

343346
resp, err = client.R().
344347
Get("https://localhost:8002/manage/v2/certificates?format=json")
348+
if err != nil {
349+
t.Fatalf(err.Error())
350+
}
345351
defer resp.Body.Close()
346352

347353
body, err = io.ReadAll(resp.Body)
@@ -353,6 +359,9 @@ func TestTLSEnabledWithNamedCert(t *testing.T) {
353359
endpoint := strings.Replace("https://localhost:8002/manage/v2/certificates/certId?format=json", "certId", certID.Str, -1)
354360
resp, err = client.R().
355361
Get(endpoint)
362+
if err != nil {
363+
t.Fatalf(err.Error())
364+
}
356365
defer resp.Body.Close()
357366

358367
body, err = io.ReadAll(resp.Body)

test/e2e/upgrade_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
2121
"github.com/stretchr/testify/assert"
2222
"github.com/tidwall/gjson"
23-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2423
)
2524

2625
func TestHelmUpgrade(t *testing.T) {
@@ -233,9 +232,14 @@ func TestMLupgrade(t *testing.T) {
233232
clusterEndpoint := fmt.Sprintf("http://%s/manage/v2?format=json", tunnel.Endpoint())
234233
t.Logf(`Endpoint: %s`, clusterEndpoint)
235234

236-
getMLversion := digestAuth.NewRequest(username, password, "GET", clusterEndpoint, "")
235+
reqClient := req.C().
236+
SetCommonDigestAuth(username, password).
237+
SetCommonRetryCount(10).
238+
SetCommonRetryFixedInterval(10 * time.Second)
239+
240+
resp, err := reqClient.R().
241+
Get(clusterEndpoint)
237242

238-
resp, err := getMLversion.Execute()
239243
if err != nil {
240244
t.Fatalf(err.Error())
241245
}

test/hugePages/huge_pages_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/gruntwork-io/terratest/modules/helm"
1515
"github.com/gruntwork-io/terratest/modules/k8s"
1616
"github.com/gruntwork-io/terratest/modules/random"
17+
"github.com/imroc/req/v3"
1718
"github.com/marklogic/marklogic-kubernetes/test/testUtil"
1819
"github.com/stretchr/testify/assert"
19-
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
2020
)
2121

2222
func TestHugePagesSettings(t *testing.T) {
@@ -136,15 +136,23 @@ func TestHugePagesSettings(t *testing.T) {
136136
defer tunnel8002.Close()
137137
tunnel8002.ForwardPort(t)
138138
endpointManage := fmt.Sprintf("http://%s/manage/v2/logs?format=text&filename=ErrorLog.txt", tunnel8002.Endpoint())
139-
request := digestAuth.NewRequest(username, password, "GET", endpointManage, "")
140-
response, err := request.Execute()
139+
client := req.C().
140+
SetCommonDigestAuth(username, password).
141+
SetCommonRetryCount(10).
142+
SetCommonRetryFixedInterval(10 * time.Second)
143+
144+
resp, err := client.R().
145+
Get(endpointManage)
146+
141147
if err != nil {
142148
t.Fatalf(err.Error())
143149
}
144-
defer response.Body.Close()
145-
assert.Equal(t, 200, response.StatusCode)
150+
defer resp.Body.Close()
151+
assert.Equal(t, 200, resp.StatusCode)
146152

147-
body, err = io.ReadAll(response.Body)
153+
if body, err = io.ReadAll(resp.Body); err != nil {
154+
t.Fatalf(err.Error())
155+
}
148156
t.Log(string(body))
149157

150158
// Verify if Huge pages are configured on the MarkLogic node

0 commit comments

Comments
 (0)