refactor: redesign how we import assets (#260)
* fix(pkg.go.dev): import a subpackage containing the assets We're trying to fix this issue that pkg.go.dev does not build. Thanks to @hellais for this very neat idea! Let's keep our fingers crossed and see whether it fixes! * feat: use embedded geoip databases Closes https://github.com/ooni/probe/issues/1372. Work done as part of https://github.com/ooni/probe/issues/1369. * fix(assetsx): add tests * feat: simplify and just vendor uncompressed DBs * remove tests that seems not necessary anymore * fix: run go mod tidy * Address https://github.com/ooni/probe-cli/pull/260/files#r605181364 * rewrite a test in a better way * fix: gently cleanup the legacy assetsdir Do not remove the whole directory with brute force. Just zap the files whose name we know. Then attempt to delete the legacy directory as well. If not empty, just fail. This is fine because it means the user has stored other files inside the directory. * fix: create .miniooni if missing
This commit is contained in:
parent
7ca32b5ce6
commit
31e478b04e
51 changed files with 243 additions and 808 deletions
|
|
@ -6,57 +6,6 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
type taskResourcesManager struct {
|
||||
asnDatabasePath string
|
||||
countryDatabasePath string
|
||||
err error
|
||||
}
|
||||
|
||||
func (c taskResourcesManager) ASNDatabasePath() string {
|
||||
return c.asnDatabasePath
|
||||
}
|
||||
|
||||
func (c taskResourcesManager) CountryDatabasePath() string {
|
||||
return c.countryDatabasePath
|
||||
}
|
||||
|
||||
func (c taskResourcesManager) MaybeUpdateResources(ctx context.Context) error {
|
||||
return c.err
|
||||
}
|
||||
|
||||
func TestLocationLookupCannotUpdateResources(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{err: expected},
|
||||
}
|
||||
ctx := context.Background()
|
||||
out, err := op.Run(ctx)
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatalf("not the error we expected: %+v", err)
|
||||
}
|
||||
if out.ASN != DefaultProbeASN {
|
||||
t.Fatal("invalid ASN value")
|
||||
}
|
||||
if out.CountryCode != DefaultProbeCC {
|
||||
t.Fatal("invalid CountryCode value")
|
||||
}
|
||||
if out.NetworkName != DefaultProbeNetworkName {
|
||||
t.Fatal("invalid NetworkName value")
|
||||
}
|
||||
if out.ProbeIP != DefaultProbeIP {
|
||||
t.Fatal("invalid ProbeIP value")
|
||||
}
|
||||
if out.ResolverASN != DefaultResolverASN {
|
||||
t.Fatal("invalid ResolverASN value")
|
||||
}
|
||||
if out.ResolverIP != DefaultResolverIP {
|
||||
t.Fatal("invalid ResolverIP value")
|
||||
}
|
||||
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
||||
t.Fatal("invalid ResolverNetworkName value")
|
||||
}
|
||||
}
|
||||
|
||||
type taskProbeIPLookupper struct {
|
||||
ip string
|
||||
err error
|
||||
|
|
@ -69,7 +18,6 @@ func (c taskProbeIPLookupper) LookupProbeIP(ctx context.Context) (string, error)
|
|||
func TestLocationLookupCannotLookupProbeIP(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{err: expected},
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
|
@ -106,14 +54,13 @@ type taskASNLookupper struct {
|
|||
name string
|
||||
}
|
||||
|
||||
func (c taskASNLookupper) LookupASN(path string, ip string) (uint, string, error) {
|
||||
func (c taskASNLookupper) LookupASN(ip string) (uint, string, error) {
|
||||
return c.asn, c.name, c.err
|
||||
}
|
||||
|
||||
func TestLocationLookupCannotLookupProbeASN(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{err: expected},
|
||||
}
|
||||
|
|
@ -150,14 +97,13 @@ type taskCCLookupper struct {
|
|||
cc string
|
||||
}
|
||||
|
||||
func (c taskCCLookupper) LookupCC(path string, ip string) (string, error) {
|
||||
func (c taskCCLookupper) LookupCC(ip string) (string, error) {
|
||||
return c.cc, c.err
|
||||
}
|
||||
|
||||
func TestLocationLookupCannotLookupProbeCC(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
||||
countryLookupper: taskCCLookupper{cc: "US", err: expected},
|
||||
|
|
@ -202,7 +148,6 @@ func (c taskResolverIPLookupper) LookupResolverIP(ctx context.Context) (string,
|
|||
func TestLocationLookupCannotLookupResolverIP(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
||||
countryLookupper: taskCCLookupper{cc: "IT"},
|
||||
|
|
@ -243,7 +188,6 @@ func TestLocationLookupCannotLookupResolverIP(t *testing.T) {
|
|||
func TestLocationLookupCannotLookupResolverNetworkName(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
||||
countryLookupper: taskCCLookupper{cc: "IT"},
|
||||
|
|
@ -284,7 +228,6 @@ func TestLocationLookupCannotLookupResolverNetworkName(t *testing.T) {
|
|||
|
||||
func TestLocationLookupSuccessWithResolverLookup(t *testing.T) {
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
||||
countryLookupper: taskCCLookupper{cc: "IT"},
|
||||
|
|
@ -325,7 +268,6 @@ func TestLocationLookupSuccessWithResolverLookup(t *testing.T) {
|
|||
|
||||
func TestLocationLookupSuccessWithoutResolverLookup(t *testing.T) {
|
||||
op := Task{
|
||||
resourcesManager: taskResourcesManager{},
|
||||
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
||||
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
||||
countryLookupper: taskCCLookupper{cc: "IT"},
|
||||
|
|
@ -364,13 +306,8 @@ func TestLocationLookupSuccessWithoutResolverLookup(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSmoke(t *testing.T) {
|
||||
maybeFetchResources(t)
|
||||
config := Config{
|
||||
EnableResolverLookup: true,
|
||||
ResourcesManager: taskResourcesManager{
|
||||
asnDatabasePath: asnDBPath,
|
||||
countryDatabasePath: countryDBPath,
|
||||
},
|
||||
}
|
||||
task := Must(NewTask(config))
|
||||
result, err := task.Run(context.Background())
|
||||
|
|
@ -384,16 +321,6 @@ func TestSmoke(t *testing.T) {
|
|||
// value is okay for all codepaths.
|
||||
}
|
||||
|
||||
func TestNewTaskWithNoResourcesManager(t *testing.T) {
|
||||
task, err := NewTask(Config{})
|
||||
if !errors.Is(err, ErrMissingResourcesManager) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if task != nil {
|
||||
t.Fatal("expected nil task here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestASNStringWorks(t *testing.T) {
|
||||
r := Results{ASN: 1234}
|
||||
if r.ASNString() != "AS1234" {
|
||||
|
|
|
|||
Loading…
Reference in a new issue