rpctest: create new rpctest package
This commit adds a new package (rpctest) which provides functionality
for writing automated black box tests to exercise the RPC interface.
An instance of a rpctest consists of an active btcd process running in
(typically) --simnet mode, a btcrpcclient instance connected to said
node, and finally an embedded in-memory wallet instance (the memWallet)
which manages any created coinbase outputs created by the mining btcd
node.
As part of the SetUp process for an RPC test, a test author can
optionally opt to have a test blockchain created. The second argument
to SetUp dictates the number of mature coinbase outputs desired. The
btcd process will then be directed to generate a test chain of length:
100 + numMatureOutputs.
The embedded memWallet instance acts as a minimal, simple wallet for
each Harness instance. The memWallet itself is a BIP 32 HD wallet
capable of creating new addresses, creating fully signed transactions,
creating+broadcasting a transaction paying to an arbitrary set of
outputs, and querying the currently confirmed balance.
In order to test various scenarios of blocks containing arbitrary
transactions, one can use the Generate rpc call via the exposed
btcrpcclient connected to the active btcd node. Additionally, the
Harness also exposes a secondary block generation API allowing callers
to create blocks with a set of hand-selected transactions, and an
arbitrary BlockVersion or Timestamp.
After execution of test logic TearDown should be called, allowing the
test instance to clean up created temporary directories, and shut down
the running processes.
Running multiple concurrent rpctest.Harness instances is supported in
order to allow for test authors to exercise complex scenarios. As a
result, the primary interface to create, and initialize an
rpctest.Harness instance is concurrent safe, with shared package level
private global variables protected by a sync.Mutex.
Fixes #116.
2016-08-20 00:36:56 +02:00
|
|
|
// Copyright (c) 2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package rpctest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2017-08-16 02:52:38 +02:00
|
|
|
rpc "github.com/btcsuite/btcd/rpcclient"
|
rpctest: create new rpctest package
This commit adds a new package (rpctest) which provides functionality
for writing automated black box tests to exercise the RPC interface.
An instance of a rpctest consists of an active btcd process running in
(typically) --simnet mode, a btcrpcclient instance connected to said
node, and finally an embedded in-memory wallet instance (the memWallet)
which manages any created coinbase outputs created by the mining btcd
node.
As part of the SetUp process for an RPC test, a test author can
optionally opt to have a test blockchain created. The second argument
to SetUp dictates the number of mature coinbase outputs desired. The
btcd process will then be directed to generate a test chain of length:
100 + numMatureOutputs.
The embedded memWallet instance acts as a minimal, simple wallet for
each Harness instance. The memWallet itself is a BIP 32 HD wallet
capable of creating new addresses, creating fully signed transactions,
creating+broadcasting a transaction paying to an arbitrary set of
outputs, and querying the currently confirmed balance.
In order to test various scenarios of blocks containing arbitrary
transactions, one can use the Generate rpc call via the exposed
btcrpcclient connected to the active btcd node. Additionally, the
Harness also exposes a secondary block generation API allowing callers
to create blocks with a set of hand-selected transactions, and an
arbitrary BlockVersion or Timestamp.
After execution of test logic TearDown should be called, allowing the
test instance to clean up created temporary directories, and shut down
the running processes.
Running multiple concurrent rpctest.Harness instances is supported in
order to allow for test authors to exercise complex scenarios. As a
result, the primary interface to create, and initialize an
rpctest.Harness instance is concurrent safe, with shared package level
private global variables protected by a sync.Mutex.
Fixes #116.
2016-08-20 00:36:56 +02:00
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// nodeConfig contains all the args, and data required to launch a btcd process
|
|
|
|
// and connect the rpc client to it.
|
|
|
|
type nodeConfig struct {
|
|
|
|
rpcUser string
|
|
|
|
rpcPass string
|
|
|
|
listen string
|
|
|
|
rpcListen string
|
|
|
|
rpcConnect string
|
|
|
|
dataDir string
|
|
|
|
logDir string
|
|
|
|
profile string
|
|
|
|
debugLevel string
|
|
|
|
extra []string
|
|
|
|
prefix string
|
|
|
|
|
|
|
|
exe string
|
|
|
|
endpoint string
|
|
|
|
certFile string
|
|
|
|
keyFile string
|
|
|
|
certificates []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// newConfig returns a newConfig with all default values.
|
|
|
|
func newConfig(prefix, certFile, keyFile string, extra []string) (*nodeConfig, error) {
|
2017-09-29 23:36:37 +02:00
|
|
|
btcdPath, err := btcdExecutablePath()
|
|
|
|
if err != nil {
|
2017-11-24 06:31:01 +01:00
|
|
|
btcdPath = "btcd"
|
2017-09-29 23:36:37 +02:00
|
|
|
}
|
|
|
|
|
rpctest: create new rpctest package
This commit adds a new package (rpctest) which provides functionality
for writing automated black box tests to exercise the RPC interface.
An instance of a rpctest consists of an active btcd process running in
(typically) --simnet mode, a btcrpcclient instance connected to said
node, and finally an embedded in-memory wallet instance (the memWallet)
which manages any created coinbase outputs created by the mining btcd
node.
As part of the SetUp process for an RPC test, a test author can
optionally opt to have a test blockchain created. The second argument
to SetUp dictates the number of mature coinbase outputs desired. The
btcd process will then be directed to generate a test chain of length:
100 + numMatureOutputs.
The embedded memWallet instance acts as a minimal, simple wallet for
each Harness instance. The memWallet itself is a BIP 32 HD wallet
capable of creating new addresses, creating fully signed transactions,
creating+broadcasting a transaction paying to an arbitrary set of
outputs, and querying the currently confirmed balance.
In order to test various scenarios of blocks containing arbitrary
transactions, one can use the Generate rpc call via the exposed
btcrpcclient connected to the active btcd node. Additionally, the
Harness also exposes a secondary block generation API allowing callers
to create blocks with a set of hand-selected transactions, and an
arbitrary BlockVersion or Timestamp.
After execution of test logic TearDown should be called, allowing the
test instance to clean up created temporary directories, and shut down
the running processes.
Running multiple concurrent rpctest.Harness instances is supported in
order to allow for test authors to exercise complex scenarios. As a
result, the primary interface to create, and initialize an
rpctest.Harness instance is concurrent safe, with shared package level
private global variables protected by a sync.Mutex.
Fixes #116.
2016-08-20 00:36:56 +02:00
|
|
|
a := &nodeConfig{
|
|
|
|
listen: "127.0.0.1:18555",
|
|
|
|
rpcListen: "127.0.0.1:18556",
|
|
|
|
rpcUser: "user",
|
|
|
|
rpcPass: "pass",
|
|
|
|
extra: extra,
|
|
|
|
prefix: prefix,
|
2017-09-29 23:36:37 +02:00
|
|
|
exe: btcdPath,
|
|
|
|
endpoint: "ws",
|
|
|
|
certFile: certFile,
|
|
|
|
keyFile: keyFile,
|
rpctest: create new rpctest package
This commit adds a new package (rpctest) which provides functionality
for writing automated black box tests to exercise the RPC interface.
An instance of a rpctest consists of an active btcd process running in
(typically) --simnet mode, a btcrpcclient instance connected to said
node, and finally an embedded in-memory wallet instance (the memWallet)
which manages any created coinbase outputs created by the mining btcd
node.
As part of the SetUp process for an RPC test, a test author can
optionally opt to have a test blockchain created. The second argument
to SetUp dictates the number of mature coinbase outputs desired. The
btcd process will then be directed to generate a test chain of length:
100 + numMatureOutputs.
The embedded memWallet instance acts as a minimal, simple wallet for
each Harness instance. The memWallet itself is a BIP 32 HD wallet
capable of creating new addresses, creating fully signed transactions,
creating+broadcasting a transaction paying to an arbitrary set of
outputs, and querying the currently confirmed balance.
In order to test various scenarios of blocks containing arbitrary
transactions, one can use the Generate rpc call via the exposed
btcrpcclient connected to the active btcd node. Additionally, the
Harness also exposes a secondary block generation API allowing callers
to create blocks with a set of hand-selected transactions, and an
arbitrary BlockVersion or Timestamp.
After execution of test logic TearDown should be called, allowing the
test instance to clean up created temporary directories, and shut down
the running processes.
Running multiple concurrent rpctest.Harness instances is supported in
order to allow for test authors to exercise complex scenarios. As a
result, the primary interface to create, and initialize an
rpctest.Harness instance is concurrent safe, with shared package level
private global variables protected by a sync.Mutex.
Fixes #116.
2016-08-20 00:36:56 +02:00
|
|
|
}
|
|
|
|
if err := a.setDefaults(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setDefaults sets the default values of the config. It also creates the
|
|
|
|
// temporary data, and log directories which must be cleaned up with a call to
|
|
|
|
// cleanup().
|
|
|
|
func (n *nodeConfig) setDefaults() error {
|
|
|
|
datadir, err := ioutil.TempDir("", n.prefix+"-data")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.dataDir = datadir
|
|
|
|
logdir, err := ioutil.TempDir("", n.prefix+"-logs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.logDir = logdir
|
|
|
|
cert, err := ioutil.ReadFile(n.certFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.certificates = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// arguments returns an array of arguments that be used to launch the btcd
|
|
|
|
// process.
|
|
|
|
func (n *nodeConfig) arguments() []string {
|
|
|
|
args := []string{}
|
|
|
|
if n.rpcUser != "" {
|
|
|
|
// --rpcuser
|
|
|
|
args = append(args, fmt.Sprintf("--rpcuser=%s", n.rpcUser))
|
|
|
|
}
|
|
|
|
if n.rpcPass != "" {
|
|
|
|
// --rpcpass
|
|
|
|
args = append(args, fmt.Sprintf("--rpcpass=%s", n.rpcPass))
|
|
|
|
}
|
|
|
|
if n.listen != "" {
|
|
|
|
// --listen
|
|
|
|
args = append(args, fmt.Sprintf("--listen=%s", n.listen))
|
|
|
|
}
|
|
|
|
if n.rpcListen != "" {
|
|
|
|
// --rpclisten
|
|
|
|
args = append(args, fmt.Sprintf("--rpclisten=%s", n.rpcListen))
|
|
|
|
}
|
|
|
|
if n.rpcConnect != "" {
|
|
|
|
// --rpcconnect
|
|
|
|
args = append(args, fmt.Sprintf("--rpcconnect=%s", n.rpcConnect))
|
|
|
|
}
|
|
|
|
// --rpccert
|
|
|
|
args = append(args, fmt.Sprintf("--rpccert=%s", n.certFile))
|
|
|
|
// --rpckey
|
|
|
|
args = append(args, fmt.Sprintf("--rpckey=%s", n.keyFile))
|
|
|
|
// --txindex
|
|
|
|
args = append(args, "--txindex")
|
|
|
|
// --addrindex
|
|
|
|
args = append(args, "--addrindex")
|
|
|
|
if n.dataDir != "" {
|
|
|
|
// --datadir
|
|
|
|
args = append(args, fmt.Sprintf("--datadir=%s", n.dataDir))
|
|
|
|
}
|
|
|
|
if n.logDir != "" {
|
|
|
|
// --logdir
|
|
|
|
args = append(args, fmt.Sprintf("--logdir=%s", n.logDir))
|
|
|
|
}
|
|
|
|
if n.profile != "" {
|
|
|
|
// --profile
|
|
|
|
args = append(args, fmt.Sprintf("--profile=%s", n.profile))
|
|
|
|
}
|
|
|
|
if n.debugLevel != "" {
|
|
|
|
// --debuglevel
|
|
|
|
args = append(args, fmt.Sprintf("--debuglevel=%s", n.debugLevel))
|
|
|
|
}
|
|
|
|
args = append(args, n.extra...)
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// command returns the exec.Cmd which will be used to start the btcd process.
|
|
|
|
func (n *nodeConfig) command() *exec.Cmd {
|
|
|
|
return exec.Command(n.exe, n.arguments()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rpcConnConfig returns the rpc connection config that can be used to connect
|
|
|
|
// to the btcd process that is launched via Start().
|
|
|
|
func (n *nodeConfig) rpcConnConfig() rpc.ConnConfig {
|
|
|
|
return rpc.ConnConfig{
|
|
|
|
Host: n.rpcListen,
|
|
|
|
Endpoint: n.endpoint,
|
|
|
|
User: n.rpcUser,
|
|
|
|
Pass: n.rpcPass,
|
|
|
|
Certificates: n.certificates,
|
|
|
|
DisableAutoReconnect: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of this nodeConfig.
|
|
|
|
func (n *nodeConfig) String() string {
|
|
|
|
return n.prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup removes the tmp data and log directories.
|
|
|
|
func (n *nodeConfig) cleanup() error {
|
|
|
|
dirs := []string{
|
|
|
|
n.logDir,
|
|
|
|
n.dataDir,
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if err = os.RemoveAll(dir); err != nil {
|
|
|
|
log.Printf("Cannot remove dir %s: %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// node houses the necessary state required to configure, launch, and manage a
|
|
|
|
// btcd process.
|
|
|
|
type node struct {
|
|
|
|
config *nodeConfig
|
|
|
|
|
|
|
|
cmd *exec.Cmd
|
|
|
|
pidFile string
|
|
|
|
|
|
|
|
dataDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNode creates a new node instance according to the passed config. dataDir
|
|
|
|
// will be used to hold a file recording the pid of the launched process, and
|
|
|
|
// as the base for the log and data directories for btcd.
|
|
|
|
func newNode(config *nodeConfig, dataDir string) (*node, error) {
|
|
|
|
return &node{
|
|
|
|
config: config,
|
|
|
|
dataDir: dataDir,
|
|
|
|
cmd: config.command(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// start creates a new btcd process, and writes its pid in a file reserved for
|
|
|
|
// recording the pid of the launched process. This file can be used to
|
|
|
|
// terminate the process in case of a hang, or panic. In the case of a failing
|
|
|
|
// test case, or panic, it is important that the process be stopped via stop(),
|
|
|
|
// otherwise, it will persist unless explicitly killed.
|
|
|
|
func (n *node) start() error {
|
|
|
|
if err := n.cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := os.Create(filepath.Join(n.dataDir,
|
|
|
|
fmt.Sprintf("%s.pid", n.config)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.pidFile = pid.Name()
|
|
|
|
if _, err = fmt.Fprintf(pid, "%d\n", n.cmd.Process.Pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pid.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop interrupts the running btcd process process, and waits until it exits
|
|
|
|
// properly. On windows, interrupt is not supported, so a kill signal is used
|
|
|
|
// instead
|
|
|
|
func (n *node) stop() error {
|
|
|
|
if n.cmd == nil || n.cmd.Process == nil {
|
|
|
|
// return if not properly initialized
|
|
|
|
// or error starting the process
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer n.cmd.Wait()
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return n.cmd.Process.Signal(os.Kill)
|
|
|
|
}
|
|
|
|
return n.cmd.Process.Signal(os.Interrupt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup cleanups process and args files. The file housing the pid of the
|
|
|
|
// created process will be deleted, as well as any directories created by the
|
|
|
|
// process.
|
|
|
|
func (n *node) cleanup() error {
|
|
|
|
if n.pidFile != "" {
|
|
|
|
if err := os.Remove(n.pidFile); err != nil {
|
|
|
|
log.Printf("unable to remove file %s: %v", n.pidFile,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.config.cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
// shutdown terminates the running btcd process, and cleans up all
|
|
|
|
// file/directories created by node.
|
|
|
|
func (n *node) shutdown() error {
|
|
|
|
if err := n.stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.cleanup(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genCertPair generates a key/cert pair to the paths provided.
|
|
|
|
func genCertPair(certFile, keyFile string) error {
|
|
|
|
org := "rpctest autogenerated cert"
|
|
|
|
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
|
|
|
|
cert, key, err := btcutil.NewTLSCertPair(org, validUntil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write cert and key files.
|
|
|
|
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
|
|
|
|
os.Remove(certFile)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|