go-emlid/reach/client/protocol/testsuite/operations.go

183 lines
3.8 KiB
Go

package testsuite
import (
"context"
"os"
"testing"
"time"
"forge.cadoles.com/cadoles/go-emlid/reach"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)
type OperationsFactoryFunc func(addr string) (protocol.Operations, error)
type operationTestCase struct {
Name string
Run func(t *testing.T, ops protocol.Operations)
}
var testCases = []operationTestCase{
{
Name: "Connect to ReachView",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
if err := ops.Connect(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
defer func() {
if err := ops.Close(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
}()
alive, err := ops.Alive(ctx)
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
if e, g := true, alive; e != g {
t.Errorf("alive: expected '%v', got '%v'", e, g)
}
},
},
{
Name: "Retrieve ReachView version",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
version, stable, err := ops.Version(ctx)
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
t.Logf("ReachView version: %s", version)
t.Logf("ReachView stable channel: %v", stable)
},
},
{
Name: "Listen to ReachView 'broadcast' messages",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
if err := ops.Connect(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
defer func() {
if err := ops.Close(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
}()
broadcastCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
messages, err := ops.On(broadcastCtx, "broadcast")
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
count := 0
for m := range messages {
t.Logf("new message: %s", spew.Sdump(m))
count++
}
if e, g := 1, count; g < e {
t.Errorf("expected total messages > %d, got %d", e, g)
}
},
},
{
Name: "Retrieve module configuration",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
config, err := ops.Configuration(ctx)
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
t.Logf("Module configuration: %s", spew.Sdump(config))
},
},
{
Name: "Set base",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
opts := []protocol.SetBaseOptionFunc{
protocol.WithBaseLatitude(47.72769),
protocol.WithBaseLongitude(4.72783),
protocol.WithBaseHeight(101),
protocol.WithBaseMode("manual"),
}
if err := ops.SetBase(ctx, opts...); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
},
},
{
Name: "Reboot",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
if err := ops.Connect(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
defer func() {
if err := ops.Close(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
}()
if err := ops.Reboot(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
},
},
}
func TestOperations(t *testing.T, opsFactory OperationsFactoryFunc) {
reach.AssertIntegrationTests(t)
addr := os.Getenv("REACHRS_HOST")
if addr == "" {
addr = "192.168.42.1"
t.Logf("Targeting '%s'. You can modify targeted host by specifying environment variable REACHRS_HOST", addr)
} else {
t.Logf("Targeting '%s'", addr)
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
ops, err := opsFactory(addr)
if err != nil {
t.Fatalf("could not initialize protocol operations: %+v", errors.WithStack(err))
}
tc.Run(t, ops)
})
}
}