@Test
  public void allocateTarget_BasicTest() throws Exception {
    final String volId = "vol-0000";
    final ISCSIVolumeInfo iscsiVolumeInfo = new ISCSIVolumeInfo();
    iscsiVolumeInfo.setVolumeId(volId);

    final TGTService tgtService = context.mock(TGTService.class);
    final BlockStorageUtilSvc blockStorageUtilSvc = context.mock(BlockStorageUtilSvc.class);
    context.checking(
        new Expectations() {
          {
            oneOf(tgtService)
                .targetExists(
                    volId, 1, null, DirectStorageInfo.getStorageInfo().getTimeoutInMillis());
            will(returnValue(Boolean.FALSE));
          }
        });

    ISCSIMetaInfo metaInfo = new ISCSIMetaInfo(StorageProperties.NAME);
    metaInfo.setStoreNumber(new Integer(1));
    metaInfo.setTid(new Integer(1));
    metaInfo.setStorePrefix("foo:");
    metaInfo.setStoreUser("unittestuser0");

    try (TransactionResource tran = Entities.transactionFor(ISCSIMetaInfo.class)) {
      metaInfo = Entities.persist(metaInfo);
      tran.commit();
    }

    ISCSIManager iscsiManager = new ISCSIManager(tgtService, blockStorageUtilSvc);
    iscsiManager.allocateTarget(iscsiVolumeInfo);

    List<ISCSIMetaInfo> remaining;
    try (TransactionResource tran = Entities.transactionFor(ISCSIMetaInfo.class)) {
      remaining = Entities.query(new ISCSIMetaInfo());
      tran.commit();
    }

    assertTrue(
        "expected to have a result set querying the eucalyptus_storage persistence context",
        remaining != null);
    ISCSIMetaInfo retrieved = remaining.get(0);
    assertTrue(
        "expected store number to be 2 but was " + retrieved.getStoreNumber(),
        retrieved.getStoreNumber().intValue() == 2);
    assertTrue("expected tid to be 2", retrieved.getTid().intValue() == 2);
    String storeName = "foo:" + StorageProperties.NAME + ":store2";
    assertTrue(
        "expected iscsiVolumeInfo store name to be " + storeName,
        storeName.equals(iscsiVolumeInfo.getStoreName()));
    assertTrue(
        "expected iscsiVolumeInfo store user to be unittestuser0 but was "
            + iscsiVolumeInfo.getStoreUser(),
        "unittestuser0".equals(iscsiVolumeInfo.getStoreUser()));
    assertTrue(
        "expected iscsiVolumeInfo tid to be 1 and lun to be 1",
        iscsiVolumeInfo.getTid().intValue() == 1 && iscsiVolumeInfo.getLun().intValue() == 1);
  }
  @Test
  public void cleanup_BasicTest() throws Exception {
    final String volId = "vol-0000";
    final ISCSIVolumeInfo iscsiVolumeInfo = new ISCSIVolumeInfo();
    iscsiVolumeInfo.setVolumeId(volId);
    iscsiVolumeInfo.setTid(new Integer(1));
    iscsiVolumeInfo.setVgName("foo");
    iscsiVolumeInfo.setLvName("foo");
    iscsiVolumeInfo.setLun(new Integer(1));

    final TGTService tgtService = context.mock(TGTService.class);
    final BlockStorageUtilSvc blockStorageUtilSvc = context.mock(BlockStorageUtilSvc.class);
    final Long timeout = DirectStorageInfo.getStorageInfo().getTimeoutInMillis();
    context.checking(
        new Expectations() {
          {
            oneOf(tgtService).targetExists(volId, 1, iscsiVolumeInfo.getAbsoluteLVPath(), timeout);
            will(returnValue(Boolean.TRUE));
            oneOf(tgtService).targetExists(volId, 1, iscsiVolumeInfo.getAbsoluteLVPath(), timeout);
            will(returnValue(Boolean.TRUE));
            oneOf(tgtService).unbindTarget(volId, 1, timeout);
            oneOf(tgtService).deleteLun(volId, 1, 1, timeout);
            oneOf(tgtService).deleteTarget(volId, 1, timeout, false);
            oneOf(tgtService).targetExists(volId, 1, null, timeout);
            will(returnValue(Boolean.FALSE));
            oneOf(tgtService).targetExists(volId, 1, iscsiVolumeInfo.getAbsoluteLVPath(), timeout);
            will(returnValue(Boolean.FALSE));
          }
        });

    try (TransactionResource tran = Entities.transactionFor(ISCSIVolumeInfo.class)) {
      Entities.persist(iscsiVolumeInfo);
      tran.commit();
    }

    ISCSIManager iscsiManager = new ISCSIManager(tgtService, blockStorageUtilSvc);
    iscsiManager.cleanup(iscsiVolumeInfo);

    List<ISCSIVolumeInfo> remaining;
    try (TransactionResource tran = Entities.transactionFor(ISCSIVolumeInfo.class)) {
      remaining = Entities.query(new ISCSIVolumeInfo());
      tran.commit();
    }

    assertTrue(
        "expected to have a result set querying the eucalyptus_storage persistence context",
        remaining != null);
    ISCSIVolumeInfo retrieved = remaining.get(0);
    assertTrue(
        "expected store name to be null but was " + retrieved.getStoreName(),
        retrieved.getStoreName() == null);
    assertTrue("expected tid to be -1", retrieved.getTid().intValue() == -1);
    assertTrue(
        "expected iscsiVolumeInfo lun to be -1 but was " + retrieved.getLun().intValue(),
        retrieved.getLun().intValue() == -1);
  }