blob: 5a8c73e162665675502fe5061690b1cfce405af4 [file]
// 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 com.cloud.alert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import com.cloud.alert.dao.ResourceAlertDao;
import com.cloud.alert.dao.ResourceAlertRuleDao;
import com.cloud.alert.dao.ResourceAlertRuleWebhookDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.utils.identity.ManagementServerNode;
import org.apache.cloudstack.utils.mailing.SMTPMailProperties;
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
import org.apache.cloudstack.webhook.WebhookHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import com.cloud.cluster.ManagementServerHostVO;
import com.cloud.cluster.dao.ManagementServerHostDao;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.host.HostStats;
import com.cloud.host.HostVO;
import com.cloud.host.Status;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.resource.ResourceState;
import com.cloud.server.ResourceTag;
import com.cloud.server.StatsCollector;
import com.cloud.storage.Storage;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.storage.StorageStats;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeStats;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.tags.dao.ResourceTagDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.Pair;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VmStats;
import com.cloud.vm.dao.UserVmDao;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@RunWith(MockitoJUnitRunner.class)
public class ResourceAlertManagerImplTest {
@Spy @InjectMocks
ResourceAlertManagerImpl manager;
@Mock ResourceAlertRuleDao ruleDao;
@Mock ResourceAlertDao alertDao;
@Mock ResourceAlertRuleWebhookDao ruleWebhookDao;
@Mock WebhookHelper webhookHelper;
@Mock UserVmDao userVmDao;
@Mock HostDao hostDao;
@Mock PrimaryDataStoreDao storagePoolDao;
@Mock VolumeDao volumeDao;
@Mock StatsCollector statsCollector;
@Mock ConfigurationDao configDao;
@Mock ResourceTagDao resourceTagDao;
@Mock AccountDao accountDao;
@Mock DomainDao domainDao;
@Mock AccountManager accountManager;
@Mock ManagementServerHostDao managementServerHostDao;
@Mock SMTPMailSender mailSender;
@Captor ArgumentCaptor<ResourceAlertVO> alertCaptor;
@Captor ArgumentCaptor<SMTPMailProperties> mailCaptor;
private static final long VM_ID = 101L;
private static final long HOST_ID = 201L;
private static final long POOL_ID = 301L;
@Before
public void setUp() throws Exception {
// stub out the AlertGenerator static call (needs Spring context in real env)
doNothing().when(manager).publishAlertEvent(anyLong(), anyString(), anyString());
lenient().doReturn(true).when(manager).isStillActive(any());
// owners and resources exist unless a test says otherwise
AccountVO defaultOwner = mock(AccountVO.class);
lenient().when(defaultOwner.getId()).thenReturn(1L);
lenient().when(defaultOwner.getType()).thenReturn(Account.Type.NORMAL);
lenient().when(accountDao.findById(anyLong())).thenReturn(defaultOwner);
UserVmVO vm = runningVm();
lenient().when(userVmDao.findById(anyLong())).thenReturn(vm);
VolumeVO volume = mock(VolumeVO.class);
lenient().when(volume.getState()).thenReturn(Volume.State.Ready);
lenient().when(volumeDao.findById(anyLong())).thenReturn(volume);
HostVO host = mock(HostVO.class);
lenient().when(host.getStatus()).thenReturn(Status.Up);
lenient().when(host.getResourceState()).thenReturn(ResourceState.Enabled);
lenient().when(hostDao.findById(anyLong())).thenReturn(host);
StoragePoolVO pool = mock(StoragePoolVO.class);
lenient().when(pool.getStatus()).thenReturn(StoragePoolStatus.Up);
lenient().when(storagePoolDao.findById(anyLong())).thenReturn(pool);
}
private UserVmVO runningVm() {
UserVmVO vm = mock(UserVmVO.class);
lenient().when(vm.getState()).thenReturn(VirtualMachine.State.Running);
return vm;
}
private ResourceAlertRuleVO vmCpuRule(Long resourceId) {
return vmCpuRuleWithEmail(resourceId, false);
}
private ResourceAlertRuleVO vmCpuRuleWithEmail(Long resourceId, boolean email) {
return new ResourceAlertRuleVO("test", ResourceAlertRule.ResourceType.VirtualMachine,
resourceId, 1L, 1L, "CPU_UTILIZATION", AlertCondition.GT, 80.0,
AlertSeverity.HIGH, "CPU high", email, 600);
}
private void injectMailSender(String... recipients) throws Exception {
Field f = ResourceAlertManagerImpl.class.getDeclaredField("mailSender");
f.setAccessible(true);
f.set(manager, mailSender);
Field r = ResourceAlertManagerImpl.class.getDeclaredField("emailRecipients");
r.setAccessible(true);
r.set(manager, recipients);
Field s = ResourceAlertManagerImpl.class.getDeclaredField("senderAddress");
s.setAccessible(true);
s.set(manager, "alerts@example.com");
// replace async executor with a synchronous one so verify() works immediately
manager.emailExecutor = new AbstractExecutorService() {
@Override public void execute(Runnable command) { command.run(); }
@Override public void shutdown() {}
@Override public List<Runnable> shutdownNow() { return Collections.emptyList(); }
@Override public boolean isShutdown() { return false; }
@Override public boolean isTerminated() { return false; }
@Override public boolean awaitTermination(long t, TimeUnit u) { return true; }
};
}
@Test
public void testVmCpuRuleFiresWhenThresholdBreached() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
ResourceAlertVO fired = alertCaptor.getValue();
assertEquals(VM_ID, (long) fired.getResourceId());
assertEquals("CPU_UTILIZATION", fired.getMetricType());
assertEquals(85.0, fired.getMetricValue(), 0.001);
assertEquals(AlertSeverity.HIGH, fired.getSeverity());
}
@Test
public void testVmCpuRuleDoesNotFireWhenBelowThreshold() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(75.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testRuleDoesNotFireWithinResetInterval() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
ResourceAlertVO recentAlert = mock(ResourceAlertVO.class);
when(recentAlert.getAlertTimestamp()).thenReturn(new Date(System.currentTimeMillis() - 10_000L));
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(recentAlert);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testRuleFiresAfterResetIntervalExpires() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
ResourceAlertVO oldAlert = mock(ResourceAlertVO.class);
when(oldAlert.getAlertTimestamp()).thenReturn(new Date(System.currentTimeMillis() - 700_000L));
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(oldAlert);
manager.evaluateRules();
verify(alertDao).persist(any());
}
@Test
public void testNullStatsSkipped() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(null);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testVmMemorySkippedWhenNoBalloonDriver() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("test",
ResourceAlertRule.ResourceType.VirtualMachine, VM_ID, 1L, 1L,
"MEMORY_UTILIZATION", AlertCondition.GT, 50.0, AlertSeverity.MEDIUM, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getMemoryKBs()).thenReturn(8192.0);
when(stats.getIntFreeMemoryKBs()).thenReturn(-1.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testVmMemoryUtilizationCalculation() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("test",
ResourceAlertRule.ResourceType.VirtualMachine, VM_ID, 1L, 1L,
"MEMORY_UTILIZATION", AlertCondition.GT, 70.0, AlertSeverity.HIGH, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getMemoryKBs()).thenReturn(8192.0);
when(stats.getIntFreeMemoryKBs()).thenReturn(2048.0); // 75% used
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(75.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testStorageUtilizationCalculation() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("test",
ResourceAlertRule.ResourceType.StoragePool, POOL_ID, 1L, 1L,
"STORAGE_UTILIZATION", AlertCondition.GT, 65.0, AlertSeverity.HIGH, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
StorageStats poolStats = mock(StorageStats.class);
when(poolStats.getCapacityBytes()).thenReturn(10000L);
when(poolStats.getByteUsed()).thenReturn(7000L); // 70%
when(statsCollector.getStoragePoolStats(POOL_ID)).thenReturn(poolStats);
when(alertDao.findLastFiredForRule(anyLong(), eq(POOL_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(70.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testGenericVmRuleFansOutToAllRunningVms() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.NORMAL);
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Arrays.asList(101L, 102L));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(101L)).thenReturn(stats);
when(statsCollector.getRecentVmStats(102L)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), anyLong())).thenReturn(null);
manager.evaluateRules();
verify(alertDao, times(2)).persist(any());
}
@Test
public void testGenericVmRuleOnlyListsRunningVms() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.NORMAL);
manager.evaluateRules();
verify(userVmDao).listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running);
verify(alertDao, never()).persist(any());
}
@Test
public void testHostCpuRuleUsesHostStats() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("test",
ResourceAlertRule.ResourceType.Host, HOST_ID, 1L, 1L,
"CPU_UTILIZATION", AlertCondition.GT, 85.0, AlertSeverity.CRITICAL, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
HostStats hostStats = mock(HostStats.class);
when(hostStats.getCpuUtilization()).thenReturn(90.0);
when(statsCollector.getHostStats(HOST_ID)).thenReturn(hostStats);
when(alertDao.findLastFiredForRule(anyLong(), eq(HOST_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(HOST_ID, (long) alertCaptor.getValue().getResourceId());
assertEquals(90.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testHostMemoryUtilizationCalculation() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("test",
ResourceAlertRule.ResourceType.Host, HOST_ID, 1L, 1L,
"MEMORY_UTILIZATION", AlertCondition.GT, 80.0, AlertSeverity.HIGH, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
HostStats hostStats = mock(HostStats.class);
when(hostStats.getTotalMemoryKBs()).thenReturn(16384.0);
when(hostStats.getFreeMemoryKBs()).thenReturn(1638.4); // ~90% used
when(statsCollector.getHostStats(HOST_ID)).thenReturn(hostStats);
when(alertDao.findLastFiredForRule(anyLong(), eq(HOST_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(90.0, alertCaptor.getValue().getMetricValue(), 0.01);
}
@Test
public void testEventBusPublishedOnFiring() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
UserVmVO vm = runningVm();
when(vm.getDataCenterId()).thenReturn(1L);
when(userVmDao.findById(VM_ID)).thenReturn(vm);
manager.evaluateRules();
verify(manager).publishAlertEvent(eq(1L), anyString(), anyString());
}
@Test
public void testEventBusNotPublishedWhenNoFiring() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(75.0); // below threshold
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
manager.evaluateRules();
verify(manager, never()).publishAlertEvent(anyLong(), anyString(), anyString());
}
@Test
public void testEmailSentWhenRuleHasEmailEnabled() throws Exception {
injectMailSender("admin@example.com");
ResourceAlertRuleVO rule = vmCpuRuleWithEmail(VM_ID, true);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(mailSender).sendMail(mailCaptor.capture());
SMTPMailProperties mail = mailCaptor.getValue();
assertTrue(mail.getSubject().contains("CPU_UTILIZATION"));
assertTrue(mail.getSubject().contains("HIGH"));
assertTrue(mail.getContent().toString().contains("85."));
}
@Test
public void testEmailSkippedWhenRuleHasEmailDisabled() throws Exception {
injectMailSender("admin@example.com");
ResourceAlertRuleVO rule = vmCpuRuleWithEmail(VM_ID, false);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(mailSender, never()).sendMail(any());
}
@Test
public void testEmailSkippedWhenNoRecipientsConfigured() throws Exception {
// mailSender injected but no recipients → should not attempt to send
injectMailSender(/* no recipients */);
ResourceAlertRuleVO rule = vmCpuRuleWithEmail(VM_ID, true);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(mailSender, never()).sendMail(any());
}
@Test
public void testSubjectContainsKeyAlertFields() throws Exception {
injectMailSender("admin@example.com");
ResourceAlertRuleVO rule = vmCpuRuleWithEmail(VM_ID, true);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
UserVmVO vm = mock(UserVmVO.class);
when(vm.getUuid()).thenReturn("vm-uuid");
when(vm.getDisplayName()).thenReturn("web-01");
when(userVmDao.findByIdIncludingRemoved(VM_ID)).thenReturn(vm);
AccountVO account = mock(AccountVO.class);
when(account.getAccountName()).thenReturn("acme");
when(accountDao.findByIdIncludingRemoved(1L)).thenReturn(account);
DomainVO domain = mock(DomainVO.class);
when(domain.getPath()).thenReturn("/acme/");
when(domainDao.findByIdIncludingRemoved(1L)).thenReturn(domain);
manager.evaluateRules();
verify(mailSender).sendMail(mailCaptor.capture());
String subject = mailCaptor.getValue().getSubject();
String body = mailCaptor.getValue().getContent().toString();
assertTrue(subject.contains("HIGH"));
assertTrue(subject.contains("CPU_UTILIZATION"));
assertTrue(subject.contains("GT"));
assertTrue(subject.contains("VirtualMachine web-01"));
assertTrue(body.contains("Resource: web-01"));
assertTrue(body.contains("Resource ID: vm-uuid"));
assertTrue(body.contains("Account: acme"));
assertTrue(body.contains("Domain: /acme/"));
}
@Test
public void testGetDataCenterIdUsesVmDao() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
UserVmVO vm = runningVm();
when(vm.getDataCenterId()).thenReturn(42L);
when(userVmDao.findById(VM_ID)).thenReturn(vm);
manager.evaluateRules();
verify(manager).publishAlertEvent(eq(42L), anyString(), anyString());
}
@Test
public void testGenericRuleSkipsOptedOutVm() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.NORMAL);
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Collections.singletonList(VM_ID));
ResourceTag optOutTag = mock(ResourceTag.class);
when(optOutTag.getValue()).thenReturn("true");
when(resourceTagDao.findByKey(VM_ID, ResourceTag.ResourceObjectType.UserVm, "resource.alert.opt.out"))
.thenReturn(optOutTag);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testGenericRuleDoesNotSkipVmWithOptOutTagValueFalse() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.NORMAL);
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Collections.singletonList(VM_ID));
ResourceTag tag = mock(ResourceTag.class);
when(tag.getValue()).thenReturn("false");
when(resourceTagDao.findByKey(VM_ID, ResourceTag.ResourceObjectType.UserVm, "resource.alert.opt.out"))
.thenReturn(tag);
when(ruleDao.existsSpecificRule(ResourceAlertRule.ResourceType.VirtualMachine, "CPU_UTILIZATION", VM_ID, 1L))
.thenReturn(false);
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(any());
}
@Test
public void testGenericRuleSkipsVmWithSpecificRuleForSameMetric() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.NORMAL);
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Collections.singletonList(VM_ID));
when(resourceTagDao.findByKey(VM_ID, ResourceTag.ResourceObjectType.UserVm, "resource.alert.opt.out"))
.thenReturn(null);
when(ruleDao.existsSpecificRule(ResourceAlertRule.ResourceType.VirtualMachine, "CPU_UTILIZATION", VM_ID, 1L))
.thenReturn(true);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testSpecificRuleIgnoresOptOutAndPrecedenceChecks() {
// specific rule (non-null resourceId) must not check opt-out or precedence
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(alertDao).persist(any());
verify(resourceTagDao, never()).findByKey(anyLong(), any(), anyString());
verify(ruleDao, never()).existsSpecificRule(any(), anyString(), anyLong(), anyLong());
}
@Test
public void testGetDataCenterIdFallsBackToZeroWhenVmNotFound() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Collections.singletonList(VM_ID));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
when(userVmDao.findById(VM_ID)).thenReturn(null);
manager.evaluateRules();
verify(manager).publishAlertEvent(eq(0L), anyString(), anyString());
}
private void stubFiringVmCpuRule(ResourceAlertRuleVO rule) {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
}
@Test
public void testFiredAlertIsDeliveredToMappedWebhooks() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
stubFiringVmCpuRule(rule);
when(ruleWebhookDao.listWebhookIdsByRule(rule.getId())).thenReturn(Arrays.asList(11L, 12L));
doReturn(webhookHelper).when(manager).getWebhookHelper();
UserVmVO vm = mock(UserVmVO.class);
when(vm.getUuid()).thenReturn("vm-uuid");
when(vm.getDisplayName()).thenReturn("web-01");
when(userVmDao.findByIdIncludingRemoved(VM_ID)).thenReturn(vm);
manager.evaluateRules();
ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class);
verify(webhookHelper).deliverToWebhooks(eq(Arrays.asList(11L, 12L)), eq(1L),
eq(ResourceAlertManagerImpl.ALERT_EVENT_TYPE), payloadCaptor.capture());
JsonObject payload = JsonParser.parseString(payloadCaptor.getValue()).getAsJsonObject();
assertEquals(rule.getUuid(), payload.get("ruleid").getAsString());
assertEquals("VirtualMachine", payload.get("resourcetype").getAsString());
assertEquals("vm-uuid", payload.get("resourceid").getAsString());
assertEquals("web-01", payload.get("resourcename").getAsString());
assertEquals("CPU_UTILIZATION", payload.get("metric").getAsString());
assertEquals(85.0, payload.get("value").getAsDouble(), 0.001);
assertEquals("HIGH", payload.get("severity").getAsString());
}
@Test
public void testFiredAlertWithoutMappedWebhooksSkipsDelivery() {
stubFiringVmCpuRule(vmCpuRule(VM_ID));
manager.evaluateRules();
verify(alertDao).persist(any());
verify(manager, never()).getWebhookHelper();
}
private ResourceAlertRuleVO hostRule(String metric, double threshold) {
return new ResourceAlertRuleVO("host", ResourceAlertRule.ResourceType.Host,
HOST_ID, 1L, 1L, metric, AlertCondition.GT, threshold,
AlertSeverity.HIGH, null, false, 600);
}
@Test
public void testHostLoadAverageRuleFires() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(hostRule("LOAD_AVERAGE", 4.0)));
HostStats stats = mock(HostStats.class);
when(stats.getLoadAverage()).thenReturn(6.5);
when(statsCollector.getHostStats(HOST_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(6.5, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testHostNetworkReadRuleUsesHostStats() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(hostRule("NETWORK_READ_KBPS", 1000.0)));
HostStats stats = mock(HostStats.class);
when(stats.getNetworkReadKBs()).thenReturn(2500.0);
when(statsCollector.getHostStats(HOST_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(2500.0, alertCaptor.getValue().getMetricValue(), 0.001);
verify(statsCollector, never()).getRecentVmStats(anyLong());
}
@Test
public void testHostNetworkWriteRuleDoesNotFireBelowThreshold() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(hostRule("NETWORK_WRITE_KBPS", 1000.0)));
HostStats stats = mock(HostStats.class);
when(stats.getNetworkWriteKBs()).thenReturn(10.0);
when(statsCollector.getHostStats(HOST_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
private static final long VOLUME_ID = 401L;
private ResourceAlertRuleVO volumeSizeRule(double thresholdGb) {
return new ResourceAlertRuleVO("vol", ResourceAlertRule.ResourceType.Volume,
VOLUME_ID, 1L, 1L, "VOLUME_USED_GB", AlertCondition.GT, thresholdGb,
AlertSeverity.MEDIUM, null, false, 600);
}
@Test
public void testVolumeUsedRuleUsesPhysicalSizeByPath() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(volumeSizeRule(10.0)));
VolumeVO vol = mock(VolumeVO.class);
when(vol.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
when(vol.getState()).thenReturn(Volume.State.Ready);
when(vol.getPath()).thenReturn("vol-path");
when(volumeDao.findById(VOLUME_ID)).thenReturn(vol);
VolumeStats stats = mock(VolumeStats.class);
when(stats.getPhysicalSize()).thenReturn(20L * 1024 * 1024 * 1024);
when(statsCollector.getVolumeStats("vol-path")).thenReturn(stats);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(20.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testVolumeUtilizationIsUsedOverDiskSize() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("vol-pct", ResourceAlertRule.ResourceType.Volume,
VOLUME_ID, 1L, 1L, "VOLUME_UTILIZATION", AlertCondition.GT, 50.0,
AlertSeverity.MEDIUM, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VolumeVO vol = mock(VolumeVO.class);
when(vol.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
when(vol.getState()).thenReturn(Volume.State.Ready);
when(vol.getPath()).thenReturn("vol-path");
when(volumeDao.findById(VOLUME_ID)).thenReturn(vol);
VolumeStats stats = mock(VolumeStats.class);
when(stats.getPhysicalSize()).thenReturn(6L * 1024 * 1024 * 1024);
when(stats.getVirtualSize()).thenReturn(8L * 1024 * 1024 * 1024);
when(statsCollector.getVolumeStats("vol-path")).thenReturn(stats);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(75.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testVolumeUsedRuleUsesChainInfoForOva() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(volumeSizeRule(10.0)));
VolumeVO vol = mock(VolumeVO.class);
when(vol.getFormat()).thenReturn(Storage.ImageFormat.OVA);
when(vol.getState()).thenReturn(Volume.State.Ready);
when(vol.getChainInfo()).thenReturn("chain-info");
when(volumeDao.findById(VOLUME_ID)).thenReturn(vol);
manager.evaluateRules();
verify(statsCollector).getVolumeStats("chain-info");
verify(alertDao, never()).persist(any());
}
private ResourceAlertRuleVO poolIopsRule(double threshold) {
return new ResourceAlertRuleVO("pool-iops", ResourceAlertRule.ResourceType.StoragePool,
POOL_ID, 1L, 1L, "STORAGE_USED_IOPS", AlertCondition.GT, threshold,
AlertSeverity.HIGH, null, false, 600);
}
@Test
public void testStoragePoolIopsRuleFires() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(poolIopsRule(1000.0)));
StorageStats stats = mock(StorageStats.class);
when(stats.getUsedIops()).thenReturn(5000L);
when(statsCollector.getStoragePoolStats(POOL_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(5000.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testStoragePoolIopsRuleSkippedWhenDriverDoesNotReportIops() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(poolIopsRule(1000.0)));
StorageStats stats = mock(StorageStats.class);
when(stats.getUsedIops()).thenReturn(null);
when(statsCollector.getStoragePoolStats(POOL_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
private AccountVO stubOwner(Account.Type type) {
AccountVO owner = mock(AccountVO.class);
when(owner.getType()).thenReturn(type);
lenient().when(owner.getId()).thenReturn(1L);
lenient().when(owner.getDomainId()).thenReturn(5L);
when(accountDao.findById(1L)).thenReturn(owner);
return owner;
}
@Test
public void testGenericRuleScopeForRootAdminIsWholeCloud() {
stubOwner(Account.Type.ADMIN);
Pair<Long, List<Long>> scope = manager.getGenericRuleScope(vmCpuRule(null));
assertNull(scope.first());
assertNull(scope.second());
}
@Test
public void testGenericRuleScopeForDomainAdminIsDomainTree() {
stubOwner(Account.Type.DOMAIN_ADMIN);
when(domainDao.getDomainAndChildrenIds(5L)).thenReturn(Arrays.asList(5L, 6L));
Pair<Long, List<Long>> scope = manager.getGenericRuleScope(vmCpuRule(null));
assertNull(scope.first());
assertEquals(Arrays.asList(5L, 6L), scope.second());
}
@Test
public void testGenericRuleScopeForUserIsOwnAccount() {
stubOwner(Account.Type.NORMAL);
Pair<Long, List<Long>> scope = manager.getGenericRuleScope(vmCpuRule(null));
assertEquals(Long.valueOf(1L), scope.first());
assertNull(scope.second());
}
@Test
public void testGenericVolumeRuleForRootAdminListsReadyVolumesCloudWide() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("vol", ResourceAlertRule.ResourceType.Volume,
null, 1L, 1L, "VOLUME_USED_GB", AlertCondition.GT, 10.0, AlertSeverity.LOW, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
stubOwner(Account.Type.ADMIN);
manager.evaluateRules();
verify(volumeDao).listUserVolumeIdsByAccountOrDomainsAndState(null, null, Volume.State.Ready);
}
@Test
public void testRuleRemovedWhenOwnerMissing() {
ResourceAlertRuleVO rule = vmCpuRule(null);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
when(accountDao.findById(1L)).thenReturn(null);
manager.evaluateRules();
verify(alertDao).removeByAlertRuleId(rule.getId());
verify(ruleDao).remove(rule.getId());
verify(userVmDao, never()).listIdsByAccountOrDomainsAndState(any(), any(), any());
}
@Test
public void testSpecificRuleRemovedWhenResourceExpunged() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
when(userVmDao.findById(VM_ID)).thenReturn(null);
manager.evaluateRules();
verify(ruleDao).remove(rule.getId());
verify(statsCollector, never()).getRecentVmStats(anyLong());
}
@Test
public void testSpecificHostRuleKeptWhileHostExists() {
ResourceAlertRuleVO rule = hostRule("CPU_UTILIZATION", 90.0);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
manager.evaluateRules();
verify(ruleDao, never()).remove(anyLong());
}
@Test
public void testEvaluatesOnLongestRunningManagementServer() {
ManagementServerHostVO msHost = mock(ManagementServerHostVO.class);
when(msHost.getMsid()).thenReturn(ManagementServerNode.getManagementServerId());
when(managementServerHostDao.findOneByLongestRuntime()).thenReturn(msHost);
assertTrue(manager.isEvaluatingServer());
}
@Test
public void testDoesNotEvaluateOnOtherManagementServers() {
ManagementServerHostVO msHost = mock(ManagementServerHostVO.class);
when(msHost.getMsid()).thenReturn(ManagementServerNode.getManagementServerId() + 1);
when(managementServerHostDao.findOneByLongestRuntime()).thenReturn(msHost);
assertFalse(manager.isEvaluatingServer());
}
@Test
public void testDoesNotEvaluateWhenNoManagementServerFound() {
assertFalse(manager.isEvaluatingServer());
}
@Test
public void testRemoveExpiredAlertsUsesRetentionDays() {
long before = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(30);
manager.removeExpiredAlerts();
ArgumentCaptor<Date> cutoff = ArgumentCaptor.forClass(Date.class);
verify(alertDao).removeOlderThan(cutoff.capture());
long diff = Math.abs(cutoff.getValue().getTime() - before);
assertTrue("cutoff should be about 30 days ago", diff < 60_000L);
}
@Test
public void testEvaluationIntervalFallsBackWhenBelowOne() {
doReturn(0).when(manager).configuredEvaluationInterval();
assertEquals(60, manager.getEvaluationInterval());
doReturn(-5).when(manager).configuredEvaluationInterval();
assertEquals(60, manager.getEvaluationInterval());
doReturn(null).when(manager).configuredEvaluationInterval();
assertEquals(60, manager.getEvaluationInterval());
}
@Test
public void testEvaluationIntervalUsesValidSetting() {
doReturn(30).when(manager).configuredEvaluationInterval();
assertEquals(30, manager.getEvaluationInterval());
}
@Test
public void testGenericRuleNotSkippedBySpecificRuleOfAnotherAccount() {
ResourceAlertRuleVO rule = vmCpuRule(null);
stubFiringVmCpuRule(rule);
stubOwner(Account.Type.NORMAL);
when(userVmDao.listIdsByAccountOrDomainsAndState(1L, null, VirtualMachine.State.Running))
.thenReturn(Collections.singletonList(VM_ID));
lenient().when(ruleDao.existsSpecificRule(ResourceAlertRule.ResourceType.VirtualMachine, "CPU_UTILIZATION", VM_ID, 2L))
.thenReturn(true);
when(alertDao.findLastFiredForRule(anyLong(), eq(VM_ID))).thenReturn(null);
manager.evaluateRules();
verify(ruleDao).existsSpecificRule(ResourceAlertRule.ResourceType.VirtualMachine, "CPU_UTILIZATION", VM_ID, 1L);
verify(alertDao).persist(any());
}
@Test
public void testSpecificRuleSkipsStoppedVm() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
stubFiringVmCpuRule(rule);
UserVmVO vm = mock(UserVmVO.class);
when(vm.getState()).thenReturn(VirtualMachine.State.Stopped);
when(userVmDao.findById(VM_ID)).thenReturn(vm);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testIsInServiceChecksResourceState() {
HostVO host = mock(HostVO.class);
when(host.getStatus()).thenReturn(Status.Up);
when(host.getResourceState()).thenReturn(ResourceState.Maintenance);
when(hostDao.findById(HOST_ID)).thenReturn(host);
assertFalse(manager.isInService(ResourceAlertRule.ResourceType.Host, HOST_ID));
StoragePoolVO pool = mock(StoragePoolVO.class);
when(pool.getStatus()).thenReturn(StoragePoolStatus.Maintenance);
when(storagePoolDao.findById(POOL_ID)).thenReturn(pool);
assertFalse(manager.isInService(ResourceAlertRule.ResourceType.StoragePool, POOL_ID));
VolumeVO volume = mock(VolumeVO.class);
when(volume.getState()).thenReturn(Volume.State.Allocated);
when(volumeDao.findById(7L)).thenReturn(volume);
assertFalse(manager.isInService(ResourceAlertRule.ResourceType.Volume, 7L));
assertTrue(manager.isInService(ResourceAlertRule.ResourceType.VirtualMachine, VM_ID));
}
@Test
public void testRuleRemovedWhenOwnerLostAccessToResource() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
doThrow(new PermissionDeniedException("moved")).when(accountManager).checkAccess(any(Account.class), any(), anyBoolean(), any(ControlledEntity.class));
manager.evaluateRules();
verify(alertDao).removeByAlertRuleId(rule.getId());
verify(ruleDao).remove(rule.getId());
verify(alertDao, never()).persist(any());
}
@Test
public void testVolumeDiskRuleNeverUsesVmStats() {
ResourceAlertRuleVO rule = new ResourceAlertRuleVO("vol-io", ResourceAlertRule.ResourceType.Volume,
VOLUME_ID, 1L, 1L, "DISK_READ_IOPS", AlertCondition.GTE, 0.0,
AlertSeverity.LOW, null, false, 600);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VolumeVO vol = mock(VolumeVO.class);
when(vol.getState()).thenReturn(Volume.State.Ready);
when(volumeDao.findById(VOLUME_ID)).thenReturn(vol);
manager.evaluateRules();
verify(statsCollector, never()).getRecentVmStats(anyLong());
verify(alertDao, never()).persist(any());
}
@Test
public void testVmStatsReadOnceForRulesOnSameVm() {
ResourceAlertRuleVO cpuRule = vmCpuRule(VM_ID);
ResourceAlertRuleVO netRule = new ResourceAlertRuleVO("vm-net", ResourceAlertRule.ResourceType.VirtualMachine,
VM_ID, 1L, 1L, "NETWORK_READ_KBPS", AlertCondition.GT, 1000.0,
AlertSeverity.LOW, null, false, 600);
when(ruleDao.listActive()).thenReturn(Arrays.asList(cpuRule, netRule));
VmStats stats = mock(VmStats.class);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
manager.evaluateRules();
manager.evaluateRules();
verify(statsCollector, times(2)).getRecentVmStats(VM_ID);
}
@Test
public void testEmailSenderIsBounded() {
ThreadPoolExecutor executor = (ThreadPoolExecutor) new ResourceAlertManagerImpl().emailExecutor;
assertEquals(1, executor.getMaximumPoolSize());
assertEquals(ResourceAlertManagerImpl.EMAIL_QUEUE_SIZE, executor.getQueue().remainingCapacity());
executor.shutdownNow();
}
@Test
public void testRemoveStaleWebhookLinks() {
when(ruleWebhookDao.removeLinksToRemovedWebhooks()).thenReturn(2);
manager.removeStaleWebhookLinks();
verify(ruleWebhookDao).removeLinksToRemovedWebhooks();
}
@Test
public void testBrokenRuleDoesNotStopOtherRules() {
ResourceAlertRuleVO broken = new ResourceAlertRuleVO("broken", ResourceAlertRule.ResourceType.VirtualMachine,
VM_ID, 1L, 1L, "NO_SUCH_METRIC", AlertCondition.GT, 1.0,
AlertSeverity.LOW, null, false, 600);
when(ruleDao.listActive()).thenReturn(Arrays.asList(broken, vmCpuRule(VM_ID)));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(85.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
manager.evaluateRules();
verify(alertDao, times(1)).persist(any());
}
private ResourceAlertVO firedSecondsAgo(long seconds) {
ResourceAlertVO last = mock(ResourceAlertVO.class);
when(last.getAlertTimestamp()).thenReturn(new Date(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(seconds)));
return last;
}
@Test
public void testCooldownEqualToCheckIntervalFiresOnNextCheck() {
ResourceAlertVO last = firedSecondsAgo(59);
when(alertDao.findLastFiredForRule(1L, VM_ID)).thenReturn(last);
assertTrue(manager.canFire(1L, VM_ID, 60));
}
@Test
public void testCooldownStillBlocksWellBeforeItEnds() {
ResourceAlertVO last = firedSecondsAgo(50);
when(alertDao.findLastFiredForRule(1L, VM_ID)).thenReturn(last);
assertFalse(manager.canFire(1L, VM_ID, 60));
}
@Test
public void testDisabledRuleIsNotChecked() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
rule.setState(ResourceAlertRule.State.Disabled);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
manager.evaluateRules();
verify(statsCollector, never()).getRecentVmStats(anyLong());
verify(alertDao, never()).persist(any());
}
@Test
public void testDisabledRuleIsStillRemovedWhenResourceIsGone() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
rule.setState(ResourceAlertRule.State.Disabled);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
when(userVmDao.findById(VM_ID)).thenReturn(null);
manager.evaluateRules();
verify(ruleDao).remove(rule.getId());
}
private ResourceAlertRuleVO vmDiskReadIopsRule(double threshold) {
return new ResourceAlertRuleVO("io", ResourceAlertRule.ResourceType.VirtualMachine,
VM_ID, 1L, 1L, "DISK_READ_IOPS", AlertCondition.GT, threshold,
AlertSeverity.HIGH, null, false, 600);
}
private void stubVmDiskReadIos(HypervisorType hypervisor, double ios) {
UserVmVO vm = runningVm();
when(vm.getHypervisorType()).thenReturn(hypervisor);
when(userVmDao.findById(VM_ID)).thenReturn(vm);
VmStats stats = mock(VmStats.class);
when(stats.getDiskReadIOs()).thenReturn(ios);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
}
@Test
public void testKvmVmDiskIopsIsDividedBySampleTime() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(vmDiskReadIopsRule(500.0)));
stubVmDiskReadIos(HypervisorType.KVM, 60000.0);
when(statsCollector.getRecentVmStatsSampleMillis(VM_ID)).thenReturn(60000L);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(1000.0, alertCaptor.getValue().getMetricValue(), 0.001);
}
@Test
public void testKvmVmDiskIopsBelowThresholdOncePerSecond() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(vmDiskReadIopsRule(500.0)));
stubVmDiskReadIos(HypervisorType.KVM, 6000.0);
when(statsCollector.getRecentVmStatsSampleMillis(VM_ID)).thenReturn(60000L);
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testVmwareVmDiskIopsIsUsedAsReported() {
when(ruleDao.listActive()).thenReturn(Collections.singletonList(vmDiskReadIopsRule(500.0)));
stubVmDiskReadIos(HypervisorType.VMware, 600.0);
manager.evaluateRules();
verify(alertDao).persist(alertCaptor.capture());
assertEquals(600.0, alertCaptor.getValue().getMetricValue(), 0.001);
verify(statsCollector, never()).getRecentVmStatsSampleMillis(anyLong());
}
private void stubKvmVm() {
UserVmVO vm = runningVm();
when(vm.getHypervisorType()).thenReturn(HypervisorType.KVM);
when(userVmDao.findById(VM_ID)).thenReturn(vm);
}
@Test
public void testKvmVmRateUsesTimeSincePreviousSample() {
stubKvmVm();
when(statsCollector.getRecentVmStatsSampleMillis(VM_ID)).thenReturn(20000L);
assertEquals(300.0, manager.toVmRate(VM_ID, 6000.0), 0.001);
}
@Test
public void testKvmVmRateSkippedWithoutPreviousSample() {
stubKvmVm();
when(statsCollector.getRecentVmStatsSampleMillis(VM_ID)).thenReturn(null);
assertNull(manager.toVmRate(VM_ID, 6000.0));
}
private void stubKvmHost() {
HostVO host = mock(HostVO.class);
when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
when(hostDao.findById(HOST_ID)).thenReturn(host);
}
@Test
public void testKvmHostNetworkRateIsChangeBetweenReadings() {
stubKvmHost();
doReturn(1000L, 61000L, 121000L).when(manager).currentTimeMillis();
assertNull(manager.toHostNetworkRate(HOST_ID, "read", 50000.0));
assertEquals(100.0, manager.toHostNetworkRate(HOST_ID, "read", 56000.0), 0.001);
assertEquals(100.0, manager.toHostNetworkRate(HOST_ID, "read", 56000.0), 0.001);
assertEquals(50.0, manager.toHostNetworkRate(HOST_ID, "read", 59000.0), 0.001);
}
@Test
public void testKvmHostNetworkRateSkipsReadingAfterCounterReset() {
stubKvmHost();
doReturn(1000L, 61000L).when(manager).currentTimeMillis();
assertNull(manager.toHostNetworkRate(HOST_ID, "write", 50000.0));
assertNull(manager.toHostNetworkRate(HOST_ID, "write", 10.0));
}
@Test
public void testNonKvmHostNetworkIsUsedAsReported() {
HostVO host = mock(HostVO.class);
when(host.getHypervisorType()).thenReturn(HypervisorType.XenServer);
when(hostDao.findById(HOST_ID)).thenReturn(host);
assertEquals(2500.0, manager.toHostNetworkRate(HOST_ID, "read", 2500.0), 0.001);
}
@Test
public void testNoAlertWhenRuleDeletedDuringRun() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(95.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
doReturn(false).when(manager).isStillActive(any());
manager.evaluateRules();
verify(alertDao, never()).persist(any());
}
@Test
public void testAlertRemovedAndNotSentWhenRuleDeletedWhileSaving() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
VmStats stats = mock(VmStats.class);
when(stats.getCPUUtilization()).thenReturn(95.0);
when(statsCollector.getRecentVmStats(VM_ID)).thenReturn(stats);
doReturn(true).doReturn(false).when(manager).isStillActive(any());
manager.evaluateRules();
verify(alertDao).persist(any());
verify(alertDao).remove((Long) any());
verify(manager, never()).publishAlertEvent(anyLong(), anyString(), anyString());
}
@Test
public void testIsStillActiveRereadsRule() {
doCallRealMethod().when(manager).isStillActive(any());
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
ResourceAlertRuleVO disabled = vmCpuRule(VM_ID);
disabled.setState(ResourceAlertRule.State.Disabled);
when(ruleDao.findById(anyLong())).thenReturn(null);
assertFalse(manager.isStillActive(rule));
when(ruleDao.findById(anyLong())).thenReturn(disabled);
assertFalse(manager.isStillActive(rule));
when(ruleDao.findById(anyLong())).thenReturn(vmCpuRule(VM_ID));
assertTrue(manager.isStillActive(rule));
}
@Test
public void testRemoveExpiredAlertsKeepsAlertsInsideLongestCooldown() {
ResourceAlertRuleVO rule = vmCpuRule(VM_ID);
rule.setResetInterval((int) TimeUnit.DAYS.toSeconds(45));
when(ruleDao.listActive()).thenReturn(Collections.singletonList(rule));
long before = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(45);
manager.removeExpiredAlerts();
ArgumentCaptor<Date> cutoff = ArgumentCaptor.forClass(Date.class);
verify(alertDao).removeOlderThan(cutoff.capture());
assertTrue("cutoff should be about 45 days ago", Math.abs(cutoff.getValue().getTime() - before) < 60_000L);
}
}