Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development geoip docs tests #2386

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmds/modules/contd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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
Expand All @@ -115,7 +116,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 // 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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
22 changes: 22 additions & 0 deletions pkg/geoip/README.md
Original file line number Diff line number Diff line change
@@ -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 couldn'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.
4 changes: 2 additions & 2 deletions pkg/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand Down Expand Up @@ -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
}
Expand Down
79 changes: 79 additions & 0 deletions pkg/geoip/geoip_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})

}
Loading