@@ -13,6 +13,7 @@ import (
13
13
"io"
14
14
"io/ioutil"
15
15
"net/http"
16
+ "strconv"
16
17
"strings"
17
18
"testing"
18
19
@@ -253,6 +254,64 @@ func TestSendFileMultipleChunksUsesBackingIndex(t *testing.T) {
253
254
require .NoError (t , err )
254
255
}
255
256
257
+ func TestSendFileHandlesDisorderedChunks (t * testing.T ) {
258
+ buf := bytes .NewBuffer (nil )
259
+
260
+ fakeBulk := itesting .NewMockBulk ()
261
+ esClient , esMock := mockESClient (t )
262
+
263
+ const fileID = "xyz"
264
+ idx := fmt .Sprintf (FileDataIndexPattern , "endpoint" ) + "-0001"
265
+ sampleDocBody := hexDecode ("A7665F696E64657878212E666C6565742D66696C6564656C69766572792D646174612D656E64706F696E74635F69646578797A2E30685F76657273696F6E01675F7365715F6E6F016D5F7072696D6172795F7465726D0165666F756E64F5666669656C6473A164646174618142ABCD" )
266
+
267
+ chunks := []file.ChunkInfo {
268
+ {Index : idx , ID : fileID + ".20" , Pos : 20 },
269
+ {Index : idx , ID : fileID + ".21" , Pos : 21 },
270
+ {Index : idx , ID : fileID + ".22" , Pos : 22 },
271
+ {Index : idx , ID : fileID + ".9" , Pos : 9 },
272
+ {Index : idx , ID : fileID + ".10" , Pos : 10 },
273
+ {Index : idx , ID : fileID + ".11" , Pos : 11 },
274
+ {Index : idx , ID : fileID + ".12" , Pos : 12 },
275
+ {Index : idx , ID : fileID + ".13" , Pos : 13 },
276
+ {Index : idx , ID : fileID + ".14" , Pos : 14 },
277
+ {Index : idx , ID : fileID + ".15" , Pos : 15 },
278
+ {Index : idx , ID : fileID + ".16" , Pos : 16 },
279
+ {Index : idx , ID : fileID + ".17" , Pos : 17 },
280
+ {Index : idx , ID : fileID + ".18" , Pos : 18 },
281
+ {Index : idx , ID : fileID + ".19" , Pos : 19 },
282
+ {Index : idx , ID : fileID + ".0" , Pos : 0 },
283
+ {Index : idx , ID : fileID + ".1" , Pos : 1 },
284
+ {Index : idx , ID : fileID + ".2" , Pos : 2 },
285
+ {Index : idx , ID : fileID + ".3" , Pos : 3 },
286
+ {Index : idx , ID : fileID + ".4" , Pos : 4 },
287
+ {Index : idx , ID : fileID + ".5" , Pos : 5 },
288
+ {Index : idx , ID : fileID + ".6" , Pos : 6 },
289
+ {Index : idx , ID : fileID + ".7" , Pos : 7 },
290
+ {Index : idx , ID : fileID + ".8" , Pos : 8 },
291
+ }
292
+
293
+ expectedIdx := 0
294
+
295
+ esMock .RoundTripFn = func (req * http.Request ) (* http.Response , error ) {
296
+
297
+ // Parse out the chunk number requested
298
+ parts := strings .Split (req .URL .Path , "/" ) // ["", ".fleet-filedelivery-data-endpoint-0001", "_doc", "xyz.1"]
299
+ docIdx := strings .TrimPrefix (parts [3 ], fileID + "." )
300
+ docnum , err := strconv .Atoi (docIdx )
301
+ require .NoError (t , err )
302
+
303
+ // should be our expected increasing counter
304
+ assert .Equal (t , expectedIdx , docnum )
305
+ expectedIdx += 1
306
+
307
+ return sendBodyBytes (sampleDocBody ), nil
308
+ }
309
+
310
+ d := New (esClient , fakeBulk , - 1 )
311
+ err := d .SendFile (context .Background (), zerolog.Logger {}, buf , chunks , fileID )
312
+ require .NoError (t , err )
313
+ }
314
+
256
315
/*
257
316
Setup to convert a *elasticsearch.Client as a harmless mock
258
317
by replacing the Transport to nowhere
0 commit comments