2018-02-07 19:02:18 +01:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2018-03-22 15:22:29 +01:00
|
|
|
"errors"
|
2018-03-19 13:20:42 +01:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2018-06-22 14:55:00 +02:00
|
|
|
"strings"
|
2018-02-13 17:11:22 +01:00
|
|
|
"time"
|
|
|
|
|
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"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/root"
|
|
|
|
"github.com/ooni/probe-cli/internal/database"
|
|
|
|
"github.com/ooni/probe-cli/nettests"
|
|
|
|
"github.com/ooni/probe-cli/nettests/groups"
|
2018-05-31 12:33:08 +02:00
|
|
|
"github.com/ooni/probe-cli/utils"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd := root.Command("run", "Run a test group or OONI Run link")
|
|
|
|
|
2018-06-22 14:55:00 +02:00
|
|
|
var nettestGroupNames []string
|
|
|
|
for name := range groups.NettestGroups {
|
2018-06-29 16:50:05 +02:00
|
|
|
nettestGroupNames = append(nettestGroupNames, color.BlueString(name))
|
2018-06-22 14:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nettestGroup := cmd.Arg("name",
|
|
|
|
fmt.Sprintf("the nettest group to run. Supported tests are: %s",
|
|
|
|
strings.Join(nettestGroupNames, ", "))).Required().String()
|
2018-02-07 19:02:18 +01:00
|
|
|
|
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
2018-02-21 16:06:30 +01:00
|
|
|
log.Infof("Starting %s", *nettestGroup)
|
2018-03-23 12:10:14 +01:00
|
|
|
ctx, 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
|
|
|
|
|
|
|
if err = ctx.MaybeOnboarding(); err != nil {
|
|
|
|
log.WithError(err).Error("failed to perform onboarding")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:22:29 +01:00
|
|
|
group, ok := groups.NettestGroups[*nettestGroup]
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("No test group named %s", *nettestGroup)
|
|
|
|
return errors.New("invalid test group name")
|
|
|
|
}
|
2018-02-13 17:11:22 +01:00
|
|
|
log.Debugf("Running test group %s", group.Label)
|
2018-02-13 16:16:23 +01:00
|
|
|
|
2018-03-23 12:41:06 +01:00
|
|
|
err = ctx.MaybeLocationLookup()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to lookup the location of the probe")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-06 16:13:04 +02:00
|
|
|
network := database.Network{
|
|
|
|
ASN: ctx.Location.ASN,
|
|
|
|
CountryCode: ctx.Location.CountryCode,
|
2018-05-03 18:40:52 +02:00
|
|
|
NetworkName: ctx.Location.NetworkName,
|
2018-09-06 16:13:04 +02:00
|
|
|
IP: ctx.Location.IP,
|
|
|
|
}
|
|
|
|
newID, err := ctx.DB.Collection("networks").Insert(network)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to create the network row")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
network.ID = newID.(int64)
|
|
|
|
|
|
|
|
result, err := database.CreateResult(ctx.DB, ctx.Home, database.Result{
|
|
|
|
TestGroupName: *nettestGroup,
|
|
|
|
StartTime: time.Now().UTC(),
|
|
|
|
NetworkID: network.ID,
|
2018-02-13 17:11:22 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-03-08 11:53:04 +01:00
|
|
|
log.Errorf("DB result error: %s", err)
|
2018-02-13 17:11:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nt := range group.Nettests {
|
2018-03-14 14:44:37 +01:00
|
|
|
log.Debugf("Running test %T", nt)
|
2018-03-19 13:20:42 +01:00
|
|
|
msmtPath := filepath.Join(ctx.TempDir,
|
2018-03-19 19:28:32 +01:00
|
|
|
fmt.Sprintf("msmt-%T-%s.jsonl", nt,
|
2018-05-31 12:33:08 +02:00
|
|
|
time.Now().UTC().Format(utils.ResultTimestamp)))
|
2018-03-19 13:20:42 +01:00
|
|
|
|
2018-03-19 16:23:30 +01:00
|
|
|
ctl := nettests.NewController(nt, ctx, result, msmtPath)
|
2018-03-20 14:19:19 +01:00
|
|
|
if err = nt.Run(ctl); err != nil {
|
2018-03-08 13:46:21 +01:00
|
|
|
log.WithError(err).Errorf("Failed to run %s", group.Label)
|
|
|
|
return err
|
|
|
|
}
|
2018-02-13 17:11:22 +01:00
|
|
|
}
|
2018-03-20 14:19:19 +01:00
|
|
|
if err = result.Finished(ctx.DB, group.Summary); err != nil {
|
2018-03-20 12:38:33 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-02-07 19:02:18 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|