ooni-probe-cli/internal/cmd/oonireport/oonireport_test.go
kelmenhorst 0735e2018f
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>
2022-02-14 15:24:36 +01:00

86 lines
1.7 KiB
Go

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")
}
}