d57c78bc71
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
108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package run_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/dnscheck"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/run"
|
|
"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"
|
|
)
|
|
|
|
func TestExperimentNameAndVersion(t *testing.T) {
|
|
measurer := run.NewExperimentMeasurer(run.Config{})
|
|
if measurer.ExperimentName() != "run" {
|
|
t.Error("unexpected experiment name")
|
|
}
|
|
if measurer.ExperimentVersion() != "0.2.0" {
|
|
t.Error("unexpected experiment version")
|
|
}
|
|
}
|
|
|
|
func TestRunDNSCheckWithCancelledContext(t *testing.T) {
|
|
measurer := run.NewExperimentMeasurer(run.Config{})
|
|
input := `{"name": "dnscheck", "input": "https://dns.google/dns-query"}`
|
|
measurement := new(model.Measurement)
|
|
measurement.Input = model.MeasurementTarget(input)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // fail immediately
|
|
sess := &mockable.Session{MockableLogger: log.Log}
|
|
callbacks := model.NewPrinterCallbacks(log.Log)
|
|
err := measurer.Run(ctx, sess, measurement, callbacks)
|
|
// TODO(bassosimone): here we could improve the tests by checking
|
|
// whether the result makes sense for a cancelled context.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := measurement.TestKeys.(*dnscheck.TestKeys); !ok {
|
|
t.Fatal("invalid type for test keys")
|
|
}
|
|
sk, err := measurer.GetSummaryKeys(measurement)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rsk, ok := sk.(dnscheck.SummaryKeys)
|
|
if !ok {
|
|
t.Fatal("cannot convert summary keys to specific type")
|
|
}
|
|
if rsk.IsAnomaly != false {
|
|
t.Fatal("unexpected IsAnomaly value")
|
|
}
|
|
}
|
|
|
|
func TestRunURLGetterWithCancelledContext(t *testing.T) {
|
|
measurer := run.NewExperimentMeasurer(run.Config{})
|
|
input := `{"name": "urlgetter", "input": "https://google.com"}`
|
|
measurement := new(model.Measurement)
|
|
measurement.Input = model.MeasurementTarget(input)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // fail immediately
|
|
sess := &mockable.Session{MockableLogger: log.Log}
|
|
callbacks := model.NewPrinterCallbacks(log.Log)
|
|
err := measurer.Run(ctx, sess, measurement, callbacks)
|
|
if err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(measurement.Extensions) != 6 {
|
|
t.Fatal("not the expected number of extensions")
|
|
}
|
|
tk, ok := measurement.TestKeys.(*urlgetter.TestKeys)
|
|
if !ok {
|
|
t.Fatal("invalid type for test keys")
|
|
}
|
|
if len(tk.DNSCache) != 0 {
|
|
t.Fatal("not the DNSCache value we expected")
|
|
}
|
|
}
|
|
|
|
func TestRunWithInvalidJSON(t *testing.T) {
|
|
measurer := run.NewExperimentMeasurer(run.Config{})
|
|
input := `{"name": }`
|
|
measurement := new(model.Measurement)
|
|
measurement.Input = model.MeasurementTarget(input)
|
|
ctx := context.Background()
|
|
sess := &mockable.Session{MockableLogger: log.Log}
|
|
callbacks := model.NewPrinterCallbacks(log.Log)
|
|
err := measurer.Run(ctx, sess, measurement, callbacks)
|
|
if err == nil || err.Error() != "invalid character '}' looking for beginning of value" {
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
}
|
|
}
|
|
|
|
func TestRunWithUnknownExperiment(t *testing.T) {
|
|
measurer := run.NewExperimentMeasurer(run.Config{})
|
|
input := `{"name": "antani"}`
|
|
measurement := new(model.Measurement)
|
|
measurement.Input = model.MeasurementTarget(input)
|
|
ctx := context.Background()
|
|
sess := &mockable.Session{MockableLogger: log.Log}
|
|
callbacks := model.NewPrinterCallbacks(log.Log)
|
|
err := measurer.Run(ctx, sess, measurement, callbacks)
|
|
if err == nil || err.Error() != "no such experiment: antani" {
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
}
|
|
}
|