public void testWeCanReadAndWriteToDrive() throws IOException {
   drive2 =
       client.createDrive(
           new CreateDriveRequest.Builder().name(prefix + "2").size(1 * 1024 * 1024l).build());
   client.writeDrive(drive2.getUuid(), Payloads.newStringPayload("foo"));
   assertEquals(Strings2.toString(client.readDrive(drive2.getUuid(), 0, 3)), "foo");
 }
Example #2
0
  @Test
  public void test401ShouldRetry() {
    HttpCommand command = createMock(HttpCommand.class);
    HttpRequest request = createMock(HttpRequest.class);
    HttpResponse response = createMock(HttpResponse.class);
    @SuppressWarnings("unchecked")
    LoadingCache<Credentials, Access> cache = createMock(LoadingCache.class);
    BackoffLimitedRetryHandler backoffHandler = createMock(BackoffLimitedRetryHandler.class);

    expect(command.getCurrentRequest()).andReturn(request);

    cache.invalidateAll();
    expectLastCall();

    expect(response.getPayload()).andReturn(Payloads.newStringPayload("")).anyTimes();
    expect(response.getStatusCode()).andReturn(401).atLeastOnce();

    replay(command);
    replay(response);
    replay(cache);
    replay(backoffHandler);

    RetryOnRenew retry = new RetryOnRenew(cache, backoffHandler);

    assertTrue(retry.shouldRetryRequest(command, response));

    verify(command);
    verify(response);
    verify(cache);
  }
 public void execute(String containerName, String directory) {
   Blob blob = connection.newBlob(directory + directorySuffix);
   blob.setPayload(Payloads.newByteArrayPayload(new byte[] {}));
   blob.getPayload().setContentType("application/directory");
   blob.getMetadata().setType(StorageType.RELATIVE_PATH);
   connection.putBlob(containerName, blob);
 }
  @Override
  @Test(groups = {"integration", "live"})
  public void testEntrySet() throws IOException, InterruptedException {
    String bucketName = getContainerName();
    try {
      final BlobMap map = createMap(context, bucketName);
      putFiveStrings(map);
      assertConsistencyAwareMapSize(map, 5);
      Set<Entry<String, Blob>> entries = map.entrySet();
      assertEquals(entries.size(), 5);
      for (Entry<String, Blob> entry : entries) {
        assertEquals(
            fiveStrings.get(entry.getKey()), getContentAsStringOrNullAndClose(entry.getValue()));
        Blob blob = entry.getValue();
        blob.setPayload("");
        Payloads.calculateMD5(blob);
        entry.setValue(blob);
      }
      assertConsistencyAware(
          new Runnable() {
            public void run() {
              for (Blob blob : map.values()) {
                try {
                  assertEquals(getContentAsStringOrNullAndClose(blob), "");
                } catch (IOException e) {
                  Throwables.propagate(e);
                }
              }
            }
          });

    } finally {
      returnContainer(bucketName);
    }
  }
  protected void assertCodeMakes(
      String method,
      URI uri,
      int statusCode,
      String message,
      String contentType,
      String content,
      Class<? extends Exception> expected) {

    HttpErrorHandler function =
        Guice.createInjector(new SaxParserModule()).getInstance(getHandlerClass());

    HttpCommand command = createMock(HttpCommand.class);
    HttpRequest request = new HttpRequest(method, uri);
    HttpResponse response =
        new HttpResponse(statusCode, message, Payloads.newStringPayload(content));
    if (contentType != null) response.getPayload().getContentMetadata().setContentType(contentType);

    expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
    command.setException(classEq(expected));

    replay(command);

    function.handleError(command, response);

    verify(command);
  }
  private void assertCodeMakes(
      String method,
      URI uri,
      int statusCode,
      String message,
      String contentType,
      String content,
      Class<? extends Exception> expected) {

    ElasticStackErrorHandler function =
        Guice.createInjector().getInstance(ElasticStackErrorHandler.class);

    HttpCommand command = createMock(HttpCommand.class);
    HttpRequest request = new HttpRequest(method, uri);
    HttpResponse response =
        new HttpResponse(
            statusCode, message, Payloads.newInputStreamPayload(Strings2.toInputStream(content)));
    response.getPayload().getContentMetadata().setContentType(contentType);

    expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
    command.setException(classEq(expected));

    replay(command);

    function.handleError(command, response);

    verify(command);
  }
Example #7
0
  @Test
  public void test401ShouldRetry4Times() {
    HttpCommand command = createMock(HttpCommand.class);
    HttpRequest request = createMock(HttpRequest.class);
    HttpResponse response = createMock(HttpResponse.class);

    @SuppressWarnings("unchecked")
    LoadingCache<Credentials, Access> cache = createMock(LoadingCache.class);
    BackoffLimitedRetryHandler backoffHandler = createMock(BackoffLimitedRetryHandler.class);

    expect(command.getCurrentRequest()).andReturn(request).anyTimes();
    expect(request.getHeaders()).andStubReturn(null);

    cache.invalidateAll();
    expectLastCall().anyTimes();

    expect(response.getPayload()).andReturn(Payloads.newStringPayload("")).anyTimes();
    expect(response.getStatusCode()).andReturn(401).anyTimes();

    replay(command, request, response, cache);

    RetryOnRenew retry = new RetryOnRenew(cache, backoffHandler);

    for (int i = 0; i < RetryOnRenew.NUM_RETRIES - 1; ++i) {
      assertTrue(retry.shouldRetryRequest(command, response), "Expected retry to succeed");
    }

    assertFalse(
        retry.shouldRetryRequest(command, response),
        "Expected retry to fail on attempt " + RetryOnRenew.NUM_RETRIES);

    verify(command, response, cache);
  }
  public void testUploadPart()
      throws SecurityException, NegativeArraySizeException, NoSuchMethodException {
    Invokable<?, ?> method =
        method(
            AWSS3AsyncClient.class,
            "uploadPart",
            String.class,
            String.class,
            int.class,
            String.class,
            Payload.class);
    GeneratedHttpRequest request =
        processor.createRequest(
            method,
            ImmutableList.<Object>of(
                "bucket", "foo", 1, "asdsadasdas", Payloads.newStringPayload("")));

    assertRequestLineEquals(
        request, "PUT https://bucket." + url + "/foo?partNumber=1&uploadId=asdsadasdas HTTP/1.1");
    assertNonPayloadHeadersEqual(request, "Host: bucket." + url + "\n");
    assertPayloadEquals(request, "", "application/unknown", false);

    assertResponseParserClassEquals(method, request, ParseETagHeader.class);
    assertSaxResponseParserClassEquals(method, null);
    assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class);

    checkFilters(request);
  }
Example #9
0
 @Override
 public Payload create() throws Exception {
   sftp = acquire(sftpConnection);
   return Payloads.newInputStreamPayload(
       new CloseFtpChannelOnCloseInputStream(
           sftp.getSFTPEngine().open(path).getInputStream(), sftp));
 }
Example #10
0
  @Test
  public void test408ShouldRetry() {
    HttpCommand command = createMock(HttpCommand.class);
    HttpResponse response = createMock(HttpResponse.class);
    @SuppressWarnings("unchecked")
    LoadingCache<Credentials, Access> cache = createMock(LoadingCache.class);
    BackoffLimitedRetryHandler backoffHandler = createMock(BackoffLimitedRetryHandler.class);

    expect(response.getPayload())
        .andReturn(
            Payloads.newStringPayload(
                "The server has waited too long for the request to be sent by the client."))
        .times(3);
    expect(backoffHandler.shouldRetryRequest(command, response)).andReturn(true).once();
    expect(response.getStatusCode()).andReturn(408).once();

    replay(command);
    replay(response);
    replay(cache);
    replay(backoffHandler);

    RetryOnRenew retry = new RetryOnRenew(cache, backoffHandler);

    assertTrue(retry.shouldRetryRequest(command, response));

    verify(command);
    verify(response);
    verify(cache);
    verify(backoffHandler);
  }
  public void testAbortMultipartUpload()
      throws SecurityException, NegativeArraySizeException, NoSuchMethodException {
    Invokable<?, ?> method =
        method(
            AWSS3AsyncClient.class,
            "abortMultipartUpload",
            String.class,
            String.class,
            String.class);
    GeneratedHttpRequest request =
        processor.createRequest(
            method,
            ImmutableList.<Object>of(
                "bucket", "foo", "asdsadasdas", 1, Payloads.newStringPayload("")));

    assertRequestLineEquals(
        request, "DELETE https://bucket." + url + "/foo?uploadId=asdsadasdas HTTP/1.1");
    assertNonPayloadHeadersEqual(request, "Host: bucket." + url + "\n");
    assertPayloadEquals(request, "", "application/unknown", false);

    assertResponseParserClassEquals(method, request, ReleasePayloadAndReturn.class);
    assertSaxResponseParserClassEquals(method, null);
    assertFallbackClassEquals(method, VoidOnNotFoundOr404.class);

    checkFilters(request);
  }
 @Override
 protected void putStringWithMD5(Map<String, Blob> map, String key, String text)
     throws IOException {
   Blob blob = context.getBlobStore().newBlob(key);
   blob.setPayload(text);
   Payloads.calculateMD5(blob);
   map.put(key, blob);
 }
  public void testApplyInputStreamDetails() throws UnknownHostException {
    InputStream is = getClass().getResourceAsStream("/metadata.json");

    ParseMetadataFromJsonResponse parser = i.getInstance(ParseMetadataFromJsonResponse.class);
    Map<String, String> response =
        parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
    assertEquals(response, ImmutableMap.of("MD5", "IGPBYI1uC6+AJJxC4r5YBA==", "test", "1"));
  }
 public String sign(String toSign) {
   try {
     byte[] encrypted =
         toByteArray(new RSAEncryptingPayload(Payloads.newStringPayload(toSign), supplyKey.get()));
     return base64().encode(encrypted);
   } catch (IOException e) {
     throw new HttpException("error signing request", e);
   }
 }
  public void testWithoutToken() {
    HttpResponse response =
        new HttpResponse(
            200, "ok", Payloads.newPayload(getClass().getResourceAsStream("/list_basic.xml")));
    BoundedSet<DirectoryEntry> result = createFn().apply(response);

    assertEquals(ImmutableSet.copyOf(result), values());
    assertEquals(result.getToken(), null);
  }
 protected SwiftObject newSwiftObject(String data, String key) throws IOException {
   SwiftObject object = getApi().newSwiftObject();
   object.getInfo().setName(key);
   object.setPayload(data);
   Payloads.calculateMD5(object);
   object.getInfo().setContentType("text/plain");
   object.getInfo().getMetadata().put("Metadata", "metadata-value");
   return object;
 }
 public void testPutAndGet() throws IOException {
   temp = File.createTempFile("foo", "bar");
   temp.deleteOnExit();
   SshClient client = setupClient();
   client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));
   Payload input = setupClient().get(temp.getAbsolutePath());
   String contents = Strings2.toStringAndClose(input.getInput());
   assertEquals(contents, "rabbit");
 }
 @Test(groups = {"integration", "live"})
 public void testPut() throws IOException, InterruptedException {
   String bucketName = getContainerName();
   try {
     Map<String, Blob> map = createMap(context, bucketName);
     Blob blob = context.getBlobStore().newBlob("one");
     blob.setPayload(Strings2.toInputStream("apple"));
     Payloads.calculateMD5(blob);
     Blob old = map.put(blob.getMetadata().getName(), blob);
     getOneReturnsAppleAndOldValueIsNull(map, old);
     blob.setPayload(Strings2.toInputStream("bear"));
     Payloads.calculateMD5(blob);
     Blob apple = map.put(blob.getMetadata().getName(), blob);
     getOneReturnsBearAndOldValueIsApple(map, apple);
   } finally {
     returnContainer(bucketName);
   }
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNullContentLengthIllegal() {
   AtmosObject object = injector.getInstance(AtmosObject.Factory.class).create(null);
   Payload payload = Payloads.newStringPayload("");
   payload.getContentMetadata().setContentLength(null);
   object.setPayload(payload);
   HttpRequest request = new HttpRequest("GET", URI.create("http://localhost"));
   binder.bindToRequest(request, object);
 }
Example #20
0
 // TODO: change EC2 apis to add this themselves with @FormParams
 private Payload addVersionIfNecessary(Payload payload, String form) {
   if (form.indexOf(VERSION) == -1) {
     form += "&Version=" + apiVersion;
     payload = Payloads.newStringPayload(form);
     payload.getContentMetadata().setContentType(APPLICATION_FORM_URLENCODED);
     payload.getContentMetadata().setContentLength((long) form.length());
   }
   return payload;
 }
 public void testRegex() {
   assertEquals(
       handler.apply(
           new HttpResponse(
               200,
               "ok",
               Payloads.newStringPayload(
                   "{\n\"opscode-validator\": \"https://api.opscode.com/...\", \"pimp-validator\": \"https://api.opscode.com/...\"}"))),
       ImmutableSet.of("opscode-validator", "pimp-validator"));
 }
 public void testGood() {
   AtmosObject object = injector.getInstance(AtmosObject.Factory.class).create(null);
   Payload payload = Payloads.newStringPayload("");
   object.setPayload(payload);
   object.getUserMetadata().getListableMetadata().put("apple", "bear");
   object.getUserMetadata().getListableMetadata().put("sushi", "king");
   HttpRequest request = new HttpRequest("GET", URI.create("http://localhost"));
   request = binder.bindToRequest(request, object);
   assertEquals(request.getFirstHeaderOrNull("x-emc-listable-meta"), "apple=bear,sushi=king");
 }
  public void testApplyInputStreamDetails() throws UnknownHostException {
    InputStream is = getClass().getResourceAsStream("/test_list_backupschedule.json");

    UnwrapOnlyJsonValue<BackupSchedule> parser =
        i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<BackupSchedule>>() {}));
    BackupSchedule response =
        parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
    assertEquals(
        new BackupSchedule(WeeklyBackup.THURSDAY, DailyBackup.H_0400_0600, true), response);
  }
Example #24
0
 /**
  * Content stream may need to be read. However, we should always close the http stream.
  *
  * @throws IOException
  */
 public static byte[] closeClientButKeepContentStream(PayloadEnclosing response) {
   byte[] returnVal = toByteArrayOrNull(response);
   if (returnVal != null && !response.getPayload().isRepeatable()) {
     Payload newPayload = Payloads.newByteArrayPayload(returnVal);
     MutableContentMetadata fromMd = response.getPayload().getContentMetadata();
     MutableContentMetadata toMd = newPayload.getContentMetadata();
     copy(fromMd, toMd);
     response.setPayload(newPayload);
   }
   return returnVal;
 }
  public void testPrivate() throws UnknownHostException {
    InputStream is =
        getClass().getResourceAsStream("/cloudservers/test_list_addresses_private.json");

    UnwrapOnlyJsonValue<List<String>> parser =
        i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {}));
    List<String> response =
        parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));

    assertEquals(response, ImmutableList.of("10.176.42.16"));
  }
  @Test
  public void testApplyInputStreamDetails() throws UnknownHostException {
    InputStream is = getClass().getResourceAsStream("/test_error_handler.json");

    ParseErrorFromJsonResponse parser = i.getInstance(ParseErrorFromJsonResponse.class);
    ErrorResponse response =
        Iterables.getOnlyElement(
            parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))));
    assert "No object found that matches your input criteria.".equals(response.getMessage());
    assert "IllegalArgumentException".equals(response.getErrorCode());
  }
Example #27
0
  public Payload get(String path) {
    checkNotNull(path, "path");

    ChannelSftp sftp = getSftp();
    try {
      return Payloads.newInputStreamPayload(
          new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp));
    } catch (SftpException e) {
      throw new SshException(
          String.format("%s@%s:%d: Error getting path: %s", username, host, port, path), e);
    }
  }
  public void testWithToken() {
    HttpResponse response =
        new HttpResponse(
            200,
            "ok",
            Payloads.newPayload(getClass().getResourceAsStream("/list_basic.xml")),
            ImmutableMultimap.of(AtmosHeaders.TOKEN, "token"));

    BoundedSet<DirectoryEntry> result = createFn().apply(response);
    assertEquals(result, new BoundedLinkedHashSet<DirectoryEntry>(values(), "token"));
    assertEquals(result.getToken(), "token");
  }
  public void testNoSchedule() throws UnknownHostException {

    UnwrapOnlyJsonValue<BackupSchedule> parser =
        i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<BackupSchedule>>() {}));
    BackupSchedule response =
        parser.apply(
            new HttpResponse(
                200,
                "ok",
                Payloads.newStringPayload("{\"backupSchedule\":{\"enabled\" : false}}")));
    assertEquals(new BackupSchedule(), response);
  }
  public void testApplyInputStream() {
    InputStream is = getClass().getResourceAsStream("/test_list_servers.json");

    List<Server> expects =
        ImmutableList.of(new Server(1234, "sample-server"), new Server(5678, "sample-server2"));

    UnwrapOnlyJsonValue<List<Server>> parser =
        i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {}));
    List<Server> response =
        parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));

    assertEquals(response, expects);
  }