Issue 298: fixed regression on authorizationexception not propagating, and also missing gogrid file

This commit is contained in:
Adrian Cole 2010-07-02 12:05:41 -07:00
parent 32e7ca0a1f
commit 9bac52d315
10 changed files with 397 additions and 310 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.blobstore; package org.jclouds.blobstore;
import static org.jclouds.rest.RestContextFactory.createContextBuilder; import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import java.util.Properties; import java.util.Properties;
@ -63,21 +64,31 @@ public class BlobStoreContextFactory {
this.contextFactory = restContextFactory; this.contextFactory = restContextFactory;
} }
public static <S, A> BlobStoreContext buildContextUnwrappingExceptions(
BlobStoreContextBuilder<S, A> builder) {
try {
return builder.buildBlobStoreContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
}
/** /**
* @see RestContextFactory#createContextBuilder(String, String, String) * @see RestContextFactory#createContextBuilder(String, String, String)
*/ */
public BlobStoreContext createContext(String provider, String identity, String credential) { public BlobStoreContext createContext(String provider, String identity, String credential) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
contextFactory.createContextBuilder(provider, identity, credential)) .createContextBuilder(provider, identity, credential));
.buildBlobStoreContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Properties) * @see RestContextFactory#createContextBuilder(String, Properties)
*/ */
public BlobStoreContext createContext(String provider, Properties overrides) { public BlobStoreContext createContext(String provider, Properties overrides) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
contextFactory.createContextBuilder(provider, overrides)).buildBlobStoreContext(); .createContextBuilder(provider, overrides));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -85,9 +96,10 @@ public class BlobStoreContextFactory {
*/ */
public BlobStoreContext createContext(String provider, Iterable<? extends Module> modules, public BlobStoreContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) { Properties overrides) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
contextFactory.createContextBuilder(provider, modules, overrides)) .createContextBuilder(provider, modules, overrides));
.buildBlobStoreContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -95,27 +107,29 @@ public class BlobStoreContextFactory {
*/ */
public BlobStoreContext createContext(String provider, @Nullable String identity, public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) { @Nullable String credential, Iterable<? extends Module> modules) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
contextFactory.createContextBuilder(provider, identity, credential, modules)) .createContextBuilder(provider, identity, credential, modules));
.buildBlobStoreContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/ */
public BlobStoreContext createContext(String providerName, @Nullable String identity, public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) { @Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
contextFactory.createContextBuilder(providerName, identity, credential, modules, .createContextBuilder(provider, identity, credential, modules, overrides));
overrides)).buildBlobStoreContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(ContextSpec) * @see RestContextFactory#createContextBuilder(ContextSpec)
*/ */
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec) { public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec)) BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.buildBlobStoreContext(); .cast(createContextBuilder(contextSpec));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -123,16 +137,18 @@ public class BlobStoreContextFactory {
*/ */
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) { Iterable<? extends Module> modules) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, modules)) BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.buildBlobStoreContext(); .cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(ContextSpec, Properties) * @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/ */
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) { public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, overrides)) BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.buildBlobStoreContext(); .cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -140,7 +156,8 @@ public class BlobStoreContextFactory {
*/ */
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
return BlobStoreContextBuilder.class.cast( BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
createContextBuilder(contextSpec, modules, overrides)).buildBlobStoreContext(); .cast(createContextBuilder(contextSpec, modules, overrides));
return buildContextUnwrappingExceptions(builder);
} }
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.compute; package org.jclouds.compute;
import static org.jclouds.rest.RestContextFactory.createContextBuilder; import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import java.util.Properties; import java.util.Properties;
@ -63,22 +64,31 @@ public class ComputeServiceContextFactory {
this.contextFactory = restContextFactory; this.contextFactory = restContextFactory;
} }
public static <S, A> ComputeServiceContext buildContextUnwrappingExceptions(
ComputeServiceContextBuilder<S, A> builder) {
try {
return builder.buildComputeServiceContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
}
/** /**
* @see RestContextFactory#createContextBuilder(String, String, String) * @see RestContextFactory#createContextBuilder(String, String, String)
*/ */
public ComputeServiceContext createContext(String provider, String identity, String credential) { public ComputeServiceContext createContext(String provider, String identity, String credential) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
contextFactory.createContextBuilder(provider, identity, credential)) .cast(contextFactory.createContextBuilder(provider, identity, credential));
.buildComputeServiceContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Properties) * @see RestContextFactory#createContextBuilder(String, Properties)
*/ */
public ComputeServiceContext createContext(String provider, Properties overrides) { public ComputeServiceContext createContext(String provider, Properties overrides) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
contextFactory.createContextBuilder(provider, overrides)) .cast(contextFactory.createContextBuilder(provider, overrides));
.buildComputeServiceContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -86,9 +96,10 @@ public class ComputeServiceContextFactory {
*/ */
public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules, public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) { Properties overrides) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
contextFactory.createContextBuilder(provider, modules, overrides)) .cast(contextFactory.createContextBuilder(provider, modules, overrides));
.buildComputeServiceContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -96,27 +107,30 @@ public class ComputeServiceContextFactory {
*/ */
public ComputeServiceContext createContext(String provider, @Nullable String identity, public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) { @Nullable String credential, Iterable<? extends Module> modules) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
contextFactory.createContextBuilder(provider, identity, credential, modules)) .cast(contextFactory.createContextBuilder(provider, identity, credential, modules));
.buildComputeServiceContext(); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/ */
public ComputeServiceContext createContext(String providerName, @Nullable String identity, public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) { @Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
contextFactory.createContextBuilder(providerName, identity, credential, modules, .cast(contextFactory.createContextBuilder(provider, identity, credential, modules,
overrides)).buildComputeServiceContext(); overrides));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(ContextSpec) * @see RestContextFactory#createContextBuilder(ContextSpec)
*/ */
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec) { public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec)) ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.buildComputeServiceContext(); .cast(createContextBuilder(contextSpec));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -124,8 +138,9 @@ public class ComputeServiceContextFactory {
*/ */
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) { Iterable<? extends Module> modules) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, modules)) ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.buildComputeServiceContext(); .cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -133,8 +148,9 @@ public class ComputeServiceContextFactory {
*/ */
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Properties overrides) { Properties overrides) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, overrides)) ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.buildComputeServiceContext(); .cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -142,7 +158,8 @@ public class ComputeServiceContextFactory {
*/ */
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
return ComputeServiceContextBuilder.class.cast( ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
createContextBuilder(contextSpec, modules, overrides)).buildComputeServiceContext(); .cast(createContextBuilder(contextSpec, modules, overrides));
return buildContextUnwrappingExceptions(builder);
} }
} }

View File

@ -398,7 +398,7 @@ public class StubComputeServiceContextModule extends AbstractModule {
@Singleton @Singleton
Location getLocation(@org.jclouds.rest.annotations.Provider String providerName) { Location getLocation(@org.jclouds.rest.annotations.Provider String providerName) {
Location provider = new LocationImpl(LocationScope.PROVIDER, providerName, providerName, null); Location provider = new LocationImpl(LocationScope.PROVIDER, providerName, providerName, null);
return new LocationImpl(LocationScope.ZONE, "memory", "memory", provider); return new LocationImpl(LocationScope.ZONE, providerName, providerName, provider);
} }
@Provides @Provides

View File

@ -18,14 +18,12 @@
*/ */
package org.jclouds.compute.util; package org.jclouds.compute.util;
import java.util.Formatter; import java.util.Formatter;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import org.jclouds.compute.ComputeServiceContextBuilder; import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.compute.domain.Architecture; import org.jclouds.compute.domain.Architecture;
import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.ComputeMetadata;
@ -145,7 +143,8 @@ public class ComputeServiceUtils {
public static boolean isKeyAuth(NodeMetadata createdNode) { public static boolean isKeyAuth(NodeMetadata createdNode) {
return createdNode.getCredentials().credential != null return createdNode.getCredentials().credential != null
&& createdNode.getCredentials().credential.startsWith("-----BEGIN RSA PRIVATE KEY-----"); && createdNode.getCredentials().credential
.startsWith("-----BEGIN RSA PRIVATE KEY-----");
} }
/** /**

View File

@ -99,8 +99,8 @@ public abstract class BaseComputeServiceLiveTest {
protected Map<String, String> keyPair; protected Map<String, String> keyPair;
@BeforeGroups(groups = { "integration", "live" }) @BeforeGroups(groups = { "integration", "live" })
public void setupClient() throws InterruptedException, ExecutionException, public void setupClient() throws InterruptedException, ExecutionException, TimeoutException,
TimeoutException, IOException { IOException {
if (tag == null) if (tag == null)
tag = checkNotNull(provider, "provider"); tag = checkNotNull(provider, "provider");
setupCredentials(); setupCredentials();
@ -110,33 +110,29 @@ public abstract class BaseComputeServiceLiveTest {
Injector injector = createSshClientInjector(); Injector injector = createSshClientInjector();
sshFactory = injector.getInstance(SshClient.Factory.class); sshFactory = injector.getInstance(SshClient.Factory.class);
SocketOpen socketOpen = injector.getInstance(SocketOpen.class); SocketOpen socketOpen = injector.getInstance(SocketOpen.class);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
TimeUnit.SECONDS);
injector.injectMembers(socketOpen); // add logger injector.injectMembers(socketOpen); // add logger
} }
protected void setupKeyPair() throws FileNotFoundException, IOException { protected void setupKeyPair() throws FileNotFoundException, IOException {
String secretKeyFile; String secretKeyFile;
try { try {
secretKeyFile = checkNotNull(System secretKeyFile = checkNotNull(System.getProperty("jclouds.test.ssh.keyfile"),
.getProperty("jclouds.test.ssh.keyfile"), "jclouds.test.ssh.keyfile");
"jclouds.test.ssh.keyfile");
} catch (NullPointerException e) { } catch (NullPointerException e) {
secretKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa"; secretKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa";
} }
checkSecretKeyFile(secretKeyFile); checkSecretKeyFile(secretKeyFile);
String secret = Files.toString(new File(secretKeyFile), Charsets.UTF_8); String secret = Files.toString(new File(secretKeyFile), Charsets.UTF_8);
assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" + secret;
+ secret; keyPair = ImmutableMap.<String, String> of("private", secret, "public", Files.toString(
keyPair = ImmutableMap.<String, String> of("private", secret, "public", new File(secretKeyFile + ".pub"), Charsets.UTF_8));
Files.toString(new File(secretKeyFile + ".pub"), Charsets.UTF_8));
} }
protected void setupCredentials() { protected void setupCredentials() {
identity = checkNotNull(System.getProperty("jclouds.test.identity"), identity = checkNotNull(System.getProperty("jclouds.test.identity"), "jclouds.test.identity");
"jclouds.test.identity");
credential = checkNotNull(System.getProperty("jclouds.test.credential"), credential = checkNotNull(System.getProperty("jclouds.test.credential"),
"jclouds.test.credential"); "jclouds.test.credential");
} }
protected Injector createSshClientInjector() { protected Injector createSshClientInjector() {
@ -146,20 +142,16 @@ public abstract class BaseComputeServiceLiveTest {
private void initializeContextAndClient() throws IOException { private void initializeContextAndClient() throws IOException {
if (context != null) if (context != null)
context.close(); context.close();
context = new ComputeServiceContextFactory() context = new ComputeServiceContextFactory().createContext(provider, identity, credential,
.createContext(provider, identity, credential, ImmutableSet.of( ImmutableSet.of(new Log4JLoggingModule(), getSshModule()));
new Log4JLoggingModule(), getSshModule()));
client = context.getComputeService(); client = context.getComputeService();
} }
private void checkSecretKeyFile(String secretKeyFile) private void checkSecretKeyFile(String secretKeyFile) throws FileNotFoundException {
throws FileNotFoundException { Utils.checkNotEmpty(secretKeyFile,
Utils "System property: [jclouds.test.ssh.keyfile] set to an empty string");
.checkNotEmpty(secretKeyFile,
"System property: [jclouds.test.ssh.keyfile] set to an empty string");
if (!new File(secretKeyFile).exists()) { if (!new File(secretKeyFile).exists()) {
throw new FileNotFoundException("secretKeyFile not found at: " throw new FileNotFoundException("secretKeyFile not found at: " + secretKeyFile);
+ secretKeyFile);
} }
} }
@ -169,10 +161,10 @@ public abstract class BaseComputeServiceLiveTest {
@Test(enabled = true, expectedExceptions = AuthorizationException.class) @Test(enabled = true, expectedExceptions = AuthorizationException.class)
public void testCorrectAuthException() throws Exception { public void testCorrectAuthException() throws Exception {
new ComputeServiceContextFactory().createContext(provider, "MOMMA", "MIA", new ComputeServiceContextFactory().createContext(provider, "MOMMA", "MIA",
ImmutableSet.<Module> of(new Log4JLoggingModule())).close(); ImmutableSet.<Module> of(new Log4JLoggingModule())).close();
} }
@Test(enabled = true) @Test(enabled = true, dependsOnMethods = "testCorrectAuthException")
public void testImagesCache() throws Exception { public void testImagesCache() throws Exception {
client.listImages(); client.listImages();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
@ -183,9 +175,8 @@ public abstract class BaseComputeServiceLiveTest {
// since surefire and eclipse don't otherwise guarantee the order, we are // since surefire and eclipse don't otherwise guarantee the order, we are
// starting this one alphabetically before create2nodes.. // starting this one alphabetically before create2nodes..
@Test(enabled = true, dependsOnMethods = "testImagesCache") @Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testAScriptExecutionAfterBootWithBasicTemplate() public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
throws Exception {
String tag = this.tag + "run"; String tag = this.tag + "run";
try { try {
client.destroyNodesMatching(NodePredicates.withTag(tag)); client.destroyNodesMatching(NodePredicates.withTag(tag));
@ -195,21 +186,18 @@ public abstract class BaseComputeServiceLiveTest {
TemplateOptions options = client.templateOptions().blockOnPort(22, 120); TemplateOptions options = client.templateOptions().blockOnPort(22, 120);
try { try {
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, options);
options);
Credentials good = nodes.iterator().next().getCredentials(); Credentials good = nodes.iterator().next().getCredentials();
assert good.identity != null; assert good.identity != null;
assert good.credential != null; assert good.credential != null;
Image image = Iterables.get(nodes, 0).getImage(); Image image = Iterables.get(nodes, 0).getImage();
try { try {
Map<? extends NodeMetadata, ExecResponse> responses = runScriptWithCreds( Map<? extends NodeMetadata, ExecResponse> responses = runScriptWithCreds(tag, image
tag, image.getOsFamily(), new Credentials(good.identity, .getOsFamily(), new Credentials(good.identity, "romeo"));
"romeo"));
assert false : "shouldn't pass with a bad password\n" + responses; assert false : "shouldn't pass with a bad password\n" + responses;
} catch (RunScriptOnNodesException e) { } catch (RunScriptOnNodesException e) {
assert Throwables.getRootCause(e).getMessage() assert Throwables.getRootCause(e).getMessage().contains("Auth fail") : e;
.contains("Auth fail") : e;
} }
runScriptWithCreds(tag, image.getOsFamily(), good); runScriptWithCreds(tag, image.getOsFamily(), good);
@ -221,11 +209,10 @@ public abstract class BaseComputeServiceLiveTest {
} }
} }
@Test(enabled = true, dependsOnMethods = "testImagesCache") @Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testTemplateMatch() throws Exception { public void testTemplateMatch() throws Exception {
template = buildTemplate(client.templateBuilder()); template = buildTemplate(client.templateBuilder());
Template toMatch = client.templateBuilder().imageId( Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build();
template.getImage().getId()).build();
assertEquals(toMatch.getImage(), template.getImage()); assertEquals(toMatch.getImage(), template.getImage());
} }
@ -240,14 +227,14 @@ public abstract class BaseComputeServiceLiveTest {
} }
template = buildTemplate(client.templateBuilder()); template = buildTemplate(client.templateBuilder());
template.getOptions().installPrivateKey(keyPair.get("private")) template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(
.authorizePublicKey(keyPair.get("public")).runScript( keyPair.get("public")).runScript(
buildScript(template.getImage().getOsFamily()).getBytes()); buildScript(template.getImage().getOsFamily()).getBytes());
try { try {
nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template)); nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template));
} catch (RunNodesException e) { } catch (RunNodesException e) {
nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e.getNodeErrors()
.getNodeErrors().keySet())); .keySet()));
throw e; throw e;
} }
assertEquals(nodes.size(), 2); assertEquals(nodes.size(), 2);
@ -274,11 +261,9 @@ public abstract class BaseComputeServiceLiveTest {
} }
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript") @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
throws Exception {
initializeContextAndClient(); initializeContextAndClient();
TreeSet<NodeMetadata> nodes = Sets.newTreeSet(client.runNodesWithTag(tag, TreeSet<NodeMetadata> nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 1, template));
1, template));
checkNodes(nodes, tag); checkNodes(nodes, tag);
NodeMetadata node = nodes.first(); NodeMetadata node = nodes.first();
this.nodes.add(node); this.nodes.add(node);
@ -287,13 +272,11 @@ public abstract class BaseComputeServiceLiveTest {
assertEquals(node.getImage(), template.getImage()); assertEquals(node.getImage(), template.getImage());
} }
protected Map<? extends NodeMetadata, ExecResponse> runScriptWithCreds( protected Map<? extends NodeMetadata, ExecResponse> runScriptWithCreds(final String tag,
final String tag, OsFamily osFamily, Credentials creds) OsFamily osFamily, Credentials creds) throws RunScriptOnNodesException {
throws RunScriptOnNodesException {
try { try {
return client.runScriptOnNodesMatching(NodePredicates return client.runScriptOnNodesMatching(NodePredicates.runningWithTag(tag), buildScript(
.runningWithTag(tag), buildScript(osFamily).getBytes(), osFamily).getBytes(), RunScriptOptions.Builder.overrideCredentialsWith(creds));
RunScriptOptions.Builder.overrideCredentialsWith(creds));
} catch (SshException e) { } catch (SshException e) {
if (Throwables.getRootCause(e).getMessage().contains("Auth fail")) { if (Throwables.getRootCause(e).getMessage().contains("Auth fail")) {
// System.err.printf("bad credentials: %s:%s for %s%n", // System.err.printf("bad credentials: %s:%s for %s%n",
@ -304,15 +287,14 @@ public abstract class BaseComputeServiceLiveTest {
} }
} }
protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag) protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag) throws IOException {
throws IOException {
for (NodeMetadata node : nodes) { for (NodeMetadata node : nodes) {
assertNotNull(node.getProviderId()); assertNotNull(node.getProviderId());
assertNotNull(node.getTag()); assertNotNull(node.getTag());
assertEquals(node.getTag(), tag); assertEquals(node.getTag(), tag);
assertEquals(node.getState(), NodeState.RUNNING); assertEquals(node.getState(), NodeState.RUNNING);
assert node.getPublicAddresses().size() >= 1 assert node.getPublicAddresses().size() >= 1 || node.getPrivateAddresses().size() >= 1 : "no ips in"
|| node.getPrivateAddresses().size() >= 1 : "no ips in" + node; + node;
assertNotNull(node.getCredentials()); assertNotNull(node.getCredentials());
if (node.getCredentials().identity != null) { if (node.getCredentials().identity != null) {
assertNotNull(node.getCredentials().identity); assertNotNull(node.getCredentials().identity);
@ -328,55 +310,49 @@ public abstract class BaseComputeServiceLiveTest {
protected String buildScript(OsFamily osFamily) { protected String buildScript(OsFamily osFamily) {
switch (osFamily) { switch (osFamily) {
case UBUNTU: case UBUNTU:
return new StringBuilder()// return new StringBuilder()//
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")// .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")//
.append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")// .append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")//
.append( .append(
"sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")// "sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")//
.append("apt-get update\n")// .append("apt-get update\n")//
.append("apt-get install -f -y --force-yes openjdk-6-jdk\n")// .append("apt-get install -f -y --force-yes openjdk-6-jdk\n")//
.append("wget -qO/usr/bin/runurl run.alestic.com/runurl\n")// .append("wget -qO/usr/bin/runurl run.alestic.com/runurl\n")//
.append("chmod 755 /usr/bin/runurl\n")// .append("chmod 755 /usr/bin/runurl\n")//
.toString(); .toString();
case CENTOS: case CENTOS:
case RHEL: case RHEL:
return new StringBuilder() return new StringBuilder()
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n") .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")
.append( .append("echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
"echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n") .append("echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append( .append(
"echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n") "echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append( .append("echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
"echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n") .append("yum --nogpgcheck -y install java-1.6.0-openjdk\n")
.append( .append(
"echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n") "echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n")
.append("yum --nogpgcheck -y install java-1.6.0-openjdk\n") .toString();
.append( default:
"echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n") throw new IllegalArgumentException(osFamily.toString());
.toString();
default:
throw new IllegalArgumentException(osFamily.toString());
} }
} }
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired") @Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception { public void testGet() throws Exception {
Set<? extends NodeMetadata> metadataSet = Sets.newHashSet(Iterables Set<? extends NodeMetadata> metadataSet = Sets.newHashSet(Iterables.filter(client
.filter(client.listNodesDetailsMatching(NodePredicates.all()), .listNodesDetailsMatching(NodePredicates.all()), Predicates.and(NodePredicates
Predicates.and(NodePredicates.withTag(tag), Predicates .withTag(tag), Predicates.not(NodePredicates.TERMINATED))));
.not(NodePredicates.TERMINATED))));
for (NodeMetadata node : nodes) { for (NodeMetadata node : nodes) {
metadataSet.remove(node); metadataSet.remove(node);
NodeMetadata metadata = client.getNodeMetadata(node.getId()); NodeMetadata metadata = client.getNodeMetadata(node.getId());
assertEquals(metadata.getProviderId(), node.getProviderId()); assertEquals(metadata.getProviderId(), node.getProviderId());
assertEquals(metadata.getTag(), node.getTag()); assertEquals(metadata.getTag(), node.getTag());
assertLocationSameOrChild(metadata.getLocation(), template assertLocationSameOrChild(metadata.getLocation(), template.getLocation());
.getLocation());
assertEquals(metadata.getImage(), template.getImage()); assertEquals(metadata.getImage(), template.getImage());
assertEquals(metadata.getState(), NodeState.RUNNING); assertEquals(metadata.getState(), NodeState.RUNNING);
assertEquals(metadata.getPrivateAddresses(), node assertEquals(metadata.getPrivateAddresses(), node.getPrivateAddresses());
.getPrivateAddresses());
assertEquals(metadata.getPublicAddresses(), node.getPublicAddresses()); assertEquals(metadata.getPublicAddresses(), node.getPublicAddresses());
} }
assertNodeZero(metadataSet); assertNodeZero(metadataSet);
@ -384,8 +360,7 @@ public abstract class BaseComputeServiceLiveTest {
protected void assertNodeZero(Set<? extends NodeMetadata> metadataSet) { protected void assertNodeZero(Set<? extends NodeMetadata> metadataSet) {
assert metadataSet.size() == 0 : String.format( assert metadataSet.size() == 0 : String.format(
"nodes left in set: [%s] which didn't match set: [%s]", "nodes left in set: [%s] which didn't match set: [%s]", metadataSet, nodes);
metadataSet, nodes);
} }
@Test(enabled = true, dependsOnMethods = "testGet") @Test(enabled = true, dependsOnMethods = "testGet")
@ -400,7 +375,7 @@ public abstract class BaseComputeServiceLiveTest {
TemplateOptions options = new TemplateOptions().withMetadata(); TemplateOptions options = new TemplateOptions().withMetadata();
Template t = client.templateBuilder().smallest().options(options).build(); Template t = client.templateBuilder().smallest().options(options).build();
assert t.getOptions().isIncludeMetadata() : "The metadata option should be 'true' " assert t.getOptions().isIncludeMetadata() : "The metadata option should be 'true' "
+ "for the created template"; + "for the created template";
} }
public void testListNodes() throws Exception { public void testListNodes() throws Exception {
@ -412,8 +387,7 @@ public abstract class BaseComputeServiceLiveTest {
} }
public void testGetNodesWithDetails() throws Exception { public void testGetNodesWithDetails() throws Exception {
for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates.all())) {
.all())) {
assert node.getProviderId() != null : node; assert node.getProviderId() != null : node;
assert node.getLocation() != null : node; assert node.getLocation() != null : node;
assertEquals(node.getType(), ComputeType.NODE); assertEquals(node.getType(), ComputeType.NODE);
@ -427,7 +401,7 @@ public abstract class BaseComputeServiceLiveTest {
if (nodeMetadata.getState() == NodeState.RUNNING) { if (nodeMetadata.getState() == NodeState.RUNNING) {
assert nodeMetadata.getPublicAddresses() != null : nodeMetadata; assert nodeMetadata.getPublicAddresses() != null : nodeMetadata;
assert nodeMetadata.getPublicAddresses().size() > 0 assert nodeMetadata.getPublicAddresses().size() > 0
|| nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata; || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata;
assertNotNull(nodeMetadata.getPrivateAddresses()); assertNotNull(nodeMetadata.getPrivateAddresses());
} }
} }
@ -449,26 +423,26 @@ public abstract class BaseComputeServiceLiveTest {
assert location != location.getParent() : location; assert location != location.getParent() : location;
assert location.getScope() != null : location; assert location.getScope() != null : location;
switch (location.getScope()) { switch (location.getScope()) {
case PROVIDER: case PROVIDER:
assertProvider(location); assertProvider(location);
break; break;
case REGION: case REGION:
assertProvider(location.getParent()); assertProvider(location.getParent());
break; break;
case ZONE: case ZONE:
Location provider = location.getParent().getParent(); Location provider = location.getParent().getParent();
// zone can be a direct descendant of provider // zone can be a direct descendant of provider
if (provider == null) if (provider == null)
provider = location.getParent(); provider = location.getParent();
assertProvider(provider); assertProvider(provider);
break; break;
case HOST: case HOST:
Location provider2 = location.getParent().getParent().getParent(); Location provider2 = location.getParent().getParent().getParent();
// zone can be a direct descendant of provider // zone can be a direct descendant of provider
if (provider2 == null) if (provider2 == null)
provider2 = location.getParent().getParent(); provider2 = location.getParent().getParent();
assertProvider(provider2); assertProvider(provider2);
break; break;
} }
} }
} }
@ -482,18 +456,14 @@ public abstract class BaseComputeServiceLiveTest {
} }
// no inbound ports // no inbound ports
TemplateOptions options = client.templateOptions().blockUntilRunning( TemplateOptions options = client.templateOptions().blockUntilRunning(false).inboundPorts();
false).inboundPorts();
try { try {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, options);
options);
NodeMetadata node = Iterables.getOnlyElement(nodes); NodeMetadata node = Iterables.getOnlyElement(nodes);
assertEquals(node.getState(), NodeState.PENDING); assert node.getState() != NodeState.RUNNING;
long duration = System.currentTimeMillis() - time; long duration = System.currentTimeMillis() - time;
assert duration < 30 * 1000 : "duration longer than 30 seconds!: " assert duration < 30 * 1000 : "duration longer than 30 seconds!: " + duration / 1000;
+ duration / 1000;
} finally { } finally {
client.destroyNodesMatching(NodePredicates.withTag(tag)); client.destroyNodesMatching(NodePredicates.withTag(tag));
} }
@ -529,15 +499,13 @@ public abstract class BaseComputeServiceLiveTest {
} }
} }
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException {
throws IOException { IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), 22);
IPSocket socket = new IPSocket(Iterables
.get(node.getPublicAddresses(), 0), 22);
socketTester.apply(socket); // TODO add transitionTo option that accepts socketTester.apply(socket); // TODO add transitionTo option that accepts
// a socket conection // a socket conection
// state. // state.
SshClient ssh = sshFactory.create(socket, node.getCredentials().identity, SshClient ssh = sshFactory.create(socket, node.getCredentials().identity, keyPair.get(
keyPair.get("private").getBytes()); "private").getBytes());
try { try {
ssh.connect(); ssh.connect();
ExecResponse hello = ssh.exec("echo hello"); ExecResponse hello = ssh.exec("echo hello");
@ -551,13 +519,11 @@ public abstract class BaseComputeServiceLiveTest {
} }
@AfterTest @AfterTest
protected void cleanup() throws InterruptedException, ExecutionException, protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException {
TimeoutException {
if (nodes != null) { if (nodes != null) {
client.destroyNodesMatching(NodePredicates.withTag(tag)); client.destroyNodesMatching(NodePredicates.withTag(tag));
for (NodeMetadata node : Iterables.filter(client for (NodeMetadata node : Iterables.filter(client.listNodesDetailsMatching(NodePredicates
.listNodesDetailsMatching(NodePredicates.all()), NodePredicates .all()), NodePredicates.withTag(tag))) {
.withTag(tag))) {
assert node.getState() == NodeState.TERMINATED : node; assert node.getState() == NodeState.TERMINATED : node;
} }
} }

View File

@ -66,8 +66,7 @@ import com.google.inject.Module;
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "live", enabled = true, sequential = true, testName = "stub.StubComputeServiceIntegrationTest") @Test(groups = "live", enabled = true, sequential = true, testName = "stub.StubComputeServiceIntegrationTest")
public class StubComputeServiceIntegrationTest extends public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTest {
BaseComputeServiceLiveTest {
private static final ExecResponse EXEC_GOOD = new ExecResponse("", "", 0); private static final ExecResponse EXEC_GOOD = new ExecResponse("", "", 0);
private static final ExecResponse EXEC_BAD = new ExecResponse("", "", 1); private static final ExecResponse EXEC_BAD = new ExecResponse("", "", 1);
@ -85,10 +84,9 @@ public class StubComputeServiceIntegrationTest extends
@Test @Test
public void testTemplateBuilder() { public void testTemplateBuilder() {
Template defaultTemplate = client.templateBuilder().build(); Template defaultTemplate = client.templateBuilder().build();
assertEquals(defaultTemplate.getImage().getArchitecture(), assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64);
Architecture.X86_64);
assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU); assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU);
assertEquals(defaultTemplate.getLocation().getId(), "memory"); assertEquals(defaultTemplate.getLocation().getId(), provider);
assertEquals(defaultTemplate.getSize().getCores(), 4.0d); assertEquals(defaultTemplate.getSize().getCores(), 4.0d);
} }
@ -111,21 +109,17 @@ public class StubComputeServiceIntegrationTest extends
expect(open.apply(new IPSocket("144.175.1.4", 22))).andReturn(true); expect(open.apply(new IPSocket("144.175.1.4", 22))).andReturn(true);
expect( expect(
factory.create(eq(new IPSocket("144.175.1.1", 22)), factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair
eq("root"), aryEq(keyPair.get("private").getBytes()))) .get("private").getBytes()))).andReturn(client1).atLeastOnce();
.andReturn(client1).atLeastOnce();
expect( expect(
factory.create(eq(new IPSocket("144.175.1.2", 22)), factory.create(eq(new IPSocket("144.175.1.2", 22)), eq("root"), aryEq(keyPair
eq("root"), aryEq(keyPair.get("private").getBytes()))) .get("private").getBytes()))).andReturn(client2).atLeastOnce();
.andReturn(client2).atLeastOnce();
expect( expect(
factory.create(eq(new IPSocket("144.175.1.3", 22)), factory.create(eq(new IPSocket("144.175.1.3", 22)), eq("root"), aryEq(keyPair
eq("root"), aryEq(keyPair.get("private").getBytes()))) .get("private").getBytes()))).andReturn(client3).atLeastOnce();
.andReturn(client3).atLeastOnce();
expect( expect(
factory.create(eq(new IPSocket("144.175.1.4", 22)), factory.create(eq(new IPSocket("144.175.1.4", 22)), eq("root"), aryEq(keyPair
eq("root"), aryEq(keyPair.get("private").getBytes()))) .get("private").getBytes()))).andReturn(client4).atLeastOnce();
.andReturn(client4).atLeastOnce();
helloAndJava(client1); helloAndJava(client1);
helloAndJava(client2); helloAndJava(client2);
@ -147,10 +141,8 @@ public class StubComputeServiceIntegrationTest extends
private void helloAndJava(SshClient client) { private void helloAndJava(SshClient client) {
client.connect(); client.connect();
expect(client.exec("echo hello")).andReturn( expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0));
new ExecResponse("hello", "", 0)); expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0));
expect(client.exec("java -version")).andReturn(
new ExecResponse("", "OpenJDK", 0));
client.disconnect(); client.disconnect();
} }
@ -170,26 +162,21 @@ public class StubComputeServiceIntegrationTest extends
SshClient client3 = createMock(SshClient.class); SshClient client3 = createMock(SshClient.class);
SshClient client4 = createMock(SshClient.class); SshClient client4 = createMock(SshClient.class);
expect( expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "romeo")).andThrow(
factory.create(new IPSocket("144.175.1.1", 22), "root", new SshException("Auth fail"));
"romeo")).andThrow(new SshException("Auth fail")); expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "password1")).andReturn(
expect( client1).atLeastOnce();
factory.create(new IPSocket("144.175.1.1", 22), "root",
"password1")).andReturn(client1).atLeastOnce();
client1.connect(); client1.connect();
runScript(client1, "computeserv", 1); runScript(client1, "computeserv", 1);
client1.disconnect(); client1.disconnect();
expect( expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn(
factory.create(new IPSocket("144.175.1.2", 22), "root", client2).atLeastOnce();
"password2")).andReturn(client2).atLeastOnce(); expect(factory.create(new IPSocket("144.175.1.3", 22), "root", "password3")).andReturn(
expect( client3).atLeastOnce();
factory.create(new IPSocket("144.175.1.3", 22), "root", expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn(
"password3")).andReturn(client3).atLeastOnce(); client4).atLeastOnce();
expect(
factory.create(new IPSocket("144.175.1.4", 22), "root",
"password4")).andReturn(client4).atLeastOnce();
runScriptAndInstallSsh(client2, "runscript", 2); runScriptAndInstallSsh(client2, "runscript", 2);
runScriptAndInstallSsh(client3, "runscript", 3); runScriptAndInstallSsh(client3, "runscript", 3);
@ -204,17 +191,14 @@ public class StubComputeServiceIntegrationTest extends
bind(SshClient.Factory.class).toInstance(factory); bind(SshClient.Factory.class).toInstance(factory);
} }
private void runScriptAndInstallSsh(SshClient client, private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
String scriptName, int nodeId) {
client.connect(); client.connect();
runScript(client, scriptName, nodeId); runScript(client, scriptName, nodeId);
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD); expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")) expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")).andReturn(EXEC_GOOD);
.andReturn(EXEC_GOOD); expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(EXEC_GOOD);
expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(
EXEC_GOOD);
client.put(eq(".ssh/id_rsa.pub"), isEq(keyPair.get("public"))); client.put(eq(".ssh/id_rsa.pub"), isEq(keyPair.get("public")));
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD); expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
@ -228,26 +212,18 @@ public class StubComputeServiceIntegrationTest extends
private void runScript(SshClient client, String scriptName, int nodeId) { private void runScript(SshClient client, String scriptName, int nodeId) {
client.put(eq("" + scriptName + ""), isEq(initScript(scriptName, client.put(eq("" + scriptName + ""), isEq(initScript(scriptName,
buildScript(OsFamily.UBUNTU)))); buildScript(OsFamily.UBUNTU))));
expect(client.exec("chmod 755 " + scriptName + "")).andReturn( expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD);
EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce(); expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn(nodeId + "") expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce();
.atLeastOnce(); expect(client.exec("./" + scriptName + " init")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " init")).andReturn( expect(client.exec("./" + scriptName + " start")).andReturn(EXEC_GOOD);
EXEC_GOOD); expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " start")).andReturn(
EXEC_GOOD);
expect(client.exec("./" + scriptName + " status")).andReturn(
EXEC_GOOD);
// next status says the script is done, since not found. // next status says the script is done, since not found.
expect(client.exec("./" + scriptName + " status")).andReturn( expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_BAD);
EXEC_BAD); expect(client.exec("./" + scriptName + " tail")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " tail")).andReturn( expect(client.exec("./" + scriptName + " tailerr")).andReturn(EXEC_GOOD);
EXEC_GOOD);
expect(client.exec("./" + scriptName + " tailerr")).andReturn(
EXEC_GOOD);
} }
}; };
@ -264,11 +240,10 @@ public class StubComputeServiceIntegrationTest extends
} }
public static String initScript(String scriptName, String script) { public static String initScript(String scriptName, String script) {
return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" + scriptName, ImmutableMap
+ scriptName, ImmutableMap.<String, String> of(), Iterables .<String, String> of(), Iterables.toArray(Splitter.on("\n").split(
.toArray(Splitter.on("\n").split( new String(checkNotNull(script, "script"))), String.class))
new String(checkNotNull(script, "script"))), String.class)) .build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX);
.build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX);
} }
public static InputStream isEq(String value) { public static InputStream isEq(String value) {
@ -279,12 +254,10 @@ public class StubComputeServiceIntegrationTest extends
public void testAssignability() throws Exception { public void testAssignability() throws Exception {
@SuppressWarnings("unused") @SuppressWarnings("unused")
RestContext<ConcurrentMap<Integer, StubNodeMetadata>, ConcurrentMap<Integer, StubNodeMetadata>> stubContext = new ComputeServiceContextFactory() RestContext<ConcurrentMap<Integer, StubNodeMetadata>, ConcurrentMap<Integer, StubNodeMetadata>> stubContext = new ComputeServiceContextFactory()
.createContext(provider, identity, credential) .createContext(provider, identity, credential).getProviderSpecificContext();
.getProviderSpecificContext();
} }
private static class InputStreamEquals implements IArgumentMatcher, private static class InputStreamEquals implements IArgumentMatcher, Serializable {
Serializable {
private static final long serialVersionUID = 583055160049982067L; private static final long serialVersionUID = 583055160049982067L;
@ -330,8 +303,8 @@ public class StubComputeServiceIntegrationTest extends
if (o == null || !this.getClass().equals(o.getClass())) if (o == null || !this.getClass().equals(o.getClass()))
return false; return false;
InputStreamEquals other = (InputStreamEquals) o; InputStreamEquals other = (InputStreamEquals) o;
return this.expected == null && other.expected == null return this.expected == null && other.expected == null || this.expected != null
|| this.expected != null && this.expected.equals(other.expected); && this.expected.equals(other.expected);
} }
@Override @Override
@ -343,8 +316,50 @@ public class StubComputeServiceIntegrationTest extends
@Override @Override
protected void setupKeyPair() throws FileNotFoundException, IOException { protected void setupKeyPair() throws FileNotFoundException, IOException {
keyPair = ImmutableMap.<String, String> of("public", "ssh-rsa", keyPair = ImmutableMap.<String, String> of("public", "ssh-rsa", "private",
"private", "-----BEGIN RSA PRIVATE KEY-----"); "-----BEGIN RSA PRIVATE KEY-----");
}
// TODO: I have absolutely no idea why I have to redeclare all this cruft. If I don't, then we
// get all sorts of not allowed to depend on errors.
@Override
public void testImagesCache() throws Exception {
super.testImagesCache();
}
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
super.testAScriptExecutionAfterBootWithBasicTemplate();
}
@Test(enabled = true, dependsOnMethods = "testTemplateMatch")
public void testCreateTwoNodesWithRunScript() throws Exception {
super.testCreateTwoNodesWithRunScript();
}
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
super.testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired();
}
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception {
super.testGet();
}
@Test(enabled = true, dependsOnMethods = "testGet")
public void testOptionToNotBlock() throws Exception {
super.testOptionToNotBlock();
}
@Test(enabled = true, dependsOnMethods = "testGet")
public void testReboot() throws Exception {
super.testReboot();
}
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testTemplateMatch() throws Exception {
super.testTemplateMatch();
} }
} }

View File

@ -19,6 +19,11 @@
package org.jclouds.rest; package org.jclouds.rest;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.toArray;
import static org.jclouds.util.Utils.initContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import static org.jclouds.util.Utils.resolveContextBuilderClass;
import static org.jclouds.util.Utils.resolvePropertiesBuilderClass;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
@ -27,11 +32,9 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import org.jclouds.PropertiesBuilder; import org.jclouds.PropertiesBuilder;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
@ -239,8 +242,8 @@ public class RestContextFactory {
Class<S> sync; Class<S> sync;
Class<A> async; Class<A> async;
try { try {
contextBuilderClass = Utils.resolveContextBuilderClass(providerName, props); contextBuilderClass = resolveContextBuilderClass(providerName, props);
propertiesBuilderClass = Utils.resolvePropertiesBuilderClass(providerName, props); propertiesBuilderClass = resolvePropertiesBuilderClass(providerName, props);
sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null); sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null);
async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null); async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null);
} catch (Exception e) { } catch (Exception e) {
@ -271,7 +274,6 @@ public class RestContextFactory {
public static <S, A> RestContextBuilder<S, A> createContextBuilder( public static <S, A> RestContextBuilder<S, A> createContextBuilder(
ContextSpec<S, A> contextSpec, Iterable<? extends Module> modules, Properties overrides) { ContextSpec<S, A> contextSpec, Iterable<? extends Module> modules, Properties overrides) {
try { try {
PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor( PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor(
Properties.class).newInstance(overrides); Properties.class).newInstance(overrides);
@ -283,69 +285,70 @@ public class RestContextFactory {
if (contextSpec.endpoint != null) if (contextSpec.endpoint != null)
builder.endpoint(contextSpec.endpoint); builder.endpoint(contextSpec.endpoint);
RestContextBuilder<S, A> contextBuilder = Utils.initContextBuilder( RestContextBuilder<S, A> contextBuilder = initContextBuilder(
contextSpec.contextBuilderClass, contextSpec.sync, contextSpec.async, builder contextSpec.contextBuilderClass, contextSpec.sync, contextSpec.async, builder
.build()); .build());
contextBuilder.withModules(Iterables.toArray(modules, Module.class)); contextBuilder.withModules(toArray(modules, Module.class));
return contextBuilder; return contextBuilder;
} catch (Exception e) { } catch (Exception e) {
AuthorizationException aex = Utils return propagateAuthorizationOrOriginalException(e);
.getFirstThrowableOfType(e, AuthorizationException.class);
if (aex != null)
throw aex;
Throwables.propagate(e);
assert false : "exception should have propogated " + e;
return null;
} }
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, String, String) * @see RestContextFactory#createContextBuilder(String, String, String)
*/ */
@SuppressWarnings("unchecked") public <S, A> RestContext<S, A> createContext(String provider, String identity, String credential) {
public <S, A> RestContext<S, A> createContext(String provider, String identity, RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential);
String credential) { return buildContextUnwrappingExceptions(builder);
return (RestContext<S, A>) createContextBuilder(provider, identity, credential) }
.buildContext();
public static <S, A> RestContext<S, A> buildContextUnwrappingExceptions(
RestContextBuilder<S, A> builder) {
try {
return builder.buildContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Properties) * @see RestContextFactory#createContextBuilder(String, Properties)
*/ */
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, Properties overrides) { public <S, A> RestContext<S, A> createContext(String provider, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(provider, overrides).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(provider, overrides);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Iterable) * @see RestContextFactory#createContextBuilder(String, Iterable)
*/ */
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, public <S, A> RestContext<S, A> createContext(String provider,
Iterable<? extends Module> modules, Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(provider, modules, overrides).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(provider, modules, overrides);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable) * @see RestContextFactory#createContextBuilder(String, String,String, Iterable)
*/ */
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity, public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) { @Nullable String credential, Iterable<? extends Module> modules) {
return (RestContext<S, A>) createContextBuilder(provider, identity, credential, modules) RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential,
.buildContext(); modules);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/ */
@SuppressWarnings("unchecked") public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity,
public <S, A> RestContext<S, A> createContext(String providerName, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) { @Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(providerName, identity, credential, modules, RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential,
overrides).buildContext(); modules, overrides);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -353,14 +356,16 @@ public class RestContextFactory {
*/ */
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
return createContextBuilder(contextSpec, modules, overrides).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules, overrides);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(ContextSpec) * @see RestContextFactory#createContextBuilder(ContextSpec)
*/ */
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec) { public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec) {
return createContextBuilder(contextSpec).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(contextSpec);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -368,7 +373,8 @@ public class RestContextFactory {
*/ */
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) { Iterable<? extends Module> modules) {
return createContextBuilder(contextSpec, modules).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules);
return buildContextUnwrappingExceptions(builder);
} }
/** /**
@ -376,6 +382,7 @@ public class RestContextFactory {
*/ */
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Properties overrides) { Properties overrides) {
return createContextBuilder(contextSpec, overrides).buildContext(); RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, overrides);
return buildContextUnwrappingExceptions(builder);
} }
} }

View File

@ -20,6 +20,14 @@ package org.jclouds.util;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.util.Patterns.CHAR_TO_PATTERN; import static org.jclouds.util.Patterns.CHAR_TO_PATTERN;
import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN; import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN;
@ -44,17 +52,16 @@ import javax.annotation.Resource;
import org.jclouds.PropertiesBuilder; import org.jclouds.PropertiesBuilder;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.RestContextBuilder; import org.jclouds.rest.RestContextBuilder;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
@ -69,6 +76,15 @@ import com.google.inject.spi.Message;
*/ */
public class Utils { public class Utils {
public static <T> T propagateAuthorizationOrOriginalException(Exception e) {
AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class);
if (aex != null)
throw aex;
Throwables.propagate(e);
assert false : "exception should have propogated " + e;
return null;
}
/** /**
* Like Ordering, but handle the case where there are multiple valid maximums * Like Ordering, but handle the case where there are multiple valid maximums
*/ */
@ -100,28 +116,31 @@ public class Utils {
} }
public static Object propagateOrNull(Exception from) { public static Object propagateOrNull(Exception from) {
Throwables.propagate(from); propagate(from);
assert false : "exception should have propogated"; assert false : "exception should have propogated";
return null; return null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends Throwable> T getFirstThrowableOfType(Throwable from, Class<T> clazz) { public static <T extends Throwable> T getFirstThrowableOfType(Throwable from, Class<T> clazz) {
if (from instanceof ProvisionException)
return getFirstThrowableOfType(ProvisionException.class.cast(from), clazz);
try { try {
return (T) Iterables.find(Throwables.getCausalChain(from), Predicates.instanceOf(clazz)); return (T) find(getCausalChain(from), instanceOf(clazz));
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
return null; return null;
} }
} }
public static Throwable firstRootCauseOrOriginalException(ProvisionException e) { public static <T extends Throwable> T getFirstThrowableOfType(ProvisionException e,
Class<T> clazz) {
for (Message message : e.getErrorMessages()) { for (Message message : e.getErrorMessages()) {
Throwable cause = Throwables.getRootCause(message.getCause()); T cause = getFirstThrowableOfType(message.getCause(), clazz);
if (cause instanceof ProvisionException) if (cause instanceof ProvisionException)
return firstRootCauseOrOriginalException(ProvisionException.class.cast(cause)); return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz);
return cause; return cause;
} }
return e; return null;
} }
public static String replaceTokens(String value, Iterable<Entry<String, String>> tokenValues) { public static String replaceTokens(String value, Iterable<Entry<String, String>> tokenValues) {
@ -319,7 +338,7 @@ public class Utils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Iterable<String> getSupportedProvidersOfTypeInProperties( public static Iterable<String> getSupportedProvidersOfTypeInProperties(
final Class<? extends RestContextBuilder> type, final Properties properties) { final Class<? extends RestContextBuilder> type, final Properties properties) {
return Iterables.filter(Iterables.transform(Iterables.filter(properties.entrySet(), return filter(transform(filter(properties.entrySet(),
new Predicate<Map.Entry<Object, Object>>() { new Predicate<Map.Entry<Object, Object>>() {
@Override @Override
@ -334,19 +353,19 @@ public class Utils {
public String apply(Entry<Object, Object> from) { public String apply(Entry<Object, Object> from) {
String keyString = from.getKey().toString(); String keyString = from.getKey().toString();
try { try {
String provider = Iterables.get(Splitter.on('.').split(keyString), 0); String provider = get(Splitter.on('.').split(keyString), 0);
Class<RestContextBuilder<Object, Object>> clazz = resolveContextBuilderClass( Class<RestContextBuilder<Object, Object>> clazz = resolveContextBuilderClass(
provider, properties); provider, properties);
if (type.isAssignableFrom(clazz)) if (type.isAssignableFrom(clazz))
return provider; return provider;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
} catch (Exception e) { } catch (Exception e) {
Throwables.propagate(e); propagate(e);
} }
return null; return null;
} }
}), Predicates.notNull()); }), notNull());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -45,7 +45,7 @@ public class UtilsTest {
AuthorizationException aex = createMock(AuthorizationException.class); AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex); Message message = new Message(ImmutableList.of(), "test", aex);
ProvisionException pex = new ProvisionException(ImmutableSet.of(message)); ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
assertEquals(Utils.firstRootCauseOrOriginalException(pex), aex); assertEquals(Utils.getFirstThrowableOfType(pex, AuthorizationException.class), aex);
} }
public void testGetFirstThrowableOfTypeOuter() { public void testGetFirstThrowableOfTypeOuter() {

View File

@ -0,0 +1,47 @@
/**
*
* Copyright (C) 2009 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.gogrid.functions;
import static org.jclouds.util.Utils.propagateOrNull;
import java.util.Set;
import javax.inject.Singleton;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ReturnEmptySetOnNotFound implements Function<Exception, Set<Server>> {
@SuppressWarnings("unchecked")
public Set<Server> apply(Exception from) {
if (from instanceof ResourceNotFoundException) {
return ImmutableSet.<Server> of();
}
return Set.class.cast(propagateOrNull(from));
}
}