From 8b26d3a686378774b386c9ba2a23b29c61032651 Mon Sep 17 00:00:00 2001 From: "S. Matthew English" Date: Fri, 17 May 2019 17:20:14 +0000 Subject: [PATCH] [PAN-2577] Integration Integration test(s) on p2p of 'net_services' (#1402) --- ...NetServicesReturnsAllServicesAsActive.java | 55 +++++++++++++++++++ ...ctNetServicesReturnsOnlyJsonRpcActive.java | 46 ++++++++++++++++ .../tests/acceptance/dsl/jsonrpc/Net.java | 10 ++++ .../acceptance/dsl/node/PantheonNode.java | 2 + .../dsl/node/factory/PantheonNodeFactory.java | 24 ++++++++ .../CustomNetJsonRpcRequestFactory.java | 36 ++++++++++++ .../dsl/transaction/JsonRequestFactories.java | 7 +++ .../net/NetServicesTransaction.java | 42 ++++++++++++++ .../dsl/transaction/net/NetTransactions.java | 4 ++ .../jsonrpc/NetServicesAcceptanceTest.java | 55 +++++++++++++++++++ 10 files changed, 281 insertions(+) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsOnlyJsonRpcActive.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/CustomNetJsonRpcRequestFactory.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java new file mode 100644 index 0000000000..77a822b064 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java @@ -0,0 +1,55 @@ +/* + * 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.dsl.condition.net; + +import static org.assertj.core.api.Assertions.assertThat; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetServicesTransaction; +import tech.pegasys.pantheon.util.NetworkUtility; + +import java.util.Arrays; +import java.util.Map; + +import com.google.common.net.InetAddresses; + +public class ExpectNetServicesReturnsAllServicesAsActive implements Condition { + + private final NetServicesTransaction transaction; + + public ExpectNetServicesReturnsAllServicesAsActive(final NetServicesTransaction transaction) { + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + final Map> result = node.execute(transaction); + assertThat(result.keySet()) + .containsExactlyInAnyOrderElementsOf(Arrays.asList("p2p", "jsonrpc", "ws")); + + assertThat(InetAddresses.isUriInetAddress(result.get("p2p").get("host"))).isTrue(); + final int p2pPort = Integer.valueOf(result.get("p2p").get("port")); + assertThat(NetworkUtility.isValidPort(p2pPort)).isTrue(); + + assertThat(InetAddresses.isUriInetAddress(result.get("ws").get("host"))).isTrue(); + final int wsPort = Integer.valueOf(result.get("ws").get("port")); + // TODO: Port should not be 0-valued. Refer to PAN-2703 + assertThat(NetworkUtility.isValidPort(p2pPort) || wsPort == 0).isTrue(); + + assertThat(InetAddresses.isUriInetAddress(result.get("jsonrpc").get("host"))).isTrue(); + final int jsonRpcPort = Integer.valueOf(result.get("jsonrpc").get("port")); + // TODO: Port should not be 0-valued. Refer to PAN-2703 + assertThat(NetworkUtility.isValidPort(p2pPort) || jsonRpcPort == 0).isTrue(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsOnlyJsonRpcActive.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsOnlyJsonRpcActive.java new file mode 100644 index 0000000000..65202cc5b2 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsOnlyJsonRpcActive.java @@ -0,0 +1,46 @@ +/* + * 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.dsl.condition.net; + +import static org.assertj.core.api.Assertions.assertThat; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetServicesTransaction; +import tech.pegasys.pantheon.util.NetworkUtility; + +import java.util.Collections; +import java.util.Map; + +import com.google.common.net.InetAddresses; + +public class ExpectNetServicesReturnsOnlyJsonRpcActive implements Condition { + + private final NetServicesTransaction transaction; + + public ExpectNetServicesReturnsOnlyJsonRpcActive(final NetServicesTransaction transaction) { + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + final Map> result = node.execute(transaction); + assertThat(result.keySet()) + .containsExactlyInAnyOrderElementsOf(Collections.singletonList("jsonrpc")); + + assertThat(InetAddresses.isUriInetAddress(result.get("jsonrpc").get("host"))).isTrue(); + final int jsonrpcPort = Integer.valueOf(result.get("jsonrpc").get("port")); + // TODO: Port should not be 0-valued. Refer to PAN-2703 + assertThat(NetworkUtility.isValidPort(jsonrpcPort) || jsonrpcPort == 0).isTrue(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java index 0b8ba3226c..3450d2cf46 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java @@ -15,6 +15,8 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.AwaitNetPeerCount; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.AwaitNetPeerCountException; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetServicesReturnsAllServicesAsActive; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetServicesReturnsOnlyJsonRpcActive; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionConnectionException; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionConnectionExceptionWithCause; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionIsNotBlank; @@ -32,6 +34,14 @@ public Net(final NetTransactions transactions) { this.transactions = transactions; } + public Condition netServicesAllActive() { + return new ExpectNetServicesReturnsAllServicesAsActive(transactions.netServices()); + } + + public Condition netServicesOnlyJsonRpcEnabled() { + return new ExpectNetServicesReturnsOnlyJsonRpcActive(transactions.netServices()); + } + public Condition netVersion() { return new ExpectNetVersionIsNotBlank(transactions.netVersion()); } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java index 7061dabce2..ec53bb674d 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java @@ -31,6 +31,7 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.httptransaction.HttpTransaction; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.AdminJsonRpcRequestFactory; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.CliqueJsonRpcRequestFactory; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.CustomNetJsonRpcRequestFactory; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EeaJsonRpcRequestFactory; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.IbftJsonRpcRequestFactory; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.JsonRequestFactories; @@ -282,6 +283,7 @@ private JsonRequestFactories jsonRequestFactories() { new PermissioningJsonRpcRequestFactory(web3jService), new AdminJsonRpcRequestFactory(web3jService), new EeaJsonRpcRequestFactory(web3jService), + new CustomNetJsonRpcRequestFactory(web3jService), websocketService); } 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 d4cccdc272..fde5a3fc83 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 @@ -132,6 +132,30 @@ public PantheonNode createArchiveNodeWithDiscoveryDisabledAndAdmin(final String .build()); } + public PantheonNode createArchiveNodeNetServicesEnabled(final String name) throws IOException { + // TODO: Enable metrics coverage in the acceptance tests. See PIE-1606 + // final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); + // metricsConfiguration.setEnabled(true); + // metricsConfiguration.setPort(0); + return create( + new PantheonFactoryConfigurationBuilder() + .name(name) + // .setMetricsConfiguration(metricsConfiguration) + .jsonRpcConfiguration(jsonRpcConfigWithAdmin()) + .webSocketEnabled() + .p2pEnabled(true) + .build()); + } + + public PantheonNode createArchiveNodeNetServicesDisabled(final String name) throws IOException { + return create( + new PantheonFactoryConfigurationBuilder() + .name(name) + .jsonRpcConfiguration(jsonRpcConfigWithAdmin()) + .p2pEnabled(false) + .build()); + } + public PantheonNode createArchiveNodeWithAuthentication(final String name) throws IOException, URISyntaxException { return create( diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/CustomNetJsonRpcRequestFactory.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/CustomNetJsonRpcRequestFactory.java new file mode 100644 index 0000000000..a7d50d68ff --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/CustomNetJsonRpcRequestFactory.java @@ -0,0 +1,36 @@ +/* + * 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.dsl.transaction; + +import java.util.Collections; +import java.util.Map; + +import org.web3j.protocol.Web3jService; +import org.web3j.protocol.core.Request; +import org.web3j.protocol.core.Response; + +public class CustomNetJsonRpcRequestFactory { + + public static class NetServicesResponse extends Response>> {} + + private final Web3jService web3jService; + + public CustomNetJsonRpcRequestFactory(final Web3jService web3jService) { + this.web3jService = web3jService; + } + + public Request netServices() { + return new Request<>( + "net_services", Collections.emptyList(), web3jService, NetServicesResponse.class); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/JsonRequestFactories.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/JsonRequestFactories.java index 4cb6812a17..6efaf7b865 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/JsonRequestFactories.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/JsonRequestFactories.java @@ -25,6 +25,7 @@ public class JsonRequestFactories { private final PermissioningJsonRpcRequestFactory perm; private final AdminJsonRpcRequestFactory admin; private final EeaJsonRpcRequestFactory eea; + private final CustomNetJsonRpcRequestFactory customNet; private final Optional websocketService; public JsonRequestFactories( @@ -34,6 +35,7 @@ public JsonRequestFactories( final PermissioningJsonRpcRequestFactory perm, final AdminJsonRpcRequestFactory admin, final EeaJsonRpcRequestFactory eea, + final CustomNetJsonRpcRequestFactory customNet, final Optional websocketService) { this.netEth = netEth; this.clique = clique; @@ -41,6 +43,7 @@ public JsonRequestFactories( this.perm = perm; this.admin = admin; this.eea = eea; + this.customNet = customNet; this.websocketService = websocketService; } @@ -68,6 +71,10 @@ public AdminJsonRpcRequestFactory admin() { return admin; } + public CustomNetJsonRpcRequestFactory customNet() { + return customNet; + } + public EeaJsonRpcRequestFactory eea() { return eea; } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java new file mode 100644 index 0000000000..06fa6a7e18 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java @@ -0,0 +1,42 @@ +/* + * 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.dsl.transaction.net; + +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.CustomNetJsonRpcRequestFactory; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.JsonRequestFactories; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + +import java.util.Map; + +import org.web3j.protocol.core.Request; + +public class NetServicesTransaction implements Transaction>> { + + NetServicesTransaction() {} + + @Override + public Map> execute(final JsonRequestFactories requestFactories) { + CustomNetJsonRpcRequestFactory.NetServicesResponse netServicesResponse = null; + try { + final CustomNetJsonRpcRequestFactory netServicesJsonRpcRequestFactory = + requestFactories.customNet(); + final Request request = + netServicesJsonRpcRequestFactory.netServices(); + + netServicesResponse = request.send(); + } catch (final Exception e) { + throw new RuntimeException(e); + } + return netServicesResponse.getResult(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java index 1b059bf5c3..535f9d5679 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java @@ -18,6 +18,10 @@ public NetVersionTransaction netVersion() { return new NetVersionTransaction(); } + public NetServicesTransaction netServices() { + return new NetServicesTransaction(); + } + public NetPeerCountTransaction peerCount() { return new NetPeerCountTransaction(); } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java new file mode 100644 index 0000000000..db7849abc2 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java @@ -0,0 +1,55 @@ +/* + * 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; + +import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; +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.Test; + +public class NetServicesAcceptanceTest extends AcceptanceTestBase { + + private Cluster noDiscoveryCluster; + + private Node nodeA; + private Node nodeB; + + @Test + public void shouldIndicateNetServicesEnabled() throws Exception { + final ClusterConfiguration clusterConfiguration = + new ClusterConfigurationBuilder().awaitPeerDiscovery(false).build(); + noDiscoveryCluster = new Cluster(clusterConfiguration, net); + nodeA = pantheon.createArchiveNodeNetServicesEnabled("nodeA"); + nodeB = pantheon.createArchiveNodeNetServicesEnabled("nodeB"); + noDiscoveryCluster.start(nodeA, nodeB); + + nodeA.verify(net.netServicesAllActive()); + nodeB.verify(net.netServicesAllActive()); + } + + @Test + public void shouldNotDisplayDisabledServices() throws Exception { + final ClusterConfiguration clusterConfiguration = + new ClusterConfigurationBuilder().awaitPeerDiscovery(false).build(); + noDiscoveryCluster = new Cluster(clusterConfiguration, net); + nodeA = pantheon.createArchiveNodeNetServicesDisabled("nodeA"); + nodeB = pantheon.createArchiveNodeNetServicesDisabled("nodeB"); + noDiscoveryCluster.start(nodeA, nodeB); + + nodeA.verify(net.netServicesOnlyJsonRpcEnabled()); + nodeB.verify(net.netServicesOnlyJsonRpcEnabled()); + } +}