Esempio n. 1
0
  @Test(enabled = true)
  public void testRegisterTemplate() throws Exception {
    Zone zone = Iterables.getFirst(client.getZoneClient().listZones(), null);
    assertNotNull(zone);
    Iterable<Network> networks =
        client
            .getNetworkClient()
            .listNetworks(ListNetworksOptions.Builder.zoneId(zone.getId()).isDefault(true));
    networks =
        Iterables.filter(
            networks,
            new Predicate<Network>() {
              @Override
              public boolean apply(@Nullable Network network) {
                return network != null && network.getState().equals("Implemented");
              }
            });
    assertEquals(Iterables.size(networks), 1);
    Network network = Iterables.getOnlyElement(networks, null);
    assertNotNull(network);
    Set<OSType> osTypes = client.getGuestOSClient().listOSTypes();
    OSType osType = Iterables.getFirst(osTypes, null);

    // Register a template
    RegisterTemplateOptions options = RegisterTemplateOptions.Builder.bits(32).isExtractable(true);
    TemplateMetadata templateMetadata =
        TemplateMetadata.builder()
            .name(prefix + "-registerTemplate")
            .osTypeId(osType.getId())
            .displayText("jclouds live testRegisterTemplate")
            .build();
    Set<Template> templates =
        client
            .getTemplateClient()
            .registerTemplate(
                templateMetadata, "VHD", "XenServer", IMPORT_VHD_URL, zone.getId(), options);
    registeredTemplate = Iterables.getOnlyElement(templates, null);
    assertNotNull(registeredTemplate);

    // Ensure it is available
    final long zoneId = zone.getId();
    Predicate<Template> templateReadyPredicate =
        new Predicate<Template>() {
          @Override
          public boolean apply(@Nullable Template template) {
            if (template == null) return false;
            Template t2 = client.getTemplateClient().getTemplateInZone(template.getId(), zoneId);
            Logger.CONSOLE.info("%s", t2.getStatus());
            return "Download Complete".equals(t2.getStatus());
          }
        };
    assertTrue(
        new RetryablePredicate<Template>(templateReadyPredicate, 60000).apply(registeredTemplate));

    // Create a VM that uses this template
    vmForRegistration =
        VirtualMachineClientLiveTest.createVirtualMachineInNetwork(
            network, registeredTemplate.getId(), client, jobComplete, virtualMachineRunning);
    assertNotNull(vmForRegistration);
  }
Esempio n. 2
0
  @Test(enabled = true)
  public void testCreateTemplate() throws Exception {
    Zone zone = Iterables.getFirst(client.getZoneClient().listZones(), null);
    assertNotNull(zone);
    Iterable<Network> networks =
        client
            .getNetworkClient()
            .listNetworks(ListNetworksOptions.Builder.zoneId(zone.getId()).isDefault(true));
    networks =
        Iterables.filter(
            networks,
            new Predicate<Network>() {
              @Override
              public boolean apply(@Nullable Network network) {
                return network != null && network.getState().equals("Implemented");
              }
            });
    assertEquals(Iterables.size(networks), 1);
    Network network = Iterables.getOnlyElement(networks, null);
    assertNotNull(network);

    // Create a VM and stop it
    Long templateId = (imageId != null && !"".equals(imageId)) ? new Long(imageId) : null;
    vmForCreation =
        VirtualMachineClientLiveTest.createVirtualMachineInNetwork(
            network, templateId, client, jobComplete, virtualMachineRunning);
    assertTrue(
        jobComplete.apply(
            client.getVirtualMachineClient().stopVirtualMachine(vmForCreation.getId())),
        vmForCreation.toString());

    // Work out the VM's volume
    Set<Volume> volumes =
        client
            .getVolumeClient()
            .listVolumes(ListVolumesOptions.Builder.virtualMachineId(vmForCreation.getId()));
    assertEquals(volumes.size(), 1);
    Volume volume = Iterables.getOnlyElement(volumes);

    // Create a template
    CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId());
    AsyncCreateResponse response =
        client
            .getTemplateClient()
            .createTemplate(
                TemplateMetadata.builder()
                    .name(prefix + "-createTemplate")
                    .osTypeId(vmForCreation.getGuestOSId())
                    .displayText("jclouds live testCreateTemplate")
                    .build(),
                options);
    assertTrue(jobComplete.apply(response.getJobId()), vmForCreation.toString());
    createdTemplate =
        client.getTemplateClient().getTemplateInZone(response.getId(), vmForCreation.getZoneId());

    // Assertions
    assertNotNull(createdTemplate);
  }