ooni-probe-cli/internal/engine/experiment/run/run_test.go
Simone Basso 273b70bacc
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
2022-01-03 13:53:23 +01:00

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/mockable"
"github.com/ooni/probe-cli/v3/internal/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)
}
}