Skip to content

Commit afb7810

Browse files
nmueschSami Tabet
authored andcommitted
Add retry to logs test (#384)
* Add retry to logs test * Add retry to v2 tests
1 parent a16d729 commit afb7810

File tree

2 files changed

+106
-44
lines changed

2 files changed

+106
-44
lines changed

tests/api/v1/datadog/api_logs_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ func TestLogsList(t *testing.T) {
7373
// Find first log item
7474
logsRequest.SetLimit(1)
7575

76-
logsResponse, httpresp, err = Client(ctx).LogsApi.ListLogs(ctx).Body(logsRequest).Execute()
76+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
77+
logsResponse, httpresp, err = Client(ctx).LogsApi.ListLogs(ctx).Body(logsRequest).Execute()
78+
if err != nil {
79+
t.Fatalf("Error listing logs: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
80+
}
81+
return 200 == httpresp.StatusCode && len(logsResponse.GetNextLogId()) > 0
82+
})
7783
if err != nil {
78-
t.Fatalf("Error listing logs: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
84+
t.Fatalf("%v", err)
7985
}
86+
8087
assert.Equal(200, httpresp.StatusCode)
8188

8289
for _, log := range logsResponse.GetLogs() {
@@ -89,11 +96,17 @@ func TestLogsList(t *testing.T) {
8996
assert.True(len(logsResponse.GetNextLogId()) > 0)
9097
logsRequest.SetStartAt(logsResponse.GetNextLogId())
9198

92-
logsResponse, httpresp, err = Client(ctx).LogsApi.ListLogs(ctx).Body(logsRequest).Execute()
99+
assert.Equal(200, httpresp.StatusCode)
100+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
101+
logsResponse, httpresp, err = Client(ctx).LogsApi.ListLogs(ctx).Body(logsRequest).Execute()
102+
if err != nil {
103+
t.Fatalf("Error listing logs: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
104+
}
105+
return 200 == httpresp.StatusCode && len(logsResponse.GetLogs()) > 0
106+
})
93107
if err != nil {
94-
t.Fatalf("Error listing logs: Response %s: %v", err.(datadog.GenericOpenAPIError).Body(), err)
108+
t.Fatalf("%v", err)
95109
}
96-
assert.Equal(200, httpresp.StatusCode)
97110

98111
for _, log := range logsResponse.GetLogs() {
99112
content := log.GetContent()

tests/api/v2/datadog/api_logs_test.go

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ func TestLogsList(t *testing.T) {
6060

6161
// Sort works correctly
6262
request.SetSort(datadog.LOGSSORT_TIMESTAMP_ASCENDING)
63-
64-
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
63+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
64+
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
65+
if err != nil {
66+
t.Fatalf("Could not list logs: %v", err)
67+
}
68+
return 200 == httpResp.StatusCode && 2 == len(response.GetData())
69+
})
6570
if err != nil {
66-
t.Fatalf("Could not list logs: %v", err)
71+
t.Fatalf("%v", err)
6772
}
6873
assert.Equal(200, httpResp.StatusCode)
6974
assert.Equal(2, len(response.GetData()))
@@ -75,9 +80,15 @@ func TestLogsList(t *testing.T) {
7580

7681
request.SetSort(datadog.LOGSSORT_TIMESTAMP_DESCENDING)
7782

78-
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
83+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
84+
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
85+
if err != nil {
86+
t.Fatalf("Could not list logs: %v", err)
87+
}
88+
return 200 == httpResp.StatusCode && 2 == len(response.GetData())
89+
})
7990
if err != nil {
80-
t.Fatalf("Could not list logs: %v", err)
91+
t.Fatalf("%v", err)
8192
}
8293
assert.Equal(200, httpResp.StatusCode)
8394
assert.Equal(2, len(response.GetData()))
@@ -91,9 +102,15 @@ func TestLogsList(t *testing.T) {
91102
page := datadog.NewLogsListRequestPage()
92103
page.SetLimit(1)
93104
request.SetPage(*page)
94-
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
105+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
106+
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
107+
if err != nil {
108+
t.Fatalf("Could not list logs: %v", err)
109+
}
110+
return 200 == httpResp.StatusCode && 1 == len(response.GetData())
111+
})
95112
if err != nil {
96-
t.Fatalf("Could not list logs: %v", err)
113+
t.Fatalf("%v", err)
97114
}
98115

99116
assert.Equal(200, httpResp.StatusCode)
@@ -104,10 +121,17 @@ func TestLogsList(t *testing.T) {
104121
firstId := response.GetData()[0].GetId()
105122

106123
request.Page.SetCursor(cursor)
107-
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
124+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
125+
response, httpResp, err = client.LogsApi.ListLogs(ctx).Body(*request).Execute()
126+
if err != nil {
127+
t.Fatalf("Could not list logs: %v", err)
128+
}
129+
return 200 == httpResp.StatusCode && 1 == len(response.GetData())
130+
})
108131
if err != nil {
109-
t.Fatalf("Could not list logs: %v", err)
132+
t.Fatalf("%v", err)
110133
}
134+
111135
assert.Equal(200, httpResp.StatusCode)
112136
assert.Equal(1, len(response.GetData()))
113137
secondId := response.GetData()[0].GetId()
@@ -151,16 +175,22 @@ func TestLogsListGet(t *testing.T) {
151175
}
152176

153177
// Sort works correctly
154-
155-
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
156-
FilterQuery(*suffix).
157-
FilterFrom(from).
158-
FilterTo(to).
159-
Sort(datadog.LOGSSORT_TIMESTAMP_ASCENDING).
160-
Execute()
178+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
179+
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
180+
FilterQuery(*suffix).
181+
FilterFrom(from).
182+
FilterTo(to).
183+
Sort(datadog.LOGSSORT_TIMESTAMP_ASCENDING).
184+
Execute()
185+
if err != nil {
186+
t.Fatalf("Could not list logs: %v", err)
187+
}
188+
return 200 == httpResp.StatusCode && 2 == len(response.GetData())
189+
})
161190
if err != nil {
162-
t.Fatalf("Could not list logs: %v", err)
191+
t.Fatalf("%v", err)
163192
}
193+
164194
assert.Equal(200, httpResp.StatusCode)
165195
assert.Equal(2, len(response.GetData()))
166196
attributes := response.GetData()[0].GetAttributes()
@@ -169,15 +199,22 @@ func TestLogsListGet(t *testing.T) {
169199
attributes = response.GetData()[1].GetAttributes()
170200
assert.Equal("test-log-list-2 "+*suffix, attributes.GetMessage())
171201

172-
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
173-
FilterQuery(*suffix).
174-
FilterFrom(from).
175-
FilterTo(to).
176-
Sort(datadog.LOGSSORT_TIMESTAMP_DESCENDING).
177-
Execute()
202+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
203+
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
204+
FilterQuery(*suffix).
205+
FilterFrom(from).
206+
FilterTo(to).
207+
Sort(datadog.LOGSSORT_TIMESTAMP_DESCENDING).
208+
Execute()
209+
if err != nil {
210+
t.Fatalf("Could not list logs: %v", err)
211+
}
212+
return 200 == httpResp.StatusCode && 2 == len(response.GetData())
213+
})
178214
if err != nil {
179-
t.Fatalf("Could not list logs: %v", err)
215+
t.Fatalf("%v", err)
180216
}
217+
181218
assert.Equal(200, httpResp.StatusCode)
182219
assert.Equal(2, len(response.GetData()))
183220
attributes = response.GetData()[0].GetAttributes()
@@ -187,14 +224,20 @@ func TestLogsListGet(t *testing.T) {
187224
assert.Equal("test-log-list-1 "+*suffix, attributes.GetMessage())
188225

189226
// Paging
190-
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
191-
FilterQuery(*suffix).
192-
FilterFrom(from).
193-
FilterTo(to).
194-
PageLimit(1).
195-
Execute()
227+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
228+
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
229+
FilterQuery(*suffix).
230+
FilterFrom(from).
231+
FilterTo(to).
232+
PageLimit(1).
233+
Execute()
234+
if err != nil {
235+
t.Fatalf("Could not list logs: %v", err)
236+
}
237+
return 200 == httpResp.StatusCode && 1 == len(response.GetData())
238+
})
196239
if err != nil {
197-
t.Fatalf("Could not list logs: %v", err)
240+
t.Fatalf("%v", err)
198241
}
199242

200243
assert.Equal(200, httpResp.StatusCode)
@@ -204,15 +247,21 @@ func TestLogsListGet(t *testing.T) {
204247
cursor := respPage.GetAfter()
205248
firstId := response.GetData()[0].GetId()
206249

207-
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
208-
FilterQuery(*suffix).
209-
FilterFrom(from).
210-
FilterTo(to).
211-
PageLimit(1).
212-
PageCursor(cursor).
213-
Execute()
250+
err = tests.Retry(time.Duration(5)*time.Second, 30, func() bool {
251+
response, httpResp, err = client.LogsApi.ListLogsGet(ctx).
252+
FilterQuery(*suffix).
253+
FilterFrom(from).
254+
FilterTo(to).
255+
PageLimit(1).
256+
PageCursor(cursor).
257+
Execute()
258+
if err != nil {
259+
t.Fatalf("Could not list logs: %v", err)
260+
}
261+
return 200 == httpResp.StatusCode && 1 == len(response.GetData())
262+
})
214263
if err != nil {
215-
t.Fatalf("Could not list logs: %v", err)
264+
t.Fatalf("%v", err)
216265
}
217266
assert.Equal(200, httpResp.StatusCode)
218267
assert.Equal(1, len(response.GetData()))

0 commit comments

Comments
 (0)