mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Fixup alerting and logging error in BGPServiceImpl (#10252)
* Fixup alerting and logging error in BGPServiceImpl * Update unit tests * Apply suggestions from code review Co-authored-by: dahn <daan.hoogland@gmail.com> --------- Co-authored-by: dahn <daan.hoogland@gmail.com>
This commit is contained in:
parent
c121d5bb6d
commit
6a3314c40b
@ -778,13 +778,14 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi
|
|||||||
AlertVO alert = null;
|
AlertVO alert = null;
|
||||||
Long clusterId = cluster == null ? null : cluster.getId();
|
Long clusterId = cluster == null ? null : cluster.getId();
|
||||||
Long podId = pod == null ? null : pod.getId();
|
Long podId = pod == null ? null : pod.getId();
|
||||||
|
long dcId = dataCenter == null ? 0L : dataCenter.getId();
|
||||||
if ((alertType != AlertManager.AlertType.ALERT_TYPE_HOST) && (alertType != AlertManager.AlertType.ALERT_TYPE_USERVM)
|
if ((alertType != AlertManager.AlertType.ALERT_TYPE_HOST) && (alertType != AlertManager.AlertType.ALERT_TYPE_USERVM)
|
||||||
&& (alertType != AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY)
|
&& (alertType != AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY)
|
||||||
&& (alertType != AlertManager.AlertType.ALERT_TYPE_SSVM) && (alertType != AlertManager.AlertType.ALERT_TYPE_STORAGE_MISC)
|
&& (alertType != AlertManager.AlertType.ALERT_TYPE_SSVM) && (alertType != AlertManager.AlertType.ALERT_TYPE_STORAGE_MISC)
|
||||||
&& (alertType != AlertManager.AlertType.ALERT_TYPE_MANAGEMENT_NODE) && (alertType != AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED)
|
&& (alertType != AlertManager.AlertType.ALERT_TYPE_MANAGEMENT_NODE) && (alertType != AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED)
|
||||||
&& (alertType != AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED) && (alertType != AlertManager.AlertType.ALERT_TYPE_OOBM_AUTH_ERROR)
|
&& (alertType != AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED) && (alertType != AlertManager.AlertType.ALERT_TYPE_OOBM_AUTH_ERROR)
|
||||||
&& (alertType != AlertManager.AlertType.ALERT_TYPE_HA_ACTION) && (alertType != AlertManager.AlertType.ALERT_TYPE_CA_CERT)) {
|
&& (alertType != AlertManager.AlertType.ALERT_TYPE_HA_ACTION) && (alertType != AlertManager.AlertType.ALERT_TYPE_CA_CERT)) {
|
||||||
alert = _alertDao.getLastAlert(alertType.getType(), dataCenter.getId(), podId, clusterId);
|
alert = _alertDao.getLastAlert(alertType.getType(), dcId, podId, clusterId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alert == null) {
|
if (alert == null) {
|
||||||
@ -794,7 +795,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi
|
|||||||
newAlert.setContent(content);
|
newAlert.setContent(content);
|
||||||
newAlert.setClusterId(clusterId);
|
newAlert.setClusterId(clusterId);
|
||||||
newAlert.setPodId(podId);
|
newAlert.setPodId(podId);
|
||||||
newAlert.setDataCenterId(dataCenter.getId());
|
newAlert.setDataCenterId(dcId);
|
||||||
newAlert.setSentCount(1);
|
newAlert.setSentCount(1);
|
||||||
newAlert.setLastSent(new Date());
|
newAlert.setLastSent(new Date());
|
||||||
newAlert.setName(alertType.getName());
|
newAlert.setName(alertType.getName());
|
||||||
|
|||||||
@ -255,9 +255,9 @@ public class BGPServiceImpl implements BGPService {
|
|||||||
netName = network.getName();
|
netName = network.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.debug("Allocating the AS Number {} to {} on zone {}", asNumber::toString,
|
String networkName = Objects.nonNull(vpcId) ? ("VPC " + vpc) : ("network " + network);
|
||||||
(Objects.nonNull(vpcId) ? "VPC " + vpc : "network " + network)::toString,
|
LOGGER.debug("Allocating the AS Number {} to {} on zone {}", asNumberVO::toString,
|
||||||
() -> dataCenterDao.findById(zoneId));
|
networkName::toString, () -> dataCenterDao.findById(zoneId));
|
||||||
asNumberVO.setAllocated(true);
|
asNumberVO.setAllocated(true);
|
||||||
asNumberVO.setAllocatedTime(new Date());
|
asNumberVO.setAllocatedTime(new Date());
|
||||||
if (Objects.nonNull(vpcId)) {
|
if (Objects.nonNull(vpcId)) {
|
||||||
|
|||||||
@ -26,8 +26,10 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
|
|||||||
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
|
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
@ -48,6 +50,12 @@ import com.cloud.host.HostVO;
|
|||||||
import com.cloud.host.dao.HostDao;
|
import com.cloud.host.dao.HostDao;
|
||||||
import com.cloud.storage.StorageManager;
|
import com.cloud.storage.StorageManager;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class AlertManagerImplTest {
|
public class AlertManagerImplTest {
|
||||||
|
|
||||||
@ -56,7 +64,7 @@ public class AlertManagerImplTest {
|
|||||||
AlertManagerImpl alertManagerImplMock;
|
AlertManagerImpl alertManagerImplMock;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
AlertDao alertDaoMock;
|
AlertDao _alertDao;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private DataCenterDao _dcDao;
|
private DataCenterDao _dcDao;
|
||||||
@ -88,6 +96,15 @@ public class AlertManagerImplTest {
|
|||||||
@Mock
|
@Mock
|
||||||
SMTPMailSender mailSenderMock;
|
SMTPMailSender mailSenderMock;
|
||||||
|
|
||||||
|
private final String[] recipients = new String[]{"test@test.com"};
|
||||||
|
private final String senderAddress = "sender@test.com";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
alertManagerImplMock.recipients = recipients;
|
||||||
|
alertManagerImplMock.senderAddress = senderAddress;
|
||||||
|
}
|
||||||
|
|
||||||
private void sendMessage() {
|
private void sendMessage() {
|
||||||
try {
|
try {
|
||||||
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
|
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
|
||||||
@ -100,7 +117,7 @@ public class AlertManagerImplTest {
|
|||||||
Mockito.when(cluster.getId()).thenReturn(1L);
|
Mockito.when(cluster.getId()).thenReturn(1L);
|
||||||
Mockito.when(_clusterDao.findById(1L)).thenReturn(cluster);
|
Mockito.when(_clusterDao.findById(1L)).thenReturn(cluster);
|
||||||
|
|
||||||
alertManagerImplMock.sendAlert(AlertManager.AlertType.ALERT_TYPE_CPU, 0, 1l, 1l, "", "");
|
alertManagerImplMock.sendAlert(AlertManager.AlertType.ALERT_TYPE_CPU, 0, 1L, 1L, "", "");
|
||||||
} catch (UnsupportedEncodingException | MessagingException e) {
|
} catch (UnsupportedEncodingException | MessagingException e) {
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
}
|
}
|
||||||
@ -108,39 +125,69 @@ public class AlertManagerImplTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendAlertTestSendMail() {
|
public void sendAlertTestSendMail() {
|
||||||
Mockito.doReturn(null).when(alertDaoMock).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
Mockito.doReturn(null).when(_alertDao).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
||||||
Mockito.anyLong(), Mockito.anyLong());
|
Mockito.anyLong(), Mockito.anyLong());
|
||||||
Mockito.doReturn(null).when(alertDaoMock).persist(Mockito.any());
|
Mockito.doReturn(null).when(_alertDao).persist(any());
|
||||||
alertManagerImplMock.recipients = new String[]{""};
|
alertManagerImplMock.recipients = new String[]{""};
|
||||||
|
|
||||||
sendMessage();
|
sendMessage();
|
||||||
|
|
||||||
Mockito.verify(alertManagerImplMock).sendMessage(Mockito.any());
|
Mockito.verify(alertManagerImplMock).sendMessage(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendAlertTestDebugLogging() {
|
public void sendAlertTestDebugLogging() {
|
||||||
Mockito.doReturn(0).when(alertVOMock).getSentCount();
|
Mockito.doReturn(0).when(alertVOMock).getSentCount();
|
||||||
Mockito.doReturn(alertVOMock).when(alertDaoMock).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
Mockito.doReturn(alertVOMock).when(_alertDao).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
||||||
Mockito.anyLong(), Mockito.anyLong());
|
Mockito.anyLong(), Mockito.anyLong());
|
||||||
|
|
||||||
sendMessage();
|
sendMessage();
|
||||||
|
|
||||||
Mockito.verify(alertManagerImplMock.logger).debug(Mockito.anyString());
|
Mockito.verify(alertManagerImplMock.logger).debug(Mockito.anyString());
|
||||||
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(Mockito.any());
|
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendAlertTestWarnLogging() {
|
public void sendAlertTestWarnLogging() {
|
||||||
Mockito.doReturn(null).when(alertDaoMock).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
Mockito.doReturn(null).when(_alertDao).getLastAlert(Mockito.anyShort(), Mockito.anyLong(),
|
||||||
Mockito.anyLong(), Mockito.anyLong());
|
Mockito.anyLong(), Mockito.anyLong());
|
||||||
Mockito.doReturn(null).when(alertDaoMock).persist(Mockito.any());
|
Mockito.doReturn(null).when(_alertDao).persist(Mockito.any());
|
||||||
alertManagerImplMock.recipients = null;
|
alertManagerImplMock.recipients = null;
|
||||||
|
|
||||||
sendMessage();
|
sendMessage();
|
||||||
|
|
||||||
Mockito.verify(alertManagerImplMock.logger, Mockito.times(2)).warn(Mockito.anyString());
|
Mockito.verify(alertManagerImplMock.logger, Mockito.times(2)).warn(Mockito.anyString());
|
||||||
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(Mockito.any());
|
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendAlertWithNullParameters() throws MessagingException, UnsupportedEncodingException {
|
||||||
|
// Given
|
||||||
|
String subject = "Test Subject";
|
||||||
|
String content = "Test Content";
|
||||||
|
AlertManager.AlertType alertType = AlertManager.AlertType.ALERT_TYPE_MEMORY;
|
||||||
|
|
||||||
|
// When
|
||||||
|
alertManagerImplMock.sendAlert(alertType, null, null, null, subject, content);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
ArgumentCaptor<AlertVO> alertCaptor = ArgumentCaptor.forClass(AlertVO.class);
|
||||||
|
verify(_alertDao).persist(alertCaptor.capture());
|
||||||
|
|
||||||
|
AlertVO capturedAlert = alertCaptor.getValue();
|
||||||
|
assertNotNull("Captured alert should not be null", capturedAlert);
|
||||||
|
assertEquals(0L, capturedAlert.getDataCenterId());
|
||||||
|
assertNull(capturedAlert.getPodId());
|
||||||
|
assertNull(capturedAlert.getClusterId());
|
||||||
|
assertEquals(subject, capturedAlert.getSubject());
|
||||||
|
assertEquals(content, capturedAlert.getContent());
|
||||||
|
assertEquals(alertType.getType(), capturedAlert.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void testSendAlertWithNullAlertType() throws MessagingException, UnsupportedEncodingException {
|
||||||
|
// When
|
||||||
|
alertManagerImplMock.sendAlert(null, 0, 1L, 1L, "subject", "content");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user