use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImplTest method runDeleteGlobalLoadBalancerRuleTestWithLbRules.
void runDeleteGlobalLoadBalancerRuleTestWithLbRules() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runDeleteGlobalLoadBalancerRuleTestWithLbRules");
GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
DeleteGlobalLoadBalancerRuleCmd deleteCmd = new DeleteGlobalLoadBalancerRuleCmdExtn();
Class<?> _class = deleteCmd.getClass().getSuperclass();
Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
Field gslbRuleId = _class.getDeclaredField("id");
gslbRuleId.setAccessible(true);
gslbRuleId.set(deleteCmd, new Long(1));
GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active);
when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
GlobalLoadBalancerLbRuleMapVO gslbLmMap = new GlobalLoadBalancerLbRuleMapVO(1, 1, 1);
List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = new ArrayList<GlobalLoadBalancerLbRuleMapVO>();
gslbLbMapVos.add(gslbLmMap);
when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(gslbLbMapVos);
try {
gslbServiceImpl.deleteGlobalLoadBalancerRule(deleteCmd);
Assert.assertTrue(gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke);
Assert.assertTrue(gslbLmMap.isRevoke() == true);
} catch (Exception e) {
s_logger.info("exception in testing runDeleteGlobalLoadBalancerRuleTestWithLbRules. " + e.toString());
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImplTest method runDeleteGlobalLoadBalancerRuleTestWithNoLbRules.
void runDeleteGlobalLoadBalancerRuleTestWithNoLbRules() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runDeleteGlobalLoadBalancerRuleTestWithNoLbRules");
GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
DeleteGlobalLoadBalancerRuleCmd deleteCmd = new DeleteGlobalLoadBalancerRuleCmdExtn();
Class<?> _class = deleteCmd.getClass().getSuperclass();
Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
Field gslbRuleId = _class.getDeclaredField("id");
gslbRuleId.setAccessible(true);
gslbRuleId.set(deleteCmd, new Long(1));
GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active);
when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
GlobalLoadBalancerLbRuleMapVO gslbLbMap = new GlobalLoadBalancerLbRuleMapVO();
gslbLbMap.setGslbLoadBalancerId(1);
gslbLbMap.setLoadBalancerId(1);
List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapList = new ArrayList<GlobalLoadBalancerLbRuleMapVO>();
gslbLbMapList.add(gslbLbMap);
when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(gslbLbMapList);
try {
gslbServiceImpl.deleteGlobalLoadBalancerRule(deleteCmd);
Assert.assertTrue(gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke);
} catch (Exception e) {
s_logger.info("exception in testing runDeleteGlobalLoadBalancerRuleTestWithNoLbRules. " + e.toString());
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImplTest method runAssignToGlobalLoadBalancerRuleTestSameZoneLb.
void runAssignToGlobalLoadBalancerRuleTestSameZoneLb() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb");
GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn();
Class<?> _class = assignCmd.getClass().getSuperclass();
Account account = new AccountVO("testaccount", 3, "networkdomain", (short) 0, UUID.randomUUID().toString());
when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
Field gslbRuleId = _class.getDeclaredField("id");
gslbRuleId.setAccessible(true);
gslbRuleId.set(assignCmd, new Long(1));
GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 3, 1, GlobalLoadBalancerRule.State.Active);
when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
LoadBalancerVO lbRule1 = new LoadBalancerVO();
lbRule1.setState(FirewallRule.State.Active);
Field networkIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
Field accountIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
Field domainIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
networkIdField1.setAccessible(true);
accountIdField1.setAccessible(true);
domainIdField1.setAccessible(true);
networkIdField1.set(lbRule1, new Long(1));
accountIdField1.set(lbRule1, new Long(3));
domainIdField1.set(lbRule1, new Long(1));
Field idField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
idField1.setAccessible(true);
idField1.set(lbRule1, new Long(1));
LoadBalancerVO lbRule2 = new LoadBalancerVO();
lbRule2.setState(FirewallRule.State.Active);
Field networkIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
Field accountIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
Field domainIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
networkIdField2.setAccessible(true);
accountIdField2.setAccessible(true);
domainIdField2.setAccessible(true);
networkIdField2.set(lbRule2, new Long(1));
accountIdField2.set(lbRule2, new Long(3));
domainIdField2.set(lbRule2, new Long(1));
Field idField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
idField2.setAccessible(true);
idField2.set(lbRule2, new Long(2));
when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule1);
when(gslbServiceImpl._lbDao.findById(new Long(2))).thenReturn(lbRule2);
Field lbRules = _class.getDeclaredField("loadBalancerRulesIds");
lbRules.setAccessible(true);
List<Long> lbRuleIds = new ArrayList<Long>();
lbRuleIds.add(new Long(1));
lbRuleIds.add(new Long(2));
lbRules.set(assignCmd, lbRuleIds);
NetworkVO networkVo = new NetworkVO();
Field dcID = NetworkVO.class.getDeclaredField("dataCenterId");
dcID.setAccessible(true);
dcID.set(networkVo, new Long(1));
when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo);
try {
gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd);
} catch (InvalidParameterValueException e) {
s_logger.info(e.getMessage());
Assert.assertTrue(e.getMessage().contains("Load balancer rule specified should be in unique zone"));
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class SnapshotSchedulerImpl method scheduleNextSnapshotJob.
@Override
@DB
public Date scheduleNextSnapshotJob(final SnapshotPolicyVO policy) {
if (policy == null) {
return null;
}
// If display attribute is false then remove schedules if any and return.
if (!policy.isDisplay()) {
removeSchedule(policy.getVolumeId(), policy.getId());
return null;
}
final long policyId = policy.getId();
if (policyId == Snapshot.MANUAL_POLICY_ID) {
return null;
}
final Date nextSnapshotTimestamp = getNextScheduledTime(policyId, _currentTimestamp);
SnapshotScheduleVO spstSchedVO = _snapshotScheduleDao.findOneByVolumePolicy(policy.getVolumeId(), policy.getId());
if (spstSchedVO == null) {
spstSchedVO = new SnapshotScheduleVO(policy.getVolumeId(), policyId, nextSnapshotTimestamp);
_snapshotScheduleDao.persist(spstSchedVO);
} else {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
try {
spstSchedVO = _snapshotScheduleDao.acquireInLockTable(spstSchedVO.getId());
spstSchedVO.setPolicyId(policyId);
spstSchedVO.setScheduledTimestamp(nextSnapshotTimestamp);
spstSchedVO.setAsyncJobId(null);
spstSchedVO.setSnapshotId(null);
_snapshotScheduleDao.update(spstSchedVO.getId(), spstSchedVO);
txn.commit();
} finally {
if (spstSchedVO != null) {
_snapshotScheduleDao.releaseFromLockTable(spstSchedVO.getId());
}
txn.close();
}
}
return nextSnapshotTimestamp;
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class CreatePrivateNetworkTest method createInvalidlyHostedPrivateNetwork.
@Test
@DB
public void createInvalidlyHostedPrivateNetwork() {
TransactionLegacy __txn;
__txn = TransactionLegacy.open("createInvalidlyHostedPrivateNetworkTest");
/* Network nw; */
try {
/* nw = */
networkService.createPrivateNetwork("bla", "fake", 1L, "vlan:1", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, true, 1L);
/* nw = */
networkService.createPrivateNetwork("bla", "fake", 1L, "lswitch:3", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, false, 1L);
boolean invalid = false;
boolean unsupported = false;
try {
/* nw = */
networkService.createPrivateNetwork("bla", "fake", 1, "bla:2", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, true, 1L);
} catch (CloudRuntimeException e) {
Assert.assertEquals("unexpected parameter exception", "string 'bla:2' has an unknown BroadcastDomainType.", e.getMessage());
invalid = true;
}
try {
/* nw = */
networkService.createPrivateNetwork("bla", "fake", 1, "mido://4", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, false, 1L);
} catch (InvalidParameterValueException e) {
Assert.assertEquals("unexpected parameter exception", "unsupported type of broadcastUri specified: mido://4", e.getMessage());
unsupported = true;
}
Assert.assertEquals("'bla' should not be accepted as scheme", true, invalid);
Assert.assertEquals("'mido' should not yet be supported as scheme", true, unsupported);
} catch (ResourceAllocationException e) {
s_logger.error("no resources", e);
fail("no resources");
} catch (ConcurrentOperationException e) {
s_logger.error("another one is in the way", e);
fail("another one is in the way");
} catch (InsufficientCapacityException e) {
s_logger.error("no capacity", e);
fail("no capacity");
} finally {
__txn.close("createInvalidlyHostedPrivateNetworkTest");
}
}
Aggregations