2021-02-04 14:25:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-02-10 07:40:48 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/execabs"
|
2021-02-04 14:25:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func fatalOnError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measurement contains a OONI measurement.
|
|
|
|
type Measurement struct {
|
|
|
|
ReportID string `json:"report_id"`
|
|
|
|
Input *string `json:"input"`
|
|
|
|
SoftwareName string `json:"software_name"`
|
|
|
|
SoftwareVersion string `json:"software_version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-10-08 13:14:11 +02:00
|
|
|
backend := flag.String("backend", "https://api.ooni.io/", "Backend to use")
|
2021-02-04 14:25:03 +01:00
|
|
|
expected := flag.Int("expected", 0, "Expected number of measurement files")
|
|
|
|
flag.Parse()
|
|
|
|
if *expected <= 0 {
|
|
|
|
log.Fatal("You MUST specify `-expected N`")
|
|
|
|
}
|
|
|
|
// based off https://flaviocopes.com/go-list-files/
|
|
|
|
var files []string
|
|
|
|
outputdir := "./E2E/"
|
|
|
|
err := filepath.Walk(outputdir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if strings.HasSuffix(path, ".jsonl") {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
fatalOnError(err)
|
|
|
|
if len(files) <= 0 {
|
|
|
|
log.Fatal("no files to process?!")
|
|
|
|
}
|
|
|
|
var found int
|
|
|
|
for _, file := range files {
|
2021-06-15 14:01:45 +02:00
|
|
|
data, err := os.ReadFile(file)
|
2021-02-04 14:25:03 +01:00
|
|
|
fatalOnError(err)
|
|
|
|
measurements := bytes.Split(data, []byte("\n"))
|
|
|
|
for _, measurement := range measurements {
|
|
|
|
if len(measurement) <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var entry Measurement
|
|
|
|
err = json.Unmarshal(measurement, &entry)
|
|
|
|
fatalOnError(err)
|
|
|
|
log.Printf("processing: %+v", entry)
|
|
|
|
options := []string{
|
|
|
|
"run",
|
|
|
|
"./internal/cmd/apitool",
|
2022-10-08 13:14:11 +02:00
|
|
|
"-backend", *backend,
|
2021-02-04 14:25:03 +01:00
|
|
|
"-mode", "meta",
|
|
|
|
"-report-id", entry.ReportID,
|
|
|
|
}
|
|
|
|
found++
|
|
|
|
if entry.Input != nil {
|
|
|
|
options = append(options, "-input")
|
|
|
|
options = append(options, *entry.Input)
|
|
|
|
}
|
|
|
|
log.Printf("run: go %s", strings.Join(options, " "))
|
2021-02-10 07:40:48 +01:00
|
|
|
cmd := execabs.Command("go", options...)
|
2021-02-04 14:25:03 +01:00
|
|
|
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
fatalOnError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found != *expected {
|
|
|
|
log.Fatalf("expected %d measurements, found %d measurements", *expected, found)
|
|
|
|
}
|
|
|
|
}
|