mirror of https://github.com/apache/jclouds.git
Issue 830: Record created VApp names for cleanUp
This commit is contained in:
parent
b4579518c4
commit
d081f771b4
|
@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
import org.jclouds.vcloud.director.v1_5.domain.Reference;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
/**
|
||||
* Predicates for working with {@link Reference} collections.
|
||||
|
@ -78,6 +79,30 @@ public class ReferenceTypePredicates {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches {@link Reference}s with names in the given collection.
|
||||
*
|
||||
* @param T type of the reference, for example {@link Link}
|
||||
* @param names collection of values for the name attribute of the referenced object
|
||||
* @return predicate that will match references with names starting with the given prefix
|
||||
*/
|
||||
public static <T extends Reference> Predicate<T> nameIn(final Iterable<String> names) {
|
||||
checkNotNull(names, "names must be defined");
|
||||
|
||||
return new Predicate<T>() {
|
||||
@Override
|
||||
public boolean apply(T reference) {
|
||||
String name = reference.getName();
|
||||
return Iterables.contains(names, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "nameIn(" + Iterables.toString(names) + ")";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches {@link Reference}s of the given type.
|
||||
*
|
||||
|
|
|
@ -32,6 +32,7 @@ import java.math.BigInteger;
|
|||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
|
||||
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
|
||||
|
@ -63,6 +64,7 @@ import com.google.common.base.Predicates;
|
|||
import com.google.common.base.Strings;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
/**
|
||||
* Shared code to test the behaviour of {@link VAppClient} and {@link VAppTemplateClient}.
|
||||
|
@ -96,8 +98,6 @@ public abstract class AbstractVAppClientLiveTest extends BaseVCloudDirectorClien
|
|||
protected VApp vApp;
|
||||
protected VAppTemplate vAppTemplate;
|
||||
|
||||
protected final Random random = new Random();
|
||||
|
||||
/**
|
||||
* Retrieves the required clients from the REST API context
|
||||
*
|
||||
|
@ -121,7 +121,7 @@ public abstract class AbstractVAppClientLiveTest extends BaseVCloudDirectorClien
|
|||
* Retrieves the test {@link Vdc} and {@link VAppTemplate} from their configured {@link URI}s.
|
||||
* Instantiates a new test VApp.
|
||||
*/
|
||||
@BeforeClass(alwaysRun = true, description = "Cleans up the environment")
|
||||
@BeforeClass(alwaysRun = true, description = "Sets up the environment")
|
||||
protected void setupEnvironment() {
|
||||
// Get the configured Vdc for the tests
|
||||
vdc = vdcClient.getVdc(vdcURI);
|
||||
|
@ -179,25 +179,22 @@ public abstract class AbstractVAppClientLiveTest extends BaseVCloudDirectorClien
|
|||
}
|
||||
}
|
||||
|
||||
// NOTE This method is also called by the BeforeClass method setupRequiredClients
|
||||
@AfterClass(alwaysRun = true, description = "Cleans up the environment by deleting created VApps named 'test-vapp-*' or 'new-name-*'")
|
||||
protected void cleanUp() throws Exception {
|
||||
@AfterClass(alwaysRun = true, description = "Cleans up the environment by deleting created VApps")
|
||||
protected void cleanUp() {
|
||||
// Find references in the Vdc with the VApp type and named 'test-vapp' or 'new-name'
|
||||
Iterable<Reference> vApps = Iterables.filter(
|
||||
vdc.getResourceEntities().getResourceEntities(),
|
||||
Predicates.and(
|
||||
ReferenceTypePredicates.<Reference>typeEquals(VCloudDirectorMediaType.VAPP),
|
||||
Predicates.or(
|
||||
ReferenceTypePredicates.<Reference>nameStartsWith("test-vapp-"),
|
||||
ReferenceTypePredicates.<Reference>nameStartsWith("new-name-")
|
||||
)
|
||||
ReferenceTypePredicates.<Reference>nameIn(vAppNames)
|
||||
)
|
||||
);
|
||||
vAppNames.clear();
|
||||
|
||||
// If we found any references, delete the VApp they point to
|
||||
if (vApps != null && !Iterables.isEmpty(vApps)) {
|
||||
for (Reference ref : vApps) {
|
||||
cleanUpVApp(ref.getHref());
|
||||
cleanUpVApp(ref.getHref()); // NOTE may fail, but should continue deleting
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,7 +100,6 @@ import org.testng.annotations.Test;
|
|||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Sets;
|
||||
|
@ -146,6 +145,7 @@ public class VAppClientLiveTest extends AbstractVAppClientLiveTest {
|
|||
.name("new-name-" + Integer.toString(random.nextInt(Integer.MAX_VALUE)))
|
||||
.description("New Description")
|
||||
.build();
|
||||
vAppNames.add(newVApp.getName());
|
||||
|
||||
// The method under test
|
||||
Task modifyVApp = vAppClient.modifyVApp(vApp.getHref(), newVApp);
|
||||
|
|
|
@ -81,6 +81,7 @@ import com.google.common.base.Strings;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Module;
|
||||
|
||||
|
@ -120,7 +121,8 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
|
|||
protected URI vdcURI;
|
||||
protected URI userURI;
|
||||
|
||||
protected static Random random = new Random();
|
||||
protected final Set<String> vAppNames = Sets.newLinkedHashSet();
|
||||
protected static final Random random = new Random();
|
||||
|
||||
protected BaseVCloudDirectorClientLiveTest() {
|
||||
provider = "vcloud-director";
|
||||
|
@ -268,6 +270,9 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
|
|||
VApp vAppInstantiated = vdcClient.instantiateVApp(vdcURI, instantiate);
|
||||
assertNotNull(vAppInstantiated, String.format(ENTITY_NON_NULL, VAPP));
|
||||
|
||||
// Save VApp name for cleanUp
|
||||
vAppNames.add(name);
|
||||
|
||||
return vAppInstantiated;
|
||||
}
|
||||
|
||||
|
@ -331,12 +336,12 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
|
|||
assertTaskSucceeds(task);
|
||||
}
|
||||
|
||||
protected void cleanUpVApp(VApp vApp) throws Exception {
|
||||
protected void cleanUpVApp(VApp vApp) {
|
||||
cleanUpVApp(vApp.getHref());
|
||||
}
|
||||
|
||||
// TODO code tidy for cleanUpVApp? Seems extremely verbose!
|
||||
protected void cleanUpVApp(URI vAppUri) throws Exception {
|
||||
protected void cleanUpVApp(URI vAppUri) {
|
||||
VAppClient vappClient = context.getApi().getVAppClient();
|
||||
|
||||
VApp vApp;
|
||||
|
@ -389,7 +394,6 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
|
|||
}
|
||||
|
||||
logger.warn(e, "Deleting vApp failed: vApp="+vApp);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue