示例#1
0
  private void checkNic(VmInstanceVO vm, List<DhcpInfo> info) {
    VmNicVO nic = vm.getVmNics().iterator().next();
    DhcpInfo target = null;
    for (DhcpInfo i : info) {
      if (i.mac.equals(nic.getMac())) {
        target = i;
        break;
      }
    }

    Assert.assertNotNull(target);
    Assert.assertEquals(nic.getIp(), target.ip);
    Assert.assertEquals(nic.getNetmask(), target.netmask);
    Assert.assertEquals(nic.getGateway(), target.gateway);
    Assert.assertEquals(true, target.isDefaultL3Network);
    Assert.assertEquals(
        VmSystemTags.HOSTNAME.getTokenByResourceUuid(vm.getUuid(), VmSystemTags.HOSTNAME_TOKEN),
        target.hostname);
    L3NetworkVO l3 = dbf.findByUuid(nic.getL3NetworkUuid(), L3NetworkVO.class);
    Assert.assertEquals(l3.getDnsDomain(), target.dnsDomain);
    Assert.assertNotNull(target.dns);
    List<String> dns =
        CollectionUtils.transformToList(
            l3.getDns(),
            new Function<String, L3NetworkDnsVO>() {
              @Override
              public String call(L3NetworkDnsVO arg) {
                return arg.getDns();
              }
            });
    Assert.assertTrue(dns.containsAll(target.dns));
    Assert.assertTrue(target.dns.containsAll(dns));
  }
示例#2
0
  @Test
  public void test() throws ApiSenderException, InterruptedException {
    VmInstanceInventory vm1 = deployer.vms.get("TestVm1");
    VmInstanceInventory vm2 = deployer.vms.get("TestVm2");
    api.setVmHaLevel(vm1.getUuid(), VmHaLevel.NeverStop, null);
    api.setVmHaLevel(vm2.getUuid(), VmHaLevel.OnHostFailure, null);

    api.maintainHost(vm1.getHostUuid());

    HostInventory host2 = deployer.hosts.get("host2");
    VmInstanceVO vmvo1 = dbf.findByUuid(vm1.getUuid(), VmInstanceVO.class);
    VmInstanceVO vmvo2 = dbf.findByUuid(vm2.getUuid(), VmInstanceVO.class);
    Assert.assertEquals(VmInstanceState.Running, vmvo1.getState());
    Assert.assertEquals(host2.getUuid(), vmvo1.getHostUuid());
    Assert.assertEquals(VmInstanceState.Stopped, vmvo2.getState());
  }
示例#3
0
  @Test
  public void test() throws ApiSenderException, InterruptedException {
    final L3NetworkInventory l3 = deployer.l3Networks.get("TestL3Network4");
    VmInstanceInventory vm = deployer.vms.get("TestVm");

    APIGetIpAddressCapacityReply ipcap = api.getIpAddressCapacityByAll();
    long avail1 = ipcap.getAvailableCapacity();

    int num = 50;
    final CountDownLatch latch = new CountDownLatch(num);
    final String vmUuid = vm.getUuid();

    class Ret {
      int count;
    }

    final Ret ret = new Ret();
    for (int i = 0; i < num; i++) {
      new Runnable() {
        @Override
        @AsyncThread
        public void run() {
          try {
            VmInstanceInventory v = api.attachNic(vmUuid, l3.getUuid());
            ret.count += 1;
          } catch (Exception e) {
            logger.warn(e.getMessage(), e);
          } finally {
            latch.countDown();
          }
        }
      }.run();
    }

    latch.await(120, TimeUnit.SECONDS);

    VmInstanceVO vmvo = dbf.findByUuid(vmUuid, VmInstanceVO.class);
    final CountDownLatch latch1 = new CountDownLatch(vmvo.getVmNics().size());
    for (final VmNicVO nic : vmvo.getVmNics()) {
      new Runnable() {
        @Override
        @AsyncThread
        public void run() {
          try {
            api.detachNic(nic.getUuid());
          } catch (Exception e) {
            logger.warn(e.getMessage(), e);
          } finally {
            latch1.countDown();
          }
        }
      }.run();
    }

    latch1.await(120, TimeUnit.SECONDS);

    TimeUnit.SECONDS.sleep(5);
    ipcap = api.getIpAddressCapacityByAll();
    long avail2 = ipcap.getAvailableCapacity();

    Assert.assertEquals(avail1, avail2 - 3);
  }
示例#4
0
  private List<VmInstanceInventory> vmFromDeleteAction(CascadeAction action) {
    List<VmInstanceInventory> ret = null;
    if (HostVO.class.getSimpleName().equals(action.getParentIssuer())) {
      List<HostInventory> hosts = action.getParentIssuerContext();
      List<String> huuids =
          CollectionUtils.transformToList(
              hosts,
              new Function<String, HostInventory>() {
                @Override
                public String call(HostInventory arg) {
                  return arg.getUuid();
                }
              });

      Map<String, VmInstanceVO> vmvos = new HashMap<String, VmInstanceVO>();
      SimpleQuery<VmInstanceVO> q = dbf.createQuery(VmInstanceVO.class);
      q.add(VmInstanceVO_.hostUuid, SimpleQuery.Op.IN, huuids);
      q.add(VmInstanceVO_.type, Op.EQ, VmInstanceConstant.USER_VM_TYPE);
      List<VmInstanceVO> lst = q.list();
      for (VmInstanceVO vo : lst) {
        vmvos.put(vo.getUuid(), vo);
      }

      if (ClusterVO.class.getSimpleName().equals(action.getRootIssuer())) {
        List<ClusterInventory> clusters = action.getRootIssuerContext();
        List<String> clusterUuids =
            CollectionUtils.transformToList(
                clusters,
                new Function<String, ClusterInventory>() {
                  @Override
                  public String call(ClusterInventory arg) {
                    return arg.getUuid();
                  }
                });
        q = dbf.createQuery(VmInstanceVO.class);
        q.add(VmInstanceVO_.clusterUuid, Op.IN, clusterUuids);
        q.add(VmInstanceVO_.type, Op.EQ, VmInstanceConstant.USER_VM_TYPE);
        lst = q.list();
        for (VmInstanceVO vo : lst) {
          vmvos.put(vo.getUuid(), vo);
        }
      } else if (ZoneVO.class.getSimpleName().equals(action.getRootIssuer())) {
        List<ZoneInventory> zones = action.getRootIssuerContext();
        List<String> zoneUuids =
            CollectionUtils.transformToList(
                zones,
                new Function<String, ZoneInventory>() {
                  @Override
                  public String call(ZoneInventory arg) {
                    return arg.getUuid();
                  }
                });
        q = dbf.createQuery(VmInstanceVO.class);
        q.add(VmInstanceVO_.zoneUuid, Op.IN, zoneUuids);
        q.add(VmInstanceVO_.type, Op.EQ, VmInstanceConstant.USER_VM_TYPE);
        lst = q.list();
        for (VmInstanceVO vo : lst) {
          vmvos.put(vo.getUuid(), vo);
        }
      }

      if (!vmvos.isEmpty()) {
        ret = VmInstanceInventory.valueOf(vmvos.values());
      }
    } else if (NAME.equals(action.getParentIssuer())) {
      return action.getParentIssuerContext();
    } else if (PrimaryStorageVO.class.getSimpleName().equals(action.getParentIssuer())) {
      final List<String> pruuids =
          CollectionUtils.transformToList(
              (List<PrimaryStorageInventory>) action.getParentIssuerContext(),
              new Function<String, PrimaryStorageInventory>() {
                @Override
                public String call(PrimaryStorageInventory arg) {
                  return arg.getUuid();
                }
              });

      List<VmInstanceVO> vmvos =
          new Callable<List<VmInstanceVO>>() {
            @Override
            @Transactional(readOnly = true)
            public List<VmInstanceVO> call() {
              String sql =
                  "select vm from VmInstanceVO vm, VolumeVO vol, PrimaryStorageVO pr where vm.type = :vmType and vm.uuid = vol.vmInstanceUuid"
                      + " and vol.primaryStorageUuid = pr.uuid and vol.type = :volType and pr.uuid in (:uuids) group by vm.uuid";
              TypedQuery<VmInstanceVO> q =
                  dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
              q.setParameter("vmType", VmInstanceConstant.USER_VM_TYPE);
              q.setParameter("uuids", pruuids);
              q.setParameter("volType", VolumeType.Root);
              return q.getResultList();
            }
          }.call();

      if (!vmvos.isEmpty()) {
        ret = VmInstanceInventory.valueOf(vmvos);
      }
    } else if (L3NetworkVO.class.getSimpleName().equals(action.getParentIssuer())) {
      final List<String> l3uuids =
          CollectionUtils.transformToList(
              (List<L3NetworkInventory>) action.getParentIssuerContext(),
              new Function<String, L3NetworkInventory>() {
                @Override
                public String call(L3NetworkInventory arg) {
                  return arg.getUuid();
                }
              });

      List<VmInstanceVO> vmvos =
          new Callable<List<VmInstanceVO>>() {
            @Override
            @Transactional(readOnly = true)
            public List<VmInstanceVO> call() {
              String sql =
                  "select vm from VmInstanceVO vm, L3NetworkVO l3, VmNicVO nic where vm.type = :vmType and vm.uuid = nic.vmInstanceUuid and vm.state not in (:vmStates)"
                      + " and nic.l3NetworkUuid = l3.uuid and l3.uuid in (:uuids) group by vm.uuid";
              TypedQuery<VmInstanceVO> q =
                  dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
              q.setParameter("vmType", VmInstanceConstant.USER_VM_TYPE);
              q.setParameter(
                  "vmStates", Arrays.asList(VmInstanceState.Stopped, VmInstanceState.Stopping));
              q.setParameter("uuids", l3uuids);
              return q.getResultList();
            }
          }.call();

      if (!vmvos.isEmpty()) {
        ret = VmInstanceInventory.valueOf(vmvos);
      }
    } else if (IpRangeVO.class.getSimpleName().equals(action.getParentIssuer())) {
      final List<String> ipruuids =
          CollectionUtils.transformToList(
              (List<IpRangeInventory>) action.getParentIssuerContext(),
              new Function<String, IpRangeInventory>() {
                @Override
                public String call(IpRangeInventory arg) {
                  return arg.getUuid();
                }
              });

      List<VmInstanceVO> vmvos =
          new Callable<List<VmInstanceVO>>() {
            @Override
            @Transactional(readOnly = true)
            public List<VmInstanceVO> call() {
              String sql =
                  "select vm from VmInstanceVO vm, VmNicVO nic, UsedIpVO ip, IpRangeVO ipr where vm.type = :vmType and vm.uuid = nic.vmInstanceUuid and vm.state not in (:vmStates)"
                      + " and nic.usedIpUuid = ip.uuid and ip.ipRangeUuid = ipr.uuid and ipr.uuid in (:uuids) group by vm.uuid";
              TypedQuery<VmInstanceVO> q =
                  dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
              q.setParameter("vmType", VmInstanceConstant.USER_VM_TYPE);
              q.setParameter(
                  "vmStates", Arrays.asList(VmInstanceState.Stopped, VmInstanceState.Stopping));
              q.setParameter("uuids", ipruuids);
              return q.getResultList();
            }
          }.call();

      if (!vmvos.isEmpty()) {
        ret = VmInstanceInventory.valueOf(vmvos);
      }
    } else if (AccountVO.class.getSimpleName().equals(action.getParentIssuer())) {
      final List<String> auuids =
          CollectionUtils.transformToList(
              (List<AccountInventory>) action.getParentIssuerContext(),
              new Function<String, AccountInventory>() {
                @Override
                public String call(AccountInventory arg) {
                  return arg.getUuid();
                }
              });

      List<VmInstanceVO> vmvos =
          new Callable<List<VmInstanceVO>>() {
            @Override
            @Transactional(readOnly = true)
            public List<VmInstanceVO> call() {
              String sql =
                  "select d from VmInstanceVO d, AccountResourceRefVO r where d.uuid = r.resourceUuid and"
                      + " r.resourceType = :rtype and r.accountUuid in (:auuids) group by d.uuid";
              TypedQuery<VmInstanceVO> q =
                  dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
              q.setParameter("rtype", VmInstanceVO.class.getSimpleName());
              q.setParameter("auuids", auuids);
              return q.getResultList();
            }
          }.call();

      if (!vmvos.isEmpty()) {
        ret = VmInstanceInventory.valueOf(vmvos);
      }
    }

    return ret;
  }