feat: add oonireport client (#682)
The oonireport client (re-)uploads a measurement report file. This can be helpful when the measurement was not uploaded at runtime. Usage: `./oonireport upload <file>`, where `<file>` is a json(l) file containing one OONI measurement per line. This pull request refers to https://github.com/ooni/probe/issues/2003 and https://github.com/ooni/probe/issues/950. Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
parent
fbae9ddece
commit
0735e2018f
4 changed files with 243 additions and 0 deletions
85
internal/cmd/oonireport/oonireport_test.go
Normal file
85
internal/cmd/oonireport/oonireport_test.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCanOpen(t *testing.T) {
|
||||
ok := canOpen("testdata/testmeasurement.json")
|
||||
if !ok {
|
||||
t.Fatal("unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLines(t *testing.T) {
|
||||
lines := readLines("testdata/testmeasurement.json")
|
||||
if lines == nil {
|
||||
t.Fatal("unexpected error")
|
||||
}
|
||||
if len(lines) != 2 {
|
||||
t.Fatal("unexpected number of measurements")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSessionAndSubmitter(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sess := newSession(ctx)
|
||||
if sess == nil {
|
||||
t.Fatal("unexpected nil session")
|
||||
}
|
||||
subm := newSubmitter(sess, ctx)
|
||||
if subm == nil {
|
||||
t.Fatal("unexpected nil submitter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMeasurement(t *testing.T) {
|
||||
lines := readLines("testdata/testmeasurement.json")
|
||||
line := lines[0]
|
||||
mm := toMeasurement(line)
|
||||
if mm == nil {
|
||||
t.Fatal("unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainMissingFile(t *testing.T) {
|
||||
defer func() {
|
||||
var s interface{}
|
||||
if s = recover(); s == nil {
|
||||
t.Fatal("expected a panic here")
|
||||
}
|
||||
if s != "Cannot open measurement file" {
|
||||
t.Fatal("unexpected panic message")
|
||||
}
|
||||
}()
|
||||
mainWithArgs([]string{"upload", "notexist.json"})
|
||||
}
|
||||
|
||||
func TestMainEmptyFile(t *testing.T) {
|
||||
defer func() {
|
||||
var s interface{}
|
||||
if s = recover(); s != nil {
|
||||
t.Fatal("unexpected panic")
|
||||
}
|
||||
}()
|
||||
mainWithArgs([]string{"upload", "testdata/noentries.json"})
|
||||
}
|
||||
|
||||
func TestSubmitAllFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sess := newSession(ctx)
|
||||
subm := newSubmitter(sess, ctx)
|
||||
lines := readLines("testdata/testmeasurement.json")
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
cancel() // fail immediately
|
||||
|
||||
n, err := submitAll(ctx, lines, subm)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatal("nothing should be submitted here")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue