|
| 1 | +package parser_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "path" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "cloud.google.com/go/bigquery" |
| 11 | + "cloud.google.com/go/civil" |
| 12 | + "github.com/go-test/deep" |
| 13 | + "github.com/m-lab/etl/parser" |
| 14 | + "github.com/m-lab/etl/schema" |
| 15 | + "github.com/m-lab/go/rtx" |
| 16 | + "github.com/m-lab/uuid-annotator/annotator" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + hopAnnotation1Filename = "20210818T174432Z_1e0b318cf3c2_91.189.88.152.json" |
| 21 | + hopAnnotation1GCSPath = "gs://archive-measurement-lab/ndt/hopannotation1/2021/07/30/" |
| 22 | +) |
| 23 | + |
| 24 | +func TestHopAnnotation1Parser_ParseAndInsert(t *testing.T) { |
| 25 | + ins := newInMemorySink() |
| 26 | + n := parser.NewHopAnnotation1Parser(ins, "test", "_suffix", &fakeAnnotator{}) |
| 27 | + |
| 28 | + data, err := ioutil.ReadFile(path.Join("testdata/HopAnnotation1/", hopAnnotation1Filename)) |
| 29 | + rtx.Must(err, "failed to load test file") |
| 30 | + |
| 31 | + date := civil.Date{Year: 2021, Month: 07, Day: 30} |
| 32 | + |
| 33 | + meta := map[string]bigquery.Value{ |
| 34 | + "filename": path.Join(hopAnnotation1GCSPath, hopAnnotation1Filename), |
| 35 | + "date": date, |
| 36 | + } |
| 37 | + |
| 38 | + if err := n.ParseAndInsert(meta, hopAnnotation1Filename, data); err != nil { |
| 39 | + t.Errorf("HopAnnotation1Parser.ParseAndInsert() error = %v, wantErr %v", err, true) |
| 40 | + } |
| 41 | + |
| 42 | + if n.Accepted() != 1 { |
| 43 | + t.Fatal("Failed to insert snaplog data", ins) |
| 44 | + } |
| 45 | + n.Flush() |
| 46 | + |
| 47 | + row := ins.data[0].(*schema.HopAnnotation1Row) |
| 48 | + |
| 49 | + expectedParseInfo := schema.ParseInfo{ |
| 50 | + Version: "https://github.com/m-lab/etl/tree/foobar", |
| 51 | + Time: row.Parser.Time, |
| 52 | + ArchiveURL: path.Join(hopAnnotation1GCSPath, hopAnnotation1Filename), |
| 53 | + Filename: hopAnnotation1Filename, |
| 54 | + Priority: 0, |
| 55 | + GitCommit: "12345678", |
| 56 | + } |
| 57 | + |
| 58 | + expectedGeolocation := annotator.Geolocation{ |
| 59 | + ContinentCode: "EU", |
| 60 | + CountryCode: "GB", |
| 61 | + CountryName: "United Kingdom", |
| 62 | + Subdivision1ISOCode: "ENG", |
| 63 | + Subdivision1Name: "England", |
| 64 | + City: "London", |
| 65 | + PostalCode: "EC2V", |
| 66 | + Latitude: 51.5095, |
| 67 | + Longitude: -0.0955, |
| 68 | + AccuracyRadiusKm: 200, |
| 69 | + } |
| 70 | + |
| 71 | + expectedNetwork := annotator.Network{ |
| 72 | + CIDR: "91.189.88.0/21", |
| 73 | + ASNumber: 41231, |
| 74 | + ASName: "Canonical Group Limited", |
| 75 | + Systems: []annotator.System{ |
| 76 | + {ASNs: []uint32{41231}}, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + expectedAnnotations := annotator.ClientAnnotations{ |
| 81 | + Geo: &expectedGeolocation, |
| 82 | + Network: &expectedNetwork, |
| 83 | + } |
| 84 | + |
| 85 | + expectedRaw := schema.HopAnnotation1{ |
| 86 | + ID: "20210818_1e0b318cf3c2_91.189.88.152", |
| 87 | + Timestamp: time.Date(2021, 8, 18, 17, 44, 32, 0, time.UTC), |
| 88 | + Annotations: &expectedAnnotations, |
| 89 | + } |
| 90 | + |
| 91 | + expectedHopAnnotation1Row := schema.HopAnnotation1Row{ |
| 92 | + ID: "20210818_1e0b318cf3c2_91.189.88.152", |
| 93 | + Parser: expectedParseInfo, |
| 94 | + Date: date, |
| 95 | + Raw: &expectedRaw, |
| 96 | + } |
| 97 | + |
| 98 | + if diff := deep.Equal(row, &expectedHopAnnotation1Row); diff != nil { |
| 99 | + t.Errorf("HopAnnotation1Parser.ParseAndInsert() different row: %s", strings.Join(diff, "\n")) |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +func TestHopAnnotation1_IsParsable(t *testing.T) { |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + testName string |
| 108 | + want bool |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "success-hopannotation1", |
| 112 | + testName: hopAnnotation1Filename, |
| 113 | + want: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "error-bad-extension", |
| 117 | + testName: "badfile.badextension", |
| 118 | + want: false, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for _, tt := range tests { |
| 123 | + t.Run(tt.name, func(t *testing.T) { |
| 124 | + data, err := ioutil.ReadFile(path.Join(`testdata/HopAnnotation1/`, tt.testName)) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf(err.Error()) |
| 127 | + } |
| 128 | + p := &parser.HopAnnotation1Parser{} |
| 129 | + _, got := p.IsParsable(tt.testName, data) |
| 130 | + if got != tt.want { |
| 131 | + t.Errorf("HopAnnotation1Parser.IsParsable() got = %v, want %v", got, tt.want) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments