Skip to content

Commit f9035bd

Browse files
Merge pull request #41 from ilanRosenbaum/feature/CLD-599
CLD-599: Implement test case scenarios for k8s E/D node story
2 parents 4a03482 + c20633b commit f9035bd

File tree

2 files changed

+207
-39
lines changed

2 files changed

+207
-39
lines changed

test/e2e/install_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package e2e
33
import (
44
"crypto/tls"
55
"fmt"
6+
"io/ioutil"
7+
"net/http"
68
"os"
79
"path/filepath"
810
"strings"
@@ -13,6 +15,8 @@ import (
1315
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
1416
"github.com/gruntwork-io/terratest/modules/k8s"
1517
"github.com/gruntwork-io/terratest/modules/random"
18+
"github.com/tidwall/gjson"
19+
digestAuth "github.com/xinsnake/go-http-digest-auth-client"
1620
)
1721

1822
func TestHelmInstall(t *testing.T) {
@@ -23,6 +27,11 @@ func TestHelmInstall(t *testing.T) {
2327
}
2428
imageRepo, repoPres := os.LookupEnv("dockerRepository")
2529
imageTag, tagPres := os.LookupEnv("dockerVersion")
30+
username := "admin"
31+
password := "admin"
32+
var resp *http.Response
33+
var body []byte
34+
var err error
2635

2736
if !repoPres {
2837
imageRepo = "marklogic-centos/marklogic-server-centos"
@@ -40,9 +49,11 @@ func TestHelmInstall(t *testing.T) {
4049
KubectlOptions: kubectlOptions,
4150
SetValues: map[string]string{
4251
"persistence.enabled": "false",
43-
"replicaCount": "1",
52+
"replicaCount": "2",
4453
"image.repository": imageRepo,
4554
"image.tag": imageTag,
55+
"auth.adminUsername": username,
56+
"auth.adminPassword": password,
4657
"logCollection.enabled": "false",
4758
},
4859
}
@@ -78,4 +89,27 @@ func TestHelmInstall(t *testing.T) {
7889
return statusCode == 200
7990
},
8091
)
92+
93+
tunnel8002 := k8s.NewTunnel(
94+
kubectlOptions, k8s.ResourceTypePod, podName, 8002, 8002)
95+
defer tunnel8002.Close()
96+
tunnel8002.ForwardPort(t)
97+
98+
// Verify no groups beyond enode were created/modified
99+
groupStatusEndpoint := fmt.Sprintf("http://%s/manage/v2/groups?format=json", tunnel8002.Endpoint())
100+
groupStatus := digestAuth.NewRequest(username, password, "GET", groupStatusEndpoint, "")
101+
t.Logf(`groupStatusEndpoint: %s`, groupStatusEndpoint)
102+
if resp, err = groupStatus.Execute(); err != nil {
103+
t.Fatalf(err.Error())
104+
}
105+
if body, err = ioutil.ReadAll(resp.Body); err != nil {
106+
t.Fatalf(err.Error())
107+
}
108+
groupQuantityJSON := gjson.Get(string(body), "group-default-list.list-items.list-count.value")
109+
110+
if groupQuantityJSON.Num != 1 {
111+
t.Errorf("Only one group should exist, instead %v groups exist", groupQuantityJSON.Num)
112+
}
113+
114+
t.Logf("Groups status response:\n" + string(body))
81115
}

test/e2e/separate_nodes_test.go

Lines changed: 172 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,27 @@ import (
1818
)
1919

2020
func TestSeparateEDnode(t *testing.T) {
21-
// Path to the helm chart we will test
22-
helmChartPath, e := filepath.Abs("../../charts")
23-
if e != nil {
24-
t.Fatalf(e.Error())
25-
}
26-
username := "admin"
27-
password := "admin"
2821
var resp *http.Response
2922
var body []byte
3023
var err error
3124

25+
username := "admin"
26+
password := "admin"
3227
imageRepo, repoPres := os.LookupEnv("dockerRepository")
3328
imageTag, tagPres := os.LookupEnv("dockerVersion")
29+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
30+
kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
31+
dnodeReleaseName := "test-dnode-group"
32+
enodeReleaseName := "test-enode-group"
33+
dnodePodName := dnodeReleaseName + "-marklogic-0"
34+
enodePodName0 := enodeReleaseName + "-marklogic-0"
35+
enodePodName1 := enodeReleaseName + "-marklogic-1"
36+
37+
// Path to the helm chart we will test
38+
helmChartPath, e := filepath.Abs("../../charts")
39+
if e != nil {
40+
t.Fatalf(e.Error())
41+
}
3442

3543
if !repoPres {
3644
imageRepo = "marklogic-centos/marklogic-server-centos"
@@ -42,8 +50,6 @@ func TestSeparateEDnode(t *testing.T) {
4250
t.Logf("No imageTag variable present, setting to default value: " + imageTag)
4351
}
4452

45-
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
46-
kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
4753
options := &helm.Options{
4854
KubectlOptions: kubectlOptions,
4955
SetValues: map[string]string{
@@ -64,39 +70,35 @@ func TestSeparateEDnode(t *testing.T) {
6470
defer t.Logf("====Deleting namespace: " + namespaceName)
6571
defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName)
6672

67-
dnodeReleaseName := "test-dnode-group"
68-
t.Logf("====Installing Helm Chart" + dnodeReleaseName)
73+
t.Logf("====Installing Helm Chart " + dnodeReleaseName)
6974
helm.Install(t, options, helmChartPath, dnodeReleaseName)
7075

71-
dnodePodName := dnodeReleaseName + "-marklogic-0"
72-
73-
// wait until the pod is in Ready status
76+
// wait until the pod is in ready status
7477
k8s.WaitUntilPodAvailable(t, kubectlOptions, dnodePodName, 10, 20*time.Second)
7578

76-
time.Sleep(10 * time.Second)
7779
tunnel := k8s.NewTunnel(
7880
kubectlOptions, k8s.ResourceTypePod, dnodePodName, 8002, 8002)
7981
defer tunnel.Close()
8082
tunnel.ForwardPort(t)
83+
8184
hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint())
8285
t.Logf(`Endpoint: %s`, hostsEndpoint)
8386

84-
dr := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
87+
getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
8588

86-
if resp, err = dr.Execute(); err != nil {
89+
if resp, err = getHostsDR.Execute(); err != nil {
8790
t.Fatalf(err.Error())
8891
}
8992
defer resp.Body.Close()
90-
9193
if body, err = ioutil.ReadAll(resp.Body); err != nil {
9294
t.Fatalf(err.Error())
9395
}
94-
t.Logf("Response:\n" + string(body))
95-
bootstrapHost := gjson.Get(string(body), `host-default-list.list-items.list-item.#(roleref="bootstrap").nameref`)
96-
t.Logf(`BootstrapHost: = %s`, bootstrapHost)
96+
t.Logf("Get hosts response:\n" + string(body))
9797

98+
bootstrapHostJSON := gjson.Get(string(body), `host-default-list.list-items.list-item.#(roleref="bootstrap").nameref`)
99+
t.Logf(`BootstrapHost: = %s`, bootstrapHostJSON)
98100
// verify bootstrap host exists on the cluster
99-
if bootstrapHost.String() == "" {
101+
if bootstrapHostJSON.Str == "" {
100102
t.Errorf("Bootstrap does not exists on cluster")
101103
}
102104

@@ -110,64 +112,196 @@ func TestSeparateEDnode(t *testing.T) {
110112
"auth.adminUsername": username,
111113
"auth.adminPassword": password,
112114
"group.name": "enode",
113-
"bootstrapHostName": bootstrapHost.String(),
115+
"bootstrapHostName": bootstrapHostJSON.Str,
114116
"logCollection.enabled": "false",
115117
},
116118
}
117-
enodeReleaseName := "test-enode-group"
118119
t.Logf("====Installing Helm Chart " + enodeReleaseName)
119120
helm.Install(t, enodeOptions, helmChartPath, enodeReleaseName)
120121

121-
enodePodName0 := enodeReleaseName + "-marklogic-0"
122-
123122
// wait until the first enode pod is in Ready status
124123
k8s.WaitUntilPodAvailable(t, kubectlOptions, enodePodName0, 45, 20*time.Second)
125124

126125
groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups", tunnel.Endpoint())
127126
t.Logf(`Endpoint: %s`, groupEndpoint)
128127

129-
drGroups := digestAuth.NewRequest(username, password, "GET", groupEndpoint, "")
128+
getGroupsDR := digestAuth.NewRequest(username, password, "GET", groupEndpoint, "")
130129

131-
if resp, err = drGroups.Execute(); err != nil {
130+
if resp, err = getGroupsDR.Execute(); err != nil {
132131
t.Fatalf(err.Error())
133132
}
134133
defer resp.Body.Close()
135-
136134
if body, err = ioutil.ReadAll(resp.Body); err != nil {
137135
t.Fatalf(err.Error())
138136
}
139-
t.Logf("Response:\n" + string(body))
137+
t.Logf("Groups status response:\n" + string(body))
140138

141139
// verify groups dnode, enode exists on the cluster
142140
if !strings.Contains(string(body), "<nameref>dnode</nameref>") && !strings.Contains(string(body), "<nameref>enode</nameref>") {
143141
t.Errorf("Groups does not exists on cluster")
144142
}
145143

146-
enodePodName1 := enodeReleaseName + "-marklogic-1"
147-
148144
// wait until the second enode pod is in Ready status
149145
k8s.WaitUntilPodAvailable(t, kubectlOptions, enodePodName1, 45, 20*time.Second)
150146

151147
enodeEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/enode?format=json", tunnel.Endpoint())
152148
t.Logf(`Endpoint: %s`, enodeEndpoint)
153149

154-
drEnode := digestAuth.NewRequest(username, password, "GET", enodeEndpoint, "")
150+
getEnodeDR := digestAuth.NewRequest(username, password, "GET", enodeEndpoint, "")
155151

156-
if resp, err = drEnode.Execute(); err != nil {
152+
if resp, err = getEnodeDR.Execute(); err != nil {
157153
t.Fatalf(err.Error())
158154
}
159155
defer resp.Body.Close()
160156

161157
if body, err = ioutil.ReadAll(resp.Body); err != nil {
162158
t.Fatalf(err.Error())
163159
}
164-
t.Logf("Response:\n" + string(body))
160+
t.Logf("Get enode group response:\n" + string(body))
165161

166-
enodeHostCount := gjson.Get(string(body), `group-default.relations.relation-group.#(typeref="hosts").relation-count.value`)
167-
t.Logf(`enodeHostCount: = %s`, enodeHostCount)
162+
enodeHostCountJSON := gjson.Get(string(body), `group-default.relations.relation-group.#(typeref="hosts").relation-count.value`)
163+
t.Logf(`enodeHostCount: = %s`, enodeHostCountJSON)
168164

169165
// verify bootstrap host exists on the cluster
170-
if !strings.Contains(enodeHostCount.String(), "2") {
166+
if enodeHostCountJSON.Num != 2 {
171167
t.Errorf("enode hosts does not exists on cluster")
172168
}
173169
}
170+
171+
func TestIncorrectBootsrapHostname(t *testing.T) {
172+
var resp *http.Response
173+
var body []byte
174+
var err error
175+
176+
username := "admin"
177+
password := "admin"
178+
imageRepo, repoPres := os.LookupEnv("dockerRepository")
179+
imageTag, tagPres := os.LookupEnv("dockerVersion")
180+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
181+
kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
182+
dnodeReleaseName := "test-dnode-group"
183+
enodeReleaseName := "test-enode-group"
184+
dnodePodName := dnodeReleaseName + "-marklogic-0"
185+
186+
// Incorrect boostrap hostname for negative test
187+
bootstrapHost := "Incorrect Host Name"
188+
189+
// Path to the helm chart we will test
190+
helmChartPath, e := filepath.Abs("../../charts")
191+
192+
if e != nil {
193+
t.Fatalf(e.Error())
194+
}
195+
196+
if !repoPres {
197+
imageRepo = "marklogic-centos/marklogic-server-centos"
198+
t.Logf("No imageRepo variable present, setting to default value: " + imageRepo)
199+
}
200+
201+
if !tagPres {
202+
imageTag = "10-internal"
203+
t.Logf("No imageTag variable present, setting to default value: " + imageTag)
204+
}
205+
206+
// Helm options for dnode creation
207+
options := &helm.Options{
208+
KubectlOptions: kubectlOptions,
209+
SetValues: map[string]string{
210+
"persistence.enabled": "false",
211+
"replicaCount": "1",
212+
"image.repository": imageRepo,
213+
"image.tag": imageTag,
214+
"auth.adminUsername": username,
215+
"auth.adminPassword": password,
216+
"group.name": "dnode",
217+
"logCollection.enabled": "false",
218+
},
219+
}
220+
221+
t.Logf("====Creating namespace: " + namespaceName)
222+
k8s.CreateNamespace(t, kubectlOptions, namespaceName)
223+
224+
defer t.Logf("====Deleting namespace: " + namespaceName)
225+
defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName)
226+
227+
t.Logf("====Installing D Node Helm Chart " + dnodeReleaseName)
228+
helm.Install(t, options, helmChartPath, dnodeReleaseName)
229+
230+
// wait until the pod is in ready status
231+
k8s.WaitUntilPodAvailable(t, kubectlOptions, dnodePodName, 10, 20*time.Second)
232+
233+
tunnel := k8s.NewTunnel(
234+
kubectlOptions, k8s.ResourceTypePod, dnodePodName, 8002, 8002)
235+
236+
defer tunnel.Close()
237+
238+
tunnel.ForwardPort(t)
239+
hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint())
240+
t.Logf(`Endpoint: %s`, hostsEndpoint)
241+
242+
getHostsRequest := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "")
243+
244+
if resp, err = getHostsRequest.Execute(); err != nil {
245+
t.Fatalf(err.Error())
246+
}
247+
248+
defer resp.Body.Close()
249+
250+
if body, err = ioutil.ReadAll(resp.Body); err != nil {
251+
t.Fatalf(err.Error())
252+
}
253+
254+
t.Logf("Response:\n" + string(body))
255+
t.Logf(`BootstrapHost: = %s`, bootstrapHost)
256+
257+
// Helm options for enode creation
258+
enodeOptions := &helm.Options{
259+
KubectlOptions: kubectlOptions,
260+
SetValues: map[string]string{
261+
"persistence.enabled": "false",
262+
"replicaCount": "1",
263+
"image.repository": imageRepo,
264+
"image.tag": imageTag,
265+
"auth.adminUsername": username,
266+
"auth.adminPassword": password,
267+
"group.name": "enode",
268+
"bootstrapHostName": bootstrapHost,
269+
"logCollection.enabled": "false",
270+
},
271+
}
272+
273+
t.Logf("====Installing E Node Helm Chart " + enodeReleaseName)
274+
helm.Install(t, enodeOptions, helmChartPath, enodeReleaseName)
275+
276+
// Give pod time to fail before checking if it did
277+
time.Sleep(20 * time.Second)
278+
279+
// Verify clustering failed given incorrect hostname
280+
clusterStatusEndpoint := fmt.Sprintf("http://%s/manage/v2?view=status", tunnel.Endpoint())
281+
clusterStatus := digestAuth.NewRequest(username, password, "GET", clusterStatusEndpoint, "")
282+
t.Logf(`clusterStatusEndpoint: %s`, clusterStatusEndpoint)
283+
if resp, err = clusterStatus.Execute(); err != nil {
284+
t.Fatalf(err.Error())
285+
}
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+
}
291+
t.Logf("\nCluster Status Response:\n\n" + string(body))
292+
293+
// Verify enode group creation failed given incorrect hostname
294+
enodeGroupStatusEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/enode", tunnel.Endpoint())
295+
groupStatus := digestAuth.NewRequest(username, password, "GET", enodeGroupStatusEndpoint, "")
296+
t.Logf(`enodeGroupStatusEndpoint: %s`, enodeGroupStatusEndpoint)
297+
if resp, err = groupStatus.Execute(); err != nil {
298+
t.Fatalf(err.Error())
299+
}
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))
307+
}

0 commit comments

Comments
 (0)