refactor(ooni): introduce interfaces for testability
This commit is contained in:
parent
c55f67273e
commit
fa803300bb
13 changed files with 129 additions and 70 deletions
|
|
@ -12,28 +12,28 @@ func init() {
|
|||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
output.SectionTitle("GeoIP lookup")
|
||||
ctx, err := root.Init()
|
||||
probeCLI, err := root.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess, err := ctx.NewSession()
|
||||
engine, err := probeCLI.NewProbeEngine()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sess.Close()
|
||||
defer engine.Close()
|
||||
|
||||
err = sess.MaybeLookupLocation()
|
||||
err = engine.MaybeLookupLocation()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"type": "table",
|
||||
"asn": sess.ProbeASNString(),
|
||||
"network_name": sess.ProbeNetworkName(),
|
||||
"country_code": sess.ProbeCC(),
|
||||
"ip": sess.ProbeIP(),
|
||||
"asn": engine.ProbeASNString(),
|
||||
"network_name": engine.ProbeNetworkName(),
|
||||
"country_code": engine.ProbeCC(),
|
||||
"ip": engine.ProbeIP(),
|
||||
}).Info("Looked up your location")
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -10,16 +10,16 @@ func init() {
|
|||
cmd := root.Command("info", "Display information about OONI Probe")
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
ctx, err := root.Init()
|
||||
probeCLI, err := root.Init()
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
return err
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"path": ctx.Home,
|
||||
"path": probeCLI.Home(),
|
||||
}).Info("Home")
|
||||
log.WithFields(log.Fields{
|
||||
"path": ctx.TempDir,
|
||||
"path": probeCLI.TempDir(),
|
||||
}).Info("TempDir")
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -12,13 +12,13 @@ func init() {
|
|||
cmd := root.Command("list", "List results")
|
||||
resultID := cmd.Arg("id", "the id of the result to list measurements for").Int64()
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
ctx, err := root.Init()
|
||||
probeCLI, err := root.Init()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to initialize root context")
|
||||
return err
|
||||
}
|
||||
if *resultID > 0 {
|
||||
measurements, err := database.ListMeasurements(ctx.DB, *resultID)
|
||||
measurements, err := database.ListMeasurements(probeCLI.DB(), *resultID)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to list measurements")
|
||||
return err
|
||||
|
|
@ -61,7 +61,7 @@ func init() {
|
|||
}
|
||||
output.MeasurementSummary(msmtSummary)
|
||||
} else {
|
||||
doneResults, incompleteResults, err := database.ListResults(ctx.DB)
|
||||
doneResults, incompleteResults, err := database.ListResults(probeCLI.DB())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to list results")
|
||||
return err
|
||||
|
|
@ -91,11 +91,11 @@ func init() {
|
|||
netCount := make(map[uint]int)
|
||||
output.SectionTitle("Results")
|
||||
for idx, result := range doneResults {
|
||||
totalCount, anmlyCount, err := database.GetMeasurementCounts(ctx.DB, result.Result.ID)
|
||||
totalCount, anmlyCount, err := database.GetMeasurementCounts(probeCLI.DB(), result.Result.ID)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to list measurement counts")
|
||||
}
|
||||
testKeys, err := database.GetResultTestKeys(ctx.DB, result.Result.ID)
|
||||
testKeys, err := database.GetResultTestKeys(probeCLI.DB(), result.Result.ID)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to get testKeys")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,11 +138,11 @@ func Onboarding(config *config.Config) error {
|
|||
// MaybeOnboarding will run the onboarding process only if the informed consent
|
||||
// config option is set to false
|
||||
func MaybeOnboarding(probe *ooni.Probe) error {
|
||||
if probe.Config.InformedConsent == false {
|
||||
if probe.IsBatch == true {
|
||||
if probe.Config().InformedConsent == false {
|
||||
if probe.IsBatch() == true {
|
||||
return errors.New("cannot run onboarding in batch mode")
|
||||
}
|
||||
if err := Onboarding(probe.Config); err != nil {
|
||||
if err := Onboarding(probe.Config()); err != nil {
|
||||
return errors.Wrap(err, "onboarding")
|
||||
}
|
||||
}
|
||||
|
|
@ -161,20 +161,20 @@ func init() {
|
|||
}
|
||||
|
||||
if *yes == true {
|
||||
probe.Config.Lock()
|
||||
probe.Config.InformedConsent = true
|
||||
probe.Config.Unlock()
|
||||
probe.Config().Lock()
|
||||
probe.Config().InformedConsent = true
|
||||
probe.Config().Unlock()
|
||||
|
||||
if err := probe.Config.Write(); err != nil {
|
||||
if err := probe.Config().Write(); err != nil {
|
||||
log.WithError(err).Error("failed to write config file")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if probe.IsBatch == true {
|
||||
if probe.IsBatch() == true {
|
||||
return errors.New("cannot do onboarding in batch mode")
|
||||
}
|
||||
|
||||
return Onboarding(probe.Config)
|
||||
return Onboarding(probe.Config())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,16 +20,16 @@ func init() {
|
|||
}
|
||||
// We need to first the DB otherwise the DB will be rewritten on close when
|
||||
// we delete the home directory.
|
||||
err = ctx.DB.Close()
|
||||
err = ctx.DB().Close()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to close the DB")
|
||||
return err
|
||||
}
|
||||
if *force == true {
|
||||
os.RemoveAll(ctx.Home)
|
||||
log.Infof("Deleted %s", ctx.Home)
|
||||
os.RemoveAll(ctx.Home())
|
||||
log.Infof("Deleted %s", ctx.Home())
|
||||
} else {
|
||||
log.Infof("Run with --force to delete %s", ctx.Home)
|
||||
log.Infof("Run with --force to delete %s", ctx.Home())
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -65,11 +65,11 @@ func init() {
|
|||
}
|
||||
|
||||
if *all == true {
|
||||
return deleteAll(ctx.DB, *yes)
|
||||
return deleteAll(ctx.DB(), *yes)
|
||||
}
|
||||
|
||||
if *yes == true {
|
||||
err = database.DeleteResult(ctx.DB, *resultID)
|
||||
err = database.DeleteResult(ctx.DB(), *resultID)
|
||||
if err == db.ErrNoMoreRows {
|
||||
return errors.New("result not found")
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ func init() {
|
|||
if answer == "false" {
|
||||
return errors.New("canceled by user")
|
||||
}
|
||||
err = database.DeleteResult(ctx.DB, *resultID)
|
||||
err = database.DeleteResult(ctx.DB(), *resultID)
|
||||
if err == db.ErrNoMoreRows {
|
||||
return errors.New("result not found")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ func init() {
|
|||
return nil, err
|
||||
}
|
||||
if *isBatch {
|
||||
probe.IsBatch = true
|
||||
probe.SetIsBatch(true)
|
||||
}
|
||||
|
||||
return probe, nil
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func runNettestGroup(tg string, ctx *ooni.Probe, network *database.Network) erro
|
|||
log.WithError(err).Error("Failed to lookup the location of the probe")
|
||||
return err
|
||||
}
|
||||
network, err = database.CreateNetwork(ctx.DB, sess)
|
||||
network, err = database.CreateNetwork(ctx.DB(), sess)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create the network row")
|
||||
return err
|
||||
|
|
@ -48,7 +48,7 @@ func runNettestGroup(tg string, ctx *ooni.Probe, network *database.Network) erro
|
|||
}
|
||||
log.Debugf("Running test group %s", group.Label)
|
||||
|
||||
result, err := database.CreateResult(ctx.DB, ctx.Home, tg, network.ID)
|
||||
result, err := database.CreateResult(ctx.DB(), ctx.Home(), tg, network.ID)
|
||||
if err != nil {
|
||||
log.Errorf("DB result error: %s", err)
|
||||
return err
|
||||
|
|
@ -69,7 +69,7 @@ func runNettestGroup(tg string, ctx *ooni.Probe, network *database.Network) erro
|
|||
}
|
||||
}
|
||||
|
||||
if err = result.Finished(ctx.DB); err != nil {
|
||||
if err = result.Finished(ctx.DB()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
@ -102,7 +102,7 @@ func init() {
|
|||
}
|
||||
|
||||
if *noCollector == true {
|
||||
probe.Config.Sharing.UploadResults = false
|
||||
probe.Config().Sharing.UploadResults = false
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ func init() {
|
|||
log.WithError(err).Error("failed to initialize root context")
|
||||
return err
|
||||
}
|
||||
msmt, err := database.GetMeasurementJSON(ctx.DB, *msmtID)
|
||||
msmt, err := database.GetMeasurementJSON(ctx.DB(), *msmtID)
|
||||
if err != nil {
|
||||
log.Errorf("error: %v", err)
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in a new issue