refactor: interfaces and data types into the model package (#642)

## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
This commit is contained in:
Simone Basso
2022-01-03 13:53:23 +01:00
committed by GitHub
parent 69aca619ea
commit 273b70bacc
275 changed files with 1281 additions and 1375 deletions
+10 -10
View File
@@ -4,12 +4,12 @@ import (
"context"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
// Default returns the default probe services
func Default() []model.Service {
return []model.Service{{
func Default() []model.OOAPIService {
return []model.OOAPIService{{
Address: "https://ps1.ooni.io",
Type: "https",
}, {
@@ -23,7 +23,7 @@ func Default() []model.Service {
}
// SortEndpoints gives priority to https, then cloudfronted, then onion.
func SortEndpoints(in []model.Service) (out []model.Service) {
func SortEndpoints(in []model.OOAPIService) (out []model.OOAPIService) {
for _, entry := range in {
if entry.Type == "https" {
out = append(out, entry)
@@ -43,7 +43,7 @@ func SortEndpoints(in []model.Service) (out []model.Service) {
}
// OnlyHTTPS returns the HTTPS endpoints only.
func OnlyHTTPS(in []model.Service) (out []model.Service) {
func OnlyHTTPS(in []model.OOAPIService) (out []model.OOAPIService) {
for _, entry := range in {
if entry.Type == "https" {
out = append(out, entry)
@@ -53,7 +53,7 @@ func OnlyHTTPS(in []model.Service) (out []model.Service) {
}
// OnlyFallbacks returns the fallback endpoints only.
func OnlyFallbacks(in []model.Service) (out []model.Service) {
func OnlyFallbacks(in []model.OOAPIService) (out []model.OOAPIService) {
for _, entry := range SortEndpoints(in) {
if entry.Type != "https" {
out = append(out, entry)
@@ -71,10 +71,10 @@ type Candidate struct {
Err error
// Endpoint is the service endpoint.
Endpoint model.Service
Endpoint model.OOAPIService
// TestHelpers contains the data returned by the endpoint.
TestHelpers map[string][]model.Service
TestHelpers map[string][]model.OOAPIService
}
func (c *Candidate) try(ctx context.Context, sess Session) {
@@ -91,7 +91,7 @@ func (c *Candidate) try(ctx context.Context, sess Session) {
sess.Logger().Debugf("probe services: %+v: %+v %s", c.Endpoint, err, c.Duration)
}
func try(ctx context.Context, sess Session, svc model.Service) *Candidate {
func try(ctx context.Context, sess Session, svc model.OOAPIService) *Candidate {
candidate := &Candidate{Endpoint: svc}
candidate.try(ctx, sess)
return candidate
@@ -107,7 +107,7 @@ func try(ctx context.Context, sess Session, svc model.Service) *Candidate {
// such case, you will see a list of N failing HTTPS candidates, followed by a single
// successful fallback candidate (e.g. cloudfronted). If all candidates fail, you
// see in output a list containing all entries where Err is not nil.
func TryAll(ctx context.Context, sess Session, in []model.Service) (out []*Candidate) {
func TryAll(ctx context.Context, sess Session, in []model.OOAPIService) (out []*Candidate) {
var found bool
for _, svc := range OnlyHTTPS(in) {
candidate := try(ctx, sess, svc)
+2 -2
View File
@@ -3,12 +3,12 @@ package probeservices
import (
"context"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
// GetTestHelpers is like GetCollectors but for test helpers.
func (c Client) GetTestHelpers(
ctx context.Context) (output map[string][]model.Service, err error) {
ctx context.Context) (output map[string][]model.OOAPIService, err error) {
err = c.Client.GetJSON(ctx, "/api/v1/test-helpers", &output)
return
}
+4 -4
View File
@@ -3,18 +3,18 @@ package probeservices
import (
"context"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
type checkInResult struct {
Tests model.CheckInInfo `json:"tests"`
V int `json:"v"`
Tests model.OOAPICheckInInfo `json:"tests"`
V int `json:"v"`
}
// CheckIn function is called by probes asking if there are tests to be run
// The config argument contains the mandatory settings.
// Returns the list of tests to run and the URLs, on success, or an explanatory error, in case of failure.
func (c Client) CheckIn(ctx context.Context, config model.CheckInConfig) (*model.CheckInInfo, error) {
func (c Client) CheckIn(ctx context.Context, config model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
var response checkInResult
if err := c.Client.PostJSON(ctx, "/api/v1/check-in", config, &response); err != nil {
return nil, err
@@ -5,13 +5,13 @@ import (
"strings"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestCheckInSuccess(t *testing.T) {
client := newclient()
client.BaseURL = "https://ams-pg-test.ooni.org"
config := model.CheckInConfig{
config := model.OOAPICheckInConfig{
Charging: true,
OnWiFi: true,
Platform: "android",
@@ -20,7 +20,7 @@ func TestCheckInSuccess(t *testing.T) {
RunType: "timed",
SoftwareName: "ooniprobe-android",
SoftwareVersion: "2.7.1",
WebConnectivity: model.CheckInConfigWebConnectivity{
WebConnectivity: model.OOAPICheckInConfigWebConnectivity{
CategoryCodes: []string{"NEWS", "CULTR"},
},
}
@@ -48,7 +48,7 @@ func TestCheckInSuccess(t *testing.T) {
func TestCheckInFailure(t *testing.T) {
client := newclient()
client.BaseURL = "https://\t\t\t/" // cause test to fail
config := model.CheckInConfig{
config := model.OOAPICheckInConfig{
Charging: true,
OnWiFi: true,
Platform: "android",
@@ -57,7 +57,7 @@ func TestCheckInFailure(t *testing.T) {
RunType: "timed",
SoftwareName: "ooniprobe-android",
SoftwareVersion: "2.7.1",
WebConnectivity: model.CheckInConfigWebConnectivity{
WebConnectivity: model.OOAPICheckInConfigWebConnectivity{
CategoryCodes: []string{"NEWS", "CULTR"},
},
}
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"reflect"
"sync"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
const (
@@ -13,8 +13,8 @@ import (
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@@ -30,7 +30,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
var (
@@ -89,7 +89,7 @@ func (c Client) GetCredsAndAuth() (*LoginCredentials, *LoginAuth, error) {
// NewClient creates a new client for the specified probe services endpoint. This
// function fails, e.g., we don't support the specified endpoint.
func NewClient(sess Session, endpoint model.Service) (*Client, error) {
func NewClient(sess Session, endpoint model.OOAPIService) (*Client, error) {
client := &Client{
Client: httpx.Client{
BaseURL: endpoint.Address,
@@ -13,9 +13,9 @@ import (
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices/testorchestra"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@@ -25,7 +25,7 @@ func newclient() *probeservices.Client {
MockableHTTPClient: http.DefaultClient,
MockableLogger: log.Log,
},
model.Service{
model.OOAPIService{
Address: "https://ams-pg-test.ooni.org/",
Type: "https",
},
@@ -38,7 +38,7 @@ func newclient() *probeservices.Client {
func TestNewClientHTTPS(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://x.org",
Type: "https",
})
@@ -52,7 +52,7 @@ func TestNewClientHTTPS(t *testing.T) {
func TestNewClientUnsupportedEndpoint(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://x.org",
Type: "onion",
})
@@ -66,7 +66,7 @@ func TestNewClientUnsupportedEndpoint(t *testing.T) {
func TestNewClientCloudfrontInvalidURL(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "\t\t\t",
Type: "cloudfront",
})
@@ -80,7 +80,7 @@ func TestNewClientCloudfrontInvalidURL(t *testing.T) {
func TestNewClientCloudfrontInvalidURLScheme(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "http://x.org",
Type: "cloudfront",
})
@@ -94,7 +94,7 @@ func TestNewClientCloudfrontInvalidURLScheme(t *testing.T) {
func TestNewClientCloudfrontInvalidURLWithPort(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://x.org:54321",
Type: "cloudfront",
})
@@ -108,7 +108,7 @@ func TestNewClientCloudfrontInvalidURLWithPort(t *testing.T) {
func TestNewClientCloudfrontInvalidFront(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://x.org",
Type: "cloudfront",
Front: "\t\t\t",
@@ -123,7 +123,7 @@ func TestNewClientCloudfrontInvalidFront(t *testing.T) {
func TestNewClientCloudfrontGood(t *testing.T) {
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://x.org",
Type: "cloudfront",
Front: "google.com",
@@ -144,7 +144,7 @@ func TestCloudfront(t *testing.T) {
t.Skip("skip test in short mode")
}
client, err := probeservices.NewClient(
&mockable.Session{}, model.Service{
&mockable.Session{}, model.OOAPIService{
Address: "https://meek.azureedge.net",
Type: "cloudfront",
Front: "ajax.aspnetcdn.com",
@@ -197,7 +197,7 @@ func TestDefaultProbeServicesWorkAsIntended(t *testing.T) {
}
func TestSortEndpoints(t *testing.T) {
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -208,7 +208,7 @@ func TestSortEndpoints(t *testing.T) {
Type: "https",
Address: "https://ams-ps2.ooni.nu:443",
}}
expect := []model.Service{{
expect := []model.OOAPIService{{
Type: "https",
Address: "https://ams-ps2.ooni.nu:443",
}, {
@@ -227,7 +227,7 @@ func TestSortEndpoints(t *testing.T) {
}
func TestOnlyHTTPS(t *testing.T) {
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -244,7 +244,7 @@ func TestOnlyHTTPS(t *testing.T) {
Type: "https",
Address: "https://mia-ps-nonexistent.ooni.io",
}}
expect := []model.Service{{
expect := []model.OOAPIService{{
Type: "https",
Address: "https://ams-ps-nonexistent.ooni.io",
}, {
@@ -263,7 +263,7 @@ func TestOnlyHTTPS(t *testing.T) {
func TestOnlyFallbacks(t *testing.T) {
// put onion first so we also verify that we sort the endpoints
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -280,7 +280,7 @@ func TestOnlyFallbacks(t *testing.T) {
Type: "https",
Address: "https://mia-ps-nonexistent.ooni.io",
}}
expect := []model.Service{{
expect := []model.OOAPIService{{
Front: "dkyhjv0wpi2dk.cloudfront.net",
Type: "cloudfront",
Address: "https://dkyhjv0wpi2dk.cloudfront.net",
@@ -297,7 +297,7 @@ func TestOnlyFallbacks(t *testing.T) {
func TestTryAllCanceledContext(t *testing.T) {
// put onion first so we also verify that we sort the endpoints
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -401,7 +401,7 @@ func TestTryAllIntegrationWeRaceForFastestHTTPS(t *testing.T) {
}
const pattern = "^https://ps[1-4].ooni.io$"
// put onion first so we also verify that we sort the endpoints
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -472,7 +472,7 @@ func TestTryAllIntegrationWeFallback(t *testing.T) {
t.Skip("skip test in short mode")
}
// put onion first so we also verify that we sort the endpoints
in := []model.Service{{
in := []model.OOAPIService{{
Type: "onion",
Address: "httpo://jehhrikjjqrlpufu.onion",
}, {
@@ -573,32 +573,32 @@ func TestSelectBestOnlyFailures(t *testing.T) {
func TestSelectBestSelectsTheFastest(t *testing.T) {
in := []*probeservices.Candidate{{
Duration: 10 * time.Millisecond,
Endpoint: model.Service{
Endpoint: model.OOAPIService{
Address: "https://ps1.ooni.io",
Type: "https",
},
}, {
Duration: 4 * time.Millisecond,
Endpoint: model.Service{
Endpoint: model.OOAPIService{
Address: "https://ps2.ooni.io",
Type: "https",
},
}, {
Duration: 7 * time.Millisecond,
Endpoint: model.Service{
Endpoint: model.OOAPIService{
Address: "https://ps3.ooni.io",
Type: "https",
},
}, {
Duration: 11 * time.Millisecond,
Endpoint: model.Service{
Endpoint: model.OOAPIService{
Address: "https://ps4.ooni.io",
Type: "https",
},
}}
expected := &probeservices.Candidate{
Duration: 4 * time.Millisecond,
Endpoint: model.Service{
Endpoint: model.OOAPIService{
Address: "https://ps2.ooni.io",
Type: "https",
},
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"encoding/json"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
// State is the state stored inside the state file
+2 -2
View File
@@ -5,11 +5,11 @@ import (
"fmt"
"net/url"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
// FetchTorTargets returns the targets for the tor experiment.
func (c Client) FetchTorTargets(ctx context.Context, cc string) (result map[string]model.TorTarget, err error) {
func (c Client) FetchTorTargets(ctx context.Context, cc string) (result map[string]model.OOAPITorTarget, err error) {
_, auth, err := c.GetCredsAndAuth()
if err != nil {
return nil, err
+3 -3
View File
@@ -6,17 +6,17 @@ import (
"net/url"
"strings"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
type urlListResult struct {
Results []model.URLInfo `json:"results"`
Results []model.OOAPIURLInfo `json:"results"`
}
// FetchURLList fetches the list of URLs used by WebConnectivity. The config
// argument contains the optional settings. Returns the list of URLs, on success,
// or an explanatory error, in case of failure.
func (c Client) FetchURLList(ctx context.Context, config model.URLListConfig) ([]model.URLInfo, error) {
func (c Client) FetchURLList(ctx context.Context, config model.OOAPIURLListConfig) ([]model.OOAPIURLInfo, error) {
query := url.Values{}
if config.CountryCode != "" {
query.Set("country_code", config.CountryCode)
+3 -3
View File
@@ -5,7 +5,7 @@ import (
"strings"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestFetchURLListSuccess(t *testing.T) {
@@ -14,7 +14,7 @@ func TestFetchURLListSuccess(t *testing.T) {
}
client := newclient()
client.BaseURL = "https://ams-pg-test.ooni.org"
config := model.URLListConfig{
config := model.OOAPIURLListConfig{
Categories: []string{"NEWS", "CULTR"},
CountryCode: "IT",
Limit: 17,
@@ -37,7 +37,7 @@ func TestFetchURLListSuccess(t *testing.T) {
func TestFetchURLListFailure(t *testing.T) {
client := newclient()
client.BaseURL = "https://\t\t\t/" // cause test to fail
config := model.URLListConfig{
config := model.OOAPIURLListConfig{
Categories: []string{"NEWS", "CULTR"},
CountryCode: "IT",
Limit: 17,