chore: merge probe-engine into probe-cli (#201)
This is how I did it: 1. `git clone https://github.com/ooni/probe-engine internal/engine` 2. ``` (cd internal/engine && git describe --tags) v0.23.0 ``` 3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod` 4. `rm -rf internal/.git internal/engine/go.{mod,sum}` 5. `git add internal/engine` 6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;` 7. `go build ./...` (passes) 8. `go test -race ./...` (temporary failure on RiseupVPN) 9. `go mod tidy` 10. this commit message Once this piece of work is done, we can build a new version of `ooniprobe` that is using `internal/engine` directly. We need to do more work to ensure all the other functionality in `probe-engine` (e.g. making mobile packages) are still WAI. Part of https://github.com/ooni/probe/issues/1335
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
// Package psiphon implements the psiphon network experiment. This
|
||||
// implements, in particular, v0.2.0 of the spec.
|
||||
//
|
||||
// See https://github.com/ooni/spec/blob/master/nettests/ts-015-psiphon.md
|
||||
package psiphon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
)
|
||||
|
||||
const (
|
||||
testName = "psiphon"
|
||||
testVersion = "0.5.0"
|
||||
)
|
||||
|
||||
// Config contains the experiment's configuration.
|
||||
type Config struct{}
|
||||
|
||||
// TestKeys contains the experiment's result.
|
||||
type TestKeys struct {
|
||||
urlgetter.TestKeys
|
||||
MaxRuntime float64 `json:"max_runtime"`
|
||||
}
|
||||
|
||||
// Measurer is the psiphon measurer.
|
||||
type Measurer struct {
|
||||
BeforeGetHook func(g urlgetter.Getter)
|
||||
Config Config
|
||||
}
|
||||
|
||||
// ExperimentName returns the experiment name
|
||||
func (m *Measurer) ExperimentName() string {
|
||||
return testName
|
||||
}
|
||||
|
||||
// ExperimentVersion returns the experiment version
|
||||
func (m *Measurer) ExperimentVersion() string {
|
||||
return testVersion
|
||||
}
|
||||
|
||||
func (m *Measurer) printprogress(
|
||||
ctx context.Context, wg *sync.WaitGroup,
|
||||
maxruntime int, callbacks model.ExperimentCallbacks,
|
||||
) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
step := 1 / float64(maxruntime)
|
||||
var progress float64
|
||||
defer callbacks.OnProgress(1.0, "psiphon experiment complete")
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
progress += step
|
||||
callbacks.OnProgress(progress, "psiphon experiment running")
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run runs the measurement
|
||||
func (m *Measurer) Run(
|
||||
ctx context.Context, sess model.ExperimentSession,
|
||||
measurement *model.Measurement, callbacks model.ExperimentCallbacks,
|
||||
) error {
|
||||
const maxruntime = 60
|
||||
ctx, cancel := context.WithTimeout(ctx, maxruntime*time.Second)
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
config urlgetter.Config
|
||||
)
|
||||
wg.Add(1)
|
||||
go m.printprogress(ctx, &wg, maxruntime, callbacks)
|
||||
config.Tunnel = "psiphon" // force to use psiphon tunnel
|
||||
urlgetter.RegisterExtensions(measurement)
|
||||
target := "https://www.google.com/humans.txt"
|
||||
if measurement.Input != "" {
|
||||
target = string(measurement.Input)
|
||||
}
|
||||
g := urlgetter.Getter{
|
||||
Config: config,
|
||||
Session: sess,
|
||||
Target: target,
|
||||
}
|
||||
if m.BeforeGetHook != nil {
|
||||
m.BeforeGetHook(g)
|
||||
}
|
||||
tk, err := g.Get(ctx)
|
||||
cancel()
|
||||
wg.Wait()
|
||||
measurement.TestKeys = &TestKeys{
|
||||
TestKeys: tk,
|
||||
MaxRuntime: maxruntime,
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// NewExperimentMeasurer creates a new ExperimentMeasurer.
|
||||
func NewExperimentMeasurer(config Config) model.ExperimentMeasurer {
|
||||
return &Measurer{Config: config}
|
||||
}
|
||||
|
||||
// SummaryKeys contains summary keys for this experiment.
|
||||
//
|
||||
// Note that this structure is part of the ABI contract with probe-cli
|
||||
// therefore we should be careful when changing it.
|
||||
type SummaryKeys struct {
|
||||
BootstrapTime float64 `json:"bootstrap_time"`
|
||||
Failure string `json:"failure"`
|
||||
IsAnomaly bool `json:"-"`
|
||||
}
|
||||
|
||||
// GetSummaryKeys implements model.ExperimentMeasurer.GetSummaryKeys.
|
||||
func (m Measurer) GetSummaryKeys(measurement *model.Measurement) (interface{}, error) {
|
||||
sk := SummaryKeys{IsAnomaly: false}
|
||||
tk, ok := measurement.TestKeys.(*TestKeys)
|
||||
if !ok {
|
||||
return sk, errors.New("invalid test keys type")
|
||||
}
|
||||
if tk.Failure != nil {
|
||||
sk.Failure = *tk.Failure
|
||||
sk.IsAnomaly = true
|
||||
}
|
||||
sk.BootstrapTime = tk.BootstrapTime
|
||||
return sk, nil
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package psiphon_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/psiphon"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
)
|
||||
|
||||
// Implementation note: integration test performed by
|
||||
// the $topdir/experiment_test.go file
|
||||
|
||||
func TestNewExperimentMeasurer(t *testing.T) {
|
||||
measurer := psiphon.NewExperimentMeasurer(psiphon.Config{})
|
||||
if measurer.ExperimentName() != "psiphon" {
|
||||
t.Fatal("unexpected name")
|
||||
}
|
||||
if measurer.ExperimentVersion() != "0.5.0" {
|
||||
t.Fatal("unexpected version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunWithCancelledContext(t *testing.T) {
|
||||
measurer := psiphon.NewExperimentMeasurer(psiphon.Config{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
measurement := new(model.Measurement)
|
||||
err := measurer.Run(ctx, newfakesession(), measurement,
|
||||
model.NewPrinterCallbacks(log.Log))
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
tk := measurement.TestKeys.(*psiphon.TestKeys)
|
||||
if tk.MaxRuntime <= 0 {
|
||||
t.Fatal("you did not set the max runtime")
|
||||
}
|
||||
sk, err := measurer.GetSummaryKeys(measurement)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := sk.(psiphon.SummaryKeys); !ok {
|
||||
t.Fatal("invalid type for summary keys")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunWithCustomInputAndCancelledContext(t *testing.T) {
|
||||
expected := "http://x.org"
|
||||
measurement := &model.Measurement{
|
||||
Input: model.MeasurementTarget(expected),
|
||||
}
|
||||
measurer := psiphon.NewExperimentMeasurer(psiphon.Config{})
|
||||
measurer.(*psiphon.Measurer).BeforeGetHook = func(g urlgetter.Getter) {
|
||||
if g.Target != expected {
|
||||
t.Fatal("target was not correctly set")
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
err := measurer.Run(ctx, newfakesession(), measurement,
|
||||
model.NewPrinterCallbacks(log.Log))
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
tk := measurement.TestKeys.(*psiphon.TestKeys)
|
||||
if tk.MaxRuntime <= 0 {
|
||||
t.Fatal("you did not set the max runtime")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunWillPrintSomethingWithCancelledContext(t *testing.T) {
|
||||
measurement := new(model.Measurement)
|
||||
measurer := psiphon.NewExperimentMeasurer(psiphon.Config{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
measurer.(*psiphon.Measurer).BeforeGetHook = func(g urlgetter.Getter) {
|
||||
time.Sleep(2 * time.Second)
|
||||
cancel() // fail after we've given the printer a chance to run
|
||||
}
|
||||
observer := observerCallbacks{progress: atomicx.NewInt64()}
|
||||
err := measurer.Run(ctx, newfakesession(), measurement, observer)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
tk := measurement.TestKeys.(*psiphon.TestKeys)
|
||||
if tk.MaxRuntime <= 0 {
|
||||
t.Fatal("you did not set the max runtime")
|
||||
}
|
||||
if observer.progress.Load() < 2 {
|
||||
t.Fatal("not enough progress emitted?!")
|
||||
}
|
||||
}
|
||||
|
||||
type observerCallbacks struct {
|
||||
progress *atomicx.Int64
|
||||
}
|
||||
|
||||
func (d observerCallbacks) OnProgress(percentage float64, message string) {
|
||||
d.progress.Add(1)
|
||||
}
|
||||
|
||||
func newfakesession() model.ExperimentSession {
|
||||
return &mockable.Session{MockableLogger: log.Log}
|
||||
}
|
||||
|
||||
func TestSummaryKeysInvalidType(t *testing.T) {
|
||||
measurement := new(model.Measurement)
|
||||
m := &psiphon.Measurer{}
|
||||
_, err := m.GetSummaryKeys(measurement)
|
||||
if err.Error() != "invalid test keys type" {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummaryKeysGood(t *testing.T) {
|
||||
measurement := &model.Measurement{TestKeys: &psiphon.TestKeys{TestKeys: urlgetter.TestKeys{
|
||||
BootstrapTime: 123,
|
||||
}}}
|
||||
m := &psiphon.Measurer{}
|
||||
osk, err := m.GetSummaryKeys(measurement)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sk := osk.(psiphon.SummaryKeys)
|
||||
if sk.BootstrapTime != 123 {
|
||||
t.Fatal("invalid latency")
|
||||
}
|
||||
if sk.Failure != "" {
|
||||
t.Fatal("invalid failure")
|
||||
}
|
||||
if sk.IsAnomaly {
|
||||
t.Fatal("invalid isAnomaly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummaryKeysFailure(t *testing.T) {
|
||||
expected := io.EOF.Error()
|
||||
measurement := &model.Measurement{TestKeys: &psiphon.TestKeys{TestKeys: urlgetter.TestKeys{
|
||||
BootstrapTime: 123,
|
||||
Failure: &expected,
|
||||
}}}
|
||||
m := &psiphon.Measurer{}
|
||||
osk, err := m.GetSummaryKeys(measurement)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sk := osk.(psiphon.SummaryKeys)
|
||||
if sk.BootstrapTime != 123 {
|
||||
t.Fatal("invalid latency")
|
||||
}
|
||||
if sk.Failure != expected {
|
||||
t.Fatal("invalid failure")
|
||||
}
|
||||
if sk.IsAnomaly == false {
|
||||
t.Fatal("invalid isAnomaly")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user