@AfterMethod(alwaysRun = true)
 @Override
 public void tearDown() throws Exception {
   try {
     super.tearDown();
   } finally {
     if (context != null) context.close();
   }
 }
Ejemplo n.º 2
0
  public void testNoSsh() throws Exception {

    Map<String, String> keyPair = ComputeTestUtils.setupKeyPair();

    AWSInstanceClient instanceClient =
        AWSEC2Client.class.cast(context.getApi()).getInstanceServices();

    String group = PREFIX + "unssh";
    computeContext.getComputeService().destroyNodesMatching(inGroup(group));

    TemplateOptions options = computeContext.getComputeService().templateOptions();

    options.authorizePublicKey(keyPair.get("public")).as(AWSEC2TemplateOptions.class);

    ComputeServiceContext noSshContext = null;
    try {
      noSshContext =
          new ComputeServiceContextFactory()
              .createContext(
                  provider, ImmutableSet.of(new Log4JLoggingModule()), setupProperties());

      Set<? extends NodeMetadata> nodes =
          noSshContext.getComputeService().createNodesInGroup(group, 1, options);

      NodeMetadata first = get(nodes, 0);
      assert first.getCredentials() != null : first;
      assert first.getCredentials().identity != null : first;
      assert first.getCredentials().credential == null : first;

      AWSRunningInstance instance = getInstance(instanceClient, first.getProviderId());

      assert instance.getSpotInstanceRequestId() != null : instance;
      assertEquals(instance.getKeyName(), "jclouds#" + group);

      Map<? extends NodeMetadata, ExecResponse> responses =
          computeContext
              .getComputeService()
              .runScriptOnNodesMatching(
                  runningInGroup(group),
                  exec("echo hello"),
                  overrideCredentialsWith(
                          new Credentials(first.getCredentials().identity, keyPair.get("private")))
                      .wrapInInitScript(false)
                      .runAsRoot(false));

      ExecResponse hello = getOnlyElement(responses.values());
      assertEquals(hello.getOutput().trim(), "hello");

    } finally {
      noSshContext.close();
      computeContext.getComputeService().destroyNodesMatching(inGroup(group));
    }
  }
  @Test
  public void testTemplateBuilderFindsMegabitUplink() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(PROPERTY_SOFTLAYER_VIRTUALGUEST_PORT_SPEED, "100");

      context = createView(overrides, setupModules());

      // TODO add something to the template about port speed?
      context.getComputeService().templateBuilder().build();

    } finally {
      if (context != null) context.close();
    }
  }
  @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();
    }
  }
Ejemplo n.º 6
0
  public static Map<Integer, Integer> dockerPortMappingsFor(
      JcloudsLocation docker, String containerId) {
    ComputeServiceContext context = null;
    try {
      Properties properties = new Properties();
      properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, Boolean.toString(true));
      properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, Boolean.toString(true));
      context =
          ContextBuilder.newBuilder("docker")
              .endpoint(docker.getEndpoint())
              .credentials(docker.getIdentity(), docker.getCredential())
              .overrides(properties)
              .modules(ImmutableSet.<Module>of(new SLF4JLoggingModule(), new SshjSshClientModule()))
              .build(ComputeServiceContext.class);
      DockerApi api = context.unwrapApi(DockerApi.class);
      Container container = api.getContainerApi().inspectContainer(containerId);
      Map<Integer, Integer> portMappings = Maps.newLinkedHashMap();
      Map<String, List<Map<String, String>>> ports = container.networkSettings().ports();
      if (ports == null) ports = ImmutableMap.<String, List<Map<String, String>>>of();

      LOG.debug("Docker will forward these ports {}", ports);
      for (Map.Entry<String, List<Map<String, String>>> entrySet : ports.entrySet()) {
        String containerPort = Iterables.get(Splitter.on("/").split(entrySet.getKey()), 0);
        String hostPort =
            Iterables.getOnlyElement(
                Iterables.transform(
                    entrySet.getValue(),
                    new Function<Map<String, String>, String>() {
                      @Override
                      public String apply(Map<String, String> hostIpAndPort) {
                        return hostIpAndPort.get("HostPort");
                      }
                    }));
        portMappings.put(Integer.parseInt(containerPort), Integer.parseInt(hostPort));
      }
      return portMappings;
    } finally {
      if (context != null) {
        context.close();
      }
    }
  }
Ejemplo n.º 7
0
 @Test(enabled = true, expectedExceptions = AuthorizationException.class)
 public void testCorrectAuthException() throws Exception {
   ComputeServiceContext context = null;
   try {
     Properties overrides = setupProperties();
     overrides.setProperty(provider + ".identity", "MOM:MA");
     overrides.setProperty(provider + ".credential", "MIA");
     context =
         newBuilder()
             .modules(ImmutableSet.of(getLoggingModule(), credentialStoreModule))
             .overrides(overrides)
             .build(ComputeServiceContext.class);
     context.getComputeService().listNodes();
   } catch (AuthorizationException e) {
     throw e;
   } catch (RuntimeException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (context != null) context.close();
   }
 }
 @Test(expectedExceptions = AuthorizationException.class)
 @Override
 public void testCorrectAuthException() throws Exception {
   ComputeServiceContext context = null;
   try {
     String credential = toStringAndClose(getClass().getResourceAsStream("/test"));
     Properties overrides = setupProperties();
     overrides.setProperty(provider + ".identity", "*****@*****.**");
     overrides.setProperty(provider + ".credential", credential);
     context =
         newBuilder()
             .modules(ImmutableSet.of(getLoggingModule(), credentialStoreModule))
             .overrides(overrides)
             .build(ComputeServiceContext.class);
     context.getComputeService().listNodes();
   } catch (AuthorizationException e) {
     throw e;
   } catch (RuntimeException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (context != null) context.close();
   }
 }
Ejemplo n.º 9
0
 public void close() {
   mComputeServiceContext.close();
 }