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

Start increasing testing and code coverage #175

Merged
merged 2 commits into from
Nov 20, 2020
Merged
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
70 changes: 43 additions & 27 deletions internal/cli/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
ctx, err := root.Init()
if err != nil {
return err
}

sess, err := ctx.NewSession()
if err != nil {
return err
}
defer sess.Close()

err = sess.MaybeLookupLocation()
if err != nil {
return err
}

log.WithFields(log.Fields{
"type": "table",
"asn": sess.ProbeASNString(),
"network_name": sess.ProbeNetworkName(),
"country_code": sess.ProbeCC(),
"ip": sess.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
}
134 changes: 134 additions & 0 deletions internal/cli/geoip/geoip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package geoip

import (
"errors"
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/internal/ooni"
"github.com/ooni/probe-cli/internal/oonitest"
)

func TestNewProbeCLIFailed(t *testing.T) {
fo := &oonitest.FakeOutput{}
expected := errors.New("mocked error")
err := dogeoip(dogeoipconfig{
SectionTitle: fo.SectionTitle,
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return nil, expected
},
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if len(fo.FakeSectionTitle) != 1 {
t.Fatal("invalid section title list size")
}
if fo.FakeSectionTitle[0] != "GeoIP lookup" {
t.Fatal("unexpected string")
}
}

func TestNewProbeEngineFailed(t *testing.T) {
fo := &oonitest.FakeOutput{}
expected := errors.New("mocked error")
cli := &oonitest.FakeProbeCLI{
FakeProbeEngineErr: expected,
}
err := dogeoip(dogeoipconfig{
SectionTitle: fo.SectionTitle,
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return cli, nil
},
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if len(fo.FakeSectionTitle) != 1 {
t.Fatal("invalid section title list size")
}
if fo.FakeSectionTitle[0] != "GeoIP lookup" {
t.Fatal("unexpected string")
}
}

func TestMaybeLookupLocationFailed(t *testing.T) {
fo := &oonitest.FakeOutput{}
expected := errors.New("mocked error")
engine := &oonitest.FakeProbeEngine{
FakeMaybeLookupLocation: expected,
}
cli := &oonitest.FakeProbeCLI{
FakeProbeEnginePtr: engine,
}
err := dogeoip(dogeoipconfig{
SectionTitle: fo.SectionTitle,
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return cli, nil
},
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if len(fo.FakeSectionTitle) != 1 {
t.Fatal("invalid section title list size")
}
if fo.FakeSectionTitle[0] != "GeoIP lookup" {
t.Fatal("unexpected string")
}
}

func TestMaybeLookupLocationSuccess(t *testing.T) {
fo := &oonitest.FakeOutput{}
engine := &oonitest.FakeProbeEngine{
FakeProbeASNString: "AS30722",
FakeProbeCC: "IT",
FakeProbeNetworkName: "Vodafone Italia S.p.A.",
FakeProbeIP: "130.25.90.216",
}
cli := &oonitest.FakeProbeCLI{
FakeProbeEnginePtr: engine,
}
handler := &oonitest.FakeLoggerHandler{}
err := dogeoip(dogeoipconfig{
SectionTitle: fo.SectionTitle,
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return cli, nil
},
Logger: &log.Logger{
Handler: handler,
Level: log.DebugLevel,
},
})
if err != nil {
t.Fatal(err)
}
if len(fo.FakeSectionTitle) != 1 {
t.Fatal("invalid section title list size")
}
if fo.FakeSectionTitle[0] != "GeoIP lookup" {
t.Fatal("unexpected string")
}
if len(handler.FakeEntries) != 1 {
t.Fatal("invalid number of written entries")
}
entry := handler.FakeEntries[0]
if entry.Level != log.InfoLevel {
t.Fatal("invalid log level")
}
if entry.Message != "Looked up your location" {
t.Fatal("invalid .Message")
}
if entry.Fields["asn"].(string) != "AS30722" {
t.Fatal("invalid asn")
}
if entry.Fields["country_code"].(string) != "IT" {
t.Fatal("invalid asn")
}
if entry.Fields["network_name"].(string) != "Vodafone Italia S.p.A." {
t.Fatal("invalid asn")
}
if entry.Fields["ip"].(string) != "130.25.90.216" {
t.Fatal("invalid asn")
}
}
37 changes: 23 additions & 14 deletions internal/cli/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/ooni"
)

func init() {
cmd := root.Command("info", "Display information about OONI Probe")

cmd.Action(func(_ *kingpin.ParseContext) error {
ctx, err := root.Init()
if err != nil {
log.Errorf("%s", err)
return err
}
log.WithFields(log.Fields{
"path": ctx.Home,
}).Info("Home")
log.WithFields(log.Fields{
"path": ctx.TempDir,
}).Info("TempDir")

return nil
return doinfo(defaultconfig)
})
}

type doinfoconfig struct {
Logger log.Interface
NewProbeCLI func() (ooni.ProbeCLI, error)
}

var defaultconfig = doinfoconfig{
Logger: log.Log,
NewProbeCLI: root.NewProbeCLI,
}

func doinfo(config doinfoconfig) error {
probeCLI, err := config.NewProbeCLI()
if err != nil {
config.Logger.Errorf("%s", err)
return err
}
config.Logger.WithFields(log.Fields{"path": probeCLI.Home()}).Info("Home")
config.Logger.WithFields(log.Fields{"path": probeCLI.TempDir()}).Info("TempDir")
return nil
}
80 changes: 80 additions & 0 deletions internal/cli/info/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package info

import (
"errors"
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/internal/ooni"
"github.com/ooni/probe-cli/internal/oonitest"
)

func TestNewProbeCLIFailed(t *testing.T) {
expected := errors.New("mocked error")
handler := &oonitest.FakeLoggerHandler{}
err := doinfo(doinfoconfig{
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return nil, expected
},
Logger: &log.Logger{
Handler: handler,
Level: log.DebugLevel,
},
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if len(handler.FakeEntries) != 1 {
t.Fatal("invalid number of log entries")
}
entry := handler.FakeEntries[0]
if entry.Level != log.ErrorLevel {
t.Fatal("invalid log level")
}
if entry.Message != "mocked error" {
t.Fatal("invalid .Message")
}
}

func TestSuccess(t *testing.T) {
handler := &oonitest.FakeLoggerHandler{}
cli := &oonitest.FakeProbeCLI{
FakeHome: "fakehome",
FakeTempDir: "faketempdir",
}
err := doinfo(doinfoconfig{
NewProbeCLI: func() (ooni.ProbeCLI, error) {
return cli, nil
},
Logger: &log.Logger{
Handler: handler,
Level: log.DebugLevel,
},
})
if err != nil {
t.Fatal(err)
}
if len(handler.FakeEntries) != 2 {
t.Fatal("invalid number of log entries")
}
entry := handler.FakeEntries[0]
if entry.Level != log.InfoLevel {
t.Fatal("invalid log level")
}
if entry.Message != "Home" {
t.Fatal("invalid .Message")
}
if entry.Fields["path"].(string) != "fakehome" {
t.Fatal("invalid path")
}
entry = handler.FakeEntries[1]
if entry.Level != log.InfoLevel {
t.Fatal("invalid log level")
}
if entry.Message != "TempDir" {
t.Fatal("invalid .Message")
}
if entry.Fields["path"].(string) != "faketempdir" {
t.Fatal("invalid path")
}
}
10 changes: 5 additions & 5 deletions internal/cli/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ func init() {
cmd := root.Command("list", "List results")
resultID := cmd.Arg("id", "the id of the result to list measurements for").Int64()
cmd.Action(func(_ *kingpin.ParseContext) error {
ctx, err := root.Init()
probeCLI, err := root.Init()
if err != nil {
log.WithError(err).Error("failed to initialize root context")
return err
}
if *resultID > 0 {
measurements, err := database.ListMeasurements(ctx.DB, *resultID)
measurements, err := database.ListMeasurements(probeCLI.DB(), *resultID)
if err != nil {
log.WithError(err).Error("failed to list measurements")
return err
Expand Down Expand Up @@ -61,7 +61,7 @@ func init() {
}
output.MeasurementSummary(msmtSummary)
} else {
doneResults, incompleteResults, err := database.ListResults(ctx.DB)
doneResults, incompleteResults, err := database.ListResults(probeCLI.DB())
if err != nil {
log.WithError(err).Error("failed to list results")
return err
Expand Down Expand Up @@ -91,11 +91,11 @@ func init() {
netCount := make(map[uint]int)
output.SectionTitle("Results")
for idx, result := range doneResults {
totalCount, anmlyCount, err := database.GetMeasurementCounts(ctx.DB, result.Result.ID)
totalCount, anmlyCount, err := database.GetMeasurementCounts(probeCLI.DB(), result.Result.ID)
if err != nil {
log.WithError(err).Error("failed to list measurement counts")
}
testKeys, err := database.GetResultTestKeys(ctx.DB, result.Result.ID)
testKeys, err := database.GetResultTestKeys(probeCLI.DB(), result.Result.ID)
if err != nil {
log.WithError(err).Error("failed to get testKeys")
}
Expand Down
Loading