AMBARI-22458 - SNMP Trap should contain sysuptime field filled (unit test fixes) (jonathanhurley)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/aa652afe Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/aa652afe Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/aa652afe Branch: refs/heads/branch-3.0-perf Commit: aa652afe18c26ebbb52ffa1d8ecfad4270a093d7 Parents: 82af6fb Author: Jonathan Hurley <[email protected]> Authored: Tue Nov 28 12:23:31 2017 -0500 Committer: Jonathan Hurley <[email protected]> Committed: Tue Nov 28 12:31:13 2017 -0500 ---------------------------------------------------------------------- .../dispatchers/AmbariSNMPDispatcherTest.java | 23 +++++---- .../AlertNoticeDispatchServiceTest.java | 53 +++++++++++++++++--- 2 files changed, 59 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/aa652afe/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java index 0b299f6..14b6216 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java @@ -21,11 +21,11 @@ package org.apache.ambari.server.notifications.dispatchers; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.any; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.ambari.server.notifications.DispatchCallback; @@ -90,7 +91,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv1; Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); @@ -109,7 +110,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); notification.DispatchProperties = new HashMap<>(); dispatcher.dispatch(notification); verify(notification.Callback).onFailure(notification.CallbackIds); @@ -121,7 +122,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); @@ -137,7 +138,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); @@ -209,7 +210,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv2c; Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); @@ -227,7 +228,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); - notification.CallbackIds = new ArrayList<>(); + notification.CallbackIds = mock(List.class); Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); @@ -256,7 +257,7 @@ public class AmbariSNMPDispatcherTest { for (VariableBinding variableBinding : pdu.toArray()) { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID, variableBindings.get(SnmpConstants.snmpTrapOID.toString()).toValueString()); assertTrue(variableBindings.get(SnmpConstants.snmpTrapOID.toString()).getVariable() instanceof OID); assertEquals(String.valueOf(DEFINITION_ID), variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID).toValueString()); @@ -288,7 +289,7 @@ public class AmbariSNMPDispatcherTest { for (VariableBinding variableBinding : pdu.toArray()) { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals("null", variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_COMPONENT_NAME_OID).toValueString()); } @@ -305,7 +306,7 @@ public class AmbariSNMPDispatcherTest { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID, variableBindings.get(SnmpConstants.snmpTrapOID.toString()).toValueString()); assertEquals(String.valueOf(DEFINITION_ID), variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID).toValueString()); assertEquals(DEFINITION_NAME, variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_NAME_OID).toValueString()); http://git-wip-us.apache.org/repos/asf/ambari/blob/aa652afe/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java index 0bb118a..c86c51d 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java @@ -19,11 +19,15 @@ package org.apache.ambari.server.state.services; import static org.easymock.EasyMock.createNiceMock; import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; @@ -36,27 +40,39 @@ import java.util.UUID; import java.util.Vector; import java.util.concurrent.Executor; +import javax.persistence.EntityManager; + import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.notifications.DispatchFactory; import org.apache.ambari.server.notifications.Notification; import org.apache.ambari.server.notifications.NotificationDispatcher; import org.apache.ambari.server.notifications.TargetConfigurationResult; import org.apache.ambari.server.notifications.dispatchers.AmbariSNMPDispatcher; -import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.DBAccessor; +import org.apache.ambari.server.orm.dao.AlertDefinitionDAO; import org.apache.ambari.server.orm.dao.AlertDispatchDAO; +import org.apache.ambari.server.orm.dao.AlertsDAO; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.AlertHistoryEntity; import org.apache.ambari.server.orm.entities.AlertNoticeEntity; import org.apache.ambari.server.orm.entities.AlertTargetEntity; +import org.apache.ambari.server.stack.StackManagerFactory; import org.apache.ambari.server.state.AlertState; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.NotificationState; import org.apache.ambari.server.state.alert.Scope; import org.apache.ambari.server.state.alert.SourceType; import org.apache.ambari.server.state.alert.TargetType; +import org.apache.ambari.server.state.stack.OsFamily; import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; import org.snmp4j.PDU; @@ -68,6 +84,7 @@ import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.Integer32; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.TimeTicks; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; @@ -75,11 +92,12 @@ import com.google.inject.Binder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; -import com.google.inject.util.Modules; /** * Tests the {@link AlertNoticeDispatchService}. */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ AmbariSNMPDispatcher.class, ManagementFactory.class }) public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { final static String ALERT_NOTICE_UUID_1 = UUID.randomUUID().toString(); @@ -92,6 +110,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { private DispatchFactory m_dispatchFactory = null; private AlertDispatchDAO m_dao = null; private Injector m_injector; + private RuntimeMXBean m_runtimeMXBean; List<AlertDefinitionEntity> m_definitions = new ArrayList<>(); List<AlertHistoryEntity> m_histories = new ArrayList<>(); @@ -103,8 +122,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { m_metaInfo = createNiceMock(AmbariMetaInfo.class); // create an injector which will inject the mocks - m_injector = Guice.createInjector(Modules.override( - new InMemoryDefaultTestModule()).with(new MockModule())); + m_injector = Guice.createInjector(new MockModule()); Assert.assertNotNull(m_injector); @@ -150,7 +168,17 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { m_histories.add(history); } } - } + + // mock out the uptime to be a while (since most tests are not testing + // system uptime) + m_runtimeMXBean = EasyMock.createNiceMock(RuntimeMXBean.class); + PowerMock.mockStatic(ManagementFactory.class); + expect(ManagementFactory.getRuntimeMXBean()).andReturn(m_runtimeMXBean).atLeastOnce(); + PowerMock.replay(ManagementFactory.class); + expect(m_runtimeMXBean.getUptime()).andReturn(360000L).atLeastOnce(); + + replay( m_runtimeMXBean); + } /** * Tests the parsing of the {@link AlertHistoryEntity} list into @@ -381,7 +409,8 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { List<Vector> expectedTrapVectors = new LinkedList<>(); Vector firstVector = new Vector(); - firstVector.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID))); + firstVector.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(360000L))); + firstVector.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID), new Integer32(new BigDecimal(1L).intValueExact()))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_NAME_OID), new OctetString("alert-definition-1"))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_HASH_OID), new OctetString("1"))); @@ -762,9 +791,20 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { */ @Override public void configure(Binder binder) { + Cluster cluster = EasyMock.createNiceMock(Cluster.class); binder.bind(AlertDispatchDAO.class).toInstance(m_dao); binder.bind(DispatchFactory.class).toInstance(m_dispatchFactory); + binder.bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class)); binder.bind(AmbariMetaInfo.class).toInstance(m_metaInfo); + binder.bind(Clusters.class).toInstance(createNiceMock(Clusters.class)); + binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); + binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); + binder.bind(Cluster.class).toInstance(cluster); + binder.bind(AlertDefinitionDAO.class).toInstance(createNiceMock(AlertDefinitionDAO.class)); + binder.bind(AlertsDAO.class).toInstance(createNiceMock(AlertsDAO.class)); + binder.bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class)); + + binder.bind(AlertNoticeDispatchService.class).toInstance(new AlertNoticeDispatchService()); EasyMock.expect(m_metaInfo.getServerVersion()).andReturn("2.0.0").anyTimes(); EasyMock.replay(m_metaInfo); @@ -782,6 +822,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { receivedTrapsVectors = new LinkedList<>(); CommandResponder trapPrinter = new CommandResponder() { + @Override public synchronized void processPdu(CommandResponderEvent e){ PDU command = e.getPDU(); if (command != null) {
