deltacloud now operates on rackspace

This commit is contained in:
Adrian Cole 2011-04-04 00:50:49 -07:00
parent 53c735fdff
commit 1a51fbc8ad
7 changed files with 217 additions and 36 deletions

View File

@ -52,7 +52,6 @@ public class InstanceStateStopped implements Predicate<RunningInstance> {
public boolean apply(RunningInstance instance) {
logger.trace("looking for state on instance %s", instance);
instance = refresh(instance);
logger.trace("%s: looking for instance state %s: currently: %s", instance.getId(),
InstanceState.STOPPED, instance.getInstanceState());

View File

@ -44,7 +44,7 @@ public class TransitionOnAction implements Transition {
@Override
public String toString() {
return "TransitionOnAction [action=" + action + ", to=" + to + "]";
return "[action=" + action + ", to=" + to + "]";
}
@Override

View File

@ -0,0 +1,48 @@
/**
*
* 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.deltacloud.predicates;
import javax.inject.Singleton;
import org.jclouds.deltacloud.DeltacloudClient;
import org.jclouds.deltacloud.domain.Instance;
import com.google.inject.Inject;
/**
*
* Tests to see if a instance is terminated
*
* @author Adrian Cole
*/
@Singleton
public class InstanceFinished extends InstanceState {
@Inject
public InstanceFinished(DeltacloudClient client) {
super(client);
}
@Override
protected Instance.State getState() {
return Instance.State.FINISH;
}
}

View File

@ -0,0 +1,48 @@
/**
*
* 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.deltacloud.predicates;
import javax.inject.Singleton;
import org.jclouds.deltacloud.DeltacloudClient;
import org.jclouds.deltacloud.domain.Instance;
import com.google.inject.Inject;
/**
*
* Tests to see if a instance is running
*
* @author Adrian Cole
*/
@Singleton
public class InstanceRunning extends InstanceState {
@Inject
public InstanceRunning(DeltacloudClient client) {
super(client);
}
@Override
protected Instance.State getState() {
return Instance.State.RUNNING;
}
}

View File

@ -0,0 +1,68 @@
/**
*
* 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.deltacloud.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.deltacloud.DeltacloudClient;
import org.jclouds.deltacloud.domain.Instance;
import org.jclouds.logging.Logger;
import com.google.common.base.Predicate;
import com.google.inject.Inject;
/**
*
* Tests to see if a instance is at a specific state
*
* @author Adrian Cole
*/
@Singleton
public abstract class InstanceState implements Predicate<Instance> {
private final DeltacloudClient client;
@Resource
protected Logger logger = Logger.NULL;
@Inject
public InstanceState(DeltacloudClient client) {
this.client = client;
}
public boolean apply(Instance instance) {
logger.trace("looking for state on instance %s", checkNotNull(instance, "instance"));
instance = refresh(instance);
if (instance == null || instance.getState() == Instance.State.FINISH)
return false;
logger.trace("%s: looking for instance state %s: currently: %s", instance.getId(), getState(), instance
.getState());
return instance.getState() == getState();
}
protected abstract Instance.State getState();
private Instance refresh(Instance instance) {
return client.getInstance(instance.getHref());
}
}

View File

@ -27,13 +27,15 @@ import java.util.logging.Logger;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.deltacloud.domain.Image;
import org.jclouds.deltacloud.domain.Instance;
import org.jclouds.deltacloud.domain.PasswordAuthentication;
import org.jclouds.deltacloud.domain.Transition;
import org.jclouds.deltacloud.domain.TransitionOnAction;
import org.jclouds.deltacloud.options.CreateInstanceOptions;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpRequest;
import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
@ -51,6 +53,7 @@ public class DeltacloudClientLiveTest extends ReadOnlyDeltacloudClientLiveTest {
protected String prefix = System.getProperty("user.name") + ".test";
protected Instance instance;
protected Credentials creds;
public void testCreateInstance() throws Exception {
Logger.getAnonymousLogger().info("starting instance");
@ -62,12 +65,13 @@ public class DeltacloudClientLiveTest extends ReadOnlyDeltacloudClientLiveTest {
}
}).getId(), CreateInstanceOptions.Builder.named(prefix));
instance = client.getInstance(instance.getHref());
if (instance.getAuthentication() != null && instance.getAuthentication() instanceof PasswordAuthentication)
creds = PasswordAuthentication.class.cast(instance.getAuthentication()).getLoginCredentials();
refreshInstance();
checkStartedInstance();
Instance newInfo = client.getInstance(instance.getHref());
checkInstanceMatchesGet(newInfo);
}
protected void checkInstanceMatchesGet(Instance newInfo) {
@ -77,49 +81,57 @@ public class DeltacloudClientLiveTest extends ReadOnlyDeltacloudClientLiveTest {
protected void checkStartedInstance() {
System.out.println(new Gson().toJson(instance));
assertEquals(instance.getName(), prefix);
assert stateChanges.get(Instance.State.RUNNING).apply(instance) : instance;
refreshInstance();
assertEquals(instance.getState(), Instance.State.RUNNING);
}
private Instance refreshInstance() {
if (instance != null)
return instance = client.getInstance(instance.getHref());
return null;
}
@Test(dependsOnMethods = "testCreateInstance")
public void testConnectivity() throws Exception {
Logger.getAnonymousLogger().info("awaiting ssh");
// TODO
// assert socketTester.apply(new IPSocket(Iterables.get(instance.getPublicAddresses(), 0),
// 22)) : instance;
// doConnectViaSsh(instance, getSshCredentials(instance));
assert socketTester.apply(new IPSocket(Iterables.get(instance.getPublicAddresses(), 0), 22)) : instance;
if (creds != null) {
Logger.getAnonymousLogger().info("will connect ssh");
doConnectViaSsh(instance, creds);
}
}
private Credentials getSshCredentials(Instance instance2) {
// TODO
return null;
}
public HttpRequest refreshInstanceAndGetAction(Instance.Action action) {
return client.getInstance(instance.getHref()).getActions().get(action);
public HttpRequest getAction(Instance.Action action) {
return instance.getActions().get(action);
}
@Test(dependsOnMethods = "testConnectivity")
public void testLifeCycle() {
client.performAction(refreshInstanceAndGetAction(Instance.Action.STOP));
assertEquals(client.getInstance(instance.getHref()).getState(), Instance.State.STOPPED);
client.performAction(refreshInstanceAndGetAction(Instance.Action.START));
assertEquals(client.getInstance(instance.getHref()).getState(), Instance.State.RUNNING);
client.performAction(refreshInstanceAndGetAction(Instance.Action.REBOOT));
assertEquals(client.getInstance(instance.getHref()).getState(), Instance.State.RUNNING);
HttpRequest rebootUri = getAction(Instance.Action.REBOOT);
if (rebootUri != null) {
client.performAction(rebootUri);
assert stateChanges.get(Instance.State.RUNNING).apply(instance) : instance;
}
}
@Test(dependsOnMethods = "testLifeCycle")
public void testDestroyInstance() {
try {
client.performAction(refreshInstanceAndGetAction(Instance.Action.STOP));
assertEquals(client.getInstance(instance.getHref()).getState(), Instance.State.STOPPED);
} catch (IllegalArgumentException e) {
for (Transition transition : findChainTo(Instance.State.FINISH, refreshInstance().getState(), client
.getInstanceStates())) {
if (refreshInstance() == null)
break;
if (transition instanceof TransitionOnAction) {
client.performAction(getAction(TransitionOnAction.class.cast(transition).getAction()));
}
Predicate<Instance> stateTester = stateChanges.get(transition.getTo());
if (stateTester != null)
assert stateTester.apply(instance) : transition + " : " + instance;
else
Logger.getAnonymousLogger().warning(String.format("no state tester for: %s", transition));
}
client.performAction(refreshInstanceAndGetAction(Instance.Action.DESTROY));
assertEquals(client.getInstance(instance.getHref()), null);
assert refreshInstance() == null;
}
protected void doConnectViaSsh(Instance instance, Credentials creds) throws IOException {
@ -138,14 +150,9 @@ public class DeltacloudClientLiveTest extends ReadOnlyDeltacloudClientLiveTest {
}
}
@AfterGroups(groups = "live")
@Override
protected void tearDown() {
try {
testDestroyInstance();
} catch (NullPointerException e) {
// no need to check null or anything as we swallow all
}
testDestroyInstance();
super.tearDown();
}

View File

@ -34,6 +34,9 @@ import org.jclouds.deltacloud.domain.Image;
import org.jclouds.deltacloud.domain.Instance;
import org.jclouds.deltacloud.domain.Realm;
import org.jclouds.deltacloud.domain.Transition;
import org.jclouds.deltacloud.domain.Instance.State;
import org.jclouds.deltacloud.predicates.InstanceFinished;
import org.jclouds.deltacloud.predicates.InstanceRunning;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
@ -46,6 +49,7 @@ import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
@ -68,6 +72,7 @@ public class ReadOnlyDeltacloudClientLiveTest {
protected String endpoint;
protected String apiversion;
protected Predicate<IPSocket> socketTester;
protected ImmutableMap<State, Predicate<Instance>> stateChanges;
protected void setupCredentials() {
identity = System.getProperty("test." + provider + ".identity", "mockuser");
@ -99,6 +104,12 @@ public class ReadOnlyDeltacloudClientLiveTest {
client = context.getApi();
socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 180, 1, TimeUnit.SECONDS);
stateChanges = ImmutableMap.<Instance.State, Predicate<Instance>> of(//
Instance.State.RUNNING, new RetryablePredicate<Instance>(new InstanceRunning(client), 600, 1,
TimeUnit.SECONDS),//
Instance.State.FINISH, new RetryablePredicate<Instance>(new InstanceFinished(client), 30, 1,
TimeUnit.SECONDS)//
);
}
@Test