From 7c835db064e814676c2a9ef0dc9e6d834e68e003 Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Wed, 30 Jan 2019 14:35:40 +1000 Subject: [PATCH] [MINOR] added acceptance test for admin_addPeer with P2P disabled (#703) * added acceptance test for calling admin_addPeer on a node that has P2P disabled Signed-off-by: Adrian Sutton --- .../acceptance/P2pDisabledAcceptanceTest.java | 2 +- .../dsl/node/factory/PantheonNodeFactory.java | 21 ++++-- ...nAddPeerWithP2PDisabledAcceptanceTest.java | 67 +++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/admin/AdminAddPeerWithP2PDisabledAcceptanceTest.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/P2pDisabledAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/P2pDisabledAcceptanceTest.java index a63bd6368c..126a7c8b37 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/P2pDisabledAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/P2pDisabledAcceptanceTest.java @@ -31,7 +31,7 @@ public class P2pDisabledAcceptanceTest extends AcceptanceTestBase { new ClusterConfigurationBuilder().setAwaitPeerDiscovery(false).build(); p2pDisabledCluster = new Cluster(clusterConfiguration, net); - node = pantheon.createArchiveNodeWithP2pDisabled("node1"); + node = pantheon.createNodeWithP2pDisabled("node1"); p2pDisabledCluster.start(node); } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java index 2d38cb0218..ecdfa0edaf 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java @@ -36,6 +36,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -110,13 +111,21 @@ public class PantheonNodeFactory { .build()); } - public PantheonNode createArchiveNodeWithP2pDisabled(final String name) throws IOException { + public PantheonNode createNodeWithP2pDisabled(final String name) throws IOException { return create( new PantheonFactoryConfigurationBuilder() .setName(name) .setP2pEnabled(false) .setJsonRpcConfiguration(jsonRpcConfigWithPermissioning()) - .webSocketEnabled() + .build()); + } + + public PantheonNode createNodeWithP2pDisabledAndAdmin(final String name) throws IOException { + return create( + new PantheonFactoryConfigurationBuilder() + .setName(name) + .setP2pEnabled(false) + .setJsonRpcConfiguration(jsonRpcConfigWithPermissioningAndAdmin()) .build()); } @@ -339,10 +348,14 @@ public class PantheonNodeFactory { return createJsonRpcConfigWithRpcApiEnabled(RpcApis.ADMIN); } - private JsonRpcConfiguration createJsonRpcConfigWithRpcApiEnabled(final RpcApi rpcApi) { + private JsonRpcConfiguration jsonRpcConfigWithPermissioningAndAdmin() { + return createJsonRpcConfigWithRpcApiEnabled(RpcApis.PERM, RpcApis.ADMIN); + } + + private JsonRpcConfiguration createJsonRpcConfigWithRpcApiEnabled(final RpcApi... rpcApi) { final JsonRpcConfiguration jsonRpcConfig = createJsonRpcEnabledConfig(); final List rpcApis = new ArrayList<>(jsonRpcConfig.getRpcApis()); - rpcApis.add(rpcApi); + rpcApis.addAll(Arrays.asList(rpcApi)); jsonRpcConfig.setRpcApis(rpcApis); return jsonRpcConfig; } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/admin/AdminAddPeerWithP2PDisabledAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/admin/AdminAddPeerWithP2PDisabledAcceptanceTest.java new file mode 100644 index 0000000000..a04b81c9c2 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/admin/AdminAddPeerWithP2PDisabledAcceptanceTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.tests.acceptance.jsonrpc.admin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.Cluster; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfiguration; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfigurationBuilder; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.web3j.protocol.exceptions.ClientConnectionException; + +public class AdminAddPeerWithP2PDisabledAcceptanceTest extends AcceptanceTestBase { + private PantheonNode nodeA; + private PantheonNode nodeB; + private Cluster p2pDisabledCluster; + + @Before + public void setUp() throws Exception { + final ClusterConfiguration clusterConfiguration = + new ClusterConfigurationBuilder().setAwaitPeerDiscovery(false).build(); + p2pDisabledCluster = new Cluster(clusterConfiguration, net); + nodeA = pantheon.createNodeWithP2pDisabledAndAdmin("nodeA"); + nodeB = pantheon.createArchiveNodeWithDiscoveryDisabledAndAdmin("nodeB"); + p2pDisabledCluster.start(nodeA, nodeB); + } + + @After + public void tearDown() { + p2pDisabledCluster.stop(); + } + + @Test + public void adminAddPeerFailsWhenP2PDisabled() { + final String nodeBEnode = nodeB.enodeUrl(); + try { + nodeA.verify(admin.addPeer(nodeBEnode)); + fail("expected exception because P2P is disabled"); + } catch (ClientConnectionException e) { + assertThat(e.getMessage().contains("P2P has been disabled")).isTrue(); + } + nodeB.verify(net.awaitPeerCount(0)); + } + + @Test + public void adminAddPeerFailsWhenP2PDisabled2() { + final String nodeAEnode = nodeA.enodeUrl(); + nodeB.verify(admin.addPeer(nodeAEnode)); + nodeB.verify(net.awaitPeerCount(0)); + } +}