walrus: workaroud date parse issue, more intelligently deduce containernotfoundexception when s3 is in 'path' mode, and adjust base test classes

This commit is contained in:
Adrian Cole 2011-01-28 10:41:40 -08:00
parent 89d201854b
commit 142aec45cb
14 changed files with 197 additions and 58 deletions

View File

@ -41,7 +41,6 @@ public class EucalyptusPropertiesBuilder extends EC2PropertiesBuilder {
properties.setProperty(PROPERTY_REGIONS, "Eucalyptus");
properties.setProperty(PROPERTY_EC2_AMI_OWNERS, "admin");
properties.setProperty(PROPERTY_TIMEOUT_PORT_OPEN, 5 * 60 * 1000 + "");
return properties;
}

View File

@ -19,7 +19,17 @@
package org.jclouds.s3.handlers;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Lists.newArrayList;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.aws.domain.AWSError;
@ -31,6 +41,9 @@ import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
/**
* @author Adrian Cole
*
@ -38,27 +51,43 @@ import org.jclouds.rest.ResourceNotFoundException;
@Singleton
public class ParseS3ErrorFromXmlContent extends ParseAWSErrorFromXmlContent {
private final String servicePath;
private final boolean isVhostStyle;
@Inject
ParseS3ErrorFromXmlContent(AWSUtils utils) {
ParseS3ErrorFromXmlContent(AWSUtils utils, @Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle,
@Named(PROPERTY_S3_SERVICE_PATH) String servicePath) {
super(utils);
this.servicePath = servicePath;
this.isVhostStyle = isVhostStyle;
}
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AWSError error,
String message) {
String message) {
switch (response.getStatusCode()) {
case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception);
String container = command.getCurrentRequest().getEndpoint().getHost();
String key = command.getCurrentRequest().getEndpoint().getPath();
if (key == null || key.equals("/"))
exception = new ContainerNotFoundException(container, message);
else
exception = new KeyNotFoundException(container, key, message);
}
return exception;
default:
return super.refineException(command, response, exception, error, message);
case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception);
if (isVhostStyle) {
String container = command.getCurrentRequest().getEndpoint().getHost();
String key = command.getCurrentRequest().getEndpoint().getPath();
if (key == null || key.equals("/"))
exception = new ContainerNotFoundException(container, message);
else
exception = new KeyNotFoundException(container, key, message);
} else if (command.getCurrentRequest().getEndpoint().getPath().indexOf(servicePath + "/") == 0) {
String path = command.getCurrentRequest().getEndpoint().getPath().substring(servicePath.length());
List<String> parts = newArrayList(filter(Splitter.on('/').split(path), not(equalTo(""))));
if (parts.size() == 1) {
exception = new ContainerNotFoundException(parts.get(0), message);
} else if (parts.size() > 1) {
exception = new KeyNotFoundException(parts.remove(0), Joiner.on('/').join(parts), message);
}
}
}
return exception;
default:
return super.refineException(command, response, exception, error, message);
}
}
}

View File

@ -0,0 +1,122 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.s3.handlers;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.reportMatcher;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
import java.net.URI;
import org.easymock.IArgumentMatcher;
import org.jclouds.blobstore.ContainerNotFoundException;
import org.jclouds.blobstore.KeyNotFoundException;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.config.SaxParserModule;
import org.jclouds.io.Payloads;
import org.jclouds.rest.RequestSigner;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.name.Names;
/**
*
* @author Adrian Cole
*/
@Test(groups = { "unit" })
public class ParseS3ErrorFromXmlContentTest {
private static final String SERVICE_PATH = "/services/Walrus";
@Test
public void test404ContainerNotFoundExceptionPath() {
assertCodeMakes("GET", URI
.create("http://partnercloud.eucalyptus.com:8773/services/Walrus/adriancole-blobstore58/"), 404, "HTTP/1.1 404 Not Found", false,
"<Error><Code>Monster.NotFound</Code></Error>", ContainerNotFoundException.class);
}
@Test
public void test404KeyNotFoundExceptionPath() {
assertCodeMakes("GET", URI
.create("http://partnercloud.eucalyptus.com:8773/services/Walrus/adriancole-blobstore58/apples"), 404, "HTTP/1.1 404 Not Found", false,
"<Error><Code>Monster.NotFound</Code></Error>", KeyNotFoundException.class);
}
private void assertCodeMakes(String method, URI uri, int statusCode, String message, final boolean virtualHost,
String content, Class<? extends Exception> expected) {
ParseS3ErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
@Override
protected void configure() {
bind(RequestSigner.class).toInstance(createMock(RequestSigner.class));
bindConstant().annotatedWith(Names.named(PROPERTY_HEADER_TAG)).to("amz");
bindConstant().annotatedWith(Names.named(PROPERTY_S3_SERVICE_PATH)).to(SERVICE_PATH);
bindConstant().annotatedWith(Names.named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS)).to(virtualHost);
}
}).getInstance(ParseS3ErrorFromXmlContent.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("application/xml");
expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
command.setException(classEq(expected));
replay(command);
function.handleError(command, response);
verify(command);
}
public static Exception classEq(final Class<? extends Exception> in) {
reportMatcher(new IArgumentMatcher() {
@Override
public void appendTo(StringBuffer buffer) {
buffer.append("classEq(");
buffer.append(in);
buffer.append(")");
}
@Override
public boolean matches(Object arg) {
return arg.getClass() == in;
}
});
return null;
}
}

View File

@ -19,22 +19,14 @@
package org.jclouds.walrus.blobstore;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest;
import org.jclouds.s3.blobstore.integration.S3BlobIntegrationLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusBlobIntegrationLiveTest")
public class WalrusBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public class WalrusBlobIntegrationLiveTest extends S3BlobIntegrationLiveTest {
@Override
@Test(expectedExceptions = IllegalArgumentException.class)
public void testPutObjectStream() throws InterruptedException, IOException, ExecutionException {
super.testPutObjectStream();
}
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseBlobLiveTest;
import org.jclouds.s3.blobstore.integration.S3BlobLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusBlobLiveTest")
public class WalrusBlobLiveTest extends BaseBlobLiveTest {
public class WalrusBlobLiveTest extends S3BlobLiveTest {
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseBlobMapIntegrationTest;
import org.jclouds.s3.blobstore.integration.S3BlobMapIntegrationLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusBlobMapIntegrationLiveTest")
public class WalrusBlobMapIntegrationLiveTest extends BaseBlobMapIntegrationTest {
public class WalrusBlobMapIntegrationLiveTest extends S3BlobMapIntegrationLiveTest {
}

View File

@ -19,7 +19,7 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseBlobSignerLiveTest;
import org.jclouds.s3.blobstore.integration.S3BlobSignerLiveTest;
import org.testng.annotations.Test;
/**
@ -27,6 +27,6 @@ import org.testng.annotations.Test;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusBlobSignerLiveTest")
public class WalrusBlobSignerLiveTest extends BaseBlobSignerLiveTest {
public class WalrusBlobSignerLiveTest extends S3BlobSignerLiveTest {
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseContainerIntegrationTest;
import org.jclouds.s3.blobstore.integration.S3ContainerIntegrationLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusContainerIntegrationLiveTest")
public class WalrusContainerIntegrationLiveTest extends BaseContainerIntegrationTest {
public class WalrusContainerIntegrationLiveTest extends S3ContainerIntegrationLiveTest {
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.jclouds.s3.blobstore.integration.S3ContainerLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusContainerLiveTest")
public class WalrusContainerLiveTest extends BaseContainerLiveTest {
public class WalrusContainerLiveTest extends S3ContainerLiveTest {
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseInputStreamMapIntegrationTest;
import org.jclouds.s3.blobstore.integration.S3InputStreamMapIntegrationLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusInputStreamMapIntegrationLiveTest")
public class WalrusInputStreamMapIntegrationLiveTest extends BaseInputStreamMapIntegrationTest {
public class WalrusInputStreamMapIntegrationLiveTest extends S3InputStreamMapIntegrationLiveTest {
}

View File

@ -19,13 +19,13 @@
package org.jclouds.walrus.blobstore;
import org.jclouds.blobstore.integration.internal.BaseServiceIntegrationTest;
import org.jclouds.s3.blobstore.integration.S3ServiceIntegrationLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "WalrusServiceIntegrationLiveTest")
public class WalrusServiceIntegrationLiveTest extends BaseServiceIntegrationTest {
public class WalrusServiceIntegrationLiveTest extends S3ServiceIntegrationLiveTest {
}

View File

@ -19,33 +19,18 @@
package org.jclouds.walrus.blobstore;
import java.io.IOException;
import org.jclouds.s3.blobstore.integration.S3TestInitializer;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStoreContextFactory;
import org.jclouds.blobstore.integration.TransientBlobStoreTestInitializer;
import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
*
* @author Adrian Cole
*/
public class WalrusTestInitializer extends TransientBlobStoreTestInitializer {
public class WalrusTestInitializer extends S3TestInitializer {
public WalrusTestInitializer() {
provider = "walrus";
BaseBlobStoreIntegrationTest.SANITY_CHECK_RETURNED_BUCKET_NAME = true;
}
@Override
protected BlobStoreContext createLiveContext(Module configurationModule, String endpoint, String apiversion,
String app, String identity, String credential) throws IOException {
return new BlobStoreContextFactory().createContext(provider, ImmutableSet.of(configurationModule,
new Log4JLoggingModule()), setupProperties(endpoint, apiversion, identity, credential));
}
}

View File

@ -97,7 +97,7 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
// Walrus
if (lastModified.startsWith("20")) {
metadata.setLastModified(dateParser.iso8601DateParse(lastModified));
metadata.setLastModified(dateParser.iso8601DateParse(lastModified.replace("+0000", "Z")));
} else {
metadata.setLastModified(dateParser.rfc822DateParse(lastModified));
}

View File

@ -86,6 +86,18 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
.rfc822DateParse("Wed, 09 Sep 2009 19:50:23 GMT"));
}
@Test
public void testSetLastModifiedIso8601() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
HttpHeaders.LAST_MODIFIED, "2011-01-28T17:35:08.000+0000"));
MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.parseLastModifiedOrThrowException(from, metadata);
assertEquals(metadata.getLastModified(), new SimpleDateFormatDateService()
.iso8601DateParse("2011-01-28T17:35:08.000Z"));
}
@Test(expectedExceptions = HttpException.class)
public void testSetLastModifiedException() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""));