Skip to content

Commit a575b44

Browse files
committed
refactor separate nodes test
1 parent 26e309b commit a575b44

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

test/e2e/separate_nodes_test.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package e2e
33
import (
44
"fmt"
55
"io/ioutil"
6-
"net/http"
76
"os"
87
"path/filepath"
98
"strings"
@@ -13,15 +12,12 @@ import (
1312
"github.com/gruntwork-io/terratest/modules/helm"
1413
"github.com/gruntwork-io/terratest/modules/k8s"
1514
"github.com/gruntwork-io/terratest/modules/random"
15+
"github.com/stretchr/testify/assert"
1616
"github.com/tidwall/gjson"
1717
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
1818
)
1919

2020
func TestSeparateEDnode(t *testing.T) {
21-
var resp *http.Response
22-
var body []byte
23-
var err error
24-
2521
username := "admin"
2622
password := "admin"
2723
imageRepo, repoPres := os.LookupEnv("dockerRepository")
@@ -86,11 +82,13 @@ func TestSeparateEDnode(t *testing.T) {
8682

8783
getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
8884

89-
if resp, err = getHostsDR.Execute(); err != nil {
85+
resp, err := getHostsDR.Execute()
86+
if err != nil {
9087
t.Fatalf(err.Error())
9188
}
9289
defer resp.Body.Close()
93-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
90+
body, err := ioutil.ReadAll(resp.Body)
91+
if err != nil {
9492
t.Fatalf(err.Error())
9593
}
9694
t.Logf("Get hosts response:\n" + string(body))
@@ -149,12 +147,14 @@ func TestSeparateEDnode(t *testing.T) {
149147

150148
getEnodeDR := digestAuth.NewRequest(username, password, "GET", enodeEndpoint, "")
151149

152-
if resp, err = getEnodeDR.Execute(); err != nil {
150+
resp, err = getEnodeDR.Execute()
151+
if err != nil {
153152
t.Fatalf(err.Error())
154153
}
155154
defer resp.Body.Close()
156155

157-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
156+
body, err = ioutil.ReadAll(resp.Body)
157+
if err != nil {
158158
t.Fatalf(err.Error())
159159
}
160160
t.Logf("Get enode group response:\n" + string(body))
@@ -169,10 +169,6 @@ func TestSeparateEDnode(t *testing.T) {
169169
}
170170

171171
func TestIncorrectBootsrapHostname(t *testing.T) {
172-
var resp *http.Response
173-
var body []byte
174-
var err error
175-
176172
username := "admin"
177173
password := "admin"
178174
imageRepo, repoPres := os.LookupEnv("dockerRepository")
@@ -184,7 +180,7 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
184180
dnodePodName := dnodeReleaseName + "-marklogic-0"
185181

186182
// Incorrect boostrap hostname for negative test
187-
bootstrapHost := "Incorrect Host Name"
183+
incorrectBootstrapHost := "Incorrect Host Name"
188184

189185
// Path to the helm chart we will test
190186
helmChartPath, e := filepath.Abs("../../charts")
@@ -240,19 +236,19 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
240236
t.Logf(`Endpoint: %s`, hostsEndpoint)
241237

242238
getHostsRequest := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
243-
244-
if resp, err = getHostsRequest.Execute(); err != nil {
239+
resp, err := getHostsRequest.Execute()
240+
if err != nil {
245241
t.Fatalf(err.Error())
246242
}
247243

248244
defer resp.Body.Close()
249245

250-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
246+
body, err := ioutil.ReadAll(resp.Body)
247+
if err != nil {
251248
t.Fatalf(err.Error())
252249
}
253250

254-
t.Logf("Response:\n" + string(body))
255-
t.Logf(`BootstrapHost: = %s`, bootstrapHost)
251+
t.Logf(`BootstrapHost: = %s`, incorrectBootstrapHost)
256252

257253
// Helm options for enode creation
258254
enodeOptions := &helm.Options{
@@ -265,7 +261,7 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
265261
"auth.adminUsername": username,
266262
"auth.adminPassword": password,
267263
"group.name": "enode",
268-
"bootstrapHostName": bootstrapHost,
264+
"bootstrapHostName": incorrectBootstrapHost,
269265
"logCollection.enabled": "false",
270266
},
271267
}
@@ -276,32 +272,34 @@ func TestIncorrectBootsrapHostname(t *testing.T) {
276272
// Give pod time to fail before checking if it did
277273
time.Sleep(20 * time.Second)
278274

275+
totalHostsJSON := gjson.Get(string(body), "host-default-list.list-items.list-count.value")
276+
277+
// Total hosts be one as second host should have failed to create
278+
if totalHostsJSON.Num != 1 {
279+
t.Errorf("Wrong number of hosts: %v instead of 1", totalHostsJSON.Num)
280+
}
281+
279282
// Verify clustering failed given incorrect hostname
280283
clusterStatusEndpoint := fmt.Sprintf("http://%s/manage/v2?view=status", tunnel.Endpoint())
281284
clusterStatus := digestAuth.NewRequest(username, password, "GET", clusterStatusEndpoint, "")
282285
t.Logf(`clusterStatusEndpoint: %s`, clusterStatusEndpoint)
283-
if resp, err = clusterStatus.Execute(); err != nil {
286+
resp, err = clusterStatus.Execute()
287+
if err != nil {
284288
t.Fatalf(err.Error())
285289
}
286-
totalHostsJSON := gjson.Get(string(body), "host-default-list.list-items.list-count.value")
287-
// Total hosts be one as second host should have failed to create
288-
if totalHostsJSON.Num != 1 {
289-
t.Errorf("Wrong number of hosts: %v instead of 1", totalHostsJSON.Num)
290+
_, err = ioutil.ReadAll(resp.Body)
291+
if err != nil {
292+
t.Fatalf(err.Error())
290293
}
291-
t.Logf("\nCluster Status Response:\n\n" + string(body))
292294

293295
// Verify enode group creation failed given incorrect hostname
294296
enodeGroupStatusEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/enode", tunnel.Endpoint())
295297
groupStatus := digestAuth.NewRequest(username, password, "GET", enodeGroupStatusEndpoint, "")
296298
t.Logf(`enodeGroupStatusEndpoint: %s`, enodeGroupStatusEndpoint)
297-
if resp, err = groupStatus.Execute(); err != nil {
299+
resp, err = groupStatus.Execute()
300+
if err != nil {
298301
t.Fatalf(err.Error())
299302
}
300-
if body, err = ioutil.ReadAll(resp.Body); err != nil {
301-
t.Fatalf(err.Error())
302-
}
303-
if !strings.Contains(string(body), "404") {
304-
t.Errorf("Enode group should not exist")
305-
}
306-
t.Logf("Enode group status response:\n" + string(body))
303+
// the request for enode should be 404
304+
assert.Equal(t, 404, resp.StatusCode)
307305
}

0 commit comments

Comments
 (0)