Skip to content

Commit

Permalink
Merge d9d0b5c into 18ef0ef
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 committed Apr 9, 2019
2 parents 18ef0ef + d9d0b5c commit 8cf35a7
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 195 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#
# yamllint disable rule:line-length

# CGO broken in go 1.12.2 with trusty, so this forces xenial, which should work.
# https://github.com/golang/go/issues/31293
dist: xenial

language: go

# Without this, annotator.sh fails, related to gcloud.
Expand Down Expand Up @@ -86,7 +90,6 @@ deploy:
- provider: script
script:
$TRAVIS_BUILD_DIR/travis/deploy_app_legacy_keyfile.sh mlab-sandbox /tmp/mlab-sandbox.json $TRAVIS_BUILD_DIR annotator.yaml
&& $TRAVIS_BUILD_DIR/cron.sh mlab-sandbox
skip_cleanup: true
on:
repo: m-lab/annotation-service
Expand All @@ -98,7 +101,6 @@ deploy:
- provider: script
script:
$TRAVIS_BUILD_DIR/travis/deploy_app_legacy_keyfile.sh mlab-staging /tmp/mlab-staging.json $TRAVIS_BUILD_DIR annotator.yaml
&& $TRAVIS_BUILD_DIR/cron.sh mlab-staging
skip_cleanup: true
on:
repo: m-lab/annotation-service
Expand All @@ -109,7 +111,6 @@ deploy:
- provider: script
script:
$TRAVIS_BUILD_DIR/travis/deploy_app_legacy_keyfile.sh mlab-oti /tmp/mlab-oti.json $TRAVIS_BUILD_DIR annotator.yaml
&& $TRAVIS_BUILD_DIR/cron.sh mlab-oti
skip_cleanup: true
on:
repo: m-lab/annotation-service
Expand Down
40 changes: 0 additions & 40 deletions geolite2/geo-ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,46 +194,6 @@ func checkCaps(str, field string) (string, error) {
return "", errors.New(output)
}

// IsEqualIPNodes returns nil if two nodes are equal
// Used by the search package
func IsEqualIPNodes(expected, node IPNode) error {
if !((node.IPAddressLow).Equal(expected.IPAddressLow)) {
output := strings.Join([]string{"IPAddress Low inconsistent\ngot:", node.IPAddressLow.String(), " \nwanted:", expected.IPAddressLow.String()}, "")
log.Println(output)
return errors.New(output)
}
if !((node.IPAddressHigh).Equal(expected.IPAddressHigh)) {
output := strings.Join([]string{"IPAddressHigh inconsistent\ngot:", node.IPAddressHigh.String(), " \nwanted:", expected.IPAddressHigh.String()}, "")
log.Println(output)
return errors.New(output)
}
if node.LocationIndex != expected.LocationIndex {
output := strings.Join([]string{"LocationIndex inconsistent\ngot:", strconv.Itoa(node.LocationIndex), " \nwanted:", strconv.Itoa(expected.LocationIndex)}, "")
log.Println(output)
return errors.New(output)
}
if node.PostalCode != expected.PostalCode {
output := strings.Join([]string{"PostalCode inconsistent\ngot:", node.PostalCode, " \nwanted:", expected.PostalCode}, "")
log.Println(output)
return errors.New(output)
}
if node.Latitude != expected.Latitude {
output := strings.Join([]string{"Latitude inconsistent\ngot:", floatToString(node.Latitude), " \nwanted:", floatToString(expected.Latitude)}, "")
log.Println(output)
return errors.New(output)
}
if node.Longitude != expected.Longitude {
output := strings.Join([]string{"Longitude inconsistent\ngot:", floatToString(node.Longitude), " \nwanted:", floatToString(expected.Longitude)}, "")
log.Println(output)
return errors.New(output)
}
return nil
}

func floatToString(num float64) string {
return strconv.FormatFloat(num, 'f', 6, 64)
}

// TODO(gfr) What are list and stack?
// handleStack finds the proper place in the stack for the new node.
// `stack` holds a stack of nested IP ranges not yet resolved.
Expand Down
43 changes: 40 additions & 3 deletions geolite2/geo-ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func assertAnnotator(f api.Annotator) {
// Returns nil if two IP Lists are equal
func isEqualIPLists(listComp, list []geolite2.IPNode) error {
for index, element := range list {
err := geolite2.IsEqualIPNodes(element, listComp[index])
err := isEqualIPNodes(element, listComp[index])
if err != nil {
return err
}
Expand Down Expand Up @@ -84,6 +84,43 @@ func isEqualLocLists(list, listComp []geolite2.LocationNode) error {
return nil
}

// isEqualIPNodes returns nil if two nodes are equal
// Used by the search package
func isEqualIPNodes(expected, node geolite2.IPNode) error {
if !((node.IPAddressLow).Equal(expected.IPAddressLow)) {
output := strings.Join([]string{"IPAddress Low inconsistent\ngot:", node.IPAddressLow.String(), " \nwanted:", expected.IPAddressLow.String()}, "")
log.Println(output)
return errors.New(output)
}
if !((node.IPAddressHigh).Equal(expected.IPAddressHigh)) {
output := strings.Join([]string{"IPAddressHigh inconsistent\ngot:", node.IPAddressHigh.String(), " \nwanted:", expected.IPAddressHigh.String()}, "")
log.Println(output)
return errors.New(output)
}
if node.LocationIndex != expected.LocationIndex {
output := strings.Join([]string{"LocationIndex inconsistent\ngot:", strconv.Itoa(node.LocationIndex), " \nwanted:", strconv.Itoa(expected.LocationIndex)}, "")
log.Println(output)
return errors.New(output)
}
if node.PostalCode != expected.PostalCode {
output := strings.Join([]string{"PostalCode inconsistent\ngot:", node.PostalCode, " \nwanted:", expected.PostalCode}, "")
log.Println(output)
return errors.New(output)
}
if node.Latitude != expected.Latitude {
strconv.FormatFloat(node.Latitude, 'f', 6, 64)
output := fmt.Sprintf("Latitude inconsistent\ngot: %.6f\nwanted: %.6f\n", node.Latitude, expected.Latitude)
log.Println(output)
return errors.New(output)
}
if node.Longitude != expected.Longitude {
output := fmt.Sprintf("Longitude inconsistent\ngot: %.6f\nwanted: %.6f\n", node.Longitude, expected.Longitude)
log.Println(output)
return errors.New(output)
}
return nil
}

func TestPopulateLocationData(t *testing.T) {
tests := []struct {
node geolite2.IPNode
Expand Down Expand Up @@ -169,7 +206,7 @@ func TestGeoLite2(t *testing.T) {
log.Println(errBin.Error(), "vs", errLin.Error())
t.Errorf("Failed Error")
}
if geolite2.IsEqualIPNodes(ipBin, ipLin) != nil {
if isEqualIPNodes(ipBin, ipLin) != nil {
log.Println("bad ", ipBin, ipLin)
t.Errorf("Failed Binary vs Linear")
}
Expand All @@ -189,7 +226,7 @@ func TestGeoLite2(t *testing.T) {
log.Println(errBin.Error(), "vs", errLin.Error())
t.Errorf("Failed Error")
}
if geolite2.IsEqualIPNodes(ipBin, ipLin) != nil {
if isEqualIPNodes(ipBin, ipLin) != nil {
log.Println("bad ", ipBin, ipLin)
t.Errorf("Failed Binary vs Linear")
}
Expand Down
227 changes: 227 additions & 0 deletions geolite2v2/g2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package geolite2v2_test

// TODO - migrate these tests to geolite2v2 before removing geolite2 package

import (
"archive/zip"
"log"
"net"
"strings"
"testing"

"github.com/go-test/deep"

"github.com/m-lab/annotation-service/geolite2v2"
"github.com/m-lab/annotation-service/iputils"
"github.com/m-lab/annotation-service/loader"
)

func init() {
// Always prepend the filename and line number.
log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func TestIPListGLite2v4(t *testing.T) {
expect := []geolite2v2.GeoIPNode{
{
BaseIPNode: iputils.BaseIPNode{
IPAddressLow: net.ParseIP("1.0.0.0"),
IPAddressHigh: net.ParseIP("1.0.0.255")},
LocationIndex: 0,
PostalCode: "3095",
Latitude: -37.7,
Longitude: 145.1833,
},
{
BaseIPNode: iputils.BaseIPNode{
IPAddressLow: net.ParseIP("1.0.1.0"),
IPAddressHigh: net.ParseIP("1.0.3.255")}, // BUG: Instead we are getting 1.0.1.255
LocationIndex: 4,
Latitude: 26.0614,
Longitude: 119.3061,
},
}

// Guess this is a fake map. Why?
locationIDMap := map[int]int{
2151718: 0,
1810821: 4,
5363990: 4,
6255148: 4,
1861060: 4,
}
reader, err := zip.OpenReader("testdata/GeoLite2City.zip")
if err != nil {
t.Fatalf("Error opening zip file")
}

csv, err := loader.FindFile("GeoLite2-City-Blocks-IPv4.csv", &reader.Reader)
if err != nil {
t.Fatalf("Failed to create io.ReaderCloser")
}
defer csv.Close()
got, err := geolite2v2.LoadIPListG2(csv, locationIDMap)
if err != nil {
t.Errorf("Failed to create ipv4")
}
if len(expect) != len(got) {
t.Errorf("wrong number of nodes. Expected: %d. Got %d.\n", len(expect), len(got))
t.Logf("Expected:\n%+v\n", expect)
t.Logf("Got:\n%+v\n", got)
} else if diff := deep.Equal(expect, got); diff != nil {
t.Error(diff)
}
}

func TestIPListGLite2v6(t *testing.T) {
// Guess this is a fake map. Why?
locationIDMap := map[int]int{
2151718: 0,
1810821: 4,
5363990: 4,
6255148: 4,
1861060: 4,
}
reader, err := zip.OpenReader("testdata/GeoLite2City.zip")
if err != nil {
t.Fatalf("Error opening zip file")
}

expect := []geolite2v2.GeoIPNode{
{
BaseIPNode: iputils.BaseIPNode{
IPAddressLow: net.ParseIP("600:8801:9400:5a1:948b:ab15:dde3:61a3"),
IPAddressHigh: net.ParseIP("600:8801:9400:5a1:948b:ab15:dde3:61a3")},
LocationIndex: 4,
PostalCode: "91941",
Latitude: 32.7596,
Longitude: -116.994,
},
{
BaseIPNode: iputils.BaseIPNode{
IPAddressLow: net.ParseIP("2001:5::"),
IPAddressHigh: net.ParseIP("2001:0005:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")},
LocationIndex: 4,
Latitude: 47,
Longitude: 8,
},
{
BaseIPNode: iputils.BaseIPNode{
IPAddressLow: net.ParseIP("2001:200::"),
IPAddressHigh: net.ParseIP("2001:0200:00FF:FFFF:FFFF:FFFF:FFFF:FFFF")},
LocationIndex: 4,
Latitude: 36,
Longitude: 138,
},
}
csv, err := loader.FindFile("GeoLite2-City-Blocks-IPv6.csv", &reader.Reader)
if err != nil {
t.Fatalf("Failed to create io.ReaderCloser")
}
defer csv.Close()
got, err := geolite2v2.LoadIPListG2(csv, locationIDMap)
if err != nil {
t.Errorf("Failed to create ipv6")
}
if len(expect) != len(got) {
t.Errorf("wrong number of nodes. Expected: %d. Got %d.\n", len(expect), len(got))
t.Logf("Expected:\n%+v\n", expect)
t.Logf("Got:\n%+v\n", got)
} else if diff := deep.Equal(expect, got); diff != nil {
t.Error(diff)
}
}

func TestLocationListGLite2(t *testing.T) {
expectedLocList := []geolite2v2.LocationNode{
{
GeonameID: 32909,
ContinentCode: "AS",
CountryCode: "IR",
CountryName: "Iran",
RegionCode: "07",
RegionName: "Ostan-e Tehran",
MetroCode: 0,
CityName: "Shahre Jadide Andisheh",
},
{
GeonameID: 49518,
ContinentCode: "AF",
CountryCode: "RW",
CountryName: "Rwanda",
},
{
GeonameID: 51537,
ContinentCode: "AF",
CountryCode: "SO",
CountryName: "Somalia",
},
{
GeonameID: 5127766,
ContinentCode: "NA",
CountryCode: "US",
CountryName: "United States",
RegionCode: "NY",
RegionName: "New York",
MetroCode: 538,
CityName: "Mount Morris",
},
}
expectedIDMap := map[int]int{
5127766: 3,
51537: 2,
49518: 1,
32909: 0,
}

reader, err := zip.OpenReader("testdata/GeoLite2City.zip")
if err != nil {
t.Fatalf("Error opening zip file")
}

rc, err := loader.FindFile("GeoLite2-City-Locations-en.csv", &reader.Reader)
if err != nil {
t.Fatalf("Failed to create io.ReaderCloser")
}
defer rc.Close()

actualLocList, actualIDMap, err := geolite2v2.LoadLocationsG2(rc)
if err != nil {
log.Println(err)
t.Errorf("Failed to LoadLocationList")
}

if diff := deep.Equal(expectedLocList, actualLocList); diff != nil {
log.Printf("Expected%+v\n", expectedLocList)
log.Printf("Actual:%+v\n", actualLocList)
t.Error(diff)
}
if diff := deep.Equal(expectedIDMap, actualIDMap); diff != nil {
log.Printf("Expected%+v\n", expectedIDMap)
log.Printf("Actual:%+v\n", actualIDMap)
t.Error(diff)
}
}

func TestCorruptData(t *testing.T) {
reader, err := zip.OpenReader("testdata/GeoLite2CityCORRUPT.zip")
if err != nil {
t.Fatalf("Error opening zip file")
}
rc, err := loader.FindFile("GeoLite2-City-Locations-en.csv", &reader.Reader)
if err != nil {
t.Fatalf("Error finding file")
}
_, _, err = geolite2v2.LoadLocationsG2(rc)
if err == nil {
t.Error("Should have errored")
} else if err.Error() != "Corrupted Data: wrong number of columns" {
if err == nil {
t.Errorf("Error inconsistent:\ngot: nil\nwanted: Corrupted Data: wrong number of columns")
}
if err != nil {
output := strings.Join([]string{"Error inconsistent:\ngot:", err.Error(), "\nwanted: Corrupted Data: wrong number of columns"}, "")
t.Errorf(output)
}
}
}
3 changes: 2 additions & 1 deletion geolite2v2/geo-ip-ip-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,6 @@ func stringToFloat(str, field string) (float64, error) {
// the error message should indicate that we need a different dataset for that date range.
func LoadIPListG2(reader io.Reader, idMap map[int]int) ([]GeoIPNode, error) {
parser := newGeoNodeParser(idMap)
return parser.list, iputils.BuildIPNodeList(reader, parser)
err := iputils.BuildIPNodeList(reader, parser)
return parser.list, err
}

0 comments on commit 8cf35a7

Please sign in to comment.