fix: ensure experiments return nil when we want to submit (#654)

Since https://github.com/ooni/probe-cli/pull/527, if an experiment
returns an error, the corresponding measurement is not submitted since
the semantics of returning an error is that something fundamental
went wrong (e.g., we could not parse the input URL).

This diff ensures that all experiments only return and error when
something fundamental was wrong and return nil otherwise.

Reference issue: https://github.com/ooni/probe/issues/1808.
This commit is contained in:
Simone Basso
2022-01-07 13:17:20 +01:00
committed by GitHub
parent 60a3c372f5
commit 99ec7ffca9
27 changed files with 132 additions and 66 deletions
+3
View File
@@ -55,6 +55,9 @@ func (mgr dialManager) dialWithTestName(ctx context.Context, testName string) (*
headers.Add("User-Agent", mgr.userAgent)
mgr.logrequest(mgr.ndt7URL, headers)
conn, _, err := dialer.DialContext(ctx, mgr.ndt7URL, headers)
if err != nil {
err = netxlite.NewTopLevelGenericErrWrapper(err)
}
mgr.logresponse(err)
return conn, err
}
+3 -3
View File
@@ -6,11 +6,11 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/apex/log"
"github.com/gorilla/websocket"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestDialDownloadWithCancelledContext(t *testing.T) {
@@ -18,7 +18,7 @@ func TestDialDownloadWithCancelledContext(t *testing.T) {
cancel() // immediately halt
mgr := newDialManager("wss://hostname.fake", log.Log, "miniooni/0.1.0-dev")
conn, err := mgr.dialDownload(ctx)
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
if err == nil || err.Error() != netxlite.FailureInterrupted {
t.Fatal("not the error we expected", err)
}
if conn != nil {
@@ -31,7 +31,7 @@ func TestDialUploadWithCancelledContext(t *testing.T) {
cancel() // immediately halt
mgr := newDialManager("wss://hostname.fake", log.Log, "miniooni/0.1.0-dev")
conn, err := mgr.dialUpload(ctx)
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
if err == nil || err.Error() != netxlite.FailureInterrupted {
t.Fatal("not the error we expected", err)
}
if conn != nil {
+4
View File
@@ -0,0 +1,4 @@
// Package ndt7 contains the ndt7 network experiment.
//
// See https://github.com/ooni/spec/blob/master/nettests/ts-022-ndt.md
package ndt7
+6 -14
View File
@@ -1,6 +1,3 @@
// Package ndt7 contains the ndt7 network experiment.
//
// See https://github.com/ooni/spec/blob/master/nettests/ts-022-ndt.md
package ndt7
import (
@@ -8,18 +5,17 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/humanize"
"github.com/ooni/probe-cli/v3/internal/mlablocatev2"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
const (
testName = "ndt"
testVersion = "0.9.0"
testVersion = "0.10.0"
)
// Config contains the experiment settings
@@ -80,11 +76,7 @@ type Measurer struct {
func (m *Measurer) discover(
ctx context.Context, sess model.ExperimentSession) (mlablocatev2.NDT7Result, error) {
httpClient := &http.Client{
Transport: netx.NewHTTPTransport(netx.Config{
Logger: sess.Logger(),
}),
}
httpClient := netxlite.NewHTTPClientStdlib(sess.Logger())
defer httpClient.CloseIdleConnections()
client := mlablocatev2.NewClient(httpClient, sess.Logger(), sess.UserAgent())
out, err := client.QueryNDT7(ctx)
@@ -228,7 +220,7 @@ func (m *Measurer) Run(
locateResult, err := m.discover(ctx, sess)
if err != nil {
tk.Failure = failureFromError(err)
return err
return nil // we still want to submit this measurement
}
tk.Server = ServerInfo{
Hostname: locateResult.Hostname,
@@ -240,7 +232,7 @@ func (m *Measurer) Run(
}
if err := m.doDownload(ctx, sess, callbacks, tk, locateResult.WSSDownloadURL); err != nil {
tk.Failure = failureFromError(err)
return err
return nil // we still want to submit this measurement
}
callbacks.OnProgress(0.5, fmt.Sprintf(" upload: url: %s", locateResult.WSSUploadURL))
if m.preUploadHook != nil {
@@ -248,7 +240,7 @@ func (m *Measurer) Run(
}
if err := m.doUpload(ctx, sess, callbacks, tk, locateResult.WSSUploadURL); err != nil {
tk.Failure = failureFromError(err)
return err
return nil // we still want to submit this measurement
}
return nil
}
+40 -12
View File
@@ -4,12 +4,12 @@ import (
"context"
"errors"
"net/http"
"strings"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestNewExperimentMeasurer(t *testing.T) {
@@ -17,7 +17,7 @@ func TestNewExperimentMeasurer(t *testing.T) {
if measurer.ExperimentName() != "ndt" {
t.Fatal("unexpected name")
}
if measurer.ExperimentVersion() != "0.9.0" {
if measurer.ExperimentVersion() != "0.10.0" {
t.Fatal("unexpected version")
}
}
@@ -52,7 +52,7 @@ func TestDoDownloadWithCancelledContext(t *testing.T) {
err := m.doDownload(
ctx, sess, model.NewPrinterCallbacks(log.Log), new(TestKeys),
"ws://host.name")
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
if err == nil || err.Error() != netxlite.FailureInterrupted {
t.Fatal("not the error we expected", err)
}
}
@@ -69,7 +69,7 @@ func TestDoUploadWithCancelledContext(t *testing.T) {
err := m.doUpload(
ctx, sess, model.NewPrinterCallbacks(log.Log), new(TestKeys),
"ws://host.name")
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
if err == nil || err.Error() != netxlite.FailureInterrupted {
t.Fatal("not the error we expected", err)
}
}
@@ -83,10 +83,19 @@ func TestRunWithCancelledContext(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately cancel
err := m.Run(ctx, sess, new(model.Measurement), model.NewPrinterCallbacks(log.Log))
if !errors.Is(err, context.Canceled) {
meas := &model.Measurement{}
err := m.Run(ctx, sess, meas, model.NewPrinterCallbacks(log.Log))
// Here we get nil because we still want to submit this measurement
if !errors.Is(err, nil) {
t.Fatal("not the error we expected")
}
if meas.TestKeys == nil {
t.Fatal("nil test keys")
}
tk := meas.TestKeys.(*TestKeys)
if tk.Failure == nil || *tk.Failure != netxlite.FailureInterrupted {
t.Fatal("unexpected tk.Failure")
}
}
func TestGood(t *testing.T) {
@@ -123,17 +132,27 @@ func TestFailDownload(t *testing.T) {
measurer.preDownloadHook = func() {
cancel()
}
meas := &model.Measurement{}
err := measurer.Run(
ctx,
&mockable.Session{
MockableHTTPClient: http.DefaultClient,
MockableLogger: log.Log,
},
new(model.Measurement),
meas,
model.NewPrinterCallbacks(log.Log),
)
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
t.Fatal("not the error we expected", err)
// We expect a nil failure here because we want to submit anyway
// a measurement that failed to connect to m-lab.
if err != nil {
t.Fatal(err)
}
if meas.TestKeys == nil {
t.Fatal("expected non-nil TestKeys here")
}
tk := meas.TestKeys.(*TestKeys)
if tk.Failure == nil || *tk.Failure != netxlite.FailureInterrupted {
t.Fatal("unexpected tk.Failure")
}
}
@@ -144,17 +163,26 @@ func TestFailUpload(t *testing.T) {
measurer.preUploadHook = func() {
cancel()
}
meas := &model.Measurement{}
err := measurer.Run(
ctx,
&mockable.Session{
MockableHTTPClient: http.DefaultClient,
MockableLogger: log.Log,
},
new(model.Measurement),
meas,
model.NewPrinterCallbacks(log.Log),
)
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
t.Fatal("not the error we expected", err)
// Here we expect a nil error because we want to submit this measurement
if err != nil {
t.Fatal(err)
}
if meas.TestKeys == nil {
t.Fatal("expected non-nil tk.TestKeys here")
}
tk := meas.TestKeys.(*TestKeys)
if tk.Failure == nil || *tk.Failure != netxlite.FailureInterrupted {
t.Fatal("unexpected tk.Failure value")
}
}