protected void cleanDb() {
   if (volumeId != null) {
     volumeDao.remove(volumeId);
     volumeId = null;
   }
   if (diskOfferingId != null) {
     diskOfferingDao.remove(diskOfferingId);
     diskOfferingId = null;
   }
   if (storagePoolId != null) {
     storagePoolDao.remove(storagePoolId);
     storagePoolId = null;
   }
   if (clusterId != null) {
     clusterDao.remove(clusterId);
     clusterId = null;
   }
   if (podId != null) {
     podDao.remove(podId);
     podId = null;
   }
   if (dcId != null) {
     dcDao.remove(dcId);
     dcId = null;
   }
 }
  @Test
  public void testLocalStorageAllocator() {
    try {
      createDb();

      StoragePoolVO pool = storagePoolDao.findById(storagePoolId);
      pool.setScope(ScopeType.HOST);
      storagePoolDao.update(pool.getId(), pool);

      DiskOfferingVO diskOff = diskOfferingDao.findById(diskOfferingId);
      diskOff.setUseLocalStorage(true);
      diskOfferingDao.update(diskOfferingId, diskOff);

      DiskProfile profile = new DiskProfile(volume, diskOff, HypervisorType.XenServer);
      VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
      Mockito.when(
              storageMgr.storagePoolHasEnoughSpace(
                  Mockito.anyListOf(Volume.class), Mockito.any(StoragePool.class)))
          .thenReturn(true);
      DeploymentPlan plan = new DataCenterDeployment(dcId, podId, clusterId, null, null, null);
      int foundAcct = 0;
      for (StoragePoolAllocator allocator : allocators) {
        List<StoragePool> pools =
            allocator.allocateToPool(profile, vmProfile, plan, new ExcludeList(), 1);
        if (!pools.isEmpty()) {
          Assert.assertEquals(pools.get(0).getId(), storage.getId());
          foundAcct++;
        }
      }

      if (foundAcct > 1 || foundAcct == 0) {
        Assert.fail();
      }
    } catch (Exception e) {
      cleanDb();
      Assert.fail();
    }
  }
  @Test
  public void testClusterAllocatorWithTags() {
    try {
      createDb();
      StoragePoolDetailVO detailVO = new StoragePoolDetailVO(this.storagePoolId, "high", "true");
      poolDetailsDao.persist(detailVO);
      DiskOfferingVO diskOff = this.diskOfferingDao.findById(diskOffering.getId());
      List<String> tags = new ArrayList<String>();
      tags.add("high");
      diskOff.setTagsArray(tags);
      diskOfferingDao.update(diskOff.getId(), diskOff);

      DiskProfile profile = new DiskProfile(volume, diskOff, HypervisorType.XenServer);
      VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
      Mockito.when(
              storageMgr.storagePoolHasEnoughSpace(
                  Mockito.anyListOf(Volume.class), Mockito.any(StoragePool.class)))
          .thenReturn(true);
      DeploymentPlan plan = new DataCenterDeployment(dcId, podId, clusterId, null, null, null);
      int foundAcct = 0;
      for (StoragePoolAllocator allocator : allocators) {
        List<StoragePool> pools =
            allocator.allocateToPool(profile, vmProfile, plan, new ExcludeList(), 1);
        if (!pools.isEmpty()) {
          Assert.assertEquals(pools.get(0).getId(), storage.getId());
          foundAcct++;
        }
      }

      if (foundAcct > 1 || foundAcct == 0) {
        Assert.fail();
      }
    } catch (Exception e) {
      cleanDb();
      Assert.fail();
    }
  }
  protected void createDb() {
    DataCenterVO dc =
        new DataCenterVO(
            UUID.randomUUID().toString(),
            "test",
            "8.8.8.8",
            null,
            "10.0.0.1",
            null,
            "10.0.0.1/24",
            null,
            null,
            NetworkType.Basic,
            null,
            null,
            true,
            true,
            null,
            null);
    dc = dcDao.persist(dc);
    dcId = dc.getId();

    HostPodVO pod =
        new HostPodVO(UUID.randomUUID().toString(), dc.getId(), "255.255.255.255", "", 8, "test");
    pod = podDao.persist(pod);
    podId = pod.getId();

    ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
    cluster.setHypervisorType(HypervisorType.XenServer.toString());
    cluster.setClusterType(ClusterType.CloudManaged);
    cluster.setManagedState(ManagedState.Managed);
    cluster = clusterDao.persist(cluster);
    clusterId = cluster.getId();

    DataStoreProvider provider =
        providerMgr.getDataStoreProvider("ancient primary data store provider");
    storage = new StoragePoolVO();
    storage.setDataCenterId(dcId);
    storage.setPodId(podId);
    storage.setPoolType(StoragePoolType.NetworkFilesystem);
    storage.setClusterId(clusterId);
    storage.setStatus(StoragePoolStatus.Up);
    storage.setScope(ScopeType.CLUSTER);
    storage.setAvailableBytes(1000);
    storage.setCapacityBytes(20000);
    storage.setHostAddress(UUID.randomUUID().toString());
    storage.setPath(UUID.randomUUID().toString());
    storage.setStorageProviderName(provider.getName());
    storage = storagePoolDao.persist(storage);
    storagePoolId = storage.getId();

    storageMgr.createCapacityEntry(storage.getId());

    diskOffering = new DiskOfferingVO();
    diskOffering.setDiskSize(500);
    diskOffering.setName("test-disk");
    diskOffering.setSystemUse(false);
    diskOffering.setUseLocalStorage(false);
    diskOffering.setCustomized(false);
    diskOffering.setRecreatable(false);
    diskOffering = diskOfferingDao.persist(diskOffering);
    diskOfferingId = diskOffering.getId();

    volume =
        new VolumeVO(
            Volume.Type.ROOT,
            "volume",
            dcId,
            1,
            1,
            diskOffering.getId(),
            diskOffering.getDiskSize());
    volume = volumeDao.persist(volume);
    volumeId = volume.getId();
  }