Skip to content

Commit f6b7235

Browse files
author
Peng Zhou
committed
add more checks for nil pointer to prevent segmentation fault issue
1 parent 78724ed commit f6b7235

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

test/e2e/clustering_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,22 @@ func TestClusterJoin(t *testing.T) {
7878
SetRetryCount(5).
7979
SetRetryFixedInterval(10 * time.Second).
8080
AddRetryCondition(func(resp *req.Response, err error) bool {
81+
if resp == nil || err != nil {
82+
t.Logf("error in AddRetryCondition: %s", err.Error())
83+
return true
84+
}
85+
if resp.Response == nil {
86+
t.Log("Could not get the Response Object, Retrying...")
87+
return true
88+
}
89+
if resp.Body == nil {
90+
t.Log("Could not get the body for the response, Retrying...")
91+
return true
92+
}
8193
body, err := io.ReadAll(resp.Body)
82-
if err != nil {
83-
t.Logf("error: %s", err.Error())
94+
if body == nil || err != nil {
95+
t.Logf("error in read response body: %s", err.Error())
96+
return true
8497
}
8598
totalHosts := gjson.Get(string(body), `host-default-list.list-items.list-count.value`)
8699
numOfHosts = int(totalHosts.Num)

test/e2e/scaling_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,22 @@ func TestHelmScaleUp(t *testing.T) {
9595
SetRetryCount(3).
9696
SetRetryFixedInterval(10 * time.Second).
9797
AddRetryCondition(func(resp *req.Response, err error) bool {
98-
98+
if resp == nil || err != nil {
99+
t.Logf("error in AddRetryCondition: %s", err.Error())
100+
return true
101+
}
102+
if resp.Response == nil {
103+
t.Log("Could not get the Response Object, Retrying...")
104+
return true
105+
}
106+
if resp.Body == nil {
107+
t.Log("Could not get the body for the response, Retrying...")
108+
return true
109+
}
99110
body, err := io.ReadAll(resp.Body)
100-
if err != nil {
101-
t.Logf("error: %s", err.Error())
111+
if body == nil || err != nil {
112+
t.Logf("error in read response body: %s", err.Error())
113+
return true
102114
}
103115
totalHosts := gjson.Get(string(body), `host-status-list.status-list-summary.total-hosts.value`)
104116
numOfHosts = int(totalHosts.Num)
@@ -198,10 +210,22 @@ func TestHelmScaleDown(t *testing.T) {
198210
SetRetryCount(3).
199211
SetRetryFixedInterval(10 * time.Second).
200212
AddRetryCondition(func(resp *req.Response, err error) bool {
201-
213+
if resp == nil || err != nil {
214+
t.Logf("error in AddRetryCondition: %s", err.Error())
215+
return true
216+
}
217+
if resp.Response == nil {
218+
t.Log("Could not get the Response Object, Retrying...")
219+
return true
220+
}
221+
if resp.Body == nil {
222+
t.Log("Could not get the body for the response, Retrying...")
223+
return true
224+
}
202225
body, err := io.ReadAll(resp.Body)
203-
if err != nil {
204-
t.Logf("error: %s", err.Error())
226+
if body == nil || err != nil {
227+
t.Logf("error in read response body: %s", err.Error())
228+
return true
205229
}
206230
totalOfflineHosts := gjson.Get(string(body), `host-status-list.status-list-summary.total-hosts-offline.value`)
207231
numOfHostsOffline = int(totalOfflineHosts.Num)

test/e2e/separate_nodes_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,27 @@ func TestSeparateEDnode(t *testing.T) {
184184

185185
enodeHostCountJSON := 0
186186
client := req.C()
187-
reqResp, reqErr := client.R().
187+
_, reqErr := client.R().
188188
SetDigestAuth(username, password).
189189
SetRetryCount(3).
190190
SetRetryFixedInterval(10 * time.Second).
191191
AddRetryCondition(func(resp *req.Response, err error) bool {
192+
if resp == nil || err != nil {
193+
t.Logf("error in AddRetryCondition: %s", err.Error())
194+
return true
195+
}
196+
if resp.Response == nil {
197+
t.Log("Could not get the Response Object, Retrying...")
198+
return true
199+
}
200+
if resp.Body == nil {
201+
t.Log("Could not get the body for the response, Retrying...")
202+
return true
203+
}
192204
body, err := io.ReadAll(resp.Body)
193-
if err != nil {
194-
t.Logf("error: %s", err.Error())
205+
if body == nil || err != nil {
206+
t.Logf("error in read response body: %s", err.Error())
207+
return true
195208
}
196209
totalHosts := gjson.Get(string(body), `group-default.relations.relation-group.#(typeref="hosts").relation-count.value`)
197210
enodeHostCountJSON = int(totalHosts.Num)
@@ -201,7 +214,6 @@ func TestSeparateEDnode(t *testing.T) {
201214
return enodeHostCountJSON != 2
202215
}).
203216
Get("http://localhost:8002/manage/v2/groups/enode?format=json")
204-
defer reqResp.Body.Close()
205217

206218
if reqErr != nil {
207219
t.Fatalf(reqErr.Error())

0 commit comments

Comments
 (0)