2018-02-07 19:02:18 +01:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2018-03-22 15:22:29 +01:00
|
|
|
"errors"
|
2018-02-13 17:11:22 +01:00
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
"github.com/alecthomas/kingpin"
|
2018-02-12 16:45:13 +01:00
|
|
|
"github.com/apex/log"
|
2018-06-29 16:50:05 +02:00
|
|
|
"github.com/fatih/color"
|
2019-12-02 14:15:50 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/onboard"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/root"
|
|
|
|
"github.com/ooni/probe-cli/internal/database"
|
2020-11-13 17:14:26 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/nettests"
|
2020-11-13 18:42:10 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/ooni"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
2020-12-01 07:21:33 +01:00
|
|
|
type runNettestGroupConfig struct {
|
|
|
|
tg string
|
|
|
|
ctx *ooni.Probe
|
|
|
|
inputFiles []string
|
|
|
|
inputs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func runNettestGroup(config runNettestGroupConfig) error {
|
|
|
|
if config.ctx.IsTerminated() == true {
|
2020-02-20 12:24:24 +01:00
|
|
|
log.Debugf("context is terminated, stopping runNettestGroup early")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:21:33 +01:00
|
|
|
sess, err := config.ctx.NewSession()
|
2020-06-04 11:19:38 +02: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
|
|
|
|
}
|
2020-12-01 07:21:33 +01:00
|
|
|
network, err := database.CreateNetwork(config.ctx.DB(), sess)
|
2020-06-04 11:19:38 +02:00
|
|
|
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 07:21:33 +01:00
|
|
|
group, ok := nettests.NettestGroups[config.tg]
|
2018-09-27 18:38:37 +02:00
|
|
|
if !ok {
|
2020-12-01 07:21:33 +01:00
|
|
|
log.Errorf("No test group named %s", config.tg)
|
2018-09-27 18:38:37 +02:00
|
|
|
return errors.New("invalid test group name")
|
|
|
|
}
|
|
|
|
log.Debugf("Running test group %s", group.Label)
|
|
|
|
|
2020-12-01 07:21:33 +01:00
|
|
|
result, err := database.CreateResult(
|
|
|
|
config.ctx.DB(), config.ctx.Home(), config.tg, network.ID)
|
2018-09-27 18:38:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("DB result error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:21:33 +01:00
|
|
|
config.ctx.ListenForSignals()
|
|
|
|
config.ctx.MaybeListenForStdinClosed()
|
2018-09-27 18:38:37 +02:00
|
|
|
for i, nt := range group.Nettests {
|
2020-12-01 07:21:33 +01:00
|
|
|
if config.ctx.IsTerminated() == true {
|
2020-02-20 12:24:24 +01:00
|
|
|
log.Debugf("context is terminated, stopping group.Nettests early")
|
2019-12-27 11:32:08 +01:00
|
|
|
break
|
|
|
|
}
|
2018-09-27 18:38:37 +02:00
|
|
|
log.Debugf("Running test %T", nt)
|
2020-12-01 07:21:33 +01:00
|
|
|
ctl := nettests.NewController(nt, config.ctx, result, sess)
|
|
|
|
ctl.InputFiles = config.inputFiles
|
|
|
|
ctl.Inputs = config.inputs
|
2018-09-27 18:38:37 +02:00
|
|
|
ctl.SetNettestIndex(i, len(group.Nettests))
|
|
|
|
if err = nt.Run(ctl); err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to run %s", group.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:21:33 +01:00
|
|
|
if err = result.Finished(config.ctx.DB()); err != nil {
|
2018-09-27 18:38:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
func init() {
|
|
|
|
cmd := root.Command("run", "Run a test group or OONI Run link")
|
|
|
|
|
2018-09-27 18:38:37 +02:00
|
|
|
var nettestGroupNamesBlue []string
|
2020-11-13 19:01:06 +01:00
|
|
|
var probe *ooni.Probe
|
2019-10-02 18:23:14 +02:00
|
|
|
|
2019-12-02 17:05:02 +01:00
|
|
|
for name := range nettests.NettestGroups {
|
2018-09-27 18:38:37 +02:00
|
|
|
nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
|
2018-06-22 14:55:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 11:51:54 +02:00
|
|
|
noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool()
|
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
2019-10-02 18:23:14 +02:00
|
|
|
var err error
|
2020-11-13 19:01:06 +01:00
|
|
|
probe, err = root.Init()
|
2018-02-12 16:45:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s", err)
|
|
|
|
return err
|
|
|
|
}
|
2018-06-22 11:01:15 +02:00
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
if err = onboard.MaybeOnboarding(probe); err != nil {
|
2018-06-22 11:01:15 +02:00
|
|
|
log.WithError(err).Error("failed to perform onboarding")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-17 11:51:54 +02:00
|
|
|
if *noCollector == true {
|
2020-11-13 20:07:30 +01:00
|
|
|
probe.Config().Sharing.UploadResults = false
|
2018-09-17 11:51:54 +02:00
|
|
|
}
|
2019-10-02 18:23:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
2018-02-13 17:11:22 +01:00
|
|
|
|
2019-10-02 18:23:14 +02:00
|
|
|
websitesCmd := cmd.Command("websites", "")
|
2020-12-01 07:21:33 +01:00
|
|
|
inputFile := websitesCmd.Flag("input-file", "File containing input URLs").Strings()
|
|
|
|
input := websitesCmd.Flag("input", "Test the specified URL").Strings()
|
2019-10-02 18:23:14 +02:00
|
|
|
websitesCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-12-01 07:21:33 +01:00
|
|
|
return runNettestGroup(runNettestGroupConfig{
|
|
|
|
tg: "websites",
|
|
|
|
ctx: probe,
|
|
|
|
inputFiles: *inputFile,
|
|
|
|
inputs: *input,
|
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
imCmd := cmd.Command("im", "")
|
|
|
|
imCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-12-01 07:21:33 +01:00
|
|
|
return runNettestGroup(runNettestGroupConfig{
|
|
|
|
tg: "im",
|
|
|
|
ctx: probe,
|
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
performanceCmd := cmd.Command("performance", "")
|
|
|
|
performanceCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-12-01 07:21:33 +01:00
|
|
|
return runNettestGroup(runNettestGroupConfig{
|
|
|
|
tg: "performance",
|
|
|
|
ctx: probe,
|
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
middleboxCmd := cmd.Command("middlebox", "")
|
|
|
|
middleboxCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-12-01 07:21:33 +01:00
|
|
|
return runNettestGroup(runNettestGroupConfig{
|
|
|
|
tg: "middlebox",
|
|
|
|
ctx: probe,
|
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
2019-12-28 17:48:07 +01:00
|
|
|
circumventionCmd := cmd.Command("circumvention", "")
|
|
|
|
circumventionCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-12-01 07:21:33 +01:00
|
|
|
return runNettestGroup(runNettestGroupConfig{
|
|
|
|
tg: "circumvention",
|
|
|
|
ctx: probe,
|
|
|
|
})
|
2019-12-28 17:48:07 +01:00
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
allCmd := cmd.Command("all", "").Default()
|
|
|
|
allCmd.Action(func(_ *kingpin.ParseContext) error {
|
|
|
|
log.Infof("Running %s tests", color.BlueString("all"))
|
2019-12-02 17:05:02 +01:00
|
|
|
for tg := range nettests.NettestGroups {
|
2020-12-01 07:21:33 +01:00
|
|
|
group := runNettestGroupConfig{tg: tg, ctx: probe}
|
|
|
|
if err := runNettestGroup(group); err != nil {
|
2019-10-02 18:23:14 +02:00
|
|
|
log.WithError(err).Errorf("failed to run %s", tg)
|
2018-03-08 13:46:21 +01:00
|
|
|
}
|
2018-02-13 17:11:22 +01:00
|
|
|
}
|
2019-10-02 18:23:14 +02:00
|
|
|
return nil
|
2018-02-07 19:02:18 +01:00
|
|
|
})
|
|
|
|
}
|