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
|
|
|
|
daemonRPC *daemon.MockRPC
|
|
|
|
daemonRPCServer *httptest.Server
|
2024-12-09 17:09:45 +00:00
|
|
|
stdout *bytes.Buffer
|
|
|
|
changeStager *changeStager
|
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)
|
|
|
|
|
|
|
|
daemonRPC = daemon.NewMockRPC(t)
|
|
|
|
daemonRPCHandler = jsonrpc2.NewHTTPHandler(daemon.NewRPCHandler(
|
|
|
|
logger.WithNamespace("rpc"), daemonRPC,
|
|
|
|
))
|
|
|
|
daemonRPCServer = httptest.NewServer(daemonRPCHandler)
|
2024-12-09 17:09:45 +00:00
|
|
|
|
|
|
|
stdout = new(bytes.Buffer)
|
|
|
|
changeStager = &changeStager{toolkit.TempDir(t)}
|
2024-11-16 10:50:55 +00:00
|
|
|
)
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
t.Cleanup(daemonRPCServer.Close)
|
|
|
|
|
2024-12-09 17:09:45 +00:00
|
|
|
return &runHarness{
|
|
|
|
ctx, logger, daemonRPC, daemonRPCServer, stdout, changeStager,
|
|
|
|
}
|
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{
|
2024-12-09 17:09:45 +00:00
|
|
|
args: args,
|
|
|
|
daemonRPC: daemonRPCClient,
|
|
|
|
stdout: h.stdout,
|
|
|
|
changeStager: h.changeStager,
|
2024-11-16 10:50:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|