-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geoip, info): write comprehensive unit tests
- Loading branch information
1 parent
fa80330
commit 7eedf4d
Showing
6 changed files
with
415 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.