From 4fff9cd0f4f5aac776bca0121c1de431df198b5a Mon Sep 17 00:00:00 2001 From: rowanxgamal Date: Tue, 30 Jul 2024 15:15:33 +0300 Subject: [PATCH 1/6] test: did geoip tests --- pkg/geoip/geoip.go | 4 +-- pkg/geoip/geoip_test.go | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 pkg/geoip/geoip_test.go diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index df108568c..7535240a1 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -22,7 +22,7 @@ type Location struct { var ( geoipURLs = []string{"https://geoip.grid.tf/", "https://02.geoip.grid.tf/", "https://03.geoip.grid.tf/"} - defaultHttpClient = &http.Client{ + defaultHTTPClient = &http.Client{ Timeout: 10 * time.Second, } ) @@ -52,7 +52,7 @@ func getLocation(geoIPService string) (Location, error) { City: "Unknown", } - resp, err := defaultHttpClient.Get(geoIPService) + resp, err := defaultHTTPClient.Get(geoIPService) if err != nil { return l, err } diff --git a/pkg/geoip/geoip_test.go b/pkg/geoip/geoip_test.go new file mode 100644 index 000000000..2d7f387ad --- /dev/null +++ b/pkg/geoip/geoip_test.go @@ -0,0 +1,79 @@ +package geoip + +import ( + "net/http" + "testing" + + "github.com/jarcoal/httpmock" + "github.com/stretchr/testify/assert" +) + +func Test_getLocation(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + t.Run("running correct response", func(t *testing.T) { + for i := 0; i < len(geoipURLs); i++ { + httpmock.RegisterResponder("GET", geoipURLs[i], + func(req *http.Request) (*http.Response, error) { + l := Location{ + Continent: "Africa", + Country: "Egypt", + City: "Cairo", + } + resp, err := httpmock.NewJsonResponse(200, l) + return resp, err + }, + ) + value, err := getLocation(geoipURLs[i]) + assert.Equal(t, nil, err) + assert.Equal(t, "Egypt", value.Country) + assert.Equal(t, "Africa", value.Continent) + assert.Equal(t, "Cairo", value.City) + if err != nil { + t.Errorf("got %v", err) + } + } + }) + + t.Run("asserting wrong response code", func(t *testing.T) { + for i := 0; i < len(geoipURLs); i++ { + httpmock.RegisterResponder("GET", geoipURLs[i], + func(req *http.Request) (*http.Response, error) { + l := Location{ + Continent: "Africa", + Country: "Egypt", + City: "Cairo", + } + resp, err := httpmock.NewJsonResponse(404, l) + return resp, err + }, + ) + value, err := getLocation(geoipURLs[i]) + assert.NotEqual(t, err, nil) + assert.Equal(t, "Unknown", value.Country) + assert.Equal(t, "Unknown", value.Continent) + assert.Equal(t, "Unknown", value.City) + } + }) + + t.Run("asserting sending wrong response data", func(t *testing.T) { + for i := 0; i < len(geoipURLs); i++ { + httpmock.RegisterResponder("GET", geoipURLs[i], + func(req *http.Request) (*http.Response, error) { + l := Location{ + City: "Cairo", + } + resp, err := httpmock.NewJsonResponse(200, l.City) + return resp, err + }, + ) + value, err := getLocation(geoipURLs[i]) + assert.NotEqual(t, err, nil) + assert.Equal(t, "Unknown", value.Country) + assert.Equal(t, "Unknown", value.Continent) + assert.Equal(t, "Unknown", value.City) + } + }) + +} From 6736f84a0b4dab3060bb2c03259fb8ae09b526f3 Mon Sep 17 00:00:00 2001 From: rowanxgamal Date: Tue, 30 Jul 2024 15:34:18 +0300 Subject: [PATCH 2/6] test-docs: did geoip tests and updated docs --- pkg/geoip/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 pkg/geoip/README.md diff --git a/pkg/geoip/README.md b/pkg/geoip/README.md new file mode 100644 index 000000000..83ceaef3e --- /dev/null +++ b/pkg/geoip/README.md @@ -0,0 +1,22 @@ +# Geoip module + +This module is used to get information about the geographical location of an IPv4 or IPv6 address. + + +## Usage + +To get the location of the system calling this function, use exported method `Fetch` from the package `geoip` + +1. use `geoip.Fetch()`: + + This method uses 3 paths of geoip, It starts with first path of `geoipURLs` if any error was produced it continues and tries out the next path, REturnes the default unknown location and the error in case it coulnd't receive correct response from all paths. + +## Tests + +`geoip_test.go` tests the driver function `getLocation` which is called by the exported function `Fetch` +It mainly tests and validates: +1. Correct response. +2. Wrong response code. +3. Wrong response body. + +#### Remark : Tests are computed on all 3 paths of `geoipURLs` to ensure correctness. \ No newline at end of file From 061ca29b6386c01aece773622201cb4bba730309 Mon Sep 17 00:00:00 2001 From: rowanxgamal Date: Tue, 30 Jul 2024 16:35:12 +0300 Subject: [PATCH 3/6] test-docs: did geoip tests and updated docs --- cmds/modules/contd/main.go | 2 +- go.mod | 2 ++ go.sum | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cmds/modules/contd/main.go b/cmds/modules/contd/main.go index 54c7da422..8e6a8cf45 100644 --- a/cmds/modules/contd/main.go +++ b/cmds/modules/contd/main.go @@ -18,7 +18,7 @@ import ( const module = "container" -//Module is contd entry point +// Module is contd entry point var Module cli.Command = cli.Command{ Name: "contd", Usage: "handles containers creations", diff --git a/go.mod b/go.mod index 21cb4fff1..eb7e01874 100644 --- a/go.mod +++ b/go.mod @@ -97,6 +97,7 @@ require ( github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/holiman/uint256 v1.2.3 // indirect + github.com/jarcoal/httpmock v1.3.1 github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/lestrrat-go/backoff/v2 v2.0.7 // indirect @@ -116,6 +117,7 @@ require ( github.com/moby/sys/mountinfo v0.6.2 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/nsf/termbox-go v1.1.1 // indirect + github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a github.com/onsi/ginkgo v1.16.4 // indirect github.com/onsi/gomega v1.16.0 // indirect github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect diff --git a/go.sum b/go.sum index dbc54f94d..4901c22a2 100644 --- a/go.sum +++ b/go.sum @@ -303,6 +303,8 @@ github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZm github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= +github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= +github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6 h1:4zOlv2my+vf98jT1nQt4bT/yKWUImevYPJ2H344CloE= github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6/go.mod h1:r/8JmuR0qjuCiEhAolkfvdZgmPiHTnJaG0UXCSeR1Zo= github.com/jgautheron/goconst v0.0.0-20170703170152-9740945f5dcb/go.mod h1:82TxjOpWQiPmywlbIaB2ZkqJoSYJdLGPgAJDvM3PbKc= @@ -386,6 +388,8 @@ github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vq github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= +github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43 h1:WgyLFv10Ov49JAQI/ZLUkCZ7VJS3r74hwFIGXJsgZlY= github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo= github.com/mdlayher/genetlink v1.0.0 h1:OoHN1OdyEIkScEmRgxLEe2M9U8ClMytqA5niynLtfj0= From 48be192edcc3f8f37e79f48b831fd864fc27ee25 Mon Sep 17 00:00:00 2001 From: rowanxgamal Date: Mon, 26 Aug 2024 22:13:02 +0300 Subject: [PATCH 4/6] fix: typos in README.md --- pkg/geoip/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/geoip/README.md b/pkg/geoip/README.md index 83ceaef3e..e7cc69ec5 100644 --- a/pkg/geoip/README.md +++ b/pkg/geoip/README.md @@ -9,7 +9,7 @@ To get the location of the system calling this function, use exported method `Fe 1. use `geoip.Fetch()`: - This method uses 3 paths of geoip, It starts with first path of `geoipURLs` if any error was produced it continues and tries out the next path, REturnes the default unknown location and the error in case it coulnd't receive correct response from all paths. + This method uses 3 paths of geoip, It starts with first path of `geoipURLs` if any error was produced it continues and tries out the next path, REturnes the default unknown location and the error in case it couldn't receive correct response from all paths. ## Tests From 19e537656b70c3a0745ea0bd6cbc854e7328c397 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Wed, 4 Dec 2024 16:23:03 +0200 Subject: [PATCH 5/6] update geoip docs --- pkg/geoip/README.md | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pkg/geoip/README.md b/pkg/geoip/README.md index e7cc69ec5..2e03744ce 100644 --- a/pkg/geoip/README.md +++ b/pkg/geoip/README.md @@ -1,22 +1,26 @@ -# Geoip module +# Geoip Module -This module is used to get information about the geographical location of an IPv4 or IPv6 address. +The `geoip` module provides a simple way to determine the geographical location of an IPv4 or IPv6 address. +This includes details such as longitude, latitude, country, city, and continent. +## Features -## Usage - -To get the location of the system calling this function, use exported method `Fetch` from the package `geoip` +- **Failover Mechanism:** The module attempts to fetch location data from multiple services to ensure high availability. If one URL fails, it logs the error and retries with the next URL. +- **Structured Location Data:** Returns structured data in a `Location` struct for easy integration. -1. use `geoip.Fetch()`: +```go +type Location struct { + Longitude float64 `json:"longitude"` + Latitude float64 `json:"latitude"` + Continent string `json:"continent"` + Country string `json:"country_name"` + CountryCode string `json:"country_code"` + City string `json:"city_name"` +} +``` - This method uses 3 paths of geoip, It starts with first path of `geoipURLs` if any error was produced it continues and tries out the next path, REturnes the default unknown location and the error in case it couldn't receive correct response from all paths. - -## Tests +## Usage -`geoip_test.go` tests the driver function `getLocation` which is called by the exported function `Fetch` -It mainly tests and validates: -1. Correct response. -2. Wrong response code. -3. Wrong response body. +The module provides a single exported function: `Fetch`. -#### Remark : Tests are computed on all 3 paths of `geoipURLs` to ensure correctness. \ No newline at end of file +The `Fetch` function retrieves the geographical location of the system calling the function. From 44441fcdff4d16dff10b9c3ce0d556d750bea3a7 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Wed, 4 Dec 2024 16:23:14 +0200 Subject: [PATCH 6/6] update geoip tests --- go.mod | 1 - pkg/geoip/README.md | 2 +- pkg/geoip/geoip.go | 1 - pkg/geoip/geoip_test.go | 87 ++++++++++++++++++----------------------- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index eb7e01874..d2374b078 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,6 @@ require ( github.com/joncrlsn/dque v0.0.0-20200702023911-3e80e3146ce5 github.com/lestrrat-go/jwx v1.1.7 github.com/machinebox/graphql v0.2.2 - github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 diff --git a/pkg/geoip/README.md b/pkg/geoip/README.md index 2e03744ce..58a8781b8 100644 --- a/pkg/geoip/README.md +++ b/pkg/geoip/README.md @@ -1,6 +1,6 @@ # Geoip Module -The `geoip` module provides a simple way to determine the geographical location of an IPv4 or IPv6 address. +The `geoip` module provides a simple way to determine the geographical location of the system executing the code. This includes details such as longitude, latitude, country, city, and continent. ## Features diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 7535240a1..b270b2378 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -29,7 +29,6 @@ var ( // Fetch retrieves the location of the system calling this function func Fetch() (Location, error) { - for _, url := range geoipURLs { l, err := getLocation(url) if err != nil { diff --git a/pkg/geoip/geoip_test.go b/pkg/geoip/geoip_test.go index 2d7f387ad..33a9cdc7a 100644 --- a/pkg/geoip/geoip_test.go +++ b/pkg/geoip/geoip_test.go @@ -5,75 +5,66 @@ import ( "testing" "github.com/jarcoal/httpmock" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func Test_getLocation(t *testing.T) { +func TestGetLocation(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - t.Run("running correct response", func(t *testing.T) { - for i := 0; i < len(geoipURLs); i++ { - httpmock.RegisterResponder("GET", geoipURLs[i], + require := require.New(t) + + t.Run("test valid response", func(t *testing.T) { + l := Location{ + Continent: "Africa", + Country: "Egypt", + City: "Cairo", + } + + for _, url := range geoipURLs { + httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { - l := Location{ - Continent: "Africa", - Country: "Egypt", - City: "Cairo", - } - resp, err := httpmock.NewJsonResponse(200, l) - return resp, err + return httpmock.NewJsonResponse(200, l) }, ) - value, err := getLocation(geoipURLs[i]) - assert.Equal(t, nil, err) - assert.Equal(t, "Egypt", value.Country) - assert.Equal(t, "Africa", value.Continent) - assert.Equal(t, "Cairo", value.City) - if err != nil { - t.Errorf("got %v", err) - } + + resp, err := getLocation(url) + require.NoError(err) + require.Equal(resp, l) } }) - t.Run("asserting wrong response code", func(t *testing.T) { - for i := 0; i < len(geoipURLs); i++ { - httpmock.RegisterResponder("GET", geoipURLs[i], + l := Location{ + Continent: "Unknown", + Country: "Unknown", + City: "Unknown", + } + + t.Run("test 404 status code", func(t *testing.T) { + for _, url := range geoipURLs { + httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { - l := Location{ - Continent: "Africa", - Country: "Egypt", - City: "Cairo", - } - resp, err := httpmock.NewJsonResponse(404, l) - return resp, err + return httpmock.NewJsonResponse(404, l) }, ) - value, err := getLocation(geoipURLs[i]) - assert.NotEqual(t, err, nil) - assert.Equal(t, "Unknown", value.Country) - assert.Equal(t, "Unknown", value.Continent) - assert.Equal(t, "Unknown", value.City) + + resp, err := getLocation(url) + require.Error(err) + require.Equal(resp, l) } }) - t.Run("asserting sending wrong response data", func(t *testing.T) { - for i := 0; i < len(geoipURLs); i++ { - httpmock.RegisterResponder("GET", geoipURLs[i], + t.Run("test invalid response data", func(t *testing.T) { + for _, url := range geoipURLs { + httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { - l := Location{ - City: "Cairo", - } - resp, err := httpmock.NewJsonResponse(200, l.City) + resp, err := httpmock.NewJsonResponse(200, "Cairo") return resp, err }, ) - value, err := getLocation(geoipURLs[i]) - assert.NotEqual(t, err, nil) - assert.Equal(t, "Unknown", value.Country) - assert.Equal(t, "Unknown", value.Continent) - assert.Equal(t, "Unknown", value.City) + resp, err := getLocation(url) + require.Error(err) + require.Equal(resp, l) } }) - }