Add support for running all tests with ooni run

This commit is contained in:
Arturo Filastò 2018-09-27 18:38:37 +02:00
parent 1b90b6f1ec
commit 34556dcbf1

View File

@ -8,30 +8,60 @@ import (
"github.com/alecthomas/kingpin" "github.com/alecthomas/kingpin"
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests" "github.com/ooni/probe-cli/nettests"
"github.com/ooni/probe-cli/nettests/groups" "github.com/ooni/probe-cli/nettests/groups"
) )
func runNettestGroup(tg string, ctx *ooni.Context, network *database.Network) error {
group, ok := groups.NettestGroups[tg]
if !ok {
log.Errorf("No test group named %s", tg)
return errors.New("invalid test group name")
}
log.Debugf("Running test group %s", group.Label)
result, err := database.CreateResult(ctx.DB, ctx.Home, tg, network.ID)
if err != nil {
log.Errorf("DB result error: %s", err)
return err
}
for i, nt := range group.Nettests {
log.Debugf("Running test %T", nt)
ctl := nettests.NewController(nt, ctx, result)
ctl.SetNettestIndex(i, len(group.Nettests))
if err = nt.Run(ctl); err != nil {
log.WithError(err).Errorf("Failed to run %s", group.Label)
return err
}
}
if err = result.Finished(ctx.DB); err != nil {
return err
}
return nil
}
func init() { func init() {
cmd := root.Command("run", "Run a test group or OONI Run link") cmd := root.Command("run", "Run a test group or OONI Run link")
var nettestGroupNames []string var nettestGroupNamesBlue []string
for name := range groups.NettestGroups { for name := range groups.NettestGroups {
nettestGroupNames = append(nettestGroupNames, color.BlueString(name)) nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
} }
nettestGroup := cmd.Arg("name", nettestGroup := cmd.Arg("name",
fmt.Sprintf("the nettest group to run. Supported tests are: %s", fmt.Sprintf("the nettest group to run. Supported tests are: %s, or nothing to run them all",
strings.Join(nettestGroupNames, ", "))).Required().String() strings.Join(nettestGroupNamesBlue, ", "))).String()
noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool() noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool()
collectorURL := cmd.Flag("collector-url", "Specify the address of a custom collector").String() collectorURL := cmd.Flag("collector-url", "Specify the address of a custom collector").String()
bouncerURL := cmd.Flag("bouncer-url", "Specify the address of a custom bouncer").String() bouncerURL := cmd.Flag("bouncer-url", "Specify the address of a custom bouncer").String()
cmd.Action(func(_ *kingpin.ParseContext) error { cmd.Action(func(_ *kingpin.ParseContext) error {
log.Infof("Starting %s", *nettestGroup)
ctx, err := root.Init() ctx, err := root.Init()
if err != nil { if err != nil {
log.Errorf("%s", err) log.Errorf("%s", err)
@ -55,44 +85,27 @@ func init() {
log.Debugf("Using collector %s", ctx.Config.Advanced.CollectorURL) log.Debugf("Using collector %s", ctx.Config.Advanced.CollectorURL)
log.Debugf("Using bouncer %s", ctx.Config.Advanced.CollectorURL) log.Debugf("Using bouncer %s", ctx.Config.Advanced.CollectorURL)
group, ok := groups.NettestGroups[*nettestGroup]
if !ok {
log.Errorf("No test group named %s", *nettestGroup)
return errors.New("invalid test group name")
}
log.Debugf("Running test group %s", group.Label)
err = ctx.MaybeLocationLookup() err = ctx.MaybeLocationLookup()
if err != nil { if err != nil {
log.WithError(err).Error("Failed to lookup the location of the probe") log.WithError(err).Error("Failed to lookup the location of the probe")
return err return err
} }
network, err := database.CreateNetwork(ctx.DB, ctx.Location) network, err := database.CreateNetwork(ctx.DB, ctx.Location)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to create the network row") log.WithError(err).Error("Failed to create the network row")
return err
}
if *nettestGroup == "" {
log.Infof("Running %s tests", color.BlueString("all"))
for tg := range groups.NettestGroups {
if err := runNettestGroup(tg, ctx, network); err != nil {
log.WithError(err).Errorf("failed to run %s", tg)
}
}
return nil return nil
} }
log.Infof("Running %s", color.BlueString(*nettestGroup))
result, err := database.CreateResult(ctx.DB, ctx.Home, *nettestGroup, network.ID) return runNettestGroup(*nettestGroup, ctx, network)
if err != nil {
log.Errorf("DB result error: %s", err)
return err
}
for i, nt := range group.Nettests {
log.Debugf("Running test %T", nt)
ctl := nettests.NewController(nt, ctx, result)
ctl.SetNettestIndex(i, len(group.Nettests))
if err = nt.Run(ctl); err != nil {
log.WithError(err).Errorf("Failed to run %s", group.Label)
return err
}
}
if err = result.Finished(ctx.DB); err != nil {
return err
}
return nil
}) })
} }