Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup alerting and logging error in BGPServiceImpl #10252

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/src/main/java/com/cloud/alert/AlertManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,14 @@
AlertVO alert = null;
Long clusterId = cluster == null ? null : cluster.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)
&& (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_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_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);

Check warning on line 748 in server/src/main/java/com/cloud/alert/AlertManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/alert/AlertManagerImpl.java#L748

Added line #L748 was not covered by tests
}

if (alert == null) {
Expand All @@ -754,7 +755,7 @@
newAlert.setContent(content);
newAlert.setClusterId(clusterId);
newAlert.setPodId(podId);
newAlert.setDataCenterId(dataCenter.getId());
newAlert.setDataCenterId(dcId);

Check warning on line 758 in server/src/main/java/com/cloud/alert/AlertManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/alert/AlertManagerImpl.java#L758

Added line #L758 was not covered by tests
newAlert.setSentCount(1);
newAlert.setLastSent(new Date());
newAlert.setName(alertType.getName());
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/java/com/cloud/bgp/BGPServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@
netName = network.getName();
}

LOGGER.debug("Allocating the AS Number {} to {} on zone {}", asNumber::toString,
(Objects.nonNull(vpcId) ? "VPC " + vpc : "network " + network)::toString,
() -> dataCenterDao.findById(zoneId));
String networkName = Objects.nonNull(vpcId) ? ("VPC " + vpc) : ("network " + network);
LOGGER.debug("Allocating the AS Number {} to {} on zone {}", asNumberVO::toString,
networkName::toString, () -> dataCenterDao.findById(zoneId));

Check warning on line 260 in server/src/main/java/com/cloud/bgp/BGPServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/com/cloud/bgp/BGPServiceImpl.java#L259-L260

Added lines #L259 - L260 were not covered by tests
asNumberVO.setAllocated(true);
asNumberVO.setAllocatedTime(new Date());
if (Objects.nonNull(vpcId)) {
Expand Down
71 changes: 59 additions & 12 deletions server/src/test/java/com/cloud/alert/AlertManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand All @@ -37,6 +39,12 @@
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;

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)
public class AlertManagerImplTest {

Expand All @@ -45,7 +53,7 @@ public class AlertManagerImplTest {
AlertManagerImpl alertManagerImplMock;

@Mock
AlertDao alertDaoMock;
AlertDao _alertDao;

@Mock
private DataCenterDao _dcDao;
Expand All @@ -65,7 +73,16 @@ public class AlertManagerImplTest {
@Mock
SMTPMailSender mailSenderMock;

private void sendMessage (){
private final String[] recipients = new String[]{"[email protected]"};
private final String senderAddress = "[email protected]";

@Before
public void setUp() {
alertManagerImplMock.recipients = recipients;
alertManagerImplMock.senderAddress = senderAddress;
}

private void sendMessage() {
try {
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
Mockito.when(zone.getId()).thenReturn(0L);
Expand All @@ -77,46 +94,76 @@ private void sendMessage (){
Mockito.when(cluster.getId()).thenReturn(1L);
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) {
Assert.fail();
}
}

@Test
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.doReturn(null).when(alertDaoMock).persist(Mockito.any());
alertManagerImplMock.recipients = new String [] {""};
Mockito.doReturn(null).when(_alertDao).persist(any());
alertManagerImplMock.recipients = new String[]{""};

sendMessage();

Mockito.verify(alertManagerImplMock).sendMessage(Mockito.any());
Mockito.verify(alertManagerImplMock).sendMessage(any());
}

@Test
public void sendAlertTestDebugLogging() {
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());

sendMessage();

Mockito.verify(alertManagerImplMock.logger).debug(Mockito.anyString());
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(Mockito.any());
Mockito.verify(alertManagerImplMock, Mockito.never()).sendMessage(any());
}

@Test
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.doReturn(null).when(alertDaoMock).persist(Mockito.any());
Mockito.doReturn(null).when(_alertDao).persist(Mockito.any());
alertManagerImplMock.recipients = null;

sendMessage();

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");
}
}
Loading