feat(geoip, info): write comprehensive unit tests
This commit is contained in:
parent
fa803300bb
commit
7eedf4d947
6 changed files with 415 additions and 41 deletions
|
|
@ -4,38 +4,54 @@ import (
|
|||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/internal/cli/root"
|
||||
"github.com/ooni/probe-cli/internal/ooni"
|
||||
"github.com/ooni/probe-cli/internal/output"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmd := root.Command("geoip", "Perform a geoip lookup")
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
output.SectionTitle("GeoIP lookup")
|
||||
probeCLI, err := root.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
engine, err := probeCLI.NewProbeEngine()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
err = engine.MaybeLookupLocation()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"type": "table",
|
||||
"asn": engine.ProbeASNString(),
|
||||
"network_name": engine.ProbeNetworkName(),
|
||||
"country_code": engine.ProbeCC(),
|
||||
"ip": engine.ProbeIP(),
|
||||
}).Info("Looked up your location")
|
||||
|
||||
return nil
|
||||
return dogeoip(defaultconfig)
|
||||
})
|
||||
}
|
||||
|
||||
type dogeoipconfig struct {
|
||||
Logger log.Interface
|
||||
NewProbeCLI func() (ooni.ProbeCLI, error)
|
||||
SectionTitle func(string)
|
||||
}
|
||||
|
||||
var defaultconfig = dogeoipconfig{
|
||||
Logger: log.Log,
|
||||
NewProbeCLI: root.NewProbeCLI,
|
||||
SectionTitle: output.SectionTitle,
|
||||
}
|
||||
|
||||
func dogeoip(config dogeoipconfig) error {
|
||||
config.SectionTitle("GeoIP lookup")
|
||||
probeCLI, err := config.NewProbeCLI()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
engine, err := probeCLI.NewProbeEngine()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
err = engine.MaybeLookupLocation()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.Logger.WithFields(log.Fields{
|
||||
"type": "table",
|
||||
"asn": engine.ProbeASNString(),
|
||||
"network_name": engine.ProbeNetworkName(),
|
||||
"country_code": engine.ProbeCC(),
|
||||
"ip": engine.ProbeIP(),
|
||||
}).Info("Looked up your location")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue