2024-11-16 10:50:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"isle/daemon"
|
2024-12-07 19:39:13 +00:00
|
|
|
"isle/daemon/jsonrpc2"
|
2024-11-16 10:50:55 +00:00
|
|
|
"isle/toolkit"
|
2024-12-07 19:39:13 +00:00
|
|
|
"net/http/httptest"
|
2024-11-16 10:50:55 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type runHarness struct {
|
2024-12-07 19:39:13 +00:00
|
|
|
ctx context.Context
|
|
|
|
logger *mlog.Logger
|
|
|
|
stdout *bytes.Buffer
|
|
|
|
daemonRPC *daemon.MockRPC
|
|
|
|
daemonRPCServer *httptest.Server
|
2024-11-16 10:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRunHarness(t *testing.T) *runHarness {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
2024-12-07 19:39:13 +00:00
|
|
|
ctx = context.Background()
|
|
|
|
logger = toolkit.NewTestLogger(t)
|
|
|
|
stdout = new(bytes.Buffer)
|
|
|
|
|
|
|
|
daemonRPC = daemon.NewMockRPC(t)
|
|
|
|
daemonRPCHandler = jsonrpc2.NewHTTPHandler(daemon.NewRPCHandler(
|
|
|
|
logger.WithNamespace("rpc"), daemonRPC,
|
|
|
|
))
|
|
|
|
daemonRPCServer = httptest.NewServer(daemonRPCHandler)
|
2024-11-16 10:50:55 +00:00
|
|
|
)
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
t.Cleanup(daemonRPCServer.Close)
|
|
|
|
|
|
|
|
return &runHarness{ctx, logger, stdout, daemonRPC, daemonRPCServer}
|
2024-11-16 10:50:55 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
func (h *runHarness) run(t *testing.T, args ...string) error {
|
|
|
|
httpClient := toolkit.NewHTTPClient(h.logger.WithNamespace("http"))
|
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.NoError(t, httpClient.Close())
|
|
|
|
})
|
|
|
|
|
|
|
|
daemonRPCClient := daemon.RPCFromClient(
|
|
|
|
jsonrpc2.NewHTTPClient(httpClient, h.daemonRPCServer.URL),
|
|
|
|
)
|
|
|
|
|
2024-11-16 10:50:55 +00:00
|
|
|
return doRootCmd(h.ctx, h.logger, &subCmdCtxOpts{
|
|
|
|
args: args,
|
2024-12-07 19:39:13 +00:00
|
|
|
daemonRPC: daemonRPCClient,
|
2024-11-16 10:50:55 +00:00
|
|
|
stdout: h.stdout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *runHarness) runAssertStdout(
|
|
|
|
t *testing.T,
|
|
|
|
want any,
|
|
|
|
args ...string,
|
|
|
|
) {
|
|
|
|
var (
|
|
|
|
gotType = reflect.ValueOf(want)
|
|
|
|
got = reflect.New(gotType.Type())
|
|
|
|
)
|
|
|
|
|
|
|
|
h.stdout.Reset()
|
|
|
|
assert.NoError(t, h.run(t, args...))
|
|
|
|
assert.NoError(t, yaml.Unmarshal(h.stdout.Bytes(), got.Interface()))
|
|
|
|
assert.Equal(t, want, got.Elem().Interface())
|
|
|
|
}
|