Skip to content

Commit a4579a1

Browse files
pengzhoumlPeng Zhou
andauthored
MLE-15631 Improve Tests (#279)
* MLE-15631: Fix nil pointer issue in AddRetryCondition * MLE-15631: Fix Group Change Test * refactor Install Test * add retry to configure_group * fix lint issue * fix linting issue * fix more retry logic --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent 5f7f0ca commit a4579a1

11 files changed

+184
-152
lines changed

charts/templates/configmap-scripts.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ data:
588588
response_code=$( \
589589
curl -s --anyauth \
590590
--user ${MARKLOGIC_ADMIN_USERNAME}:${MARKLOGIC_ADMIN_PASSWORD} \
591-
-w '%{http_code}' \
591+
-w '%{http_code}' --retry 5 \
592592
-X PUT \
593593
-H "Content-type: application/json" \
594594
$LOCAL_HTTPS_OPTION -d "${group_cfg}" \
@@ -613,11 +613,11 @@ data:
613613
info "creating group for other Helm Chart"
614614
615615
# Create a group if group is not already exits
616-
GROUP_RESP_CODE=$( curl --anyauth -m 20 -s -o /dev/null -w "%{http_code}" $HTTPS_OPTION -X GET $HTTP_PROTOCOL://${MARKLOGIC_BOOTSTRAP_HOST}:8002/manage/v2/groups/${MARKLOGIC_GROUP} --anyauth --user ${MARKLOGIC_ADMIN_USERNAME}:${MARKLOGIC_ADMIN_PASSWORD} )
616+
GROUP_RESP_CODE=$( curl --anyauth --retry 5 -m 20 -s -o /dev/null -w "%{http_code}" $HTTPS_OPTION -X GET $HTTP_PROTOCOL://${MARKLOGIC_BOOTSTRAP_HOST}:8002/manage/v2/groups/${MARKLOGIC_GROUP} --anyauth --user ${MARKLOGIC_ADMIN_USERNAME}:${MARKLOGIC_ADMIN_PASSWORD} )
617617
if [[ ${GROUP_RESP_CODE} -eq 200 ]]; then
618618
info "Skipping creation of group $MARKLOGIC_GROUP as it already exists on the MarkLogic cluster."
619619
else
620-
res_code=$(curl --anyauth --user ${MARKLOGIC_ADMIN_USERNAME}:${MARKLOGIC_ADMIN_PASSWORD} $HTTPS_OPTION -m 20 -s -w '%{http_code}' -X POST -d "${group_cfg}" -H "Content-type: application/json" $HTTP_PROTOCOL://${MARKLOGIC_BOOTSTRAP_HOST}:8002/manage/v2/groups)
620+
res_code=$(curl --anyauth --retry 5 --user ${MARKLOGIC_ADMIN_USERNAME}:${MARKLOGIC_ADMIN_PASSWORD} $HTTPS_OPTION -m 20 -s -w '%{http_code}' -X POST -d "${group_cfg}" -H "Content-type: application/json" $HTTP_PROTOCOL://${MARKLOGIC_BOOTSTRAP_HOST}:8002/manage/v2/groups)
621621
if [[ ${res_code} -eq 201 ]]; then
622622
log "Info: [initContainer] Successfully configured group $MARKLOGIC_GROUP on the MarkLogic cluster."
623623
else

test/e2e/backup_restore_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,35 @@ func RunRequests(t *testing.T, client *req.Client, dbReq string, hostsEndpoint s
100100
var retryFn = (func(resp *req.Response, err error) bool {
101101
if err != nil {
102102
t.Fatalf(err.Error())
103+
return true
104+
}
105+
if resp == nil || resp.Body == nil {
106+
t.Fatalf("error in getting response body")
107+
return true
103108
}
104109
body, err = io.ReadAll(resp.Body)
105110
if err != nil {
106-
t.Fatalf(err.Error())
111+
t.Fatalf("error reading response body, %s", err.Error())
112+
return true
107113
}
108114
result = (string(body))
109-
return true
115+
return false
110116
})
111117

112118
if operation == "backup-status" {
113119
retryFn = (func(resp *req.Response, err error) bool {
114120
if err != nil {
115121
t.Fatalf(err.Error())
116122
}
117-
body, _ := io.ReadAll(resp.Body)
123+
if resp == nil || resp.Body == nil {
124+
t.Fatalf("error in getting response body")
125+
return true
126+
}
127+
body, err := io.ReadAll(resp.Body)
128+
if body == nil || err != nil {
129+
t.Fatalf("error reading response body")
130+
return true
131+
}
118132
status = (gjson.Get(string(body), `status`)).Str
119133
if status != "completed" {
120134
fmt.Println("Waiting for backup to be completed")

test/e2e/clustering_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,16 @@ func TestClusterJoin(t *testing.T) {
131131
SetRetryCount(5).
132132
SetRetryFixedInterval(10 * time.Second).
133133
AddRetryCondition(func(resp *req.Response, err error) bool {
134-
if resp == nil || err != nil {
134+
if err != nil {
135135
t.Logf("error in AddRetryCondition: %s", err.Error())
136136
return true
137137
}
138-
if resp.Response == nil {
139-
t.Log("Could not get the Response Object, Retrying...")
140-
return true
141-
}
142-
if resp.Body == nil {
143-
t.Log("Could not get the body for the response, Retrying...")
138+
if resp == nil || resp.Body == nil {
139+
t.Log("Could not get the Response Body, Retrying...")
144140
return true
145141
}
146142
body, err := io.ReadAll(resp.Body)
147-
if body == nil || err != nil {
143+
if err != nil {
148144
t.Logf("error in read response body: %s", err.Error())
149145
return true
150146
}

test/e2e/group_cfg_test.go

Lines changed: 102 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66
"os"
77
"path/filepath"
8-
"strconv"
98
"strings"
109
"testing"
1110
"time"
@@ -19,27 +18,50 @@ import (
1918
"github.com/tidwall/gjson"
2019
)
2120

22-
func VerifyGrpNameChng(t *testing.T, groupEndpoint string, newGroupName string) (int, error) {
21+
func VerifyGroupChange(t *testing.T, groupEndpoint string, newGroupName string) (bool, error) {
2322
client := req.C().
2423
SetCommonDigestAuth("admin", "admin").
25-
SetCommonRetryCount(10).
24+
SetCommonRetryCount(5).
2625
SetCommonRetryFixedInterval(10 * time.Second)
2726

2827
t.Logf(`Endpoint: %s`, groupEndpoint)
29-
strJSONData := fmt.Sprintf(`{"group-name":"%s"}`, newGroupName)
3028

31-
resp, err := client.R().
29+
groupChanged := false
30+
31+
_, err := client.R().
3232
SetContentType("application/json").
33-
SetBodyJsonString(strJSONData).
34-
Put(groupEndpoint)
33+
AddRetryCondition(func(resp *req.Response, err error) bool {
34+
if err != nil {
35+
t.Logf("error in getting group config: %s", err.Error())
36+
return true
37+
}
38+
if resp == nil || resp.Body == nil {
39+
t.Logf("error in getting response body")
40+
return true
41+
}
42+
body, err := io.ReadAll(resp.Body)
43+
if body == nil || err != nil {
44+
t.Logf("error in read response body")
45+
return true
46+
}
47+
groupName := gjson.Get(string(body), `group-name`)
48+
t.Logf("current group name: %s", groupName)
49+
if groupName.Str != newGroupName {
50+
t.Logf("group name is not updated yet. retrying...")
51+
return true
52+
}
53+
groupChanged = true
54+
return false
55+
}).
56+
Get(groupEndpoint)
3557
if err != nil {
3658
t.Fatal(err.Error())
37-
return (resp.GetStatusCode()), err
59+
return false, err
3860
}
39-
return resp.GetStatusCode(), resp.Err
61+
return groupChanged, nil
4062
}
4163

42-
func TestSingleGrpCfgChng(t *testing.T) {
64+
func TestSingleGroupChange(t *testing.T) {
4365
// Path to the helm chart we will test
4466
helmChartPath, e := filepath.Abs("../../charts")
4567
if e != nil {
@@ -87,38 +109,44 @@ func TestSingleGrpCfgChng(t *testing.T) {
87109
t.Logf("====Installing Helm Chart")
88110
podZeroName := testUtil.HelmInstall(t, options, releaseName, kubectlOptions, helmChartPath)
89111

90-
// wait until the pod is in Ready status
91112
k8s.WaitUntilPodAvailable(t, kubectlOptions, podZeroName, 15, 20*time.Second)
113+
114+
newGroupName := "new_group"
115+
116+
helmUpgradeOptions := &helm.Options{
117+
KubectlOptions: kubectlOptions,
118+
SetValues: map[string]string{
119+
"group.name": newGroupName,
120+
},
121+
}
122+
helm.Upgrade(t, helmUpgradeOptions, helmChartPath, releaseName)
123+
124+
k8s.RunKubectl(t, kubectlOptions, "delete", "pod", podZeroName)
125+
126+
k8s.WaitUntilPodAvailable(t, kubectlOptions, podZeroName, 15, 20*time.Second)
127+
128+
// wait until the pod is in Ready status
92129
tunnel := k8s.NewTunnel(
93130
kubectlOptions, k8s.ResourceTypePod, podZeroName, 8002, 8002)
94131
defer tunnel.Close()
95132
tunnel.ForwardPort(t)
96133

97134
// change the group name for dnode and verify it passes
98-
newgroupName := "newDefault"
99-
t.Logf("====Test updating group name for %s to %s", groupName, newgroupName)
100-
groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), groupName)
101-
responseCode, err := VerifyGrpNameChng(t, groupEndpoint, newgroupName)
135+
t.Logf("====Test updating group name for %s to %s", groupName, newGroupName)
136+
groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties?format=json", tunnel.Endpoint(), newGroupName)
137+
groupChangedResult, err := VerifyGroupChange(t, groupEndpoint, newGroupName)
102138
if err != nil {
103-
t.Fatalf(err.Error())
104-
}
105-
if responseCode != 204 {
106-
t.Fatal("Failed to change group name")
139+
t.Fatalf("Error in changing group name: %s", err.Error())
107140
}
141+
assert.Equal(t, true, groupChangedResult, "Group name change failed")
108142
}
109143

110-
func TestMultiGroupCfgChng(t *testing.T) {
144+
func TestMultipleGroupChange(t *testing.T) {
111145
username := "admin"
112146
password := "admin"
113147
imageRepo, repoPres := os.LookupEnv("dockerRepository")
114148
imageTag, tagPres := os.LookupEnv("dockerVersion")
115149
var initialChartVersion string
116-
upgradeHelm, _ := os.LookupEnv("upgradeTest")
117-
runUpgradeTest, _ := strconv.ParseBool(upgradeHelm)
118-
if runUpgradeTest {
119-
initialChartVersion, _ = os.LookupEnv("initialChartVersion")
120-
t.Logf("====Setting initial Helm chart version: %s", initialChartVersion)
121-
}
122150
namespaceName := "ml-" + strings.ToLower(random.UniqueId())
123151
kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
124152
dnodeGrpName := "dnode"
@@ -169,54 +197,19 @@ func TestMultiGroupCfgChng(t *testing.T) {
169197

170198
// wait until the pod is in ready status
171199
k8s.WaitUntilPodAvailable(t, kubectlOptions, dnodePodName, 15, 20*time.Second)
172-
tunnel := k8s.NewTunnel(
173-
kubectlOptions, k8s.ResourceTypePod, dnodePodName, 8002, 8002)
174-
defer tunnel.Close()
175-
tunnel.ForwardPort(t)
176-
177-
// change the group name for dnode and verify it passes
178-
newDnodeGrpName := "newDnode"
179-
t.Logf("====Test updating group name for %s to %s", dnodeGrpName, newDnodeGrpName)
180-
groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), dnodeGrpName)
181-
responseCode, err := VerifyGrpNameChng(t, groupEndpoint, newDnodeGrpName)
182-
if err != nil {
183-
t.Fatalf(err.Error())
184-
}
185-
if responseCode != 204 {
186-
t.Fatal("Failed to change group name")
187-
}
188-
189-
hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint())
190-
t.Logf(`Endpoint: %s`, hostsEndpoint)
191-
192-
client := req.C().
193-
SetCommonDigestAuth(username, password).
194-
SetCommonRetryCount(10).
195-
SetCommonRetryFixedInterval(10 * time.Second)
196-
resp, err := client.R().
197-
Get(hostsEndpoint)
198-
if err != nil {
199-
t.Fatalf(err.Error())
200-
}
201-
defer resp.Body.Close()
202-
body, err := io.ReadAll(resp.Body)
203-
if err != nil {
204-
t.Fatalf(err.Error())
205-
}
206-
bootstrapHost := gjson.Get(string(body), `host-default-list.list-items.list-item.#(roleref="bootstrap").nameref`)
207-
t.Logf("bootstrapHost: %s", bootstrapHost)
208200

201+
bootstrapHost := fmt.Sprintf("%s-0.%s.%s.svc.cluster.local", dnodeReleaseName, dnodeReleaseName, namespaceName)
209202
enodeOptions := &helm.Options{
210203
KubectlOptions: kubectlOptions,
211204
SetValues: map[string]string{
212205
"persistence.enabled": "true",
213-
"replicaCount": "2",
206+
"replicaCount": "1",
214207
"image.repository": imageRepo,
215208
"image.tag": imageTag,
216209
"auth.adminUsername": username,
217210
"auth.adminPassword": password,
218211
"group.name": enodeGrpName,
219-
"bootstrapHostName": bootstrapHost.Str,
212+
"bootstrapHostName": bootstrapHost,
220213
"group.enableXdqpSsl": "false",
221214
"logCollection.enabled": "false",
222215
},
@@ -227,24 +220,54 @@ func TestMultiGroupCfgChng(t *testing.T) {
227220
// wait until the first enode pod is in Ready status
228221
k8s.WaitUntilPodAvailable(t, kubectlOptions, enodePodName0, 15, 20*time.Second)
229222

230-
// change the enode group name to a existing group name in the cluster and verify it fails
231-
t.Logf("====Test updating group name for %s to an existing group name(%s) should fail", enodeGrpName, newDnodeGrpName)
232-
groupEndpoint = fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), enodeGrpName)
233-
responseCode, err = VerifyGrpNameChng(t, groupEndpoint, newDnodeGrpName)
234-
if err != nil {
235-
t.Fatalf(err.Error())
223+
newDnodeGroupName := "newDnode"
224+
newEnodeGroupName := "newEnode"
225+
226+
helmUpgradeOptionsDnode := &helm.Options{
227+
KubectlOptions: kubectlOptions,
228+
SetValues: map[string]string{
229+
"group.name": newDnodeGroupName,
230+
},
236231
}
237-
assert.Equal(t, 400, responseCode)
232+
helm.Upgrade(t, helmUpgradeOptionsDnode, helmChartPath, dnodeReleaseName)
238233

239-
// change the enode group name to a new group name and verify it passes
240-
newEnodeGrpName := "newEnode"
241-
t.Logf("====Test updating group name for %s to %s", enodeGrpName, newEnodeGrpName)
242-
groupEndpoint = fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), enodeGrpName)
243-
responseCode, err = VerifyGrpNameChng(t, groupEndpoint, newEnodeGrpName)
234+
k8s.RunKubectl(t, kubectlOptions, "delete", "pod", dnodePodName)
235+
236+
k8s.WaitUntilPodAvailable(t, kubectlOptions, dnodePodName, 15, 20*time.Second)
237+
238+
tunnel := k8s.NewTunnel(
239+
kubectlOptions, k8s.ResourceTypePod, dnodePodName, 8002, 8002)
240+
defer tunnel.Close()
241+
tunnel.ForwardPort(t)
242+
243+
// change the group name for dnode and verify it passes
244+
t.Logf("====Test updating group name for %s to %s", dnodeGrpName, newDnodeGroupName)
245+
groupDnodeEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties?format=json", tunnel.Endpoint(), newDnodeGroupName)
246+
groupChangedResult, err := VerifyGroupChange(t, groupDnodeEndpoint, newDnodeGroupName)
244247
if err != nil {
245-
t.Fatalf(err.Error())
248+
t.Fatalf("Error in changing group name: %s", err.Error())
249+
}
250+
assert.Equal(t, true, groupChangedResult, "dnode Group name change failed")
251+
252+
helmUpgradeOptionsEnode := &helm.Options{
253+
KubectlOptions: kubectlOptions,
254+
SetValues: map[string]string{
255+
"group.name": newEnodeGroupName,
256+
},
246257
}
247-
if responseCode != 204 {
248-
t.Fatal("Failed to change group name")
258+
helm.Upgrade(t, helmUpgradeOptionsEnode, helmChartPath, enodeReleaseName)
259+
260+
k8s.RunKubectl(t, kubectlOptions, "delete", "pod", enodePodName0)
261+
262+
k8s.WaitUntilPodAvailable(t, kubectlOptions, enodePodName0, 15, 20*time.Second)
263+
264+
// change the group name for dnode and verify it passes
265+
t.Logf("====Test updating group name for %s to %s", enodeGrpName, newEnodeGroupName)
266+
groupEnodeEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties?format=json", tunnel.Endpoint(), newEnodeGroupName)
267+
groupChangedResult, err = VerifyGroupChange(t, groupEnodeEndpoint, newEnodeGroupName)
268+
if err != nil {
269+
t.Fatalf("Error in changing group name: %s", err.Error())
249270
}
271+
assert.Equal(t, true, groupChangedResult, "enode Group name change failed")
272+
250273
}

0 commit comments

Comments
 (0)