feat(oonirun): improve tests (#915)
See https://github.com/ooni/probe/issues/2184 While there, rename `runtimex.PanicIfFalse` to `runtimex.Assert` (it was about time...)
This commit is contained in:
@@ -281,3 +281,25 @@ type ExperimentOptionInfo struct {
|
||||
// Type contains the type.
|
||||
Type string
|
||||
}
|
||||
|
||||
// ExperimentInputLoader loads inputs from local or remote sources.
|
||||
type ExperimentInputLoader interface {
|
||||
Load(ctx context.Context) ([]OOAPIURLInfo, error)
|
||||
}
|
||||
|
||||
// Submitter submits a measurement to the OONI collector.
|
||||
type Submitter interface {
|
||||
// Submit submits the measurement and updates its
|
||||
// report ID field in case of success.
|
||||
Submit(ctx context.Context, m *Measurement) error
|
||||
}
|
||||
|
||||
// Saver saves a measurement on some persistent storage.
|
||||
type Saver interface {
|
||||
SaveMeasurement(m *Measurement) error
|
||||
}
|
||||
|
||||
// ExperimentInputProcessor processes inputs for an experiment.
|
||||
type ExperimentInputProcessor interface {
|
||||
Run(ctx context.Context) error
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// Experiment mocks model.Experiment
|
||||
type Experiment struct {
|
||||
MockKibiBytesReceived func() float64
|
||||
|
||||
MockKibiBytesSent func() float64
|
||||
|
||||
MockName func() string
|
||||
|
||||
MockGetSummaryKeys func(m *model.Measurement) (any, error)
|
||||
|
||||
MockReportID func() string
|
||||
|
||||
MockMeasureAsync func(ctx context.Context, input string) (<-chan *model.Measurement, error)
|
||||
|
||||
MockMeasureWithContext func(
|
||||
ctx context.Context, input string) (measurement *model.Measurement, err error)
|
||||
|
||||
MockSaveMeasurement func(measurement *model.Measurement, filePath string) error
|
||||
|
||||
MockSubmitAndUpdateMeasurementContext func(
|
||||
ctx context.Context, measurement *model.Measurement) error
|
||||
|
||||
MockOpenReportContext func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func (e *Experiment) KibiBytesReceived() float64 {
|
||||
return e.MockKibiBytesReceived()
|
||||
}
|
||||
|
||||
func (e *Experiment) KibiBytesSent() float64 {
|
||||
return e.MockKibiBytesSent()
|
||||
}
|
||||
|
||||
func (e *Experiment) Name() string {
|
||||
return e.MockName()
|
||||
}
|
||||
|
||||
func (e *Experiment) GetSummaryKeys(m *model.Measurement) (any, error) {
|
||||
return e.MockGetSummaryKeys(m)
|
||||
}
|
||||
|
||||
func (e *Experiment) ReportID() string {
|
||||
return e.MockReportID()
|
||||
}
|
||||
|
||||
func (e *Experiment) MeasureAsync(
|
||||
ctx context.Context, input string) (<-chan *model.Measurement, error) {
|
||||
return e.MockMeasureAsync(ctx, input)
|
||||
}
|
||||
|
||||
func (e *Experiment) MeasureWithContext(
|
||||
ctx context.Context, input string) (measurement *model.Measurement, err error) {
|
||||
return e.MockMeasureWithContext(ctx, input)
|
||||
}
|
||||
|
||||
func (e *Experiment) SaveMeasurement(measurement *model.Measurement, filePath string) error {
|
||||
return e.MockSaveMeasurement(measurement, filePath)
|
||||
}
|
||||
|
||||
func (e *Experiment) SubmitAndUpdateMeasurementContext(
|
||||
ctx context.Context, measurement *model.Measurement) error {
|
||||
return e.MockSubmitAndUpdateMeasurementContext(ctx, measurement)
|
||||
}
|
||||
|
||||
func (e *Experiment) OpenReportContext(ctx context.Context) error {
|
||||
return e.MockOpenReportContext(ctx)
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestExperiment(t *testing.T) {
|
||||
t.Run("KibiBytesReceived", func(t *testing.T) {
|
||||
expected := 1.0
|
||||
e := &Experiment{
|
||||
MockKibiBytesReceived: func() float64 {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
if e.KibiBytesReceived() != expected {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("KibiBytesSent", func(t *testing.T) {
|
||||
expected := 1.0
|
||||
e := &Experiment{
|
||||
MockKibiBytesSent: func() float64 {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
if e.KibiBytesSent() != expected {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Name", func(t *testing.T) {
|
||||
expected := "antani"
|
||||
e := &Experiment{
|
||||
MockName: func() string {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
if e.Name() != expected {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetSummaryKeys", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockGetSummaryKeys: func(m *model.Measurement) (any, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := e.GetSummaryKeys(&model.Measurement{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("invalid out")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ReportID", func(t *testing.T) {
|
||||
expect := "xyz"
|
||||
e := &Experiment{
|
||||
MockReportID: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
if e.ReportID() != expect {
|
||||
t.Fatal("invalid value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MeasureAsync", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockMeasureAsync: func(ctx context.Context, input string) (<-chan *model.Measurement, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := e.MeasureAsync(context.Background(), "xo")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MeasureWithContext", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockMeasureWithContext: func(ctx context.Context, input string) (measurement *model.Measurement, err error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := e.MeasureWithContext(context.Background(), "xo")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SaveMeasurement", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockSaveMeasurement: func(measurement *model.Measurement, filePath string) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := e.SaveMeasurement(&model.Measurement{}, "x")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SubmitAndUpdateMeasurementContext", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockSubmitAndUpdateMeasurementContext: func(ctx context.Context, measurement *model.Measurement) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := e.SubmitAndUpdateMeasurementContext(context.Background(), &model.Measurement{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OpenReportContext", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
e := &Experiment{
|
||||
MockOpenReportContext: func(ctx context.Context) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := e.OpenReportContext(context.Background())
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package mocks
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/model"
|
||||
|
||||
// ExperimentBuilder mocks model.ExperimentBuilder.
|
||||
type ExperimentBuilder struct {
|
||||
MockInterruptible func() bool
|
||||
|
||||
MockInputPolicy func() model.InputPolicy
|
||||
|
||||
MockOptions func() (map[string]model.ExperimentOptionInfo, error)
|
||||
|
||||
MockSetOptionAny func(key string, value any) error
|
||||
|
||||
MockSetOptionsAny func(options map[string]any) error
|
||||
|
||||
MockSetCallbacks func(callbacks model.ExperimentCallbacks)
|
||||
|
||||
MockNewExperiment func() model.Experiment
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) Interruptible() bool {
|
||||
return eb.MockInterruptible()
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) InputPolicy() model.InputPolicy {
|
||||
return eb.MockInputPolicy()
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) Options() (map[string]model.ExperimentOptionInfo, error) {
|
||||
return eb.MockOptions()
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) SetOptionAny(key string, value any) error {
|
||||
return eb.MockSetOptionAny(key, value)
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) SetOptionsAny(options map[string]any) error {
|
||||
return eb.MockSetOptionsAny(options)
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) SetCallbacks(callbacks model.ExperimentCallbacks) {
|
||||
eb.MockSetCallbacks(callbacks)
|
||||
}
|
||||
|
||||
func (eb *ExperimentBuilder) NewExperiment() model.Experiment {
|
||||
return eb.MockNewExperiment()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestExperimentBuilder(t *testing.T) {
|
||||
t.Run("Interruptible", func(t *testing.T) {
|
||||
eb := &ExperimentBuilder{
|
||||
MockInterruptible: func() bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
if !eb.Interruptible() {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InputPolicy", func(t *testing.T) {
|
||||
eb := &ExperimentBuilder{
|
||||
MockInputPolicy: func() model.InputPolicy {
|
||||
return model.InputOrQueryBackend
|
||||
},
|
||||
}
|
||||
if eb.InputPolicy() != model.InputOrQueryBackend {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Options", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
eb := &ExperimentBuilder{
|
||||
MockOptions: func() (map[string]model.ExperimentOptionInfo, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := eb.Options()
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
if len(out) > 0 {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SetOptionAny", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
eb := &ExperimentBuilder{
|
||||
MockSetOptionAny: func(key string, value any) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := eb.SetOptionAny("antani", 1245678)
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SetOptionsAny", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
eb := &ExperimentBuilder{
|
||||
MockSetOptionsAny: func(options map[string]any) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := eb.SetOptionsAny(make(map[string]any))
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SetCallbacks", func(t *testing.T) {
|
||||
var called bool
|
||||
eb := &ExperimentBuilder{
|
||||
MockSetCallbacks: func(callbacks model.ExperimentCallbacks) {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
eb.SetCallbacks(model.NewPrinterCallbacks(model.DiscardLogger))
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NewExperiment", func(t *testing.T) {
|
||||
exp := &Experiment{}
|
||||
eb := &ExperimentBuilder{
|
||||
MockNewExperiment: func() model.Experiment {
|
||||
return exp
|
||||
},
|
||||
}
|
||||
if out := eb.NewExperiment(); out != exp {
|
||||
t.Fatal("invalid result")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// ExperimentInputLoader mocks model.ExperimentInputLoader
|
||||
type ExperimentInputLoader struct {
|
||||
MockLoad func(ctx context.Context) ([]model.OOAPIURLInfo, error)
|
||||
}
|
||||
|
||||
var _ model.ExperimentInputLoader = &ExperimentInputLoader{}
|
||||
|
||||
// Load calls MockLoad
|
||||
func (eil *ExperimentInputLoader) Load(ctx context.Context) ([]model.OOAPIURLInfo, error) {
|
||||
return eil.MockLoad(ctx)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestExperimentInputLoader(t *testing.T) {
|
||||
t.Run("Load", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
eil := &ExperimentInputLoader{
|
||||
MockLoad: func(ctx context.Context) ([]model.OOAPIURLInfo, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := eil.Load(context.Background())
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if len(out) > 0 {
|
||||
t.Fatal("unexpected length")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package mocks
|
||||
|
||||
import "context"
|
||||
|
||||
// ExperimentInputProcessor processes inputs running the given experiment.
|
||||
type ExperimentInputProcessor struct {
|
||||
MockRun func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func (eip *ExperimentInputProcessor) Run(ctx context.Context) error {
|
||||
return eip.MockRun(ctx)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExperimentInputProcessor(t *testing.T) {
|
||||
t.Run("Run", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
eip := &ExperimentInputProcessor{
|
||||
MockRun: func(ctx context.Context) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := eip.Run(context.Background())
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package mocks
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/model"
|
||||
|
||||
// Saver saves a measurement on some persistent storage.
|
||||
type Saver struct {
|
||||
MockSaveMeasurement func(m *model.Measurement) error
|
||||
}
|
||||
|
||||
func (s *Saver) SaveMeasurement(m *model.Measurement) error {
|
||||
return s.MockSaveMeasurement(m)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestSaver(t *testing.T) {
|
||||
t.Run("SaveMeasurement", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
s := &Saver{
|
||||
MockSaveMeasurement: func(m *model.Measurement) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := s.SaveMeasurement(&model.Measurement{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// Session allows to mock sessions.
|
||||
type Session struct {
|
||||
MockGetTestHelpersByName func(name string) ([]model.OOAPIService, bool)
|
||||
|
||||
MockDefaultHTTPClient func() model.HTTPClient
|
||||
|
||||
MockFetchPsiphonConfig func(ctx context.Context) ([]byte, error)
|
||||
|
||||
MockFetchTorTargets func(
|
||||
ctx context.Context, cc string) (map[string]model.OOAPITorTarget, error)
|
||||
|
||||
MockFetchURLList func(
|
||||
ctx context.Context, config model.OOAPIURLListConfig) ([]model.OOAPIURLInfo, error)
|
||||
|
||||
MockKeyValueStore func() model.KeyValueStore
|
||||
|
||||
MockLogger func() model.Logger
|
||||
|
||||
MockMaybeResolverIP func() string
|
||||
|
||||
MockProbeASNString func() string
|
||||
|
||||
MockProbeCC func() string
|
||||
|
||||
MockProbeIP func() string
|
||||
|
||||
MockProbeNetworkName func() string
|
||||
|
||||
MockProxyURL func() *url.URL
|
||||
|
||||
MockResolverIP func() string
|
||||
|
||||
MockSoftwareName func() string
|
||||
|
||||
MockSoftwareVersion func() string
|
||||
|
||||
MockTempDir func() string
|
||||
|
||||
MockTorArgs func() []string
|
||||
|
||||
MockTorBinary func() string
|
||||
|
||||
MockTunnelDir func() string
|
||||
|
||||
MockUserAgent func() string
|
||||
|
||||
MockNewExperimentBuilder func(name string) (model.ExperimentBuilder, error)
|
||||
|
||||
MockNewSubmitter func(ctx context.Context) (model.Submitter, error)
|
||||
|
||||
MockCheckIn func(ctx context.Context,
|
||||
config *model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error)
|
||||
}
|
||||
|
||||
func (sess *Session) GetTestHelpersByName(name string) ([]model.OOAPIService, bool) {
|
||||
return sess.MockGetTestHelpersByName(name)
|
||||
}
|
||||
|
||||
func (sess *Session) DefaultHTTPClient() model.HTTPClient {
|
||||
return sess.MockDefaultHTTPClient()
|
||||
}
|
||||
|
||||
func (sess *Session) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
|
||||
return sess.MockFetchPsiphonConfig(ctx)
|
||||
}
|
||||
|
||||
func (sess *Session) FetchTorTargets(
|
||||
ctx context.Context, cc string) (map[string]model.OOAPITorTarget, error) {
|
||||
return sess.MockFetchTorTargets(ctx, cc)
|
||||
}
|
||||
|
||||
func (sess *Session) FetchURLList(
|
||||
ctx context.Context, config model.OOAPIURLListConfig) ([]model.OOAPIURLInfo, error) {
|
||||
return sess.MockFetchURLList(ctx, config)
|
||||
}
|
||||
|
||||
func (sess *Session) KeyValueStore() model.KeyValueStore {
|
||||
return sess.MockKeyValueStore()
|
||||
}
|
||||
|
||||
func (sess *Session) Logger() model.Logger {
|
||||
return sess.MockLogger()
|
||||
}
|
||||
|
||||
func (sess *Session) MaybeResolverIP() string {
|
||||
return sess.MockMaybeResolverIP()
|
||||
}
|
||||
|
||||
func (sess *Session) ProbeASNString() string {
|
||||
return sess.MockProbeASNString()
|
||||
}
|
||||
|
||||
func (sess *Session) ProbeCC() string {
|
||||
return sess.MockProbeCC()
|
||||
}
|
||||
|
||||
func (sess *Session) ProbeIP() string {
|
||||
return sess.MockProbeIP()
|
||||
}
|
||||
|
||||
func (sess *Session) ProbeNetworkName() string {
|
||||
return sess.MockProbeNetworkName()
|
||||
}
|
||||
|
||||
func (sess *Session) ProxyURL() *url.URL {
|
||||
return sess.MockProxyURL()
|
||||
}
|
||||
|
||||
func (sess *Session) ResolverIP() string {
|
||||
return sess.MockResolverIP()
|
||||
}
|
||||
|
||||
func (sess *Session) SoftwareName() string {
|
||||
return sess.MockSoftwareName()
|
||||
}
|
||||
|
||||
func (sess *Session) SoftwareVersion() string {
|
||||
return sess.MockSoftwareVersion()
|
||||
}
|
||||
|
||||
func (sess *Session) TempDir() string {
|
||||
return sess.MockTempDir()
|
||||
}
|
||||
|
||||
func (sess *Session) TorArgs() []string {
|
||||
return sess.MockTorArgs()
|
||||
}
|
||||
|
||||
func (sess *Session) TorBinary() string {
|
||||
return sess.MockTorBinary()
|
||||
}
|
||||
|
||||
func (sess *Session) TunnelDir() string {
|
||||
return sess.MockTunnelDir()
|
||||
}
|
||||
|
||||
func (sess *Session) UserAgent() string {
|
||||
return sess.MockUserAgent()
|
||||
}
|
||||
|
||||
func (sess *Session) NewExperimentBuilder(name string) (model.ExperimentBuilder, error) {
|
||||
return sess.MockNewExperimentBuilder(name)
|
||||
}
|
||||
|
||||
func (sess *Session) NewSubmitter(ctx context.Context) (model.Submitter, error) {
|
||||
return sess.MockNewSubmitter(ctx)
|
||||
}
|
||||
|
||||
func (sess *Session) CheckIn(ctx context.Context,
|
||||
config *model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
|
||||
return sess.MockCheckIn(ctx, config)
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/testingx"
|
||||
)
|
||||
|
||||
func TestSession(t *testing.T) {
|
||||
t.Run("GetTestHelpersByName", func(t *testing.T) {
|
||||
var expect []model.OOAPIService
|
||||
ff := &testingx.FakeFiller{}
|
||||
ff.Fill(&expect)
|
||||
runtimex.Assert(len(expect) > 0, "expected non-empty array")
|
||||
s := &Session{
|
||||
MockGetTestHelpersByName: func(name string) ([]model.OOAPIService, bool) {
|
||||
return expect, len(expect) > 0
|
||||
},
|
||||
}
|
||||
out, good := s.GetTestHelpersByName("xx")
|
||||
if !good {
|
||||
t.Fatal("not good")
|
||||
}
|
||||
if diff := cmp.Diff(expect, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DefaultHTTPClient", func(t *testing.T) {
|
||||
expected := &HTTPClient{}
|
||||
s := &Session{
|
||||
MockDefaultHTTPClient: func() model.HTTPClient {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
out := s.DefaultHTTPClient()
|
||||
if expected != out {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("FetchPsiphonConfig", func(t *testing.T) {
|
||||
var expected []byte
|
||||
ff := &testingx.FakeFiller{}
|
||||
ff.Fill(&expected)
|
||||
runtimex.Assert(len(expected) > 0, "expected nonempty list")
|
||||
s := &Session{
|
||||
MockFetchPsiphonConfig: func(ctx context.Context) ([]byte, error) {
|
||||
return expected, nil
|
||||
},
|
||||
}
|
||||
out, err := s.FetchPsiphonConfig(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("FetchTorTargets", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
s := &Session{
|
||||
MockFetchTorTargets: func(ctx context.Context, cc string) (map[string]model.OOAPITorTarget, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := s.FetchTorTargets(context.Background(), "IT")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if len(out) > 0 {
|
||||
t.Fatal("expected empty out")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("FetchURLList", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
s := &Session{
|
||||
MockFetchURLList: func(ctx context.Context, config model.OOAPIURLListConfig) ([]model.OOAPIURLInfo, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := s.FetchURLList(context.Background(), model.OOAPIURLListConfig{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if len(out) > 0 {
|
||||
t.Fatal("expected empty out")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("KeyValueStore", func(t *testing.T) {
|
||||
expect := &KeyValueStore{}
|
||||
s := &Session{
|
||||
MockKeyValueStore: func() model.KeyValueStore {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.KeyValueStore()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Logger", func(t *testing.T) {
|
||||
expect := &Logger{}
|
||||
s := &Session{
|
||||
MockLogger: func() model.Logger {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.Logger()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MaybeResolverIP", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockMaybeResolverIP: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.MaybeResolverIP()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProbeASNString", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockProbeASNString: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ProbeASNString()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProbeCC", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockProbeCC: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ProbeCC()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProbeIP", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockProbeIP: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ProbeIP()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProbeNetworkName", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockProbeNetworkName: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ProbeNetworkName()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProxyURL", func(t *testing.T) {
|
||||
expect := &url.URL{Scheme: "xx"}
|
||||
s := &Session{
|
||||
MockProxyURL: func() *url.URL {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ProxyURL()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ResolverIP", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockResolverIP: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.ResolverIP()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SoftwareName", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockSoftwareName: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.SoftwareName()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SoftwareVersion", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockSoftwareVersion: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.SoftwareVersion()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TempDir", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockTempDir: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.TempDir()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TorArgs", func(t *testing.T) {
|
||||
var expect []string
|
||||
ff := &testingx.FakeFiller{}
|
||||
ff.Fill(&expect)
|
||||
runtimex.Assert(len(expect) > 0, "expected non empty slice")
|
||||
s := &Session{
|
||||
MockTorArgs: func() []string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.TorArgs()
|
||||
if diff := cmp.Diff(expect, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TorBinary", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockTorBinary: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.TorBinary()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TunnelDir", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockTunnelDir: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.TunnelDir()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UserAgent", func(t *testing.T) {
|
||||
expect := "xx"
|
||||
s := &Session{
|
||||
MockUserAgent: func() string {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
out := s.UserAgent()
|
||||
if out != expect {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NewExperimentBuilder", func(t *testing.T) {
|
||||
eb := &ExperimentBuilder{}
|
||||
s := &Session{
|
||||
MockNewExperimentBuilder: func(name string) (model.ExperimentBuilder, error) {
|
||||
return eb, nil
|
||||
},
|
||||
}
|
||||
out, err := s.NewExperimentBuilder("x")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != eb {
|
||||
t.Fatal("invalid output")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NewSubmitter", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
s := &Session{
|
||||
MockNewSubmitter: func(ctx context.Context) (model.Submitter, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := s.NewSubmitter(context.Background())
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("unexpected out")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckIn", func(t *testing.T) {
|
||||
expected := errors.New("mocked err")
|
||||
s := &Session{
|
||||
MockCheckIn: func(ctx context.Context, config *model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := s.CheckIn(context.Background(), &model.OOAPICheckInConfig{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("unexpected out")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// Submitter mocks model.Submitter.
|
||||
type Submitter struct {
|
||||
MockSubmit func(ctx context.Context, m *model.Measurement) error
|
||||
}
|
||||
|
||||
// Submit calls MockSubmit
|
||||
func (s *Submitter) Submit(ctx context.Context, m *model.Measurement) error {
|
||||
return s.MockSubmit(ctx, m)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestSubmitter(t *testing.T) {
|
||||
t.Run("Submit", func(t *testing.T) {
|
||||
expect := errors.New("mocked error")
|
||||
s := &Submitter{
|
||||
MockSubmit: func(ctx context.Context, m *model.Measurement) error {
|
||||
return expect
|
||||
},
|
||||
}
|
||||
err := s.Submit(context.Background(), &model.Measurement{})
|
||||
if !errors.Is(err, expect) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user