ooni-probe-cli/internal/cli/run/run.go

170 lines
4.5 KiB
Go
Raw Normal View History

package run
import (
"errors"
2018-02-13 17:11:22 +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"
"github.com/ooni/probe-cli/internal/cli/onboard"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/nettests"
2020-11-13 18:42:10 +01:00
"github.com/ooni/probe-cli/internal/ooni"
)
type runNettestGroupConfig struct {
tg string
ctx *ooni.Probe
inputFiles []string
inputs []string
}
func runNettestGroup(config runNettestGroupConfig) error {
if config.ctx.IsTerminated() == true {
log.Debugf("context is terminated, stopping runNettestGroup early")
return nil
}
sess, err := config.ctx.NewSession()
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.ctx.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
}
group, ok := nettests.NettestGroups[config.tg]
if !ok {
log.Errorf("No test group named %s", config.tg)
return errors.New("invalid test group name")
}
log.Debugf("Running test group %s", group.Label)
result, err := database.CreateResult(
config.ctx.DB(), config.ctx.Home(), config.tg, network.ID)
if err != nil {
log.Errorf("DB result error: %s", err)
return err
}
config.ctx.ListenForSignals()
config.ctx.MaybeListenForStdinClosed()
for i, nt := range group.Nettests {
if config.ctx.IsTerminated() == true {
log.Debugf("context is terminated, stopping group.Nettests early")
break
}
log.Debugf("Running test %T", nt)
ctl := nettests.NewController(nt, config.ctx, result, sess)
ctl.InputFiles = config.inputFiles
ctl.Inputs = config.inputs
ctl.SetNettestIndex(i, len(group.Nettests))
if err = nt.Run(ctl); err != nil {
log.WithError(err).Errorf("Failed to run %s", group.Label)
}
}
if err = result.Finished(config.ctx.DB()); err != nil {
return err
}
return nil
}
func init() {
cmd := root.Command("run", "Run a test group or OONI Run link")
var nettestGroupNamesBlue []string
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 {
nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
2018-06-22 14:55:00 +02:00
}
noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool()
cmd.Action(func(_ *kingpin.ParseContext) error {
2019-10-02 18:23:14 +02:00
var err error
probe, err = root.Init()
2018-02-12 16:45:13 +01:00
if err != nil {
log.Errorf("%s", err)
return err
}
if err = onboard.MaybeOnboarding(probe); err != nil {
log.WithError(err).Error("failed to perform onboarding")
return err
}
if *noCollector == true {
probe.Config().Sharing.UploadResults = false
}
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", "")
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 {
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 {
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 {
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 {
return runNettestGroup(runNettestGroupConfig{
tg: "middlebox",
ctx: probe,
})
2019-10-02 18:23:14 +02:00
})
circumventionCmd := cmd.Command("circumvention", "")
circumventionCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{
tg: "circumvention",
ctx: probe,
})
})
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 {
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
})
}