refactor: move code to run group in nettests package (#182)

Part of https://github.com/ooni/probe/issues/1289
This commit is contained in:
Simone Basso 2020-12-01 15:04:04 +01:00 committed by GitHub
parent 2381c50dc5
commit 60d08eef3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 94 deletions

View File

@ -1,90 +1,15 @@
package run package run
import ( import (
"errors"
"github.com/alecthomas/kingpin" "github.com/alecthomas/kingpin"
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/ooni/probe-cli/internal/cli/onboard" "github.com/ooni/probe-cli/internal/cli/onboard"
"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/nettests" "github.com/ooni/probe-cli/internal/nettests"
"github.com/ooni/probe-cli/internal/ooni" "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() { 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")
@ -120,47 +45,47 @@ func init() {
inputFile := websitesCmd.Flag("input-file", "File containing input URLs").Strings() inputFile := websitesCmd.Flag("input-file", "File containing input URLs").Strings()
input := websitesCmd.Flag("input", "Test the specified URL").Strings() input := websitesCmd.Flag("input", "Test the specified URL").Strings()
websitesCmd.Action(func(_ *kingpin.ParseContext) error { websitesCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{ return nettests.RunGroup(nettests.RunGroupConfig{
tg: "websites", GroupName: "websites",
ctx: probe, Probe: probe,
inputFiles: *inputFile, InputFiles: *inputFile,
inputs: *input, Inputs: *input,
}) })
}) })
imCmd := cmd.Command("im", "") imCmd := cmd.Command("im", "")
imCmd.Action(func(_ *kingpin.ParseContext) error { imCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{ return nettests.RunGroup(nettests.RunGroupConfig{
tg: "im", GroupName: "im",
ctx: probe, Probe: probe,
}) })
}) })
performanceCmd := cmd.Command("performance", "") performanceCmd := cmd.Command("performance", "")
performanceCmd.Action(func(_ *kingpin.ParseContext) error { performanceCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{ return nettests.RunGroup(nettests.RunGroupConfig{
tg: "performance", GroupName: "performance",
ctx: probe, Probe: probe,
}) })
}) })
middleboxCmd := cmd.Command("middlebox", "") middleboxCmd := cmd.Command("middlebox", "")
middleboxCmd.Action(func(_ *kingpin.ParseContext) error { middleboxCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{ return nettests.RunGroup(nettests.RunGroupConfig{
tg: "middlebox", GroupName: "middlebox",
ctx: probe, Probe: probe,
}) })
}) })
circumventionCmd := cmd.Command("circumvention", "") circumventionCmd := cmd.Command("circumvention", "")
circumventionCmd.Action(func(_ *kingpin.ParseContext) error { circumventionCmd.Action(func(_ *kingpin.ParseContext) error {
return runNettestGroup(runNettestGroupConfig{ return nettests.RunGroup(nettests.RunGroupConfig{
tg: "circumvention", GroupName: "circumvention",
ctx: probe, Probe: probe,
}) })
}) })
allCmd := cmd.Command("all", "").Default() allCmd := cmd.Command("all", "").Default()
allCmd.Action(func(_ *kingpin.ParseContext) error { allCmd.Action(func(_ *kingpin.ParseContext) error {
log.Infof("Running %s tests", color.BlueString("all")) log.Infof("Running %s tests", color.BlueString("all"))
for tg := range nettests.NettestGroups { for tg := range nettests.NettestGroups {
group := runNettestGroupConfig{tg: tg, ctx: probe} group := nettests.RunGroupConfig{GroupName: tg, Probe: probe}
if err := runNettestGroup(group); err != nil { if err := nettests.RunGroup(group); err != nil {
log.WithError(err).Errorf("failed to run %s", tg) log.WithError(err).Errorf("failed to run %s", tg)
} }
} }

82
internal/nettests/run.go Normal file
View File

@ -0,0 +1,82 @@
package nettests
import (
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/ooni"
"github.com/pkg/errors"
)
// RunGroupConfig contains the settings for running a nettest group.
type RunGroupConfig struct {
GroupName string
Probe *ooni.Probe
InputFiles []string
Inputs []string
}
// RunGroup runs a group of nettests according to the specified config.
func RunGroup(config RunGroupConfig) error {
if config.Probe.IsTerminated() == true {
log.Debugf("context is terminated, stopping runNettestGroup early")
return nil
}
sess, err := config.Probe.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.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
}
group, ok := NettestGroups[config.GroupName]
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 {
if config.Probe.IsTerminated() == true {
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
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.Probe.DB()); err != nil {
return err
}
return nil
}