2020-12-01 15:04:04 +01:00
|
|
|
package nettests
|
|
|
|
|
|
|
|
import (
|
2021-04-05 15:28:13 +02:00
|
|
|
"context"
|
2022-05-18 15:32:38 +02:00
|
|
|
"os"
|
2021-03-30 11:59:29 +02:00
|
|
|
"sync"
|
2021-03-30 11:16:12 +02:00
|
|
|
"time"
|
|
|
|
|
2020-12-01 15:04:04 +01:00
|
|
|
"github.com/apex/log"
|
2021-02-02 10:32:46 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
|
2022-04-29 13:41:09 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2020-12-01 15:04:04 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RunGroupConfig contains the settings for running a nettest group.
|
|
|
|
type RunGroupConfig struct {
|
|
|
|
GroupName string
|
|
|
|
InputFiles []string
|
|
|
|
Inputs []string
|
2021-03-30 11:59:29 +02:00
|
|
|
Probe *ooni.Probe
|
2022-04-29 13:41:09 +02:00
|
|
|
RunType model.RunType // hint for check-in API
|
2020-12-01 15:04:04 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 11:16:12 +02:00
|
|
|
const websitesURLLimitRemoved = `WARNING: CONFIGURATION CHANGE REQUIRED:
|
|
|
|
|
|
|
|
* Since ooniprobe 3.9.0, websites_url_limit has been replaced
|
|
|
|
by websites_max_runtime in the configuration
|
|
|
|
|
|
|
|
* To silence this warning either set websites_url_limit to zero or
|
|
|
|
replace it with websites_max_runtime
|
|
|
|
|
|
|
|
* For the rest of 2021, we will automatically convert websites_url_limit
|
|
|
|
to websites_max_runtime (if the latter is not already set)
|
|
|
|
|
|
|
|
* We will consider that each URL in websites_url_limit takes five
|
|
|
|
seconds to run and thus calculate websites_max_runtime
|
|
|
|
|
|
|
|
* Since 2022, we will start silently ignoring websites_url_limit
|
|
|
|
`
|
|
|
|
|
2021-03-30 11:59:29 +02:00
|
|
|
var deprecationWarningOnce sync.Once
|
|
|
|
|
2020-12-01 15:04:04 +01:00
|
|
|
// RunGroup runs a group of nettests according to the specified config.
|
|
|
|
func RunGroup(config RunGroupConfig) error {
|
2021-03-30 11:16:12 +02:00
|
|
|
if config.Probe.Config().Nettests.WebsitesURLLimit > 0 {
|
|
|
|
if config.Probe.Config().Nettests.WebsitesMaxRuntime <= 0 {
|
|
|
|
limit := config.Probe.Config().Nettests.WebsitesURLLimit
|
|
|
|
maxRuntime := 5 * limit
|
|
|
|
config.Probe.Config().Nettests.WebsitesMaxRuntime = maxRuntime
|
|
|
|
}
|
2021-03-30 11:59:29 +02:00
|
|
|
deprecationWarningOnce.Do(func() {
|
|
|
|
log.Warn(websitesURLLimitRemoved)
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
})
|
2021-03-30 11:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Probe.IsTerminated() {
|
2020-12-01 15:04:04 +01:00
|
|
|
log.Debugf("context is terminated, stopping runNettestGroup early")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:41:09 +02:00
|
|
|
sess, err := config.Probe.NewSession(context.Background(), config.RunType)
|
2020-12-01 15:04:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to create a measurement session")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
err = sess.MaybeLookupLocation()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to lookup the location of the probe")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
network, err := database.CreateNetwork(config.Probe.DB(), sess)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to create the network row")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sess.MaybeLookupBackends(); err != nil {
|
|
|
|
log.WithError(err).Warn("Failed to discover OONI backends")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:52:12 +01:00
|
|
|
group, ok := All[config.GroupName]
|
2020-12-01 15:04:04 +01:00
|
|
|
if !ok {
|
|
|
|
log.Errorf("No test group named %s", config.GroupName)
|
|
|
|
return errors.New("invalid test group name")
|
|
|
|
}
|
|
|
|
log.Debugf("Running test group %s", group.Label)
|
|
|
|
|
|
|
|
result, err := database.CreateResult(
|
|
|
|
config.Probe.DB(), config.Probe.Home(), config.GroupName, network.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("DB result error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Probe.ListenForSignals()
|
|
|
|
config.Probe.MaybeListenForStdinClosed()
|
|
|
|
for i, nt := range group.Nettests {
|
2021-03-30 11:59:29 +02:00
|
|
|
if config.Probe.IsTerminated() {
|
2020-12-01 15:04:04 +01:00
|
|
|
log.Debugf("context is terminated, stopping group.Nettests early")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Debugf("Running test %T", nt)
|
|
|
|
ctl := NewController(nt, config.Probe, result, sess)
|
|
|
|
ctl.InputFiles = config.InputFiles
|
|
|
|
ctl.Inputs = config.Inputs
|
2021-03-30 11:59:29 +02:00
|
|
|
ctl.RunType = config.RunType
|
2020-12-01 15:04:04 +01:00
|
|
|
ctl.SetNettestIndex(i, len(group.Nettests))
|
|
|
|
if err = nt.Run(ctl); err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to run %s", group.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:32:38 +02:00
|
|
|
// Remove the directory if it's emtpy, which happens when the corresponding
|
|
|
|
// measurements have been submitted (see https://github.com/ooni/probe/issues/2090)
|
|
|
|
dir, err := os.Open(result.MeasurementDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
_, err = dir.Readdirnames(1)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(result.MeasurementDir)
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:04:04 +01:00
|
|
|
if err = result.Finished(config.Probe.DB()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|