NIFI-2066 dynamic port allocation in SNMP processors unit tests

This closes #552
This commit is contained in:
Pierre Villard 2016-06-21 19:16:43 +02:00 committed by Oleg Zhurakousky
parent ef192cc859
commit 6f3abdbcbd
10 changed files with 117 additions and 56 deletions

View File

@ -70,7 +70,7 @@ public class GetSNMPTest {
*/ */
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start(); agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject( agentv2c.registerManagedObject(
@ -82,7 +82,7 @@ public class GetSNMPTest {
MOAccessImpl.ACCESS_WRITE_ONLY, MOAccessImpl.ACCESS_WRITE_ONLY,
new Integer32(writeOnlyValue))); new Integer32(writeOnlyValue)));
agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start(); agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject( agentv1.registerManagedObject(
@ -111,7 +111,7 @@ public class GetSNMPTest {
@Test @Test
public void validateSuccessfullSnmpSetGetv2c() throws Exception { public void validateSuccessfullSnmpSetGetv2c() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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)) { try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
PDU pdu = new PDU(); PDU pdu = new PDU();
@ -128,7 +128,7 @@ public class GetSNMPTest {
runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
@ -152,7 +152,7 @@ public class GetSNMPTest {
@Test @Test
public void validateSuccessfullSnmpSetGetv1() throws Exception { public void validateSuccessfullSnmpSetGetv1() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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)) { try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
PDU pdu = new PDU(); PDU pdu = new PDU();
@ -168,7 +168,7 @@ public class GetSNMPTest {
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1");
@ -189,14 +189,14 @@ public class GetSNMPTest {
@Test @Test
public void errorUnauthorizedSnmpGet() throws Exception { public void errorUnauthorizedSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, writeOnlyOID.toString()); runner.setProperty(GetSNMP.OID, writeOnlyOID.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
@ -216,14 +216,14 @@ public class GetSNMPTest {
@Test @Test
public void errorTimeoutSnmpGet() throws Exception { public void errorTimeoutSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
@ -240,14 +240,14 @@ public class GetSNMPTest {
@Test @Test
public void errorNotExistingOIDSnmpGet() throws Exception { public void errorNotExistingOIDSnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0"); runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0");
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
@ -265,14 +265,14 @@ public class GetSNMPTest {
@Test @Test
public void errorCommunitySnmpGet() throws Exception { public void errorCommunitySnmpGet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0"); runner.setProperty(GetSNMP.OID, "1.3.6.1.2.1.1.2.0");
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "errorCommunity");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv2c");
@ -288,10 +288,11 @@ public class GetSNMPTest {
*/ */
@Test @Test
public void validateSnmpVersion3() throws Exception { public void validateSnmpVersion3() throws Exception {
int port = SNMPTestUtil.availablePort();
Thread thread= new Thread(new Runnable() { Thread thread= new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
TestSnmpAgentV3.main(new String[]{"0.0.0.0/2003"}); TestSnmpAgentV3.main(new String[]{"0.0.0.0/" + port});
} }
}); });
thread.start(); thread.start();
@ -302,14 +303,15 @@ public class GetSNMPTest {
SecurityModels.getInstance().addSecurityModel(usm); SecurityModels.getInstance().addSecurityModel(usm);
transportMapping.listen(); transportMapping.listen();
this.executeCase(snmp, "SHADES", "authPriv", "SHA", "SHADESPassword", "DES", "SHADESPassword"); this.executeCase(snmp, port, "SHADES", "authPriv", "SHA", "SHADESPassword", "DES", "SHADESPassword");
this.executeCase(snmp, "MD5DES", "authPriv", "MD5", "MD5DESAuthPassword", "DES", "MD5DESPrivPassword"); this.executeCase(snmp, port, "MD5DES", "authPriv", "MD5", "MD5DESAuthPassword", "DES", "MD5DESPrivPassword");
this.executeCase(snmp, "SHAAES128", "authPriv", "SHA", "SHAAES128AuthPassword", "AES128", "SHAAES128PrivPassword"); this.executeCase(snmp, port, "SHAAES128", "authPriv", "SHA", "SHAAES128AuthPassword", "AES128", "SHAAES128PrivPassword");
} }
/** /**
* Method to test a specific configuration for SNMP V3 * Method to test a specific configuration for SNMP V3
* @param snmp SNMP * @param snmp SNMP
* @param port Port
* @param securityName Security name * @param securityName Security name
* @param securityLevel security level * @param securityLevel security level
* @param authProt authentication protocol * @param authProt authentication protocol
@ -318,15 +320,16 @@ public class GetSNMPTest {
* @param privPwd private password * @param privPwd private password
* @throws InterruptedException exception * @throws InterruptedException exception
*/ */
private void executeCase(Snmp snmp, String securityName, String securityLevel, String authProt, String authPwd, String privProt, String privPwd) throws InterruptedException { 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/2003", SNMPUtils.getSecLevel(securityLevel), UserTarget target = SNMPUtilsTest.prepareUser(snmp, "127.0.0.1/" + port, SNMPUtils.getSecLevel(securityLevel),
securityName, SNMPUtils.getAuth(authProt), SNMPUtils.getPriv(privProt), authPwd, privPwd); 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 * Method to test a specific configuration for SNMP V3
* @param snmp SNMP * @param snmp SNMP
* @param port Port
* @param target target * @param target target
* @param securityName Security name * @param securityName Security name
* @param securityLevel security level * @param securityLevel security level
@ -336,13 +339,14 @@ public class GetSNMPTest {
* @param privPwd private password * @param privPwd private password
* @throws InterruptedException exception * @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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
runner.setProperty(GetSNMP.OID, sysDescr.toString()); runner.setProperty(GetSNMP.OID, sysDescr.toString());
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_VERSION, "SNMPv3");
runner.setProperty(GetSNMP.SNMP_SECURITY_NAME, securityName); runner.setProperty(GetSNMP.SNMP_SECURITY_NAME, securityName);
runner.setProperty(GetSNMP.SNMP_SECURITY_LEVEL, securityLevel); runner.setProperty(GetSNMP.SNMP_SECURITY_LEVEL, securityLevel);

View File

@ -59,14 +59,14 @@ public class SNMPGetTest {
public static void setUp() throws Exception { public static void setUp() throws Exception {
MOFactory factory = DefaultMOFactory.getInstance(); MOFactory factory = DefaultMOFactory.getInstance();
agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start(); agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject(factory.createScalar(sysDescr, agentv1.registerManagedObject(factory.createScalar(sysDescr,
MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_ONLY,
new OctetString(value))); new OctetString(value)));
agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start(); agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject(factory.createScalar(sysDescr, agentv2c.registerManagedObject(factory.createScalar(sysDescr,
@ -92,7 +92,7 @@ public class SNMPGetTest {
@Test @Test
public void validateSuccessfulSnmpGetVersion2c() throws IOException, TimeoutException { public void validateSuccessfulSnmpGetVersion2c() throws IOException, TimeoutException {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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)) { try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) {
ResponseEvent response = getter.get(); ResponseEvent response = getter.get();
if(response.getResponse() == null) { if(response.getResponse() == null) {
@ -110,7 +110,7 @@ public class SNMPGetTest {
@Test @Test
public void validateSuccessfulSnmpGetVersion1() throws IOException, TimeoutException { public void validateSuccessfulSnmpGetVersion1() throws IOException, TimeoutException {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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)) { try (SNMPGetter getter = new SNMPGetter(snmp, target, sysDescr)) {
ResponseEvent response = getter.get(); ResponseEvent response = getter.get();
if(response.getResponse() == null) { if(response.getResponse() == null) {

View File

@ -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
}
}
}
}

View File

@ -100,12 +100,12 @@ public class SNMPUtilsTest {
* @param securityName security name * @param securityName security name
* @return user target * @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(); UserTarget target = new UserTarget();
target.setVersion(SnmpConstants.version3); target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(securityLevel); target.setSecurityLevel(securityLevel);
target.setSecurityName(new OctetString(securityName)); target.setSecurityName(new OctetString(securityName));
target.setAddress(new UdpAddress("127.0.0.1/2003")); target.setAddress(new UdpAddress(address));
target.setRetries(0); target.setRetries(0);
target.setTimeout(500); target.setTimeout(500);
return target; return target;

View File

@ -54,9 +54,9 @@ public class SNMPWalkTest {
*/ */
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start(); agentv1.start();
agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start(); agentv2c.start();
} }
@ -84,7 +84,7 @@ public class SNMPWalkTest {
CommunityTarget target = new CommunityTarget(); CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version2c); target.setVersion(SnmpConstants.version2c);
target.setCommunity(new OctetString("public")); 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.setRetries(0);
target.setTimeout(500); target.setTimeout(500);
@ -108,7 +108,7 @@ public class SNMPWalkTest {
CommunityTarget target = new CommunityTarget(); CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version1); target.setVersion(SnmpConstants.version1);
target.setCommunity(new OctetString("public")); 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.setRetries(0);
target.setTimeout(500); target.setTimeout(500);

View File

@ -67,7 +67,7 @@ public class SetSNMPTest {
*/ */
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001"); agentv2c = new TestSnmpAgentV2c("0.0.0.0");
agentv2c.start(); agentv2c.start();
agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB()); agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
agentv2c.registerManagedObject( agentv2c.registerManagedObject(
@ -79,7 +79,7 @@ public class SetSNMPTest {
MOAccessImpl.ACCESS_READ_ONLY, MOAccessImpl.ACCESS_READ_ONLY,
new Integer32(readOnlyValue))); new Integer32(readOnlyValue)));
agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start(); agentv1.start();
agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB()); agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
agentv1.registerManagedObject( agentv1.registerManagedObject(
@ -109,7 +109,7 @@ public class SetSNMPTest {
@Test @Test
public void successTypeSnmpSet() throws Exception { public void successTypeSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -122,7 +122,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
@ -141,7 +141,7 @@ public class SetSNMPTest {
@Test @Test
public void errorTypeSnmpSet() throws Exception { public void errorTypeSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -152,7 +152,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
@ -172,7 +172,7 @@ public class SetSNMPTest {
@Test @Test
public void errorUnauthorizedSnmpSet() throws Exception { public void errorUnauthorizedSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -183,7 +183,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
@ -203,7 +203,7 @@ public class SetSNMPTest {
@Test @Test
public void errorNoOIDSnmpSet() throws Exception { public void errorNoOIDSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -213,7 +213,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
@ -230,7 +230,7 @@ public class SetSNMPTest {
@Test @Test
public void errorTimeoutSnmpSet() throws Exception { public void errorTimeoutSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -241,7 +241,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
@ -258,7 +258,7 @@ public class SetSNMPTest {
@Test @Test
public void errorNotExistingOIDSnmpSet() throws Exception { public void errorNotExistingOIDSnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -269,7 +269,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");
@ -289,7 +289,7 @@ public class SetSNMPTest {
@Test @Test
public void errorCommunitySnmpSet() throws Exception { public void errorCommunitySnmpSet() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); SetSNMP pubProc = new LocalSetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -300,7 +300,7 @@ public class SetSNMPTest {
runner.enqueue("".getBytes(), attributes); runner.enqueue("".getBytes(), attributes);
runner.setProperty(SetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "errorCommunity");
runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c"); runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv2c");

View File

@ -19,7 +19,6 @@ package org.apache.nifi.snmp.processors;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.snmp4j.TransportMapping; import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent; import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor; import org.snmp4j.agent.CommandProcessor;
@ -60,6 +59,8 @@ public class TestSnmpAgentV1 extends BaseAgent {
/** address */ /** address */
private String address; private String address;
/** port */
private int port;
/** constructor /** constructor
* @param address address * @param address address
@ -69,7 +70,8 @@ public class TestSnmpAgentV1 extends BaseAgent {
// These files have to be specified // These files have to be specified
// Read snmp4j doc for more info // Read snmp4j doc for more info
super(new File("target/conf1.agent"), new File("target/bootCounter1.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); 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); communityMIB.getSnmpCommunityEntry().addRow(row);
} }
public int getPort() {
return port;
}
} }

View File

@ -60,6 +60,8 @@ public class TestSnmpAgentV2c extends BaseAgent {
/** address */ /** address */
private String address; private String address;
/** port */
private int port;
/** constructor /** constructor
* @param address address * @param address address
@ -69,7 +71,8 @@ public class TestSnmpAgentV2c extends BaseAgent {
// These files have to be specified // These files have to be specified
// Read snmp4j doc for more info // Read snmp4j doc for more info
super(new File("target/conf2.agent"), new File("target/bootCounter2.agent"), new CommandProcessor(new OctetString(MPv3.createLocalEngineID()))); 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); communityMIB.getSnmpCommunityEntry().addRow(row);
} }
public int getPort() {
return port;
}
} }

View File

@ -194,7 +194,7 @@ public class TestSnmpAgentV3 extends BaseAgent {
* @param args arguments * @param args arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
String address = args[0]; String address = args[0] + "/" + SNMPTestUtil.availablePort();;
BasicConfigurator.configure(); BasicConfigurator.configure();
try { try {
TestSnmpAgentV3 testAgent1 = new TestSnmpAgentV3(new File("target/SNMP4JTestAgentBC.cfg"), TestSnmpAgentV3 testAgent1 = new TestSnmpAgentV3(new File("target/SNMP4JTestAgentBC.cfg"),

View File

@ -48,7 +48,7 @@ public class WalkSNMPTest {
*/ */
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
agentv1 = new TestSnmpAgentV1("0.0.0.0/2002"); agentv1 = new TestSnmpAgentV1("0.0.0.0");
agentv1.start(); agentv1.start();
} }
@ -68,7 +68,7 @@ public class WalkSNMPTest {
@Test @Test
public void validateSuccessfullSnmpWalkVersion1() throws Exception { public void validateSuccessfullSnmpWalkVersion1() throws Exception {
Snmp snmp = SNMPUtilsTest.createSnmp(); 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); GetSNMP pubProc = new LocalGetSnmp(snmp, target);
TestRunner runner = TestRunners.newTestRunner(pubProc); TestRunner runner = TestRunners.newTestRunner(pubProc);
@ -76,7 +76,7 @@ public class WalkSNMPTest {
runner.setProperty(GetSNMP.OID, root.toString()); runner.setProperty(GetSNMP.OID, root.toString());
runner.setProperty(GetSNMP.SNMP_STRATEGY, "WALK"); runner.setProperty(GetSNMP.SNMP_STRATEGY, "WALK");
runner.setProperty(GetSNMP.HOST, "127.0.0.1"); 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_COMMUNITY, "public");
runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1"); runner.setProperty(GetSNMP.SNMP_VERSION, "SNMPv1");