From 6f3abdbcbd4aa0d36428c623c83b0c1e18481e39 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Tue, 21 Jun 2016 19:16:43 +0200 Subject: [PATCH] NIFI-2066 dynamic port allocation in SNMP processors unit tests This closes #552 --- .../nifi/snmp/processors/GetSNMPTest.java | 50 ++++++++++--------- .../nifi/snmp/processors/SNMPGetTest.java | 8 +-- .../nifi/snmp/processors/SNMPTestUtil.java | 44 ++++++++++++++++ .../nifi/snmp/processors/SNMPUtilsTest.java | 4 +- .../nifi/snmp/processors/SNMPWalkTest.java | 8 +-- .../nifi/snmp/processors/SetSNMPTest.java | 32 ++++++------ .../nifi/snmp/processors/TestSnmpAgentV1.java | 10 +++- .../snmp/processors/TestSnmpAgentV2c.java | 9 +++- .../nifi/snmp/processors/TestSnmpAgentV3.java | 2 +- .../nifi/snmp/processors/WalkSNMPTest.java | 6 +-- 10 files changed, 117 insertions(+), 56 deletions(-) create mode 100644 nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPTestUtil.java diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/GetSNMPTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/GetSNMPTest.java index aed6d9cf57..1d40e25416 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/GetSNMPTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/GetSNMPTest.java @@ -70,7 +70,7 @@ public class GetSNMPTest { */ @BeforeClass public static void setUp() throws Exception { - agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); + agentv2c = new TestSnmpAgentV2c("0.0.0.0"); agentv2c.start(); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.registerManagedObject( @@ -82,7 +82,7 @@ public class GetSNMPTest { MOAccessImpl.ACCESS_WRITE_ONLY, new Integer32(writeOnlyValue))); - agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); + agentv1 = new TestSnmpAgentV1("0.0.0.0"); agentv1.start(); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.registerManagedObject( @@ -111,7 +111,7 @@ public class GetSNMPTest { @Test public void validateSuccessfullSnmpSetGetv2c() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); try (SNMPSetter setter = new SNMPSetter(snmp, target)) { PDU pdu = new PDU(); @@ -128,7 +128,7 @@ public class GetSNMPTest { runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2001"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -152,7 +152,7 @@ public class GetSNMPTest { @Test public void validateSuccessfullSnmpSetGetv1() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1); try (SNMPSetter setter = new SNMPSetter(snmp, target)) { PDU pdu = new PDU(); @@ -168,7 +168,7 @@ public class GetSNMPTest { TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2002"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv1.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1"); @@ -189,14 +189,14 @@ public class GetSNMPTest { @Test public void errorUnauthorizedSnmpGet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, writeOnlyOID.toString()); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2001"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -216,14 +216,14 @@ public class GetSNMPTest { @Test public void errorTimeoutSnmpGet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2010"); + runner.setProperty(GetSNMP.PORT, String.valueOf(SNMPTestUtil.availablePort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -240,14 +240,14 @@ public class GetSNMPTest { @Test public void errorNotExistingOIDSnmpGet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0"); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2001"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -265,14 +265,14 @@ public class GetSNMPTest { @Test public void errorCommunitySnmpGet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0"); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2001"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "errorCommunity"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -288,10 +288,11 @@ public class GetSNMPTest { */ @Test public void validateSnmpVersion3() throws Exception { + int port = SNMPTestUtil.availablePort(); Thread thread= new Thread(new Runnable() { @Override public void run() { - TestSnmpAgentV3.main(new String[]{"0.0.0.0/2003"}); + TestSnmpAgentV3.main(new String[]{"0.0.0.0/" + port}); } }); thread.start(); @@ -302,14 +303,15 @@ public class GetSNMPTest { SecurityModels.getInstance().addSecurityModel(usm); transportMapping.listen(); - this.executeCase(snmp, "SHADES", "authPriv", "SHA", "SHADESPassword", "DES", "SHADESPassword"); - this.executeCase(snmp, "MD5DES", "authPriv", "MD5", "MD5DESAuthPassword", "DES", "MD5DESPrivPassword"); - this.executeCase(snmp, "SHAAES128", "authPriv", "SHA", "SHAAES128AuthPassword", "AES128", "SHAAES128PrivPassword"); + this.executeCase(snmp, port, "SHADES", "authPriv", "SHA", "SHADESPassword", "DES", "SHADESPassword"); + this.executeCase(snmp, port, "MD5DES", "authPriv", "MD5", "MD5DESAuthPassword", "DES", "MD5DESPrivPassword"); + this.executeCase(snmp, port, "SHAAES128", "authPriv", "SHA", "SHAAES128AuthPassword", "AES128", "SHAAES128PrivPassword"); } /** * Method to test a specific configuration for SNMP V3 * @param snmp SNMP + * @param port Port * @param securityName Security name * @param securityLevel security level * @param authProt authentication protocol @@ -318,15 +320,16 @@ public class GetSNMPTest { * @param privPwd private password * @throws InterruptedException exception */ - private void executeCase(Snmp snmp, String securityName, String securityLevel, String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException { - UserTarget target = SNMPUtilsTest.prepareUser(snmp, "127.0.0.1/2003", SNMPUtils.getSecLevel(securityLevel), + private void executeCase(Snmp snmp, int port, String securityName, String securityLevel, String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException { + UserTarget target = SNMPUtilsTest.prepareUser(snmp, "127.0.0.1/" + port, SNMPUtils.getSecLevel(securityLevel), securityName, SNMPUtils.getAuth(authProt), SNMPUtils.getPriv(privProt), authPwd, privPwd); - this.testTarget(snmp, target, securityName, securityLevel, authProt, authPwd, privProt, privPwd); + this.testTarget(snmp, port, target, securityName, securityLevel, authProt, authPwd, privProt, privPwd); } /** * Method to test a specific configuration for SNMP V3 * @param snmp SNMP + * @param port Port * @param target target * @param securityName Security name * @param securityLevel security level @@ -336,13 +339,14 @@ public class GetSNMPTest { * @param privPwd private password * @throws InterruptedException exception */ - private void testTarget(Snmp snmp, UserTarget target, String securityName, String securityLevel, String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException { + private void testTarget(Snmp snmp, int port, UserTarget target, String securityName, String securityLevel, + String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException { GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2003"); + runner.setProperty(GetSNMP.PORT, String.valueOf(port)); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv3"); runner.setProperty(GetSNMP.SNMP_SECURITY_NAME, securityName); runner.setProperty(GetSNMP.SNMP_SECURITY_LEVEL, securityLevel); diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPGetTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPGetTest.java index d521a074a5..8932d610d2 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPGetTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPGetTest.java @@ -59,14 +59,14 @@ public class SNMPGetTest { public static void setUp() throws Exception { MOFactory factory = DefaultMOFactory.getInstance(); - agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); + agentv1 = new TestSnmpAgentV1("0.0.0.0"); agentv1.start(); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.registerManagedObject(factory.createScalar(sysDescr, MOAccessImpl.ACCESS_READ_ONLY, new OctetString(value))); - agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); + agentv2c = new TestSnmpAgentV2c("0.0.0.0"); agentv2c.start(); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.registerManagedObject(factory.createScalar(sysDescr, @@ -92,7 +92,7 @@ public class SNMPGetTest { @Test public void validateSuccessfulSnmpGetVersion2c() throws IOException, TimeoutException { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) { ResponseEvent response = getter.get(); if(response.getResponse() == null) { @@ -110,7 +110,7 @@ public class SNMPGetTest { @Test public void validateSuccessfulSnmpGetVersion1() throws IOException, TimeoutException { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1); try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) { ResponseEvent response = getter.get(); if(response.getResponse() == null) { diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPTestUtil.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPTestUtil.java new file mode 100644 index 0000000000..e2850a160d --- /dev/null +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPTestUtil.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.snmp.processors; + +import java.io.IOException; +import java.net.ServerSocket; + +public class SNMPTestUtil { + + /** + * Will determine an available port. + */ + public static synchronized int availablePort() { + ServerSocket s = null; + try { + s = new ServerSocket(0); + s.setReuseAddress(true); + return s.getLocalPort(); + } catch (Exception e) { + throw new IllegalStateException("Failed to discover available port.", e); + } finally { + try { + s.close(); + } catch (IOException e) { + // ignore + } + } + } + +} diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPUtilsTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPUtilsTest.java index 22be65aaeb..e7386b2096 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPUtilsTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPUtilsTest.java @@ -100,12 +100,12 @@ public class SNMPUtilsTest { * @param securityName security name * @return user target */ - protected static UserTarget createUserTarget(String address, int securityLevel, String securityName) { + private static UserTarget createUserTarget(String address, int securityLevel, String securityName) { UserTarget target = new UserTarget(); target.setVersion(SnmpConstants.version3); target.setSecurityLevel(securityLevel); target.setSecurityName(new OctetString(securityName)); - target.setAddress(new UdpAddress("127.0.0.1/2003")); + target.setAddress(new UdpAddress(address)); target.setRetries(0); target.setTimeout(500); return target; diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPWalkTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPWalkTest.java index eb2ab283d9..59a6fa135d 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPWalkTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SNMPWalkTest.java @@ -54,9 +54,9 @@ public class SNMPWalkTest { */ @BeforeClass public static void setUp() throws Exception { - agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); + agentv1 = new TestSnmpAgentV1("0.0.0.0"); agentv1.start(); - agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); + agentv2c = new TestSnmpAgentV2c("0.0.0.0"); agentv2c.start(); } @@ -84,7 +84,7 @@ public class SNMPWalkTest { CommunityTarget target = new CommunityTarget(); target.setVersion(SnmpConstants.version2c); target.setCommunity(new OctetString("public")); - target.setAddress(new UdpAddress("127.0.0.1/2001")); + target.setAddress(new UdpAddress("127.0.0.1/" + agentv2c.getPort())); target.setRetries(0); target.setTimeout(500); @@ -108,7 +108,7 @@ public class SNMPWalkTest { CommunityTarget target = new CommunityTarget(); target.setVersion(SnmpConstants.version1); target.setCommunity(new OctetString("public")); - target.setAddress(new UdpAddress("127.0.0.1/2002")); + target.setAddress(new UdpAddress("127.0.0.1/" + agentv1.getPort())); target.setRetries(0); target.setTimeout(500); diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java index 9308ee8fbb..bec6e7e971 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java @@ -67,7 +67,7 @@ public class SetSNMPTest { */ @BeforeClass public static void setUp() throws Exception { - agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); + agentv2c = new TestSnmpAgentV2c("0.0.0.0"); agentv2c.start(); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.registerManagedObject( @@ -79,7 +79,7 @@ public class SetSNMPTest { MOAccessImpl.ACCESS_READ_ONLY, new Integer32(readOnlyValue))); - agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); + agentv1 = new TestSnmpAgentV1("0.0.0.0"); agentv1.start(); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.registerManagedObject( @@ -109,7 +109,7 @@ public class SetSNMPTest { @Test public void successTypeSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -122,7 +122,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2002"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv1.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1"); @@ -141,7 +141,7 @@ public class SetSNMPTest { @Test public void errorTypeSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -152,7 +152,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2002"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv1.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1"); @@ -172,7 +172,7 @@ public class SetSNMPTest { @Test public void errorUnauthorizedSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -183,7 +183,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2001"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -203,7 +203,7 @@ public class SetSNMPTest { @Test public void errorNoOIDSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -213,7 +213,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2001"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -230,7 +230,7 @@ public class SetSNMPTest { @Test public void errorTimeoutSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -241,7 +241,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2010"); + runner.setProperty(SetSNMP.PORT, String.valueOf(SNMPTestUtil.availablePort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -258,7 +258,7 @@ public class SetSNMPTest { @Test public void errorNotExistingOIDSnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -269,7 +269,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2001"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); @@ -289,7 +289,7 @@ public class SetSNMPTest { @Test public void errorCommunitySnmpSet() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/2001", SnmpConstants.version2c); + CommunityTarget target = SNMPUtilsTest.createCommTarget("errorCommunity", "127.0.0.1/" + agentv2c.getPort(), SnmpConstants.version2c); SetSNMP pubProc = new LocalSetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -300,7 +300,7 @@ public class SetSNMPTest { runner.enqueue("".getBytes(), attributes); runner.setProperty(SetSNMP.HOST, "127.0.0.1"); - runner.setProperty(SetSNMP.PORT, "2001"); + runner.setProperty(SetSNMP.PORT, String.valueOf(agentv2c.getPort())); runner.setProperty(SetSNMP.SNMP_COMMUNITY, "errorCommunity"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV1.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV1.java index 807319ab41..7d78003564 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV1.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV1.java @@ -19,7 +19,6 @@ package org.apache.nifi.snmp.processors; import java.io.File; import java.io.IOException; - import org.snmp4j.TransportMapping; import org.snmp4j.agent.BaseAgent; import org.snmp4j.agent.CommandProcessor; @@ -60,6 +59,8 @@ public class TestSnmpAgentV1 extends BaseAgent { /** address */ private String address; + /** port */ + private int port; /** constructor * @param address address @@ -69,7 +70,8 @@ public class TestSnmpAgentV1 extends BaseAgent { // These files have to be specified // Read snmp4j doc for more info super(new File("target/conf1.agent"), new File("target/bootCounter1.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); - this.address = address; + this.port = SNMPTestUtil.availablePort(); + this.address = address + "/" + port; } /** @@ -209,4 +211,8 @@ public class TestSnmpAgentV1 extends BaseAgent { communityMIB.getSnmpCommunityEntry().addRow(row); } + + public int getPort() { + return port; + } } diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV2c.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV2c.java index 25765eca2a..8e8d127350 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV2c.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV2c.java @@ -60,6 +60,8 @@ public class TestSnmpAgentV2c extends BaseAgent { /** address */ private String address; + /** port */ + private int port; /** constructor * @param address address @@ -69,7 +71,8 @@ public class TestSnmpAgentV2c extends BaseAgent { // These files have to be specified // Read snmp4j doc for more info super(new File("target/conf2.agent"), new File("target/bootCounter2.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); - this.address = address; + this.port = SNMPTestUtil.availablePort(); + this.address = address + "/" + port; } /** @@ -209,4 +212,8 @@ public class TestSnmpAgentV2c extends BaseAgent { communityMIB.getSnmpCommunityEntry().addRow(row); } + + public int getPort() { + return port; + } } diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV3.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV3.java index c06a2ed17e..61e987e7ad 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV3.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/TestSnmpAgentV3.java @@ -194,7 +194,7 @@ public class TestSnmpAgentV3 extends BaseAgent { * @param args arguments */ public static void main(String[] args) { - String address = args[0]; + String address = args[0] + "/" + SNMPTestUtil.availablePort();; BasicConfigurator.configure(); try { TestSnmpAgentV3 testAgent1 = new TestSnmpAgentV3(new File("target/SNMP4JTestAgentBC.cfg"), diff --git a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/WalkSNMPTest.java b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/WalkSNMPTest.java index 719c80ef4b..356c48f99e 100644 --- a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/WalkSNMPTest.java +++ b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/WalkSNMPTest.java @@ -48,7 +48,7 @@ public class WalkSNMPTest { */ @BeforeClass public static void setUp() throws Exception { - agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); + agentv1 = new TestSnmpAgentV1("0.0.0.0"); agentv1.start(); } @@ -68,7 +68,7 @@ public class WalkSNMPTest { @Test public void validateSuccessfullSnmpWalkVersion1() throws Exception { Snmp snmp = SNMPUtilsTest.createSnmp(); - CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1); + CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/" + agentv1.getPort(), SnmpConstants.version1); GetSNMP pubProc = new LocalGetSnmp(snmp, target); TestRunner runner = TestRunners.newTestRunner(pubProc); @@ -76,7 +76,7 @@ public class WalkSNMPTest { runner.setProperty(GetSNMP.OID, root.toString()); runner.setProperty(GetSNMP.SNMP_STRATEGY, "WALK"); runner.setProperty(GetSNMP.HOST, "127.0.0.1"); - runner.setProperty(GetSNMP.PORT, "2002"); + runner.setProperty(GetSNMP.PORT, String.valueOf(agentv1.getPort())); runner.setProperty(GetSNMP.SNMP_COMMUNITY, "public"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1");