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