Issue 693:add ability to supply node object directly to byon

This commit is contained in:
Adrian Cole 2011-09-22 13:09:45 -07:00
parent 78d2564ba3
commit 6ad732cda7
15 changed files with 434 additions and 100 deletions

View File

@ -22,9 +22,13 @@ import java.util.List;
import java.util.Properties;
import org.jclouds.byon.config.BYONComputeServiceContextModule;
import org.jclouds.byon.config.ConfiguresNodeStore;
import org.jclouds.byon.config.YamlNodeStoreModule;
import org.jclouds.compute.StandaloneComputeServiceContextBuilder;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
/**
@ -41,6 +45,21 @@ public class BYONComputeServiceContextBuilder extends StandaloneComputeServiceCo
@Override
protected void addContextModule(List<Module> modules) {
modules.add(new BYONComputeServiceContextModule());
addNodeStoreModuleIfNotPresent(modules);
}
protected void addNodeStoreModuleIfNotPresent(List<Module> modules) {
if (!Iterables.any(modules, new Predicate<Module>() {
public boolean apply(Module input) {
return input.getClass().isAnnotationPresent(ConfiguresNodeStore.class);
}
})) {
addNodeStoreModule(modules);
}
}
protected void addNodeStoreModule(List<Module> modules) {
modules.add(new YamlNodeStoreModule());
}
}

View File

@ -20,14 +20,11 @@ package org.jclouds.byon.config;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.byon.Node;
import org.jclouds.byon.functions.NodesFromYamlStream;
import org.jclouds.byon.internal.BYONComputeServiceAdapter;
import org.jclouds.byon.suppliers.NodesParsedFromSupplier;
import org.jclouds.byon.suppliers.SupplyFromProviderURIOrNodesProperty;
import org.jclouds.compute.config.JCloudsNativeComputeServiceAdapterContextModule;
import org.jclouds.concurrent.SingleThreaded;
@ -37,6 +34,7 @@ import org.jclouds.location.suppliers.OnlyLocationOrFirstZone;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
@ -55,7 +53,7 @@ public class BYONComputeServiceContextModule extends
@Provides
@Singleton
Supplier provideApi(Supplier<Map<String, Node>> in) {
Supplier provideApi(Supplier<Cache<String, Node>> in) {
return in;
}
@ -64,15 +62,12 @@ public class BYONComputeServiceContextModule extends
super.configure();
bind(new TypeLiteral<Supplier<Location>>() {
}).to(OnlyLocationOrFirstZone.class);
bind(new TypeLiteral<Supplier<Map<String, Node>>>() {
}).to(NodesParsedFromSupplier.class);
bind(new TypeLiteral<Function<URI, InputStream>>() {
}).to(SupplyFromProviderURIOrNodesProperty.class);
bind(new TypeLiteral<Supplier<InputStream>>() {
}).annotatedWith(Provider.class).to(SupplyFromProviderURIOrNodesProperty.class);
bind(new TypeLiteral<Function<URI, InputStream>>() {
}).to(SupplyFromProviderURIOrNodesProperty.class);
// TODO make this somehow overridable via user request
bind(new TypeLiteral<Function<InputStream, Map<String, Node>>>() {
}).to(NodesFromYamlStream.class);
}
}

View File

@ -0,0 +1,64 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.byon.config;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import org.jclouds.byon.Node;
import com.google.common.annotations.Beta;
import com.google.common.base.Functions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
/**
*
* @author Adrian Cole
*/
@ConfiguresNodeStore
@Beta
public class CacheNodeStoreModule extends AbstractModule {
private final Cache<String, Node> backing;
public CacheNodeStoreModule(Cache<String, Node> backing) {
this.backing = checkNotNull(backing, "backing");
}
public CacheNodeStoreModule(Map<String, Node> backing) {
this(CacheBuilder.newBuilder().<String, Node>build(CacheLoader.from(Functions.forMap(backing))));
for (String node : backing.keySet())
this.backing.getUnchecked(node);
}
@Override
protected void configure() {
bind(new TypeLiteral<Cache<String, Node>>() {
}).toInstance(backing);
bind(new TypeLiteral<Supplier<Cache<String, Node>>>() {
}).toInstance(Suppliers.<Cache<String, Node>> ofInstance(backing));
}
}

View File

@ -27,7 +27,7 @@ import java.lang.annotation.Target;
import com.google.common.annotations.Beta;
/**
* designates the module configures a {@code Map<String, Node>}
* designates the module configures a {@code Cache<String, Node>}
*
* @author Adrian Cole
*

View File

@ -22,19 +22,30 @@ import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.byon.Node;
import org.jclouds.byon.domain.YamlNode;
import org.jclouds.byon.functions.NodesFromYamlStream;
import org.jclouds.byon.suppliers.NodesParsedFromSupplier;
import org.jclouds.collect.TransformingMap;
import org.jclouds.io.CopyInputStreamInputSupplierMap;
import org.jclouds.io.CopyInputStreamIntoSupplier;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.io.InputSupplier;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/**
*
@ -56,6 +67,10 @@ public class YamlNodeStoreModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<Supplier<Cache<String, Node>>>() {
}).to(NodesParsedFromSupplier.class);
bind(new TypeLiteral<Function<InputStream, Cache<String, Node>>>() {
}).to(NodesFromYamlStream.class);
bind(new TypeLiteral<Function<YamlNode, InputStream>>() {
}).toInstance(org.jclouds.byon.domain.YamlNode.yamlNodeToInputStream);
bind(new TypeLiteral<Function<InputStream, YamlNode>>() {
@ -66,26 +81,36 @@ public class YamlNodeStoreModule extends AbstractModule {
}).toInstance(org.jclouds.byon.domain.YamlNode.toNode);
if (backing != null) {
bind(new TypeLiteral<Map<String, InputStream>>() {
}).toInstance(backing);
}).annotatedWith(Names.named("yaml")).toInstance(backing);
} else {
bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() {
}).toInstance(BACKING);
}).annotatedWith(Names.named("yaml")).toInstance(BACKING);
bind(new TypeLiteral<Map<String, InputStream>>() {
}).to(new TypeLiteral<CopyInputStreamInputSupplierMap>() {
}).annotatedWith(Names.named("yaml")).to(new TypeLiteral<YAMLCopyInputStreamInputSupplierMap>() {
});
}
}
@Singleton
public static class YAMLCopyInputStreamInputSupplierMap extends CopyInputStreamInputSupplierMap {
@Inject
public YAMLCopyInputStreamInputSupplierMap(@Named("yaml") Map<String, InputSupplier<InputStream>> toMap,
CopyInputStreamIntoSupplier putFunction) {
super(toMap, putFunction);
}
}
@Provides
@Singleton
protected Map<String, Node> provideNodeStore(Map<String, YamlNode> backing,
Function<Node, YamlNode> yamlSerializer, Function<YamlNode, Node> yamlDeserializer) {
return new TransformingMap<String, YamlNode, Node>(backing, yamlDeserializer, yamlSerializer);
protected Cache<String, Node> provideNodeStore(Map<String, YamlNode> backing, Function<Node, YamlNode> yamlSerializer,
Function<YamlNode, Node> yamlDeserializer) {
return CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(new TransformingMap<String, YamlNode, Node>(backing, yamlDeserializer, yamlSerializer))));
}
@Provides
@Singleton
protected Map<String, YamlNode> provideYamlStore(Map<String, InputStream> backing,
protected Map<String, YamlNode> provideYamlStore(@Named("yaml") Map<String, InputStream> backing,
Function<YamlNode, InputStream> yamlSerializer, Function<InputStream, YamlNode> yamlDeserializer) {
return new TransformingMap<String, InputStream, YamlNode>(backing, yamlDeserializer, yamlSerializer);
}

View File

@ -34,6 +34,10 @@ import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
@ -65,7 +69,7 @@ import com.google.common.collect.Maps;
* @author Adrian Cole
*/
@Singleton
public class NodesFromYamlStream implements Function<InputStream, Map<String, Node>> {
public class NodesFromYamlStream implements Function<InputStream, Cache<String, Node>> {
/**
* Type-safe config class for YAML
@ -76,7 +80,7 @@ public class NodesFromYamlStream implements Function<InputStream, Map<String, No
}
@Override
public Map<String, Node> apply(InputStream source) {
public Cache<String, Node> apply(InputStream source) {
Constructor constructor = new Constructor(Config.class);
@ -87,17 +91,23 @@ public class NodesFromYamlStream implements Function<InputStream, Map<String, No
TypeDescription configDesc = new TypeDescription(Config.class);
configDesc.putListPropertyType("nodes", YamlNode.class);
constructor.addTypeDescription(configDesc);
// note that snakeyaml also throws nosuchmethod error when you use the non-deprecated
// note that snakeyaml also throws nosuchmethod error when you use the
// non-deprecated
// constructor
Yaml yaml = new Yaml(new Loader(constructor));
Config config = (Config) yaml.load(source);
checkState(config != null, "missing config: class");
checkState(config.nodes != null, "missing nodes: collection");
return Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode), new Function<Node, String>() {
Map<String, Node> backingMap = Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode),
new Function<Node, String>() {
public String apply(Node node) {
return node.getId();
}
});
Cache<String, Node> cache = CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(backingMap)));
for (String node : backingMap.keySet())
cache.getUnchecked(node);
return cache;
}
}

View File

@ -42,9 +42,11 @@ import org.jclouds.location.suppliers.JustProvider;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.UncheckedExecutionException;
/**
*
@ -52,12 +54,12 @@ import com.google.common.collect.ImmutableSet.Builder;
*/
@Singleton
public class BYONComputeServiceAdapter implements JCloudsNativeComputeServiceAdapter {
private final Supplier<Map<String, Node>> nodes;
private final Supplier<Cache<String, Node>> nodes;
private final NodeToNodeMetadata converter;
private final JustProvider locationSupplier;
@Inject
public BYONComputeServiceAdapter(Supplier<Map<String, Node>> nodes, NodeToNodeMetadata converter,
public BYONComputeServiceAdapter(Supplier<Cache<String, Node>> nodes, NodeToNodeMetadata converter,
JustProvider locationSupplier) {
this.nodes = checkNotNull(nodes, "nodes");
this.converter = checkNotNull(converter, "converter");
@ -82,14 +84,14 @@ public class BYONComputeServiceAdapter implements JCloudsNativeComputeServiceAda
@Override
public Iterable<NodeMetadata> listNodes() {
return Iterables.transform(nodes.get().values(), converter);
return Iterables.transform(nodes.get().asMap().values(), converter);
}
@Override
public Iterable<Location> listLocations() {
Builder<Location> locations = ImmutableSet.builder();
Location provider = Iterables.getOnlyElement(locationSupplier.get());
Set<String> zones = ImmutableSet.copyOf(Iterables.filter(Iterables.transform(nodes.get().values(),
Set<String> zones = ImmutableSet.copyOf(Iterables.filter(Iterables.transform(nodes.get().asMap().values(),
new Function<Node, String>() {
@Override
@ -109,7 +111,13 @@ public class BYONComputeServiceAdapter implements JCloudsNativeComputeServiceAda
@Override
public NodeMetadata getNode(String id) {
Node node = nodes.get().get(id);
Node node = null;
try {
node = nodes.get().getUnchecked(id);
} catch (UncheckedExecutionException e) {
}
return node != null ? converter.apply(node) : null;
}

View File

@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.io.InputStream;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -34,28 +33,29 @@ import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
/**
*
* @author Adrian Cole
*/
@Singleton
public class NodesParsedFromSupplier implements Supplier<Map<String, Node>> {
public class NodesParsedFromSupplier implements Supplier<Cache<String, Node>> {
@Resource
protected Logger logger = Logger.NULL;
private final Supplier<InputStream> supplier;
private final Function<InputStream, Map<String, Node>> parser;
private final Function<InputStream, Cache<String, Node>> parser;
@Inject
NodesParsedFromSupplier(@Provider Supplier<InputStream> supplier, Function<InputStream, Map<String, Node>> parser) {
NodesParsedFromSupplier(@Provider Supplier<InputStream> supplier, Function<InputStream, Cache<String, Node>> parser) {
this.supplier = checkNotNull(supplier, "supplier");
this.parser = checkNotNull(parser, "parser");
}
@Override
public Map<String, Node> get() {
Map<String, Node> nodes = parser.apply(supplier.get());
public Cache<String, Node> get() {
Cache<String, Node> nodes = parser.apply(supplier.get());
checkState(nodes != null && nodes.size() > 0, "no nodes parsed from supplier: %s", supplier);
return nodes;
}

View File

@ -24,17 +24,17 @@ import static org.jclouds.byon.functions.NodeToNodeMetadataTest.zoneCalled;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import org.jclouds.byon.config.CacheNodeStoreModule;
import org.jclouds.byon.functions.NodesFromYamlTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.domain.Location;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
@ -43,41 +43,48 @@ import com.google.inject.Module;
*
* @author Adrian Cole
*/
@Test(groups = "live")
@Test(singleThreaded = true, testName = "BYONComputeServiceTest")
public class BYONComputeServiceTest {
@Test
public void testNodesParseNodeMap() throws Exception {
assertNodesParse(
"foo",
ImmutableSet.<Module> of(new CacheNodeStoreModule(ImmutableMap.<String, Node> of(
NodesFromYamlTest.TEST1.getId(), NodesFromYamlTest.TEST1))));
}
@Test
public void testNodesParseWithFileUrl() throws Exception {
assertNodesParse("file://" + getClass().getResource("/test1.yaml").getPath());
assertNodesParse("file://" + getClass().getResource("/test1.yaml").getPath(), ImmutableSet.<Module> of());
}
@Test
public void testNodesParseWithClasspathUrl() throws Exception {
assertNodesParse("classpath:///test1.yaml");
assertNodesParse("classpath:///test1.yaml", ImmutableSet.<Module> of());
}
private void assertNodesParse(String endpoint) {
private void assertNodesParse(String endpoint, Iterable<Module> modules) {
ComputeServiceContext context = null;
try {
Location providerLocation = expectedProviderLocationFromResource(endpoint);
Properties props = new Properties();
props.setProperty("byon.endpoint", endpoint);
context = new ComputeServiceContextFactory().createContext("byon", "foo", "bar", ImmutableSet
.<Module> of(new SshjSshClientModule()), props);
context = new ComputeServiceContextFactory().createContext("byon", "foo", "bar", modules, props);
assertEquals(context.getProviderSpecificContext().getEndpoint(), URI.create(endpoint));
@SuppressWarnings("unchecked")
Supplier<Map<String, Node>> supplier = (Supplier<Map<String, Node>>) context.getProviderSpecificContext()
Supplier<Cache<String, Node>> supplier = (Supplier<Cache<String, Node>>) context.getProviderSpecificContext()
.getApi();
assertEquals(supplier.get().size(), context.getComputeService().listNodes().size());
assertEquals(supplier.get(), ImmutableMap.<String, Node> of(NodesFromYamlTest.TEST1.getId(),
NodesFromYamlTest.TEST1));
assertEquals(supplier.get().asMap(),
ImmutableMap.<String, Node> of(NodesFromYamlTest.TEST1.getId(), NodesFromYamlTest.TEST1));
assertEquals(context.getComputeService().listNodes(), ImmutableSet
.of(expectedNodeMetadataFromResource(endpoint)));
assertEquals(context.getComputeService().listNodes(),
ImmutableSet.of(expectedNodeMetadataFromResource(endpoint)));
assertEquals(context.getComputeService().listAssignableLocations(), ImmutableSet.of(providerLocation));
} finally {
if (context != null)
@ -91,25 +98,26 @@ public class BYONComputeServiceTest {
String endpoint = "file://" + getClass().getResource("/test_location.yaml").getPath();
Properties props = new Properties();
props.setProperty("byon.endpoint", endpoint);
context = new ComputeServiceContextFactory().createContext("byon", "foo", "bar", ImmutableSet
.<Module> of(new SshjSshClientModule()), props);
context = new ComputeServiceContextFactory().createContext("byon", "foo", "bar",
ImmutableSet.<Module> of(), props);
assertEquals(context.getProviderSpecificContext().getEndpoint(), URI.create(endpoint));
@SuppressWarnings("unchecked")
Supplier<Map<String, Node>> supplier = (Supplier<Map<String, Node>>) context.getProviderSpecificContext()
Supplier<Cache<String, Node>> supplier = (Supplier<Cache<String, Node>>) context.getProviderSpecificContext()
.getApi();
assertEquals(supplier.get().size(), context.getComputeService().listNodes().size());
assertEquals(supplier.get(), ImmutableMap.<String, Node> of(NodesFromYamlTest.TEST2.getId(),
assertEquals(supplier.get().asMap(), ImmutableMap.<String, Node> of(NodesFromYamlTest.TEST2.getId(),
NodesFromYamlTest.TEST2, NodesFromYamlTest.TEST3.getId(), NodesFromYamlTest.TEST3));
Location providerLocation = expectedProviderLocationFromResource(endpoint);
Location virginia = zoneCalled("virginia", providerLocation);
Location maryland = zoneCalled("maryland", providerLocation);
assertEquals(context.getComputeService().listNodes(), ImmutableSet.of(expectedNodeMetadataFromResource(1,
endpoint, virginia), expectedNodeMetadataFromResource(2, endpoint, maryland, 2022)));
assertEquals(context.getComputeService().listNodes(), ImmutableSet.of(
expectedNodeMetadataFromResource(1, endpoint, virginia),
expectedNodeMetadataFromResource(2, endpoint, maryland, 2022)));
assertEquals(context.getComputeService().listAssignableLocations(), ImmutableSet.of(virginia, maryland));
} finally {

View File

@ -0,0 +1,171 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.byon.config;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.jclouds.byon.Node;
import org.jclouds.location.Provider;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.base.Functions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", singleThreaded = true, testName = "CacheNodeStoreModuleTest")
public class CacheNodeStoreModuleTest {
@DataProvider(name = "names")
public Object[][] createData() {
return new Object[][] { { "instance1", "bear" }, { "instance2", "apple" }, { "instance2", "francis" },
{ "instance4", "robot" } };
}
public void testProvidedMapWithValue() throws IOException {
Map<String, Node> map = Maps.newConcurrentMap();
map.put("test", Node.builder().id("instance1").name("instancename").build());
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
}
public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
Map<String, Node> map = Maps.newConcurrentMap();
Injector injector = createInjectorWithProvidedMap(map);
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Cache<String, Node>>() {
})).asMap(), map);
Cache<String, Node> store = getStore(injector);
for (int i = 0; i < 10; i++)
check(map, store, "test" + i, "instance1" + i, "instancename" + i);
}
public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
Map<String, Node> map = Maps.newConcurrentMap();
put(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
}
public void testProvidedCacheConsistentAcrossMultipleInjectors() throws IOException {
Map<String, Node> map = Maps.newConcurrentMap();
Cache<String, Node> cache = CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(map)));
put(map, getStore(createInjectorWithProvidedCache(cache)), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjectorWithProvidedCache(cache)), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjectorWithProvidedCache(cache)), "test", "instance1", "instancename");
remove(map, getStore(createInjectorWithProvidedCache(cache)), "test");
}
private Cache<String, Node> getStore(Injector injector) {
return injector.getInstance(Key.get(new TypeLiteral<Cache<String, Node>>() {
}));
}
private Injector createInjectorWithProvidedMap(Map<String, Node> map) {
return Guice.createInjector(new CacheNodeStoreModule(map), new AbstractModule() {
@Override
public void configure() {
bind(new TypeLiteral<Supplier<InputStream>>() {
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
}
});
}
private Injector createInjectorWithProvidedCache(Cache<String, Node> cache) {
return Guice.createInjector(new CacheNodeStoreModule(cache), new AbstractModule() {
@Override
public void configure() {
bind(new TypeLiteral<Supplier<InputStream>>() {
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
}
});
}
private void check(Map<String, Node> map, Cache<String, Node> store, String key, String id, String name)
throws IOException {
put(map, store, key, id, name);
checkConsistent(map, store, key, id, name);
remove(map, store, key);
}
private void remove(Map<String, Node> map, Cache<String, Node> store, String key) {
store.invalidate(key);
assertEquals(store.size(), 0);
map.remove(key);
assertEquals(map.size(), 0);
try {
assertEquals(store.getUnchecked(key), null);
assert false : "should not work as null is invalid";
} catch (UncheckedExecutionException e) {
}
assertEquals(map.get(key), null);
}
private void checkConsistent(Map<String, Node> map, Cache<String, Node> store, String key, String id, String name)
throws IOException {
assertEquals(map.size(), 1);
if (store.size() == 0)
store.getUnchecked(key);
assertEquals(store.size(), 1);
// checkRepeatedRead
assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
}
private void put(Map<String, Node> map, Cache<String, Node> store, String key, String id, String name) {
assertEquals(store.size(), 0);
assertEquals(map.size(), 0);
map.put(key, Node.builder().id(id).name(name).build());
store.getUnchecked(key);
}
}

View File

@ -28,16 +28,23 @@ import java.util.concurrent.ConcurrentHashMap;
import org.jclouds.byon.Node;
import org.jclouds.io.CopyInputStreamInputSupplierMap;
import org.jclouds.location.Provider;
import org.jclouds.util.Strings2;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.cache.Cache;
import com.google.common.io.InputSupplier;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/**
*
@ -45,7 +52,7 @@ import com.google.inject.TypeLiteral;
*/
@Test(groups = "unit", singleThreaded = true)
public class YamlNodeStoreModuleTest {
Yaml json = createInjector().getInstance(Yaml.class);
Yaml yaml = createInjector().getInstance(Yaml.class);
@DataProvider(name = "names")
public Object[][] createData() {
@ -77,8 +84,8 @@ public class YamlNodeStoreModuleTest {
Injector injector = createInjectorWithProvidedMap(map);
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
})), map);
Map<String, Node> store = getStore(injector);
}, Names.named("yaml"))), map);
Cache<String, Node> store = getStore(injector);
for (int i = 0; i < 10; i++)
check(map, store, "test" + i, "instance1" + i, "instancename" + i);
@ -100,52 +107,77 @@ public class YamlNodeStoreModuleTest {
Map<String, InputStream> map = getMap(createInjector());
put(map, getStore(createInjector()), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjector()), "test", "instance1", "instancename");
checkConsistent(map, getStore(createInjector()), "test", "instance1", "instancename");
remove(map, getStore(createInjector()), "test");
}
protected Map<String, Node> getStore(Injector injector) {
return injector.getInstance(Key.get(new TypeLiteral<Map<String, Node>>() {
protected Cache<String, Node> getStore(Injector injector) {
return injector.getInstance(Key.get(new TypeLiteral<Cache<String, Node>>() {
}));
}
protected Map<String, InputStream> getMap(Injector injector) {
return injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
}));
}, Names.named("yaml")));
}
protected Injector createInjectorWithProvidedMap(Map<String, InputStream> map) {
return Guice.createInjector(new YamlNodeStoreModule(map));
return Guice.createInjector(new YamlNodeStoreModule(map), new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Supplier<InputStream>>() {
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
}
});
}
protected Injector createInjector() {
return Guice.createInjector(new YamlNodeStoreModule());
return Guice.createInjector(new YamlNodeStoreModule(), new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Supplier<InputStream>>() {
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
}
protected void check(Map<String, InputStream> map, Map<String, Node> store, String key, String id, String name)
});
}
protected void check(Map<String, InputStream> map, Cache<String, Node> store, String key, String id, String name)
throws IOException {
put(map, store, key, id, name);
checkConsistent(map, store, key, id, name);
remove(map, store, key);
}
protected void remove(Map<String, InputStream> map, Map<String, Node> store, String key) {
store.remove(key);
protected void remove(Map<String, InputStream> map, Cache<String, Node> store, String key) {
store.invalidate(key);
assertEquals(store.size(), 0);
map.remove(key);
assertEquals(map.size(), 0);
assertEquals(store.get(key), null);
try {
assertEquals(store.getUnchecked(key), null);
assert false : "should not work as null is invalid";
} catch (UncheckedExecutionException e) {
}
assertEquals(map.get(key), null);
}
protected void checkConsistent(Map<String, InputStream> map, Map<String, Node> store, String key, String id,
protected void checkConsistent(Map<String, InputStream> map, Cache<String, Node> store, String key, String id,
String name) throws IOException {
assertEquals(store.size(), 1);
assertEquals(map.size(), 1);
if (store.size() == 0)
store.getUnchecked(key);
assertEquals(store.size(), 1);
// checkRepeatedRead
assertEquals(store.get(key), Node.builder().id(id).name(name).build());
assertEquals(store.get(key), Node.builder().id(id).name(name).build());
assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
assertEquals(store.getUnchecked(key), Node.builder().id(id).name(name).build());
// checkRepeatedRead
checkToYaml(map, key, id, name);
checkToYaml(map, key, id, name);
@ -155,9 +187,10 @@ public class YamlNodeStoreModuleTest {
assertEquals(Strings2.toStringAndClose(map.get(key)), String.format("id: %s\nname: %s\n", id, name));
}
protected void put(Map<String, InputStream> map, Map<String, Node> store, String key, String id, String name) {
protected void put(Map<String, InputStream> map, Cache<String, Node> store, String key, String id, String name) {
assertEquals(store.size(), 0);
assertEquals(map.size(), 0);
store.put(key, Node.builder().id(id).name(name).build());
map.put(key, new ByteArrayInputStream(String.format("id: %s\nname: %s\n", id, name).getBytes()));
store.getUnchecked(key);
}
}

View File

@ -45,6 +45,7 @@ import com.google.common.collect.Maps;
*
* @author Adrian Cole
*/
@Test(singleThreaded = true, testName = "NodeToNodeMetadataTest")
public class NodeToNodeMetadataTest {
public static Location expectedProviderLocationFromResource(String resource) {
return new LocationBuilder().scope(LocationScope.PROVIDER).id("byon").description(resource).build();
@ -60,8 +61,8 @@ public class NodeToNodeMetadataTest {
Map<String, Credentials> credentialStore = Maps.newLinkedHashMap();
NodeToNodeMetadata parser = new NodeToNodeMetadata(Suppliers.ofInstance(provider), Suppliers
.<Set<? extends Location>> ofInstance(ImmutableSet.of(provider, zoneCalled("virginia", provider))),
NodeToNodeMetadata parser = new NodeToNodeMetadata(Suppliers.ofInstance(provider),
Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(provider, zoneCalled("virginia", provider))),
new SupplyFromProviderURIOrNodesProperty(URI.create("test")), credentialStore);
public static NodeMetadata expectedNodeMetadataFromResource(String resource) {
@ -99,8 +100,8 @@ public class NodeToNodeMetadataTest {
@Test
public void testNodesParseLocation() throws Exception {
assertEquals(parser.apply(NodesFromYamlTest.TEST2), expectedNodeMetadataFromResource(resource, zoneCalled(
"virginia", provider)));
assertEquals(parser.apply(NodesFromYamlTest.TEST2),
expectedNodeMetadataFromResource(resource, zoneCalled("virginia", provider)));
assertEquals(credentialStore, ImmutableMap.of("node#cluster-1", new Credentials("myUser", NodesFromYamlTest.key)));
}

View File

@ -58,7 +58,7 @@ public class NodesFromYamlTest {
InputStream is = getClass().getResourceAsStream("/test1.yaml");
NodesFromYamlStream parser = new NodesFromYamlStream();
assertEquals(parser.apply(is), ImmutableMap.of(TEST1.getId(), TEST1));
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
}
@Test
@ -67,7 +67,7 @@ public class NodesFromYamlTest {
InputStream is = getClass().getResourceAsStream("/test_location.yaml");
NodesFromYamlStream parser = new NodesFromYamlStream();
assertEquals(parser.apply(is), ImmutableMap.of(TEST2.getId(), TEST2, TEST3.getId(), TEST3));
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST2.getId(), TEST2, TEST3.getId(), TEST3));
}
@Test
@ -76,7 +76,7 @@ public class NodesFromYamlTest {
InputStream is = getClass().getResourceAsStream("/test_with_url.yaml");
NodesFromYamlStream parser = new NodesFromYamlStream();
assertEquals(parser.apply(is), ImmutableMap.of(TEST1.getId(), TEST1));
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
}
@Test(expectedExceptions = IllegalStateException.class)

View File

@ -168,7 +168,7 @@ public class PlacementGroupClientLiveTest {
public void testStartCCInstance() throws Exception {
Template template = context.getComputeService().templateBuilder().fastest().osVersionMatches("11.04").build();
Template template = context.getComputeService().templateBuilder().fastest().osDescriptionMatches(".*/ubuntu-images/.*").osVersionMatches("11.04").build();
assert template != null : "The returned template was null, but it should have a value.";
assertEquals(template.getHardware().getProviderId(), InstanceType.CC1_4XLARGE);
assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "ebs");

View File

@ -73,7 +73,7 @@ import com.google.inject.Module;
*
* @author Adrian Cole
*/
@Test(groups = "live", sequential = true)
@Test(groups = "live", singleThreaded = true)
public class CloudSigmaClientLiveTest {
protected long driveSize = 8 * 1024 * 1024 * 1024l;
protected int maxDriveImageTime = 300;
@ -455,12 +455,12 @@ public class CloudSigmaClientLiveTest {
}
protected Credentials getSshCredentials(Server server) {
return new Credentials("cloudsigma", "cloudsigma");
return new Credentials("root", vncPassword);
}
protected void prepareDrive() {
client.destroyDrive(drive.getUuid());
drive = client.cloneDrive("6a9cd9c2-4814-4953-8e86-f8ee6a3e57d5", drive.getName(),
drive = client.cloneDrive("f3c7c665-cd54-4a78-8fd2-7ec2f028cf29", drive.getName(),
new CloneDriveOptions().size(driveSize));
assert driveNotClaimed.apply(drive) : client.getDriveInfo(drive.getUuid());
System.err.println("after prepare" + client.getDriveInfo(drive.getUuid()));