Create convenient way to call bitcoin-cli from tests

A BitcoinCliHelper class was added.  GetBalanceTest's @BeforeClass
was simplified, and it no longer starts up the arbitration node.
This commit is contained in:
ghubstan 2020-07-16 19:22:51 -03:00
parent 7d664d9cfd
commit 6edab1a2cd
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 73 additions and 12 deletions

View File

@ -26,6 +26,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import bisq.apitest.config.ApiTestConfig;
import bisq.apitest.method.BitcoinCliHelper;
public class ApiTestCase {
@ -34,6 +35,7 @@ public class ApiTestCase {
protected static Scaffold scaffold;
protected static ApiTestConfig config;
protected static BitcoinCliHelper bitcoinCli;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@ -43,6 +45,7 @@ public class ApiTestCase {
// names, e.g. "bitcoind,seednode,arbdaemon,alicedaemon,bobdaemon"
scaffold = new Scaffold(supportingApps).setUp();
config = scaffold.config;
bitcoinCli = new BitcoinCliHelper((config));
grpcStubs = new GrpcStubs(alicedaemon, config).init();
}

View File

@ -0,0 +1,64 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.method;
import java.io.IOException;
import static java.lang.String.format;
import static org.junit.Assert.fail;
import bisq.apitest.config.ApiTestConfig;
import bisq.apitest.linux.BitcoinCli;
public final class BitcoinCliHelper {
private final ApiTestConfig config;
public BitcoinCliHelper(ApiTestConfig config) {
this.config = config;
}
// Convenience methods for making bitcoin-cli calls.
public String getNewBtcAddress() {
try {
return new BitcoinCli(config, "getnewaddress").run().getOutput();
} catch (IOException | InterruptedException ex) {
fail(ex.getMessage());
return null;
}
}
public String[] generateToAddress(int blocks, String address) {
try {
String generateToAddressCmd = format("generatetoaddress %d \"%s\"", blocks, address);
BitcoinCli generateToAddress = new BitcoinCli(config, generateToAddressCmd).run();
// Return an array of transaction ids.
return generateToAddress.getOutputValueAsStringArray();
} catch (IOException | InterruptedException ex) {
fail(ex.getMessage());
return null;
}
}
public void generateBlocks(int blocks) {
generateToAddress(blocks, getNewBtcAddress());
}
}

View File

@ -19,8 +19,6 @@ package bisq.apitest.method;
import bisq.proto.grpc.GetBalanceRequest;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
@ -28,7 +26,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import static java.lang.String.format;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@ -37,7 +34,6 @@ import static org.junit.Assert.fail;
import bisq.apitest.OrderedRunner;
import bisq.apitest.annotation.Order;
import bisq.apitest.linux.BitcoinCli;
@Slf4j
@RunWith(OrderedRunner.class)
@ -46,16 +42,14 @@ public class GetBalanceTest extends MethodTest {
@BeforeClass
public static void setUp() {
try {
setUpScaffold("bitcoind,seednode,arbdaemon,alicedaemon");
setUpScaffold("bitcoind,seednode,alicedaemon");
String newAddress = new BitcoinCli(config, "getnewaddress").run().getOutput();
String generateToAddressCmd = format("generatetoaddress %d \"%s\"", 1, newAddress);
// Have to generate 1 regtest block for alice's wallet to show 10 BTC balance.
bitcoinCli.generateBlocks(1);
BitcoinCli generateToAddress = new BitcoinCli(config, generateToAddressCmd).run();
log.info("{}\n{}", generateToAddress.getCommandWithOptions(), generateToAddress.getOutputValueAsStringArray());
MILLISECONDS.sleep(1500); // give bisq app time to parse block
} catch (IOException | InterruptedException ex) {
// Give the alicedaemon time to parse the new block.
MILLISECONDS.sleep(1500);
} catch (InterruptedException ex) {
fail(ex.getMessage());
}
}