mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 18:47:38 +01:00
Add BitcoinSServerMain test to connect to bitcoind (#3591)
* Add BitcoinSServerMain test to connect to bitcoind * Add tor tests * Fix compile * Remove println * Allow to reset BitcoinSServer promise * Remove println Co-authored-by: benthecarman <benthecarman@live.com>
This commit is contained in:
parent
184bf415df
commit
a5f4f9663c
8 changed files with 202 additions and 5 deletions
|
@ -9,7 +9,7 @@ object Cli extends App {
|
||||||
import System.err.{println => printerr}
|
import System.err.{println => printerr}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ConsoleCli.exec(args.toVector: _*) match {
|
ConsoleCli.exec(args.toVector) match {
|
||||||
case Success(output) => println(output)
|
case Success(output) => println(output)
|
||||||
case Failure(err) =>
|
case Failure(err) =>
|
||||||
printerr(err.getMessage)
|
printerr(err.getMessage)
|
||||||
|
|
|
@ -1491,8 +1491,8 @@ object ConsoleCli {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def exec(args: String*): Try[String] = {
|
def exec(args: Vector[String]): Try[String] = {
|
||||||
val config = OParser.parse(parser, args.toVector, Config()) match {
|
val config = OParser.parse(parser, args, Config()) match {
|
||||||
case None => sys.exit(1)
|
case None => sys.exit(1)
|
||||||
case Some(conf) => conf
|
case Some(conf) => conf
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package org.bitcoins.server
|
||||||
|
|
||||||
|
import org.bitcoins.cli.{CliCommand, Config, ConsoleCli}
|
||||||
|
import org.bitcoins.commons.util.ServerArgParser
|
||||||
|
import org.bitcoins.testkit.fixtures.BitcoinSAppConfigBitcoinFixtureNotStarted
|
||||||
|
|
||||||
|
/** Test starting bitcoin-s with bitcoind as the backend for app */
|
||||||
|
class BitcoinSServerMainBitcoindTest
|
||||||
|
extends BitcoinSAppConfigBitcoinFixtureNotStarted {
|
||||||
|
|
||||||
|
behavior of "BitcoinSServerMain"
|
||||||
|
|
||||||
|
it must "start our app server with bitcoind as a backend" in {
|
||||||
|
config: BitcoinSAppConfig =>
|
||||||
|
val server = new BitcoinSServerMain(ServerArgParser.empty)(system, config)
|
||||||
|
|
||||||
|
val cliConfig = Config(rpcPortOpt = Some(config.rpcPort))
|
||||||
|
|
||||||
|
for {
|
||||||
|
_ <- server.start()
|
||||||
|
// Await RPC server started
|
||||||
|
_ <- BitcoinSServer.startedF
|
||||||
|
|
||||||
|
info = ConsoleCli.exec(CliCommand.WalletInfo, cliConfig)
|
||||||
|
balance = ConsoleCli.exec(CliCommand.GetBalance(isSats = true),
|
||||||
|
cliConfig)
|
||||||
|
addr = ConsoleCli.exec(CliCommand.GetNewAddress(labelOpt = None),
|
||||||
|
cliConfig)
|
||||||
|
blockHash = ConsoleCli.exec(CliCommand.GetBestBlockHash, cliConfig)
|
||||||
|
} yield {
|
||||||
|
assert(info.isSuccess)
|
||||||
|
assert(balance.isSuccess)
|
||||||
|
assert(balance.get == "0 sats")
|
||||||
|
assert(addr.isSuccess)
|
||||||
|
assert(blockHash.isSuccess)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override def afterAll(): Unit = {
|
||||||
|
super.afterAll()
|
||||||
|
BitcoinSServer.reset()
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package org.bitcoins.server
|
||||||
|
|
||||||
|
import org.bitcoins.cli.{CliCommand, Config, ConsoleCli}
|
||||||
|
import org.bitcoins.commons.util.ServerArgParser
|
||||||
|
import org.bitcoins.testkit.fixtures.BitcoinSAppConfigBitcoinFixtureNotStarted
|
||||||
|
import org.bitcoins.testkit.tor.CachedTor
|
||||||
|
|
||||||
|
/** Test starting bitcoin-s with bitcoind as the backend for app */
|
||||||
|
class BitcoinSServerMainBitcoindTorTest
|
||||||
|
extends BitcoinSAppConfigBitcoinFixtureNotStarted
|
||||||
|
with CachedTor {
|
||||||
|
|
||||||
|
behavior of "BitcoinSServerMain"
|
||||||
|
|
||||||
|
it must "start our app server with bitcoind as a backend with tor" in {
|
||||||
|
config: BitcoinSAppConfig =>
|
||||||
|
val server = new BitcoinSServerMain(ServerArgParser.empty)(system, config)
|
||||||
|
|
||||||
|
val cliConfig: Config = Config(rpcPortOpt = Some(config.rpcPort))
|
||||||
|
|
||||||
|
for {
|
||||||
|
_ <- torF
|
||||||
|
_ <- server.start()
|
||||||
|
// Await RPC server started
|
||||||
|
_ <- BitcoinSServer.startedF
|
||||||
|
|
||||||
|
info = ConsoleCli.exec(CliCommand.WalletInfo, cliConfig)
|
||||||
|
balance = ConsoleCli.exec(CliCommand.GetBalance(isSats = true),
|
||||||
|
cliConfig)
|
||||||
|
addr = ConsoleCli.exec(CliCommand.GetNewAddress(labelOpt = None),
|
||||||
|
cliConfig)
|
||||||
|
blockHash = ConsoleCli.exec(CliCommand.GetBestBlockHash, cliConfig)
|
||||||
|
} yield {
|
||||||
|
assert(info.isSuccess)
|
||||||
|
assert(balance.isSuccess)
|
||||||
|
assert(balance.get == "0 sats")
|
||||||
|
assert(addr.isSuccess)
|
||||||
|
assert(blockHash.isSuccess)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override def afterAll(): Unit = {
|
||||||
|
super.afterAll()
|
||||||
|
BitcoinSServer.reset()
|
||||||
|
}
|
||||||
|
}
|
|
@ -445,7 +445,7 @@ object BitcoinSServerMain extends BitcoinSAppScalaDaemon {
|
||||||
}
|
}
|
||||||
|
|
||||||
object BitcoinSServer {
|
object BitcoinSServer {
|
||||||
private[server] val startedFP = Promise[Future[Http.ServerBinding]]()
|
private[server] var startedFP = Promise[Future[Http.ServerBinding]]()
|
||||||
|
|
||||||
/** Allows the above server to be bundled with other projects.
|
/** Allows the above server to be bundled with other projects.
|
||||||
*
|
*
|
||||||
|
@ -457,4 +457,8 @@ object BitcoinSServer {
|
||||||
* been initialized.
|
* been initialized.
|
||||||
*/
|
*/
|
||||||
val startedF: Future[Http.ServerBinding] = startedFP.future.flatten
|
val startedF: Future[Http.ServerBinding] = startedFP.future.flatten
|
||||||
|
|
||||||
|
def reset(): Unit = {
|
||||||
|
startedFP = Promise[Future[Http.ServerBinding]]()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -398,9 +398,11 @@ lazy val appServerTest = project
|
||||||
.in(file("app/server-test"))
|
.in(file("app/server-test"))
|
||||||
.settings(CommonSettings.testSettings: _*)
|
.settings(CommonSettings.testSettings: _*)
|
||||||
.settings(libraryDependencies ++= Deps.walletServerTest)
|
.settings(libraryDependencies ++= Deps.walletServerTest)
|
||||||
|
.settings(parallelExecution := false)
|
||||||
.dependsOn(
|
.dependsOn(
|
||||||
appServer,
|
appServer,
|
||||||
testkit
|
testkit,
|
||||||
|
cli
|
||||||
)
|
)
|
||||||
|
|
||||||
lazy val cli = project
|
lazy val cli = project
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
package org.bitcoins.testkit.fixtures
|
||||||
|
|
||||||
|
import com.typesafe.config.{Config, ConfigFactory}
|
||||||
|
import org.bitcoins.rpc.config.BitcoindInstance
|
||||||
|
import org.bitcoins.rpc.util.RpcUtil
|
||||||
|
import org.bitcoins.server.BitcoinSAppConfig
|
||||||
|
import org.bitcoins.testkit.rpc.CachedBitcoindNewest
|
||||||
|
import org.bitcoins.testkit.util.TorUtil
|
||||||
|
import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg}
|
||||||
|
import org.scalatest.FutureOutcome
|
||||||
|
|
||||||
|
import scala.concurrent.Future
|
||||||
|
|
||||||
|
sealed trait BitcoinSAppConfigFixture extends BitcoinSFixture with EmbeddedPg {
|
||||||
|
override type FixtureParam = BitcoinSAppConfig
|
||||||
|
|
||||||
|
override def afterAll(): Unit = {
|
||||||
|
super[EmbeddedPg].afterAll()
|
||||||
|
super[BitcoinSFixture].afterAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Makes a bitcoin-s app config with proper bitcoind credentials
|
||||||
|
* and bitcoin-s.node.mode=bitcoind to use bitcoin as the backend
|
||||||
|
*
|
||||||
|
* The [[BitcoinSAppConfig]] is not started
|
||||||
|
*/
|
||||||
|
trait BitcoinSAppConfigBitcoinFixtureNotStarted
|
||||||
|
extends BitcoinSAppConfigFixture
|
||||||
|
with CachedBitcoindNewest {
|
||||||
|
|
||||||
|
override type FixtureParam = BitcoinSAppConfig
|
||||||
|
|
||||||
|
override def withFixture(test: OneArgAsyncTest): FutureOutcome = {
|
||||||
|
val builder: () => Future[BitcoinSAppConfig] = () => {
|
||||||
|
for {
|
||||||
|
bitcoind <- cachedBitcoindWithFundsF
|
||||||
|
datadir = bitcoind.instance.datadir
|
||||||
|
conf = buildConfig(bitcoind.instance)
|
||||||
|
bitcoinSAppConfig = BitcoinSAppConfig(datadir.toPath, conf)
|
||||||
|
} yield bitcoinSAppConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
val destroyF: BitcoinSAppConfig => Future[Unit] = { appConfig =>
|
||||||
|
val stopF = appConfig
|
||||||
|
.stop()
|
||||||
|
.map(_ => BitcoinSTestAppConfig.deleteAppConfig(appConfig))
|
||||||
|
.map(_ => ())
|
||||||
|
stopF
|
||||||
|
}
|
||||||
|
|
||||||
|
makeDependentFixture(builder, destroyF)(test)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Builds a configuration with the proper bitcoind credentials and bitcoin-s node mode set to bitcoind
|
||||||
|
* and sets tor config
|
||||||
|
*/
|
||||||
|
private def buildConfig(instance: BitcoindInstance): Config = {
|
||||||
|
val configStr =
|
||||||
|
s"""
|
||||||
|
|bitcoin-s.bitcoind-rpc.rpcuser="${instance.authCredentials.username}"
|
||||||
|
|bitcoin-s.bitcoind-rpc.rpcpassword="${instance.authCredentials.password}"
|
||||||
|
|bitcoin-s.bitcoind-rpc.rpcbind="${instance.rpcUri.getHost}"
|
||||||
|
|bitcoin-s.bitcoind-rpc.rpcport="${instance.rpcUri.getPort}"
|
||||||
|
|bitcoin-s.bitcoind-rpc.isRemote=true
|
||||||
|
|bitcoin-s.bitcoind-rpc.version="${instance.getVersion}"
|
||||||
|
|bitcoin-s.node.mode=bitcoind
|
||||||
|
|bitcoin-s.tor.enabled=${TorUtil.torEnabled}
|
||||||
|
|bitcoin-s.proxy.enabled=${TorUtil.torEnabled}
|
||||||
|
|bitcoin-s.dlcnode.listen = "0.0.0.0:${RpcUtil.randomPort}"
|
||||||
|
|bitcoin-s.server.rpcport = ${RpcUtil.randomPort}
|
||||||
|
|""".stripMargin
|
||||||
|
|
||||||
|
ConfigFactory.parseString(configStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
override def afterAll(): Unit = {
|
||||||
|
super[CachedBitcoindNewest].afterAll()
|
||||||
|
super[BitcoinSAppConfigFixture].afterAll()
|
||||||
|
}
|
||||||
|
}
|
|
@ -189,6 +189,27 @@ trait BitcoindFixturesFundedCachedV21
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait BitcoindFixturesFundedCachedNewest
|
||||||
|
extends BitcoinSAsyncFixtureTest
|
||||||
|
with BitcoindFixturesFundedCached
|
||||||
|
with CachedBitcoindNewest {
|
||||||
|
override type FixtureParam = BitcoindRpcClient
|
||||||
|
|
||||||
|
override def withFixture(test: OneArgAsyncTest): FutureOutcome = {
|
||||||
|
val f: Future[Outcome] = for {
|
||||||
|
bitcoind <- cachedBitcoindWithFundsF
|
||||||
|
futOutcome = withNewestFundedBitcoindCached(test, bitcoind)
|
||||||
|
fut <- futOutcome.toFuture
|
||||||
|
} yield fut
|
||||||
|
new FutureOutcome(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
override def afterAll(): Unit = {
|
||||||
|
super[CachedBitcoindNewest].afterAll()
|
||||||
|
super[BitcoinSAsyncFixtureTest].afterAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Bitcoind fixtures with two cached bitcoind that are connected via p2p */
|
/** Bitcoind fixtures with two cached bitcoind that are connected via p2p */
|
||||||
trait BitcoindFixturesCachedPair[T <: BitcoindRpcClient]
|
trait BitcoindFixturesCachedPair[T <: BitcoindRpcClient]
|
||||||
extends BitcoindFixturesCached
|
extends BitcoindFixturesCached
|
||||||
|
|
Loading…
Add table
Reference in a new issue