use require() in test scripts instead of eval()
This commit is contained in:
parent
f3322a8431
commit
1269b30f15
|
@ -18,45 +18,21 @@ func TestJSClient(t *testing.T) {
|
||||||
// defer leakcheck.Check(t)
|
// defer leakcheck.Check(t)
|
||||||
// r := require.New(t)
|
// r := require.New(t)
|
||||||
|
|
||||||
// ts := newRandomSession(t)
|
ts := newRandomSession(t)
|
||||||
ts := newSession(t, nil)
|
// ts := newSession(t, nil)
|
||||||
|
|
||||||
ts.startGoServer()
|
srv := ts.startGoServer()
|
||||||
s := ts.gobot
|
|
||||||
|
|
||||||
alice := ts.startJSBot(`
|
alice := ts.startJSBot("./testscripts/simple_client.js",
|
||||||
sbot.on('rpc:connect', rpc => {
|
srv.Network.GetListenAddr(),
|
||||||
var ret = rpc.tunnel.announce()
|
srv.Whoami(),
|
||||||
t.comment('announced')
|
)
|
||||||
console.warn(ret)
|
|
||||||
pull(
|
|
||||||
rpc.tunnel.endpoints(),
|
|
||||||
pull.drain(el => {
|
|
||||||
console.warn("from roomsrv:",el)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
setTimeout(() => {
|
srv.Allow(alice, true)
|
||||||
ret = rpc.tunnel.leave()
|
|
||||||
t.comment('left')
|
|
||||||
console.warn(ret)
|
|
||||||
}, 2500)
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
t.comment('shutting down')
|
|
||||||
exit()
|
|
||||||
}, 5000)
|
|
||||||
})
|
|
||||||
run()`, ``)
|
|
||||||
|
|
||||||
s.Allow(alice, true)
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
ts.wait()
|
ts.wait()
|
||||||
|
|
||||||
// TODO: check wantManager for this connection is stopped when the jsbot exited
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSServer(t *testing.T) {
|
func TestJSServer(t *testing.T) {
|
||||||
|
@ -66,17 +42,17 @@ func TestJSServer(t *testing.T) {
|
||||||
|
|
||||||
os.RemoveAll("testrun")
|
os.RemoveAll("testrun")
|
||||||
|
|
||||||
// ts := newRandomSession(t)
|
ts := newRandomSession(t)
|
||||||
ts := newSession(t, nil)
|
// ts := newSession(t, nil)
|
||||||
|
|
||||||
ts.startGoServer()
|
client := ts.startGoServer()
|
||||||
client := ts.gobot
|
|
||||||
|
|
||||||
// alice is the server now
|
// alice is the server now
|
||||||
alice, port := ts.startJSBotAsServer("alice", "./testscripts/server.js")
|
alice, port := ts.startJSBotAsServer("alice", "./testscripts/server.js")
|
||||||
|
|
||||||
client.Allow(*alice, true)
|
client.Allow(*alice, true)
|
||||||
|
|
||||||
|
// connect to alice
|
||||||
wrappedAddr := netwrap.WrapAddr(&net.TCPAddr{
|
wrappedAddr := netwrap.WrapAddr(&net.TCPAddr{
|
||||||
IP: net.ParseIP("127.0.0.1"),
|
IP: net.ParseIP("127.0.0.1"),
|
||||||
Port: port,
|
Port: port,
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
const Path = require('path')
|
const Path = require('path')
|
||||||
const { readFileSync } = require('fs')
|
|
||||||
const { loadOrCreateSync } = require('ssb-keys')
|
const { loadOrCreateSync } = require('ssb-keys')
|
||||||
const pull = require('pull-stream') // used in eval scripts
|
|
||||||
const tape = require('tape')
|
const tape = require('tape')
|
||||||
const parallel = require('run-parallel') // used in eval scripts
|
|
||||||
const theStack = require('secret-stack')
|
const theStack = require('secret-stack')
|
||||||
const ssbCaps = require('ssb-caps')
|
const ssbCaps = require('ssb-caps')
|
||||||
|
|
||||||
|
@ -14,7 +11,6 @@ if (testSHSappKey !== false) {
|
||||||
testAppkey = testSHSappKey
|
testAppkey = testSHSappKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const createSbot = theStack({caps: {shs: testAppkey } })
|
const createSbot = theStack({caps: {shs: testAppkey } })
|
||||||
.use(require('ssb-db'))
|
.use(require('ssb-db'))
|
||||||
.use(require('ssb-gossip'))
|
.use(require('ssb-gossip'))
|
||||||
|
@ -23,11 +19,12 @@ const createSbot = theStack({caps: {shs: testAppkey } })
|
||||||
.use(require('ssb-room/tunnel/client'))
|
.use(require('ssb-room/tunnel/client'))
|
||||||
|
|
||||||
const testName = process.env.TEST_NAME
|
const testName = process.env.TEST_NAME
|
||||||
const testBob = process.env.TEST_BOB
|
|
||||||
const testAddr = process.env.TEST_GOADDR
|
|
||||||
|
|
||||||
const scriptBefore = readFileSync(process.env.TEST_BEFORE).toString()
|
// the other peer we are talking to
|
||||||
const scriptAfter = readFileSync(process.env.TEST_AFTER).toString()
|
const testPeerAddr = process.env.TEST_PEERADDR
|
||||||
|
const testPeerRef = process.env.TEST_PEERREF
|
||||||
|
|
||||||
|
const testSession = require(process.env['TEST_SESSIONSCRIPT'])
|
||||||
|
|
||||||
function bufFromEnv(evname) {
|
function bufFromEnv(evname) {
|
||||||
const has = process.env[evname]
|
const has = process.env[evname]
|
||||||
|
@ -39,20 +36,19 @@ function bufFromEnv(evname) {
|
||||||
|
|
||||||
tape.createStream().pipe(process.stderr)
|
tape.createStream().pipe(process.stderr)
|
||||||
tape(testName, function (t) {
|
tape(testName, function (t) {
|
||||||
|
|
||||||
let timeoutLength = 15000
|
let timeoutLength = 15000
|
||||||
var tapeTimeout = null
|
var tapeTimeout = null
|
||||||
function run() { // needs to be called by the before block when it's done
|
function ready() { // needs to be called by the before block when it's done
|
||||||
t.timeoutAfter(timeoutLength) // doesn't exit the process
|
t.timeoutAfter(timeoutLength) // doesn't exit the process
|
||||||
tapeTimeout = setTimeout(() => {
|
tapeTimeout = setTimeout(() => {
|
||||||
t.comment('test timeout')
|
t.comment('test timeout')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}, timeoutLength*1.25)
|
}, timeoutLength*1.25)
|
||||||
const to = `net:${testAddr}~shs:${testBob.substr(1).replace('.ed25519', '')}`
|
const to = `net:${testPeerAddr}~shs:${testPeerRef.substr(1).replace('.ed25519', '')}`
|
||||||
t.comment('dialing:' + to)
|
t.comment('dialing:' + to)
|
||||||
sbot.connect(to, (err) => {
|
sbot.connect(to, (err) => {
|
||||||
t.error(err, 'connected')
|
t.error(err, 'connected')
|
||||||
eval(scriptAfter)
|
testSession.after(sbot, exit)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,5 +78,5 @@ tape(testName, function (t) {
|
||||||
|
|
||||||
t.comment('sbot spawned, running before')
|
t.comment('sbot spawned, running before')
|
||||||
console.log(alice.id) // tell go process who's incoming
|
console.log(alice.id) // tell go process who's incoming
|
||||||
eval(scriptBefore)
|
testSession.before(sbot, ready)
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
const Path = require('path')
|
const Path = require('path')
|
||||||
const tape = require('tape')
|
const tape = require('tape')
|
||||||
const { readFileSync } = require('fs')
|
|
||||||
const { loadOrCreateSync } = require('ssb-keys')
|
const { loadOrCreateSync } = require('ssb-keys')
|
||||||
const theStack = require('secret-stack')
|
const theStack = require('secret-stack')
|
||||||
const ssbCaps = require('ssb-caps')
|
const ssbCaps = require('ssb-caps')
|
||||||
|
@ -12,8 +11,8 @@ if (testSHSappKey !== false) {
|
||||||
testAppkey = testSHSappKey
|
testAppkey = testSHSappKey
|
||||||
}
|
}
|
||||||
|
|
||||||
stackOpts = {appKey: require('ssb-caps').shs}
|
// stackOpts = {appKey: require('ssb-caps').shs}
|
||||||
// stackOpts = {caps: {shs: testAppkey } }
|
stackOpts = {caps: {shs: testAppkey } }
|
||||||
const createSbot = theStack(stackOpts)
|
const createSbot = theStack(stackOpts)
|
||||||
.use(require('ssb-db'))
|
.use(require('ssb-db'))
|
||||||
.use(require('ssb-master'))
|
.use(require('ssb-master'))
|
||||||
|
@ -23,10 +22,7 @@ const createSbot = theStack(stackOpts)
|
||||||
|
|
||||||
const testName = process.env['TEST_NAME']
|
const testName = process.env['TEST_NAME']
|
||||||
const testPort = process.env['TEST_PORT']
|
const testPort = process.env['TEST_PORT']
|
||||||
|
const testSession = require(process.env['TEST_SESSIONSCRIPT'])
|
||||||
const testSession = require(process.env['TEST_BEFORE'])
|
|
||||||
// const scriptBefore = readFileSync(
|
|
||||||
// const scriptAfter = readFileSync(process.env['TEST_AFTER']).toString()
|
|
||||||
|
|
||||||
tape.createStream().pipe(process.stderr);
|
tape.createStream().pipe(process.stderr);
|
||||||
tape(testName, function (t) {
|
tape(testName, function (t) {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -38,17 +38,6 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeFile(t *testing.T, data string) string {
|
|
||||||
r := require.New(t)
|
|
||||||
f, err := ioutil.TempFile("testrun/"+t.Name(), "*.js")
|
|
||||||
r.NoError(err)
|
|
||||||
_, err = fmt.Fprintf(f, "%s", data)
|
|
||||||
r.NoError(err)
|
|
||||||
err = f.Close()
|
|
||||||
r.NoError(err)
|
|
||||||
return f.Name()
|
|
||||||
}
|
|
||||||
|
|
||||||
type testSession struct {
|
type testSession struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
|
|
||||||
|
@ -58,9 +47,6 @@ type testSession struct {
|
||||||
|
|
||||||
keySHS []byte
|
keySHS []byte
|
||||||
|
|
||||||
// TODO: multiple by name?!
|
|
||||||
gobot *roomsrv.Server
|
|
||||||
|
|
||||||
done errgroup.Group
|
done errgroup.Group
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
@ -121,15 +107,15 @@ func (ts *testSession) startGoServer(opts ...roomsrv.Option) *roomsrv.Server {
|
||||||
|
|
||||||
srv, err := roomsrv.New(opts...)
|
srv, err := roomsrv.New(opts...)
|
||||||
r.NoError(err, "failed to init tees a server")
|
r.NoError(err, "failed to init tees a server")
|
||||||
ts.t.Logf("go server: %s", srv.Whoami())
|
ts.t.Logf("go server: %s", srv.Whoami().Ref())
|
||||||
ts.t.Cleanup(func() {
|
ts.t.Cleanup(func() {
|
||||||
srv.Close()
|
ts.t.Log("bot close:", srv.Close())
|
||||||
})
|
})
|
||||||
ts.gobot = srv
|
|
||||||
ts.done.Go(func() error {
|
ts.done.Go(func() error {
|
||||||
err := srv.Network.Serve(ts.ctx)
|
err := srv.Network.Serve(ts.ctx)
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
err = fmt.Errorf("node serve exited: %w", err)
|
err = fmt.Errorf("go server exited: %w", err)
|
||||||
ts.t.Log(err)
|
ts.t.Log(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -141,12 +127,12 @@ func (ts *testSession) startGoServer(opts ...roomsrv.Option) *roomsrv.Server {
|
||||||
|
|
||||||
var jsBotCnt = 0
|
var jsBotCnt = 0
|
||||||
|
|
||||||
func (ts *testSession) startJSBot(jsbefore, jsafter string) refs.FeedRef {
|
func (ts *testSession) startJSBot(testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef {
|
||||||
return ts.startJSBotWithName("", jsbefore, jsafter)
|
return ts.startJSBotWithName("", testScript, peerAddr, peerRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the jsbots pubkey
|
// returns the jsbots pubkey
|
||||||
func (ts *testSession) startJSBotWithName(name, jsbefore, jsafter string) refs.FeedRef {
|
func (ts *testSession) startJSBotWithName(name, testScript string, peerAddr net.Addr, peerRef refs.FeedRef) refs.FeedRef {
|
||||||
ts.t.Log("starting client", name)
|
ts.t.Log("starting client", name)
|
||||||
r := require.New(ts.t)
|
r := require.New(ts.t)
|
||||||
cmd := exec.CommandContext(ts.ctx, "node", "./sbot_client.js")
|
cmd := exec.CommandContext(ts.ctx, "node", "./sbot_client.js")
|
||||||
|
@ -160,14 +146,14 @@ func (ts *testSession) startJSBotWithName(name, jsbefore, jsafter string) refs.F
|
||||||
}
|
}
|
||||||
jsBotCnt++
|
jsBotCnt++
|
||||||
|
|
||||||
// TODO: pass goref's via function?
|
// ts.gobot.Network.GetListenAddr()
|
||||||
// TODO or nickname solution
|
// ts.gobot.Whoami()
|
||||||
|
|
||||||
env := []string{
|
env := []string{
|
||||||
"TEST_NAME=" + name,
|
"TEST_NAME=" + name,
|
||||||
"TEST_BOB=" + ts.gobot.Whoami().Ref(),
|
"TEST_PEERADDR=" + netwrap.GetAddr(peerAddr, "tcp").String(),
|
||||||
"TEST_GOADDR=" + netwrap.GetAddr(ts.gobot.Network.GetListenAddr(), "tcp").String(),
|
"TEST_PEERREF=" + peerRef.Ref(),
|
||||||
"TEST_BEFORE=" + writeFile(ts.t, jsbefore),
|
"TEST_SESSIONSCRIPT=" + testScript,
|
||||||
"TEST_AFTER=" + writeFile(ts.t, jsafter),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ts.keySHS != nil {
|
if ts.keySHS != nil {
|
||||||
|
@ -213,13 +199,10 @@ func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*ref
|
||||||
|
|
||||||
var port = 1024 + mrand.Intn(23000)
|
var port = 1024 + mrand.Intn(23000)
|
||||||
|
|
||||||
// TODO: pass goref's via function?
|
|
||||||
// TODO or nickname solution
|
|
||||||
env := []string{
|
env := []string{
|
||||||
"TEST_NAME=" + filepath.Join(ts.t.Name(), "jsbot-"+name),
|
"TEST_NAME=" + filepath.Join(ts.t.Name(), "jsbot-"+name),
|
||||||
"TEST_BOB=" + ts.gobot.Whoami().Ref(),
|
|
||||||
fmt.Sprintf("TEST_PORT=%d", port),
|
fmt.Sprintf("TEST_PORT=%d", port),
|
||||||
"TEST_BEFORE=" + testScriptFileName,
|
"TEST_SESSIONSCRIPT=" + testScriptFileName,
|
||||||
}
|
}
|
||||||
if ts.keySHS != nil {
|
if ts.keySHS != nil {
|
||||||
env = append(env, "TEST_APPKEY="+base64.StdEncoding.EncodeToString(ts.keySHS))
|
env = append(env, "TEST_APPKEY="+base64.StdEncoding.EncodeToString(ts.keySHS))
|
||||||
|
@ -249,8 +232,6 @@ func (ts *testSession) startJSBotAsServer(name, testScriptFileName string) (*ref
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *testSession) wait() {
|
func (ts *testSession) wait() {
|
||||||
ts.gobot.Shutdown()
|
|
||||||
assert.NoError(ts.t, ts.gobot.Close())
|
|
||||||
ts.cancel()
|
ts.cancel()
|
||||||
|
|
||||||
assert.NoError(ts.t, ts.done.Wait())
|
assert.NoError(ts.t, ts.done.Wait())
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
before: (sbot, ready) => {
|
||||||
|
sbot.on('rpc:connect', rpc => {
|
||||||
|
var ret = rpc.tunnel.announce()
|
||||||
|
console.warn('announced')
|
||||||
|
console.warn(ret)
|
||||||
|
pull(
|
||||||
|
rpc.tunnel.endpoints(),
|
||||||
|
pull.drain(el => {
|
||||||
|
console.warn("from roomsrv:",el)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
ret = rpc.tunnel.leave()
|
||||||
|
console.warn('left')
|
||||||
|
console.warn(ret)
|
||||||
|
}, 2500)
|
||||||
|
})
|
||||||
|
ready()
|
||||||
|
},
|
||||||
|
|
||||||
|
after: (sbot, exit) => {
|
||||||
|
console.warn('after connect...')
|
||||||
|
|
||||||
|
setTimeout(exit, 5000)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
before: (sbot, ready) => {
|
||||||
|
console.warn('before connect...')
|
||||||
|
setTimeout(ready, 1000)
|
||||||
|
},
|
||||||
|
|
||||||
|
after: (sbot, exit) => {
|
||||||
|
console.warn('after connect...')
|
||||||
|
|
||||||
|
setTimeout(exit, 5000)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue