@Test(enabled = true /* , dependsOnMethods = "testTemplateMatch" */)
 public void testTemplateOptions() throws Exception {
   TemplateOptions options = new TemplateOptions().withMetadata();
   Template t = client.templateBuilder().smallest().options(options).build();
   assert t.getOptions().isIncludeMetadata()
       : "The metadata option should be 'true' " + "for the created template";
 }
  @Test(enabled = true, dependsOnMethods = "testTemplateMatch")
  public void testCreateTwoNodesWithRunScript() throws Exception {
    try {
      client.destroyNodesMatching(NodePredicates.withTag(tag));
    } catch (HttpResponseException e) {
      // TODO hosting.com throws 400 when we try to delete a vApp
    } catch (NoSuchElementException e) {

    }
    template = buildTemplate(client.templateBuilder());

    template
        .getOptions()
        .installPrivateKey(keyPair.get("private"))
        .authorizePublicKey(keyPair.get("public"))
        .runScript(buildScript(template.getImage().getOsFamily()).getBytes());
    try {
      nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template));
    } catch (RunNodesException e) {
      nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }
    assertEquals(nodes.size(), 2);
    checkNodes(nodes, tag);
    NodeMetadata node1 = nodes.first();
    NodeMetadata node2 = nodes.last();
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertLocationSameOrChild(node1.getLocation(), template.getLocation());
    assertLocationSameOrChild(node2.getLocation(), template.getLocation());

    assertEquals(node1.getImage(), template.getImage());
    assertEquals(node2.getImage(), template.getImage());
  }
 @Override
 public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(
     String group, String name, Template template, Map<String, Credentials> credentialStore) {
   NodeMetadataBuilder builder = new NodeMetadataBuilder();
   String id = idProvider.get() + "";
   builder.ids(id);
   builder.name(name);
   // using a predictable name so tests will pass
   builder.hostname(group);
   builder.tags(template.getOptions().getTags());
   builder.group(group);
   builder.location(location.get());
   builder.imageId(template.getImage().getId());
   builder.operatingSystem(template.getImage().getOperatingSystem());
   builder.state(NodeState.PENDING);
   builder.publicAddresses(ImmutableSet.<String>of(publicIpPrefix + id));
   builder.privateAddresses(ImmutableSet.<String>of(privateIpPrefix + id));
   Credentials creds = template.getOptions().getOverridingCredentials();
   if (creds == null) creds = new Credentials(null, null);
   if (creds.identity == null) creds = creds.toBuilder().identity("root").build();
   if (creds.credential == null) creds = creds.toBuilder().credential(passwordPrefix + id).build();
   builder.credentials(creds);
   NodeMetadata node = builder.build();
   credentialStore.put("node#" + node.getId(), node.getCredentials());
   nodes.put(node.getId(), node);
   StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 100);
   return node;
 }
  @Test(enabled = true)
  public void testCreateAndRunAService() throws Exception {

    String tag = this.tag + "service";
    try {
      client.destroyNodesMatching(withTag(tag));
    } catch (Exception e) {

    }

    template =
        client
            .templateBuilder()
            .options(blockOnComplete(false).blockOnPort(8080, 600).inboundPorts(22, 8080))
            .build();
    // note this is a dependency on the template resolution
    template
        .getOptions()
        .runScript(
            RunScriptData.createScriptInstallAndStartJBoss(
                keyPair.get("public"), template.getImage().getOperatingSystem()));
    try {
      NodeMetadata node = getOnlyElement(client.runNodesWithTag(tag, 1, template));

      checkHttpGet(node);
    } finally {
      client.destroyNodesMatching(withTag(tag));
    }
  }
  @Override
  public Map<?, ListenableFuture<Void>> execute(
      String group,
      int count,
      Template template,
      Set<NodeMetadata> goodNodes,
      Map<NodeMetadata, Exception> badNodes,
      Multimap<NodeMetadata, CustomizationResponse> customizationResponses) {

    Template mutableTemplate = template.clone();

    Set<RunningInstance> started = runInstancesAndWarnOnInvisible(group, count, mutableTemplate);
    if (started.size() == 0) {
      logger.warn("<< unable to start instances(%s)", mutableTemplate);
      return ImmutableMap.of();
    }
    populateCredentials(started, template.getOptions());

    if (autoAllocateElasticIps) // before customization as the elastic ips may be needed
    blockUntilRunningAndAssignElasticIpsToInstancesOrPutIntoBadMap(started, badNodes);

    return utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(
        mutableTemplate.getOptions(),
        transform(started, runningInstanceToNodeMetadata),
        goodNodes,
        badNodes,
        customizationResponses);
  }
  @Test(enabled = true, dependsOnMethods = "testConcurrentUseOfComputeServiceToCreateNodes")
  public void testCreateTwoNodesWithRunScript() throws Exception {
    try {
      client.destroyNodesMatching(inGroup(group));
    } catch (NoSuchElementException e) {

    }
    refreshTemplate();
    try {
      nodes = newTreeSet(client.createNodesInGroup(group, 2, template));
    } catch (RunNodesException e) {
      nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }

    assertEquals(nodes.size(), 2, "expected two nodes but was " + nodes);
    checkNodes(nodes, group, "bootstrap");
    NodeMetadata node1 = nodes.first();
    NodeMetadata node2 = nodes.last();
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertLocationSameOrChild(
        checkNotNull(node1.getLocation(), "location of %s", node1), template.getLocation());
    assertLocationSameOrChild(
        checkNotNull(node2.getLocation(), "location of %s", node2), template.getLocation());
    checkImageIdMatchesTemplate(node1);
    checkImageIdMatchesTemplate(node2);
    checkOsMatchesTemplate(node1);
    checkOsMatchesTemplate(node2);
  }
 @Test(
     enabled = true,
     dependsOnMethods = {"testImagesCache"})
 public void testTemplateMatch() throws Exception {
   template = buildTemplate(client.templateBuilder());
   Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build();
   assertEquals(toMatch.getImage(), template.getImage());
 }
 @Test
 public void testBiggestTemplateBuilder() throws IOException {
   Template template = view.getComputeService().templateBuilder().biggest().build();
   assertEquals(getCores(template.getHardware()), 16.0d);
   assertEquals(template.getHardware().getRam(), 16);
   assertEquals(getSpace(template.getHardware()), 100.0d);
   assertEquals(template.getHardware().getVolumes().get(0).getType(), Volume.Type.LOCAL);
 }
 @Test
 public void testTemplateBuilder() {
   Template defaultTemplate = client.templateBuilder().build();
   assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
   assertEquals(defaultTemplate.getLocation().getDescription(), "Miami Environment 1");
   assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
 }
 @Override
 protected Template buildTemplate(TemplateBuilder templateBuilder) {
   Template template = super.buildTemplate(templateBuilder);
   Image image = template.getImage();
   assert image.getDefaultCredentials().identity != null : image;
   assert image.getDefaultCredentials().credential != null : image;
   return template;
 }
 @Override
 public void testDefaultTemplateBuilder() throws IOException {
   Template defaultTemplate = view.getComputeService().templateBuilder().build();
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "");
   assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
   assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
 }
 private Set<RunningInstance> createKeyPairAndSecurityGroupsAsNeededThenRunInstances(
     String group, int count, Template template) {
   String region = AWSUtils.getRegionFromLocationOrNull(template.getLocation());
   String zone = getZoneFromLocationOrNull(template.getLocation());
   RunInstancesOptions instanceOptions =
       createKeyPairAndSecurityGroupsAsNeededAndReturncustomize.execute(region, group, template);
   return createNodesInRegionAndZone(region, zone, group, count, template, instanceOptions);
 }
 @Test
 public void testTemplateBuilder() {
   Template defaultTemplate = this.context.getComputeService().templateBuilder().build();
   assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "10.04");
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
   assertEquals(defaultTemplate.getLocation().getId(), "elastichosts-lon-p");
   assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
 }
Exemplo n.º 14
0
 protected void checkOsMatchesTemplate(NodeMetadata node) {
   if (node.getOperatingSystem() != null)
     assert node.getOperatingSystem()
             .getFamily()
             .equals(template.getImage().getOperatingSystem().getFamily())
         : String.format(
             "expecting family %s but got %s",
             template.getImage().getOperatingSystem().getFamily(), node.getOperatingSystem());
 }
Exemplo n.º 15
0
 @Test
 public void testDefaultTemplateBuilder() throws IOException {
   Template defaultTemplate = context.getComputeService().templateBuilder().build();
   assert defaultTemplate.getImage().getOperatingSystem().getVersion().matches("1[012].[10][04]")
       : defaultTemplate.getImage().getOperatingSystem().getVersion();
   assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
   assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
 }
Exemplo n.º 16
0
  private void refreshTemplate() {
    template = buildTemplate(client.templateBuilder());

    template
        .getOptions()
        .installPrivateKey(keyPair.get("private"))
        .authorizePublicKey(keyPair.get("public"))
        .runScript(buildScript(template.getImage().getOperatingSystem()));
  }
 private Server addServer(String name, Template template, Ip availableIp) {
   Server addedServer =
       client
           .getServerServices()
           .addServer(
               name,
               checkNotNull(template.getImage().getProviderId()),
               sizeToRam.apply(template.getHardware()),
               availableIp.getIp());
   return addedServer;
 }
  public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() {
    String group = "foo";
    String name = "container" + new Random().nextInt();

    Template template = templateBuilder.imageId(defaultImage.id()).build();

    DockerTemplateOptions options = template.getOptions().as(DockerTemplateOptions.class);
    options.env(ImmutableList.of("ROOT_PASSWORD=password"));
    guest = adapter.createNodeWithGroupEncodedIntoName(group, name, template);
    assertEquals(guest.getNodeId(), guest.getNode().id());
  }
 @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
 public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
   initializeContextAndClient();
   TreeSet<NodeMetadata> nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 1, template));
   checkNodes(nodes, tag);
   NodeMetadata node = nodes.first();
   this.nodes.add(node);
   assertEquals(nodes.size(), 1);
   assertLocationSameOrChild(node.getLocation(), template.getLocation());
   assertEquals(node.getImage(), template.getImage());
 }
Exemplo n.º 20
0
 public void testFromTemplate() {
   Template defaultTemplate = context.getComputeService().templateBuilder().build();
   assertEquals(
       context
           .getComputeService()
           .templateBuilder()
           .fromTemplate(defaultTemplate)
           .build()
           .toString(),
       defaultTemplate.toString());
 }
Exemplo n.º 21
0
 public Set<NodeMetadata> createNodes(int count) throws RunNodesException {
   Set<NodeMetadata> result = Sets.newHashSet();
   Template template =
       mComputeService.templateBuilder().hardwareId(mInstanceType).imageId(mImageId).build();
   template
       .getOptions()
       .as(AWSEC2TemplateOptions.class)
       .keyPair(mkeyPair)
       .securityGroupIds(mSecurityGroup)
       .blockOnPort(22, 60)
       .spotPrice(mMaxBid)
       .tags(Collections.singletonList(mGroupTag));
   result.addAll(mComputeService.createNodesInGroup(mGroupName, count, template));
   return result;
 }
Exemplo n.º 22
0
 @Test(dataProvider = "osSupported")
 public void testTemplateBuilderCanFind(OsFamilyVersion64Bit matrix) throws InterruptedException {
   TemplateBuilder builder =
       context
           .getComputeService()
           .templateBuilder()
           .osFamily(matrix.family)
           .os64Bit(matrix.is64Bit);
   if (!matrix.version.equals("")) builder.osVersionMatches("^" + matrix.version + "$");
   Template template = builder.build();
   if (!matrix.version.equals(""))
     assertEquals(template.getImage().getOperatingSystem().getVersion(), matrix.version);
   assertEquals(template.getImage().getOperatingSystem().is64Bit(), matrix.is64Bit);
   assertEquals(template.getImage().getOperatingSystem().getFamily(), matrix.family);
 }
Exemplo n.º 23
0
  @Test(enabled = true, dependsOnMethods = "testCompareSizes")
  public void testCreateTwoNodesWithRunScript() throws Exception {
    try {
      client.destroyNodesMatching(withTag(tag));
    } catch (NoSuchElementException e) {

    }
    refreshTemplate();
    try {
      nodes = newTreeSet(client.runNodesWithTag(tag, 2, template));
    } catch (RunNodesException e) {
      nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }
    assertEquals(nodes.size(), 2);
    checkNodes(nodes, tag);
    NodeMetadata node1 = nodes.first();
    NodeMetadata node2 = nodes.last();
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertLocationSameOrChild(node1.getLocation(), template.getLocation());
    assertLocationSameOrChild(node2.getLocation(), template.getLocation());
    checkImageIdMatchesTemplate(node1);
    checkImageIdMatchesTemplate(node2);
    checkOsMatchesTemplate(node1);
    checkOsMatchesTemplate(node2);
  }
Exemplo n.º 24
0
  @Test(
      enabled = true,
      dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
  public void testGet() throws Exception {
    Map<String, ? extends NodeMetadata> metadataMap =
        newLinkedHashMap(
            uniqueIndex(
                filter(client.listNodesDetailsMatching(all()), and(withTag(tag), not(TERMINATED))),
                new Function<NodeMetadata, String>() {

                  @Override
                  public String apply(NodeMetadata from) {
                    return from.getId();
                  }
                }));
    for (NodeMetadata node : nodes) {
      metadataMap.remove(node.getId());
      NodeMetadata metadata = client.getNodeMetadata(node.getId());
      assertEquals(metadata.getProviderId(), node.getProviderId());
      assertEquals(metadata.getTag(), node.getTag());
      assertLocationSameOrChild(metadata.getLocation(), template.getLocation());
      checkImageIdMatchesTemplate(metadata);
      checkOsMatchesTemplate(metadata);
      assertEquals(metadata.getState(), NodeState.RUNNING);
      // due to DHCP the addresses can actually change in-between runs.
      assertEquals(metadata.getPrivateAddresses().size(), node.getPrivateAddresses().size());
      assertEquals(metadata.getPublicAddresses().size(), node.getPublicAddresses().size());
    }
    assertNodeZero(metadataMap.values());
  }
Exemplo n.º 25
0
  @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
  public void testCreateTwoNodesWithOneSpecifiedName() throws Exception {
    template = buildTemplate(client.templateBuilder());
    template.getOptions().nodeNames(ImmutableSet.of("first-node"));
    Set<? extends NodeMetadata> nodes;
    try {
      nodes = newTreeSet(client.createNodesInGroup(group, 2, template));
    } catch (RunNodesException e) {
      nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }

    assertEquals(nodes.size(), 2, "expected two nodes but was " + nodes);
    NodeMetadata node1 = Iterables.getFirst(nodes, null);
    NodeMetadata node2 = Iterables.getLast(nodes, null);
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertTrue(
        node1.getName().equals("first-node") || node2.getName().equals("first-node"),
        "one node should be named 'first-node'");
    assertFalse(
        node1.getName().equals("first-node") && node2.getName().equals("first-node"),
        "one node should be named something other than 'first-node");

    this.nodes.addAll(nodes);
  }
  public RunInstancesOptions execute(String region, String group, Template template) {

    RunInstancesOptions instanceOptions =
        getOptionsProvider().get().asType(template.getHardware().getId());

    String keyPairName =
        createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, template.getOptions());

    addSecurityGroups(region, group, template, instanceOptions);
    if (template.getOptions() instanceof EC2TemplateOptions) {

      if (keyPairName != null) instanceOptions.withKeyName(keyPairName);

      byte[] userData = EC2TemplateOptions.class.cast(template.getOptions()).getUserData();

      if (userData != null) instanceOptions.withUserData(userData);

      Set<BlockDeviceMapping> blockDeviceMappings =
          EC2TemplateOptions.class.cast(template.getOptions()).getBlockDeviceMappings();
      if (!blockDeviceMappings.isEmpty()) {
        checkState(
            "ebs".equals(template.getImage().getUserMetadata().get("rootDeviceType")),
            "BlockDeviceMapping only available on ebs boot");
        instanceOptions.withBlockDeviceMappings(blockDeviceMappings);
      }

      String clientToken = EC2TemplateOptions.class.cast(template.getOptions()).getClientToken();

      if (clientToken != null) {
        instanceOptions.withClientToken(clientToken);
      }
    }
    return instanceOptions;
  }
Exemplo n.º 27
0
  @Override
  @BeforeClass(groups = {"integration", "live"})
  public void setupContext() {
    super.setupContext();
    ec2Client = view.unwrap(EC2ApiMetadata.CONTEXT_TOKEN).getApi();
    runningTester = retry(new InstanceStateRunning(ec2Client), 600, 5, SECONDS);

    client = ec2Client.getAMIServices();
    if (ebsTemplate != null) {
      Template template = view.getComputeService().templateBuilder().from(ebsTemplate).build();
      regionId = template.getLocation().getId();
      imageId = template.getImage().getProviderId();
      for (Image image : client.describeImagesInRegion(regionId)) {
        if (ebsBackedImageName.equals(image.getName()))
          client.deregisterImageInRegion(regionId, image.getId());
      }
    }
  }
 private LoginCredentials resolveNodeCredentials(Template template) {
   TemplateOptions options = template.getOptions();
   LoginCredentials.Builder credentials =
       LoginCredentials.builder(template.getImage().getDefaultCredentials());
   if (!Strings.isNullOrEmpty(options.getLoginUser())) {
     credentials.user(options.getLoginUser());
   }
   if (!Strings.isNullOrEmpty(options.getLoginPrivateKey())) {
     credentials.privateKey(options.getLoginPrivateKey());
   }
   if (!Strings.isNullOrEmpty(options.getLoginPassword())) {
     credentials.password(options.getLoginPassword());
   }
   if (options.shouldAuthenticateSudo() != null) {
     credentials.authenticateSudo(options.shouldAuthenticateSudo());
   }
   return credentials.build();
 }
  @Test
  public void testBiggestTemplateBuilderWhenBootIsSAN() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(PROPERTY_SOFTLAYER_VIRTUALGUEST_DISK0_TYPE, "SAN");

      context = createView(overrides, setupModules());

      Template template = context.getComputeService().templateBuilder().biggest().build();
      assertEquals(getCores(template.getHardware()), 16.0d);
      assertEquals(template.getHardware().getRam(), 16);
      assertEquals(getSpace(template.getHardware()), 100.0d);
      assertEquals(template.getHardware().getVolumes().get(0).getType(), Volume.Type.SAN);
    } finally {
      if (context != null) context.close();
    }
  }
  @Test
  public void testBiggestTemplateBuilderWhenPrivateNetwork() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(
          PROPERTY_SOFTLAYER_VIRTUALGUEST_CPU_REGEX, "Private [0-9]+ x ([.0-9]+) GHz Core[s]?");

      context = createView(overrides, setupModules());

      Template template = context.getComputeService().templateBuilder().biggest().build();
      assertEquals(getCores(template.getHardware()), 8.0d);
      assertEquals(template.getHardware().getRam(), 16);
      assertEquals(getSpace(template.getHardware()), 100.0d);
      assertEquals(template.getHardware().getVolumes().get(0).getType(), Volume.Type.LOCAL);
    } finally {
      if (context != null) context.close();
    }
  }