Skip to content
Permalink
Browse files
Add live test for signPutBlob.
Also, actually perform the blob put and get operations to verify that
they succeed.

Signed-off-by: Shri Javadekar <shrinand@maginatics.com>
  • Loading branch information
Shri Javadekar authored and Jeremy Daggett committed Aug 27, 2014
1 parent 3243e71 commit de8348dd20c6218a5c076293a24a765058180936
Showing 1 changed file with 78 additions and 7 deletions.
@@ -20,20 +20,38 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.domain.Location;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.io.ByteStreams2;
import org.jclouds.io.MutableContentMetadata;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.rest.HttpClient;
import org.testng.annotations.Test;

import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.hash.Hashing;
import com.google.common.net.MediaType;
import com.google.common.net.HttpHeaders;

@Test(groups = "live")
public class RegionScopedBlobStoreContextLiveTest extends BaseBlobStoreIntegrationTest {
@@ -74,7 +92,8 @@ public void testListBlobs() throws InterruptedException, ExecutionException {
}

@Test
public void testSignBlob() throws InterruptedException, ExecutionException {
public void testSign() throws InterruptedException, ExecutionException,
IOException {
RegionScopedBlobStoreContext ctx = RegionScopedBlobStoreContext.class.cast(view);
for (String regionId : ctx.getConfiguredRegions()) {
BlobStore region = ctx.getBlobStore(regionId);
@@ -83,13 +102,65 @@ public void testSignBlob() throws InterruptedException, ExecutionException {
continue;
}
String containerName = Iterables.getLast(containers).getName();
PageSet<? extends StorageMetadata> blobs = region.list(containerName);
if (blobs.isEmpty()) {
continue;

final ByteSource input = ByteSource.wrap("str".getBytes());
final HttpClient client = ctx.utils().http();

// test signed put
String blobName = "test-" + UUID.randomUUID();
Blob blob2 = region.blobBuilder(blobName).forSigning()
.contentLength(input.size())
.contentMD5(input.hash(Hashing.md5()).asBytes())
.contentType(MediaType.OCTET_STREAM.toString()).build();
BlobRequestSigner signer = ctx.getSigner(regionId);
HttpResponse response;
try {
HttpRequest putRequest;
putRequest = signer.signPutBlob(containerName, blob2, 600);
MutableContentMetadata metadata = blob2.getMetadata()
.getContentMetadata();
HttpRequest.Builder<?> putRequestBuilder = putRequest.toBuilder()
.addHeader(HttpHeaders.CONTENT_TYPE,
metadata.getContentType());
putRequestBuilder.addHeader(HttpHeaders.CONTENT_LENGTH,
String.valueOf(input.size()));
putRequestBuilder.payload(input);
putRequest = putRequestBuilder.build();
Payload payload = Payloads.newByteArrayPayload(input.read());
putRequest.setPayload(payload);
assertNotNull(putRequest, "regionId=" + regionId + ", container="
+ containerName + ", blob=" + blobName);
response = client.invoke(putRequest);
if (response.getStatusCode() != 200
&& response.getStatusCode() != 201) {
fail("Signed PUT expected to return 200 or 201 but returned "
+ response.getStatusCode());
}
} catch (Exception e) {
fail("Failed signed put test: " + e);
}

// test signed get
try {
HttpRequest getRequest = signer.signGetBlob(containerName,
blobName);
assertNotNull(getRequest, "regionId=" + regionId + ", container="
+ containerName + ", blob=" + blobName);
response = client.invoke(getRequest);
if (response.getStatusCode() != 200) {
fail("Signed GET expected to return 200 but returned "
+ response.getStatusCode());
}
Payload payload = response.getPayload();
if (!Arrays.equals(ByteStreams2.toByteArrayAndClose(
payload.openStream()), input.read())) {
fail("Data with signed GET not identical to what was put: "
+ "Input: " + new String(input.read()) + ", Output :"
+ ByteStreams2.asByteSource(payload));
}
} catch (Exception e) {
fail("Failed signed GET test: " + e);
}
String blobName = Iterables.getLast(blobs).getName();
HttpRequest request = ctx.getSigner(regionId).signGetBlob(containerName, blobName);
assertNotNull(request, "regionId=" + regionId + ", container=" + containerName + ", blob=" + blobName);
}
}
}

0 comments on commit de8348d

Please sign in to comment.