Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate BFT tests to junit 5 #6350

Merged
merged 14 commits into from
Jan 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable
public void testSuccessful(final ExtensionContext extensionContext) {
// if so configured, delete logs of successful tests
if (!Boolean.getBoolean("acctests.keepLogsOfPassingTests")) {
// log4j is configured to create a file per test
// build/acceptanceTestLogs/${ctx:class}.${ctx:test}.log
String pathname =
"build/acceptanceTestLogs/"
+ extensionContext.getTestClass().get().getSimpleName()
+ "."
+ extensionContext.getTestMethod().get().getName()
+ ".log";
LOG.info("Test successful, deleting log at {}", pathname);
File file = new File(pathname);
file.delete();
try {
// log4j is configured to create a file per test
// build/acceptanceTestLogs/${ctx:class}.${ctx:test}.log
String pathname =
"build/acceptanceTestLogs/"
+ extensionContext.getTestClass().get().getSimpleName()
+ "."
+ extensionContext.getTestMethod().get().getName()
+ ".log";
LOG.info("Test successful, deleting log at {}", pathname);
final File file = new File(pathname);
file.delete();
} catch (final Exception e) {
LOG.error("could not delete test file", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.params.provider.Arguments;

public class BftAcceptanceTestParameterization {

public static List<Object[]> getFactories() {
final List<Object[]> ret = new ArrayList<>();
ret.addAll(
List.of(
new Object[] {
"ibft2",
new BftAcceptanceTestParameterization(
BesuNodeFactory::createIbft2Node, BesuNodeFactory::createIbft2NodeWithValidators)
},
new Object[] {
"qbft",
new BftAcceptanceTestParameterization(
BesuNodeFactory::createQbftNode, BesuNodeFactory::createQbftNodeWithValidators)
}));
return ret;
public static Stream<Arguments> getFactories() {
return Stream.of(
Arguments.of(
"ibft2",
new BftAcceptanceTestParameterization(
BesuNodeFactory::createIbft2Node, BesuNodeFactory::createIbft2NodeWithValidators)),
Arguments.of(
"qbft",
new BftAcceptanceTestParameterization(
BesuNodeFactory::createQbftNode, BesuNodeFactory::createQbftNodeWithValidators)));
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@
import java.util.Optional;
import java.util.TreeMap;

import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class BftBlockRewardPaymentAcceptanceTest extends ParameterizedBftTestBase {

private static final Amount BLOCK_REWARD = Amount.wei(new BigInteger("5000000000000000000", 10));

public BftBlockRewardPaymentAcceptanceTest(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void validatorsArePaidBlockReward() throws Exception {
@ParameterizedTest(name = "{0} bft node factory type")
@MethodSource("factoryFunctions")
public void validatorsArePaidBlockReward(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator"};
final BesuNode validator = nodeFactory.createNodeWithValidators(besu, "validator", validators);
final BesuNode nonValidator =
Expand All @@ -61,8 +60,11 @@ public void validatorsArePaidBlockReward() throws Exception {
Amount.ether(blockRewardEth * blockToCheck), BigInteger.valueOf(blockToCheck)));
}

@Test
public void payBlockRewardToConfiguredNode() throws Exception {
@ParameterizedTest(name = "{0} bft node factory type")
@MethodSource("factoryFunctions")
public void payBlockRewardToConfiguredNode(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator1"};
final BesuNode validator1 =
nodeFactory.createNodeWithValidators(besu, "validator1", validators);
Expand Down Expand Up @@ -90,9 +92,11 @@ public void payBlockRewardToConfiguredNode() throws Exception {
Amount.ether(blockRewardEth * blockToCheck), BigInteger.valueOf(blockToCheck)));
}

@Test
public void payBlockRewardAccordingToTransitions_defaultInitialMiningBeneficiary()
throws Exception {
@ParameterizedTest(name = "{0} bft node factory type")
@MethodSource("factoryFunctions")
public void payBlockRewardAccordingToTransitions_defaultInitialMiningBeneficiary(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final List<Address> addresses = generateAddresses(2);
final Map<Long, Optional<Address>> transitions =
Map.of(
Expand All @@ -103,9 +107,11 @@ public void payBlockRewardAccordingToTransitions_defaultInitialMiningBeneficiary
testMiningBeneficiaryTransitions(Optional.empty(), transitions);
}

@Test
public void payBlockRewardAccordingToTransitions_customInitialMiningBeneficiary()
throws Exception {
@ParameterizedTest(name = "{0} bft node factory type")
@MethodSource("factoryFunctions")
public void payBlockRewardAccordingToTransitions_customInitialMiningBeneficiary(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final List<Address> addresses = generateAddresses(4);
final Map<Long, Optional<Address>> transitions =
Map.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;

import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class BftDiscardRpcAcceptanceTest extends ParameterizedBftTestBase {

public BftDiscardRpcAcceptanceTest(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void shouldDiscardVotes() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldDiscardVotes(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator1", "validator3"};
final BesuNode validator1 =
nodeFactory.createNodeWithValidators(besu, "validator1", validators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@
import java.util.Optional;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class BftMiningAcceptanceTest extends ParameterizedBftTestBase {

public BftMiningAcceptanceTest(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void shouldMineOnSingleNodeWithPaidGas_Berlin() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnSingleNodeWithPaidGas_Berlin(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode = nodeFactory.createNode(besu, "miner1");
cluster.start(minerNode);

Expand All @@ -57,8 +56,11 @@ public void shouldMineOnSingleNodeWithPaidGas_Berlin() throws Exception {
cluster.verify(receiver.balanceEquals(3));
}

@Test
public void shouldMineOnSingleNodeWithFreeGas_Berlin() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnSingleNodeWithFreeGas_Berlin(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode = nodeFactory.createNode(besu, "miner1");
final MiningParameters zeroGasMiningParams =
ImmutableMiningParameters.builder()
Expand Down Expand Up @@ -90,8 +92,11 @@ public void shouldMineOnSingleNodeWithFreeGas_Berlin() throws Exception {
cluster.verify(receiver.balanceEquals(3));
}

@Test
public void shouldMineOnSingleNodeWithPaidGas_London() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnSingleNodeWithPaidGas_London(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode = nodeFactory.createNode(besu, "miner1");
updateGenesisConfigToLondon(minerNode, false);

Expand All @@ -115,8 +120,11 @@ public void shouldMineOnSingleNodeWithPaidGas_London() throws Exception {
cluster.verify(receiver.balanceEquals(3));
}

@Test
public void shouldMineOnSingleNodeWithFreeGas_London() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnSingleNodeWithFreeGas_London(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode = nodeFactory.createNode(besu, "miner1");
updateGenesisConfigToLondon(minerNode, true);

Expand All @@ -142,8 +150,11 @@ public void shouldMineOnSingleNodeWithFreeGas_London() throws Exception {
cluster.verify(receiver.balanceEquals(3));
}

@Test
public void shouldMineOnMultipleNodes() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnMultipleNodes(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode1 = nodeFactory.createNode(besu, "miner1");
final BesuNode minerNode2 = nodeFactory.createNode(besu, "miner2");
final BesuNode minerNode3 = nodeFactory.createNode(besu, "miner3");
Expand All @@ -168,8 +179,11 @@ public void shouldMineOnMultipleNodes() throws Exception {
cluster.verify(receiver.balanceEquals(6));
}

@Test
public void shouldMineOnMultipleNodesEvenWhenClusterContainsNonValidator() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldMineOnMultipleNodesEvenWhenClusterContainsNonValidator(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator1", "validator2", "validator3"};
final BesuNode validator1 =
nodeFactory.createNodeWithValidators(besu, "validator1", validators);
Expand All @@ -196,9 +210,11 @@ public void shouldMineOnMultipleNodesEvenWhenClusterContainsNonValidator() throw
cluster.verify(receiver.balanceEquals(3));
}

@Test
public void shouldStillMineWhenANonProposerNodeFailsAndHasSufficientValidators()
throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldStillMineWhenANonProposerNodeFailsAndHasSufficientValidators(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final BesuNode minerNode1 = nodeFactory.createNode(besu, "miner1");
final BesuNode minerNode2 = nodeFactory.createNode(besu, "miner2");
final BesuNode minerNode3 = nodeFactory.createNode(besu, "miner3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;

import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class BftProposalRpcAcceptanceTest extends ParameterizedBftTestBase {

public BftProposalRpcAcceptanceTest(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void shouldReturnProposals() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void shouldReturnProposals(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator1", "validator2", "validator3"};
final BesuNode validator1 =
nodeFactory.createNodeWithValidators(besu, "validator1", validators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
import org.hyperledger.besu.tests.acceptance.dsl.condition.Condition;
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;

import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

// These tests prove the ibft_proposeValidatorVote and ibft_getValidatorsByBlockNumber (implicitly)
// JSON RPC calls.
public class BftProposeRpcAcceptanceTest extends ParameterizedBftTestBase {

public BftProposeRpcAcceptanceTest(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void validatorsCanBeAddedAndThenRemoved() throws Exception {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("factoryFunctions")
public void validatorsCanBeAddedAndThenRemoved(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {"validator1", "validator2", "validator3"};
final BesuNode validator1 =
nodeFactory.createNodeWithValidators(besu, "validator1", validators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;

import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class BftZeroValidators extends ParameterizedBftTestBase {
public class BftZeroValidatorsAcceptanceTest extends ParameterizedBftTestBase {

public BftZeroValidators(
final String testName, final BftAcceptanceTestParameterization nodeFactory) {
super(testName, nodeFactory);
}

@Test
public void zeroValidatorsFormValidCluster() throws Exception {
@ParameterizedTest(name = "{0} bft node factory type")
@MethodSource("factoryFunctions")
public void zeroValidatorsFormValidCluster(
final String testName, final BftAcceptanceTestParameterization nodeFactory) throws Exception {
setUp(testName, nodeFactory);
final String[] validators = {};
final BesuNode node1 = nodeFactory.createNodeWithValidators(besu, "node1", validators);
final BesuNode node2 = nodeFactory.createNodeWithValidators(besu, "node2", validators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,23 @@
*/
package org.hyperledger.besu.tests.acceptance.bft;

import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase;
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBaseJunit5;

import java.util.Collection;
import java.util.stream.Stream;

import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.provider.Arguments;

@RunWith(Parameterized.class)
@Ignore("This is not a test class, it offers BFT parameterization only.")
public abstract class ParameterizedBftTestBase extends AcceptanceTestBase {
@Disabled("This is not a test class, it offers BFT parameterization only.")
public abstract class ParameterizedBftTestBase extends AcceptanceTestBaseJunit5 {
protected String bftType;
protected BftAcceptanceTestParameterization nodeFactory;

protected final String bftType;
protected final BftAcceptanceTestParameterization nodeFactory;

@Parameters(name = "{0}")
public static Collection<Object[]> factoryFunctions() {
public static Stream<Arguments> factoryFunctions() {
return BftAcceptanceTestParameterization.getFactories();
}

protected ParameterizedBftTestBase(
final String bftType, final BftAcceptanceTestParameterization input) {
protected void setUp(final String bftType, final BftAcceptanceTestParameterization input) {
this.bftType = bftType;
this.nodeFactory = input;
}
Expand Down
Loading
Loading