99 "time"
1010
1111 "cloud.google.com/go/bigquery"
12+ "github.com/go-test/deep"
13+ "github.com/m-lab/annotation-service/api"
14+ v2 "github.com/m-lab/annotation-service/api/v2"
1215 "github.com/m-lab/etl/parser"
1316 "github.com/m-lab/etl/schema"
1417)
@@ -252,18 +255,83 @@ func TestParseLegacyFormatData(t *testing.T) {
252255}
253256
254257func TestParseJSONL (t * testing.T ) {
255- rawData , err := ioutil .ReadFile ("testdata/PT/20190927T070859Z_ndt-qtfh8_1565996043_0000000000003B64.jsonl" )
258+ m := map [string ]* api.Annotations {
259+ "91.213.30.229" : & api.Annotations {
260+ Geo : & api.GeolocationIP {
261+ ContinentCode : "NA" ,
262+ CountryCode : "US" ,
263+ Latitude : 1.0 ,
264+ Longitude : 2.0 ,
265+ },
266+ Network : & api.ASData {
267+ ASNumber : 1234 ,
268+ Systems : []api.System {
269+ {ASNs : []uint32 {1234 }},
270+ },
271+ },
272+ },
273+ "91.169.126.135" : & api.Annotations {
274+ Geo : & api.GeolocationIP {
275+ ContinentCode : "EU" ,
276+ CountryCode : "DE" ,
277+ Latitude : 3.0 ,
278+ Longitude : 4.0 ,
279+ },
280+ Network : & api.ASData {
281+ ASNumber : 4321 ,
282+ Systems : []api.System {
283+ {ASNs : []uint32 {4321 }},
284+ },
285+ },
286+ },
287+ }
288+ ins := newInMemoryInserter ()
289+ pt := parser .NewPTParser (ins , newFakeAnnotator (m ))
290+
291+ filename := "testdata/PT/20190927T070859Z_ndt-qtfh8_1565996043_0000000000003B64.jsonl"
292+ rawData , err := ioutil .ReadFile (filename )
256293 if err != nil {
257294 t .Fatalf (err .Error ())
258295 return
259296 }
260- ptTest , err := parser .ParseJSONL ("20190927T070859Z_ndt-qtfh8_1565996043_0000000000003B64.jsonl" , []byte (rawData ), "" , "" )
297+
298+ url := "gs://archive-measurement-lab/ndt/traceroute/2019/09/27/20190927T000540.410989Z-traceroute-mlab2-nuq07-ndt.tgz"
299+ meta := map [string ]bigquery.Value {"filename" : url }
300+ err = pt .ParseAndInsert (meta , filename , rawData )
261301 if err != nil {
262302 t .Fatalf (err .Error ())
263303 }
264304
305+ if pt .NumRowsForTest () != 1 {
306+ fmt .Println (pt .NumRowsForTest ())
307+ t .Fatalf ("The data is not inserted, in buffer now." )
308+ }
309+ pt .Flush ()
310+
311+ if len (ins .data ) != 1 {
312+ fmt .Println (len (ins .data ))
313+ t .Fatalf ("Number of rows in inserter is wrong." )
314+ }
315+
316+ ptTest := ins .data [0 ].(* schema.PTTest )
317+ if ptTest .Parseinfo .TaskFileName != url {
318+ t .Fatalf ("Wrong TaskFilenName; got %q, want %q" , ptTest .Parseinfo .TaskFileName , url )
319+ }
320+
265321 if ptTest .UUID != "ndt-qtfh8_1565996043_0000000000003B64" {
266- t .Fatalf ("UUID parsing error %s" , ptTest .UUID )
322+ t .Fatalf ("Wrong UUID; got %q, want %q" , ptTest .UUID , "ndt-qtfh8_1565996043_0000000000003B64" )
323+ }
324+
325+ // Verify the client and server annotations match.
326+ cx := v2 .ConvertAnnotationsToClientAnnotations (m ["91.169.126.135" ])
327+ if diff := deep .Equal (& ptTest .ClientX , cx ); diff != nil {
328+ t .Errorf ("ClientX annotation does not match; %#v" , diff )
329+ }
330+ sx := v2 .ConvertAnnotationsToServerAnnotations (m ["91.213.30.229" ])
331+ sx .Site = "nuq07"
332+ sx .Machine = "mlab2"
333+ if diff := deep .Equal (& ptTest .ServerX , sx ); diff != nil {
334+ t .Errorf ("ServerX annotation does not match; %#v" , diff )
267335 }
268336}
269337
@@ -320,12 +388,43 @@ func TestParse(t *testing.T) {
320388
321389func TestAnnotateAndPutAsync (t * testing.T ) {
322390 ins := newInMemoryInserter ()
323- pt := parser .NewPTParser (ins , & fakeAnnotator {})
391+ m := map [string ]* api.Annotations {
392+ "172.17.94.34" : & api.Annotations {
393+ Geo : & api.GeolocationIP {
394+ ContinentCode : "NA" ,
395+ CountryCode : "US" ,
396+ Latitude : 1.0 ,
397+ Longitude : 2.0 ,
398+ },
399+ Network : & api.ASData {
400+ ASNumber : 1234 ,
401+ Systems : []api.System {
402+ {ASNs : []uint32 {1234 }},
403+ },
404+ },
405+ },
406+ "74.125.224.100" : & api.Annotations {
407+ Geo : & api.GeolocationIP {
408+ ContinentCode : "EU" ,
409+ CountryCode : "DE" ,
410+ Latitude : 3.0 ,
411+ Longitude : 4.0 ,
412+ },
413+ Network : & api.ASData {
414+ ASNumber : 4321 ,
415+ Systems : []api.System {
416+ {ASNs : []uint32 {4321 }},
417+ },
418+ },
419+ },
420+ }
421+ pt := parser .NewPTParser (ins , newFakeAnnotator (m ))
324422 rawData , err := ioutil .ReadFile ("testdata/PT/20170320T23:53:10Z-172.17.94.34-33456-74.125.224.100-33457.paris" )
325423 if err != nil {
326424 t .Fatalf ("cannot read testdata." )
327425 }
328- meta := map [string ]bigquery.Value {"filename" : "gs://fake-bucket/fake-archive.tgz" }
426+ url := "gs://archive-measurement-lab/ndt/traceroute/2017/03/20/20170320T000540.410989Z-paris-traceroute-mlab4-nuq07-ndt.tgz"
427+ meta := map [string ]bigquery.Value {"filename" : url }
329428 err = pt .ParseAndInsert (meta , "testdata/PT/20170320T23:53:10Z-172.17.94.34-33456-74.125.224.100-33457.paris" , rawData )
330429 if err != nil {
331430 t .Fatalf (err .Error ())
@@ -336,24 +435,70 @@ func TestAnnotateAndPutAsync(t *testing.T) {
336435 t .Fatalf ("Number of rows in PT table is wrong." )
337436 }
338437 pt .AnnotateAndPutAsync ("traceroute" )
339- //pt.Inserter.Flush()
438+
340439 if len (ins .data ) != 1 {
341440 fmt .Println (len (ins .data ))
342441 t .Fatalf ("Number of rows in inserter is wrong." )
343442 }
344- if ins .data [0 ].(* schema.PTTest ).Parseinfo .TaskFileName != "gs://fake-bucket/fake-archive.tgz" {
345- t .Fatalf ("Task filename is wrong." )
443+ ptTest := ins .data [0 ].(* schema.PTTest )
444+ if ptTest .Parseinfo .TaskFileName != url {
445+ t .Fatalf ("Wrong TaskFilenName; got %q, want %q" , ptTest .Parseinfo .TaskFileName , url )
446+ }
447+
448+ // Verify the client and server annotations match.
449+ cx := v2 .ConvertAnnotationsToClientAnnotations (m ["74.125.224.100" ])
450+ if diff := deep .Equal (& ptTest .ClientX , cx ); diff != nil {
451+ t .Errorf ("ClientX annotation does not match; %#v" , diff )
452+ }
453+ sx := v2 .ConvertAnnotationsToServerAnnotations (m ["172.17.94.34" ])
454+ sx .Site = "nuq07"
455+ sx .Machine = "mlab4"
456+ if diff := deep .Equal (& ptTest .ServerX , sx ); diff != nil {
457+ t .Errorf ("ServerX annotation does not match; %#v" , diff )
346458 }
347459}
348460
349461func TestParseAndInsert (t * testing.T ) {
462+
463+ m := map [string ]* api.Annotations {
464+ "2.80.132.33" : & api.Annotations {
465+ Geo : & api.GeolocationIP {
466+ ContinentCode : "NA" ,
467+ CountryCode : "US" ,
468+ Latitude : 1.0 ,
469+ Longitude : 2.0 ,
470+ },
471+ Network : & api.ASData {
472+ ASNumber : 1234 ,
473+ Systems : []api.System {
474+ {ASNs : []uint32 {1234 }},
475+ },
476+ },
477+ },
478+ "91.239.96.102" : & api.Annotations {
479+ Geo : & api.GeolocationIP {
480+ ContinentCode : "NA" ,
481+ CountryCode : "US" ,
482+ Latitude : 1.0 ,
483+ Longitude : 2.0 ,
484+ },
485+ Network : & api.ASData {
486+ ASNumber : 1234 ,
487+ Systems : []api.System {
488+ {ASNs : []uint32 {1234 }},
489+ },
490+ },
491+ },
492+ }
493+
350494 ins := newInMemoryInserter ()
351- pt := parser .NewPTParser (ins , & fakeAnnotator {} )
495+ pt := parser .NewPTParser (ins , newFakeAnnotator ( m ) )
352496 rawData , err := ioutil .ReadFile ("testdata/PT/20130524T00:04:44Z_ALL5729.paris" )
353497 if err != nil {
354498 t .Fatalf ("cannot read testdata." )
355499 }
356- meta := map [string ]bigquery.Value {"filename" : "gs://fake-bucket/fake-archive.tgz" }
500+ url := "gs://archive-measurement-lab/paris-traceroute/2013/05/24/20130524T000000Z-mlab3-akl01-paris-traceroute-0000.tgz"
501+ meta := map [string ]bigquery.Value {"filename" : url }
357502 err = pt .ParseAndInsert (meta , "testdata/PT/20130524T00:04:44Z_ALL5729.paris" , rawData )
358503 if err != nil {
359504 t .Fatalf (err .Error ())
@@ -369,13 +514,26 @@ func TestParseAndInsert(t *testing.T) {
369514 fmt .Println (len (ins .data ))
370515 t .Fatalf ("Number of rows in inserter is wrong." )
371516 }
372- if ins .data [0 ].(* schema.PTTest ).Parseinfo .TaskFileName != "gs://fake-bucket/fake-archive.tgz" {
517+ if ins .data [0 ].(* schema.PTTest ).Parseinfo .TaskFileName != url {
373518 t .Fatalf ("Task filename is wrong." )
374519 }
375520 // echo -n 2013-05-24T00:04:44Z-91.239.96.102-2.80.132.33 | openssl dgst -binary -md5 | base64 | tr '/+' '_-' | tr -d '='
376521 if ins .data [0 ].(* schema.PTTest ).UUID != "R9_wGx1-cSmqtSAt5aQtNg" {
377522 t .Fatalf ("UUID is wrong; got %q, want %q" , ins .data [0 ].(* schema.PTTest ).UUID , "R9_wGx1-cSmqtSAt5aQtNg" )
378523 }
524+ p := ins .data [0 ].(* schema.PTTest )
525+
526+ // Verify the client and server annotations match.
527+ cx := v2 .ConvertAnnotationsToClientAnnotations (m ["91.239.96.102" ])
528+ if diff := deep .Equal (& p .ClientX , cx ); diff != nil {
529+ t .Errorf ("ClientX annotation does not match; %#v" , diff )
530+ }
531+ sx := v2 .ConvertAnnotationsToServerAnnotations (m ["2.80.132.33" ])
532+ sx .Site = "akl01"
533+ sx .Machine = "mlab3"
534+ if diff := deep .Equal (& p .ServerX , sx ); diff != nil {
535+ t .Errorf ("ServerX annotation does not match; %#v" , diff )
536+ }
379537}
380538
381539func TestProcessLastTests (t * testing.T ) {
0 commit comments