Skip to content

Commit 0f5fe58

Browse files
authored
Merge pull request #2386 from threefoldtech/development_geoip_docs_tests
Development geoip docs tests
2 parents c5b0421 + 44441fc commit 0f5fe58

File tree

6 files changed

+105
-5
lines changed

6 files changed

+105
-5
lines changed

cmds/modules/contd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
const module = "container"
2020

21-
//Module is contd entry point
21+
// Module is contd entry point
2222
var Module cli.Command = cli.Command{
2323
Name: "contd",
2424
Usage: "handles containers creations",

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ require (
3939
github.com/joncrlsn/dque v0.0.0-20200702023911-3e80e3146ce5
4040
github.com/lestrrat-go/jwx v1.1.7
4141
github.com/machinebox/graphql v0.2.2
42-
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a
4342
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
4443
github.com/patrickmn/go-cache v2.1.0+incompatible
4544
github.com/pkg/errors v0.9.1
@@ -97,6 +96,7 @@ require (
9796
github.com/hashicorp/go-multierror v1.1.1 // indirect
9897
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
9998
github.com/holiman/uint256 v1.2.3 // indirect
99+
github.com/jarcoal/httpmock v1.3.1
100100
github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect
101101
github.com/klauspost/compress v1.16.7 // indirect
102102
github.com/lestrrat-go/backoff/v2 v2.0.7 // indirect
@@ -116,6 +116,7 @@ require (
116116
github.com/moby/sys/mountinfo v0.6.2 // indirect
117117
github.com/ncruces/go-strftime v0.1.9 // indirect
118118
github.com/nsf/termbox-go v1.1.1 // indirect
119+
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a
119120
github.com/onsi/ginkgo v1.16.4 // indirect
120121
github.com/onsi/gomega v1.16.0 // indirect
121122
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZm
303303
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
304304
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
305305
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
306+
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
307+
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
306308
github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6 h1:4zOlv2my+vf98jT1nQt4bT/yKWUImevYPJ2H344CloE=
307309
github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6/go.mod h1:r/8JmuR0qjuCiEhAolkfvdZgmPiHTnJaG0UXCSeR1Zo=
308310
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
386388
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
387389
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
388390
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
391+
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
392+
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
389393
github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43 h1:WgyLFv10Ov49JAQI/ZLUkCZ7VJS3r74hwFIGXJsgZlY=
390394
github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo=
391395
github.com/mdlayher/genetlink v1.0.0 h1:OoHN1OdyEIkScEmRgxLEe2M9U8ClMytqA5niynLtfj0=

pkg/geoip/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Geoip Module
2+
3+
The `geoip` module provides a simple way to determine the geographical location of the system executing the code.
4+
This includes details such as longitude, latitude, country, city, and continent.
5+
6+
## Features
7+
8+
- **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.
9+
- **Structured Location Data:** Returns structured data in a `Location` struct for easy integration.
10+
11+
```go
12+
type Location struct {
13+
Longitude float64 `json:"longitude"`
14+
Latitude float64 `json:"latitude"`
15+
Continent string `json:"continent"`
16+
Country string `json:"country_name"`
17+
CountryCode string `json:"country_code"`
18+
City string `json:"city_name"`
19+
}
20+
```
21+
22+
## Usage
23+
24+
The module provides a single exported function: `Fetch`.
25+
26+
The `Fetch` function retrieves the geographical location of the system calling the function.

pkg/geoip/geoip.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ type Location struct {
2222
var (
2323
geoipURLs = []string{"https://geoip.grid.tf/", "https://02.geoip.grid.tf/", "https://03.geoip.grid.tf/"}
2424

25-
defaultHttpClient = &http.Client{
25+
defaultHTTPClient = &http.Client{
2626
Timeout: 10 * time.Second,
2727
}
2828
)
2929

3030
// Fetch retrieves the location of the system calling this function
3131
func Fetch() (Location, error) {
32-
3332
for _, url := range geoipURLs {
3433
l, err := getLocation(url)
3534
if err != nil {
@@ -52,7 +51,7 @@ func getLocation(geoIPService string) (Location, error) {
5251
City: "Unknown",
5352
}
5453

55-
resp, err := defaultHttpClient.Get(geoIPService)
54+
resp, err := defaultHTTPClient.Get(geoIPService)
5655
if err != nil {
5756
return l, err
5857
}

pkg/geoip/geoip_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package geoip
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/jarcoal/httpmock"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestGetLocation(t *testing.T) {
12+
httpmock.Activate()
13+
defer httpmock.DeactivateAndReset()
14+
15+
require := require.New(t)
16+
17+
t.Run("test valid response", func(t *testing.T) {
18+
l := Location{
19+
Continent: "Africa",
20+
Country: "Egypt",
21+
City: "Cairo",
22+
}
23+
24+
for _, url := range geoipURLs {
25+
httpmock.RegisterResponder("GET", url,
26+
func(req *http.Request) (*http.Response, error) {
27+
return httpmock.NewJsonResponse(200, l)
28+
},
29+
)
30+
31+
resp, err := getLocation(url)
32+
require.NoError(err)
33+
require.Equal(resp, l)
34+
}
35+
})
36+
37+
l := Location{
38+
Continent: "Unknown",
39+
Country: "Unknown",
40+
City: "Unknown",
41+
}
42+
43+
t.Run("test 404 status code", func(t *testing.T) {
44+
for _, url := range geoipURLs {
45+
httpmock.RegisterResponder("GET", url,
46+
func(req *http.Request) (*http.Response, error) {
47+
return httpmock.NewJsonResponse(404, l)
48+
},
49+
)
50+
51+
resp, err := getLocation(url)
52+
require.Error(err)
53+
require.Equal(resp, l)
54+
}
55+
})
56+
57+
t.Run("test invalid response data", func(t *testing.T) {
58+
for _, url := range geoipURLs {
59+
httpmock.RegisterResponder("GET", url,
60+
func(req *http.Request) (*http.Response, error) {
61+
resp, err := httpmock.NewJsonResponse(200, "Cairo")
62+
return resp, err
63+
},
64+
)
65+
resp, err := getLocation(url)
66+
require.Error(err)
67+
require.Equal(resp, l)
68+
}
69+
})
70+
}

0 commit comments

Comments
 (0)