Deprecate async apis. Fixes #70

* Deprecated all async apis.
* Refactored tests to extend BaseApiLiveTest.
* ChefContext now extends View.
* HostedChefApi now extends ChefApi.
* PatchedChefApi has been removed.
* Removed HostedChefContext and PrivateChefContext because they are now
  redundant. The concrete apis can be unwrapped from teh ChefContext
  view.
This commit is contained in:
Ignasi Barrera 2013-04-22 19:00:53 +02:00
parent 9d66de545b
commit 892f9f9493
18 changed files with 257 additions and 539 deletions

View File

@ -39,8 +39,6 @@
<test.chef.build-version />
<test.chef.identity>chef-webui</test.chef.identity>
<test.chef.credential>${user.home}/.chef/webui.pem</test.chef.credential>
<test.chef.validator.identity>chef-validator</test.chef.validator.identity>
<test.chef.validator.credential>${user.home}/.chef/validation.pem</test.chef.validator.credential>
<jclouds.osgi.import>
org.jclouds;version=${project.version},
org.jclouds*;version=${project.version},
@ -119,8 +117,7 @@
<test.chef.api-version>${test.chef.api-version}</test.chef.api-version>
<test.chef.build-version>${test.chef.build-version}</test.chef.build-version>
<test.chef.identity>${test.chef.identity}</test.chef.identity>
<test.chef.validator.identity>${test.chef.validator.identity}</test.chef.validator.identity>
<test.chef.validator.credential>${test.chef.validator.credential}</test.chef.validator.credential>
<test.chef.credential>${test.chef.credential}</test.chef.credential>
</systemPropertyVariables>
</configuration>
</execution>

View File

@ -25,7 +25,6 @@ import static org.jclouds.Constants.PROPERTY_TIMEOUTS_PREFIX;
import static org.jclouds.chef.config.ChefProperties.CHEF_BOOTSTRAP_DATABAG;
import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEMS;
import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEM_SYSTEM;
import static org.jclouds.reflect.Reflection2.typeToken;
import java.net.URI;
import java.util.Properties;
@ -93,7 +92,7 @@ public class ChefApiMetadata extends BaseRestApiMetadata {
.documentation(URI.create("http://wiki.opscode.com/display/chef/Server+API"))
.defaultEndpoint("http://localhost:4000")
.defaultProperties(ChefApiMetadata.defaultProperties())
.context(typeToken(ChefContext.class))
.view(ChefContext.class)
.defaultModules(
ImmutableSet.<Class<? extends Module>> of(ChefRestClientModule.class, ChefParserModule.class,
ChefBootstrapModule.class, JMXOhaiModule.class));

View File

@ -95,7 +95,10 @@ import java.util.Set;
*
* @see ChefApi
* @author Adrian Cole
* @deprecated please use {@code org.jclouds.ContextBuilder#buildApi(ChefApi.class)} as
* {@link ChefAsyncApi} interface will be removed in jclouds 1.7.
*/
@Deprecated
@RequestFilters(SignedHeaderAuth.class)
@Headers(keys = "X-Chef-Version", values = "{" + Constants.PROPERTY_API_VERSION + "}")
@Consumes(MediaType.APPLICATION_JSON)

View File

@ -18,8 +18,10 @@
*/
package org.jclouds.chef;
import java.io.Closeable;
import org.jclouds.View;
import org.jclouds.chef.internal.ChefContextImpl;
import org.jclouds.rest.RestContext;
import com.google.inject.ImplementedBy;
@ -30,7 +32,7 @@ import com.google.inject.ImplementedBy;
*
*/
@ImplementedBy(ChefContextImpl.class)
public interface ChefContext extends RestContext<ChefApi, ChefAsyncApi> {
public interface ChefContext extends View, Closeable {
ChefService getChefService();

View File

@ -1,135 +0,0 @@
/**
* 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.chef;
import static org.jclouds.reflect.Reflection2.typeToken;
import java.util.NoSuchElementException;
import java.util.Properties;
import org.jclouds.ContextBuilder;
import org.jclouds.View;
import org.jclouds.apis.Apis;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.Providers;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
*
* @see ContextBuilder
* @author Adrian Cole
*/
@Deprecated
public class ChefContextFactory {
/**
* for porting old code to {@link ContextBuilder}
*/
public ChefContextFactory() {
}
/**
* for porting old code to {@link ContextBuilder}
*/
public ChefContextFactory(Properties properties) {
}
/**
* @see #createContext(String, String,String, Iterable, Properties)
*/
public ChefContext createContext(String providerOrApi, String identity, String credential) {
return createContext(providerOrApi, identity, credential, ImmutableSet.<Module> of(), new Properties());
}
/**
* @see #createContext(String, String, String, Iterable, Properties)
*/
public ChefContext createContext(String providerOrApi, Properties overrides) {
return createContext(providerOrApi, null, null, ImmutableSet.<Module> of(), overrides);
}
/**
* @see #createContext(String, String,String, Iterable, Properties)
*/
public ChefContext createContext(String providerOrApi, Iterable<? extends Module> wiring, Properties overrides) {
return createContext(providerOrApi, null, null, wiring, overrides);
}
/**
* @see #createContext(String, String,String, Iterable, Properties)
*/
public ChefContext createContext(String providerOrApi, @Nullable String identity, @Nullable String credential,
Properties overrides) {
return createContext(providerOrApi, identity, credential, ImmutableSet.<Module> of(), overrides);
}
/**
* @see createContext(String, String,String, Iterable, Properties)
*/
public ChefContext createContext(String providerOrApi, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> wiring) {
return createContext(providerOrApi, identity, credential, wiring, new Properties());
}
/**
* for porting old code to {@link ContextBuilder}
*
* @param providerOrApi
* @param identity
* nullable, if credentials are present in the overrides
* @param credential
* nullable, if credentials are present in the overrides
* @param wiring
* Configuration you'd like to pass to the context. Ex.
* ImmutableSet.<Module>of(new ExecutorServiceModule(myexecutor))
* @param overrides
* properties to override defaults with.
* @return initialized context ready for use
*/
@SuppressWarnings("unchecked")
public ChefContext createContext(String providerOrApi, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> wiring, Properties overrides) {
ContextBuilder builder = null;
try {
ProviderMetadata pm = Providers.withId(providerOrApi);
builder = ContextBuilder.newBuilder(pm);
} catch (NoSuchElementException e) {
builder = ContextBuilder.newBuilder(Apis.withId(providerOrApi));
}
builder.modules(Iterable.class.cast(wiring));
builder.overrides(overrides);
if (identity != null)
builder.credentials(identity, credential);
Object context = builder.build();
if (context instanceof ChefContext) {
return ChefContext.class.cast(context);
} else if (context instanceof View) {
View tctx = View.class.cast(context);
return tctx.unwrap(typeToken(ChefContext.class));
} else {
throw new IllegalArgumentException("provider " + providerOrApi + " contains an unknown context type: "
+ context.getClass().getSimpleName());
}
}
}

View File

@ -18,14 +18,21 @@
*/
package org.jclouds.chef.internal;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.chef.config.ChefProperties.CHEF_BOOTSTRAP_DATABAG;
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.ChefService;
import org.jclouds.chef.config.ChefProperties;
@ -55,18 +62,14 @@ import org.jclouds.json.Json;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.Statement;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.chef.config.ChefProperties.CHEF_BOOTSTRAP_DATABAG;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
/**
* @author Adrian Cole
@ -75,6 +78,7 @@ import static org.jclouds.chef.config.ChefProperties.CHEF_BOOTSTRAP_DATABAG;
public class BaseChefService implements ChefService {
private final ChefContext chefContext;
private final ChefApi api;
private final CleanupStaleNodesAndClients cleanupStaleNodesAndClients;
private final CreateNodeAndPopulateAutomaticAttributes createNodeAndPopulateAutomaticAttributes;
private final DeleteAllNodesInList deleteAllNodesInList;
@ -89,20 +93,23 @@ public class BaseChefService implements ChefService {
private final RunListForGroup runListForGroup;
private final ListCookbookVersions listCookbookVersions;
private final ListEnvironments listEnvironments;
private final Json json;
@Resource
@Named(ChefProperties.CHEF_LOGGER)
protected Logger logger = Logger.NULL;
@Inject
protected BaseChefService(ChefContext chefContext, CleanupStaleNodesAndClients cleanupStaleNodesAndClients,
CreateNodeAndPopulateAutomaticAttributes createNodeAndPopulateAutomaticAttributes,
DeleteAllNodesInList deleteAllNodesInList, ListNodes listNodes, DeleteAllClientsInList deleteAllClientsInList,
ListClients listClients, ListCookbookVersions listCookbookVersions,
UpdateAutomaticAttributesOnNode updateAutomaticAttributesOnNode, Supplier<PrivateKey> privateKey,
@Named(CHEF_BOOTSTRAP_DATABAG) String databag, GroupToBootScript groupToBootScript,
BootstrapConfigForGroup bootstrapConfigForGroup, RunListForGroup runListForGroup,
ListEnvironments listEnvironments) {
protected BaseChefService(ChefContext chefContext, ChefApi api,
CleanupStaleNodesAndClients cleanupStaleNodesAndClients,
CreateNodeAndPopulateAutomaticAttributes createNodeAndPopulateAutomaticAttributes,
DeleteAllNodesInList deleteAllNodesInList, ListNodes listNodes, DeleteAllClientsInList deleteAllClientsInList,
ListClients listClients, ListCookbookVersions listCookbookVersions,
UpdateAutomaticAttributesOnNode updateAutomaticAttributesOnNode, Supplier<PrivateKey> privateKey,
@Named(CHEF_BOOTSTRAP_DATABAG) String databag, GroupToBootScript groupToBootScript,
BootstrapConfigForGroup bootstrapConfigForGroup, RunListForGroup runListForGroup,
ListEnvironments listEnvironments, Json json) {
this.chefContext = checkNotNull(chefContext, "chefContext");
this.api = checkNotNull(api, "api");
this.cleanupStaleNodesAndClients = checkNotNull(cleanupStaleNodesAndClients, "cleanupStaleNodesAndClients");
this.createNodeAndPopulateAutomaticAttributes = checkNotNull(createNodeAndPopulateAutomaticAttributes,
"createNodeAndPopulateAutomaticAttributes");
@ -119,6 +126,7 @@ public class BaseChefService implements ChefService {
this.bootstrapConfigForGroup = checkNotNull(bootstrapConfigForGroup, "bootstrapConfigForGroup");
this.runListForGroup = checkNotNull(runListForGroup, "runListForGroup");
this.listEnvironments = checkNotNull(listEnvironments, "listEnvironments");
this.json = checkNotNull(json, "json");
}
@Override
@ -215,7 +223,7 @@ public class BaseChefService implements ChefService {
@Override
public void updateBootstrapConfigForGroup(Iterable<String> runList, @Nullable JsonBall jsonAttributes, String group) {
try {
chefContext.getApi().createDatabag(databag);
api.createDatabag(databag);
} catch (IllegalStateException e) {
}
@ -223,10 +231,10 @@ public class BaseChefService implements ChefService {
String bootstrapConfig = buildBootstrapConfiguration(runList, Optional.fromNullable(jsonAttributes));
DatabagItem runlist = new DatabagItem(group, bootstrapConfig);
if (chefContext.getApi().getDatabagItem(databag, group) == null) {
chefContext.getApi().createDatabagItem(databag, runlist);
if (api.getDatabagItem(databag, group) == null) {
api.createDatabagItem(databag, runlist);
} else {
chefContext.getApi().updateDatabagItem(databag, runlist);
api.updateDatabagItem(databag, runlist);
}
}
@ -257,7 +265,6 @@ public class BaseChefService implements ChefService {
checkNotNull(runList, "runList must not be null");
checkNotNull(jsonAttributes, "jsonAttributes must not be null");
Json json = chefContext.utils().json();
Map<String, Object> bootstrapConfig = Maps.newHashMap();
bootstrapConfig.put("run_list", Lists.newArrayList(runList));
if (jsonAttributes.isPresent()) {

View File

@ -18,38 +18,33 @@
*/
package org.jclouds.chef.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.annotations.Name;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefAsyncApi;
import org.jclouds.Context;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.ChefService;
import org.jclouds.domain.Credentials;
import org.jclouds.lifecycle.Closer;
import org.jclouds.internal.BaseView;
import org.jclouds.location.Provider;
import org.jclouds.providers.ProviderMetadata;
import org.jclouds.rest.Utils;
import org.jclouds.rest.internal.RestContextImpl;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import com.google.common.reflect.TypeToken;
/**
* @author Adrian Cole
*/
@Singleton
public class ChefContextImpl extends RestContextImpl<ChefApi, ChefAsyncApi> implements ChefContext {
public class ChefContextImpl extends BaseView implements ChefContext {
private final ChefService chefService;
@Inject
protected ChefContextImpl(@Name String name, ProviderMetadata providerMetadata,
@Provider Supplier<Credentials> creds, Utils utils, Closer closer, Injector injector, ChefService chefService) {
super(name, providerMetadata, creds, utils, closer, injector, TypeLiteral.get(ChefApi.class), TypeLiteral
.get(ChefAsyncApi.class));
this.chefService = chefService;
protected ChefContextImpl(@Provider Context backend, @Provider TypeToken<? extends Context> backendType,
ChefService chefService) {
super(backend, backendType);
this.chefService = checkNotNull(chefService, "checkNotNull");
}
@Override
@ -57,4 +52,9 @@ public class ChefContextImpl extends RestContextImpl<ChefApi, ChefAsyncApi> impl
return chefService;
}
@Override
public void close() throws IOException {
delegate().close();
}
}

View File

@ -66,8 +66,9 @@ import com.google.common.util.concurrent.ListeningExecutorService;
* In-memory chef simulator.
*
* @author Adrian Cole
* @deprecated {@link TransientChefAsyncApi} interface will be removed in jclouds 1.7.
*/
@Deprecated
public class TransientChefAsyncApi implements ChefAsyncApi {
@Singleton
private static class StorageMetadataToName implements Function<PageSet<? extends StorageMetadata>, Set<String>> {

View File

@ -18,37 +18,19 @@
*/
package org.jclouds.chef;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertNotNull;
import org.jclouds.chef.domain.CookbookVersion;
import org.jclouds.chef.internal.BaseChefApiLiveTest;
import org.testng.annotations.Test;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code ChefApi} against a Chef Server <= 0.9.8.
*
* @author Adrian Cole
*/
@Test(groups = { "live" })
public class ChefApiLiveTest extends BaseChefApiLiveTest<ChefContext> {
public class ChefApiLiveTest extends BaseChefApiLiveTest<ChefApi> {
@Test
public void testListCookbookVersionsWithChefService() throws Exception {
Iterable<? extends CookbookVersion> cookbooks = context.getChefService().listCookbookVersions();
assertNotNull(cookbooks);
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
protected ChefApiLiveTest() {
provider = "chef";
}
}

View File

@ -36,7 +36,6 @@ import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.jclouds.Context;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.domain.ChecksumStatus;
import org.jclouds.chef.domain.Client;
@ -53,10 +52,8 @@ import org.jclouds.chef.options.SearchOptions;
import org.jclouds.crypto.Pems;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.FilePayload;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
@ -71,39 +68,13 @@ import com.google.common.primitives.Bytes;
* @author Adrian Cole
*/
@Test(groups = { "live", "integration" })
public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefContextLiveTest<C> {
public static final String PREFIX = System.getProperty("user.name") + "-jcloudstest";
public static final String ADMIN_PREFIX = System.getProperty("user.name") + "-jcloudstest-adm";
public static final String VALIDATOR_PREFIX = System.getProperty("user.name") + "-jcloudstest-val";
protected String validatorIdentity;
protected String validatorCredential;
protected C validatorContext;
protected ChefApi validatorClient;
public abstract class BaseChefApiLiveTest<A extends ChefApi> extends BaseChefLiveTest<A> {
public static final String PREFIX = "jcloudstest-" + System.getProperty("user.name");
public static final String ADMIN_PREFIX = "jcloudstest-adm-" + System.getProperty("user.name");
// It may take a bit until the search index is populated
protected int maxWaitForIndexInMs = 60000;
protected ChefApi chefApi;
protected Properties setupValidatorProperties() {
Properties overrides = setupProperties();
validatorIdentity = setIfTestSystemPropertyPresent(overrides, provider + ".validator.identity");
validatorCredential = setCredentialFromPemFile(overrides, validatorIdentity, provider + ".validator.credential");
overrides.setProperty(provider + ".identity", validatorIdentity);
overrides.setProperty(provider + ".credential", validatorCredential);
return overrides;
}
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
validatorContext = createContext(setupValidatorProperties(), setupModules());
chefApi = getChefApi(context);
validatorClient = getChefApi(validatorContext);
}
private Node node;
private Role role;
protected DatabagItem databagItem;
@ -123,7 +94,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
List<Byte> md5 = Bytes.asList(content.getContentMetadata().getContentMD5());
// request an upload site for this file
UploadSandbox site = chefApi.getUploadSandboxForChecksums(ImmutableSet.of(md5));
UploadSandbox site = api.getUploadSandboxForChecksums(ImmutableSet.of(md5));
try {
assert site.getChecksums().containsKey(md5) : md5 + " not in " + site.getChecksums();
@ -131,13 +102,13 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
ChecksumStatus status = site.getChecksums().get(md5);
if (status.needsUpload()) {
// context.utils().http().put(status.getUrl(), content);
chefApi.uploadContent(status.getUrl(), content);
api.uploadContent(status.getUrl(), content);
}
chefApi.commitSandbox(site.getSandboxId(), true);
api.commitSandbox(site.getSandboxId(), true);
} catch (RuntimeException e) {
chefApi.commitSandbox(site.getSandboxId(), false);
api.commitSandbox(site.getSandboxId(), false);
}
// create a new cookbook
@ -145,29 +116,29 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
cookbook.getRootFiles().add(new Resource(content));
// upload the cookbook to the remote server
chefApi.updateCookbook(PREFIX, "0.0.0", cookbook);
api.updateCookbook(PREFIX, "0.0.0", cookbook);
}
@Test(dependsOnMethods = "testCreateClient")
public void testGenerateKeyForClient() throws Exception {
String credential = Pems.pem(chefApi.generateKeyForClient(PREFIX).getPrivateKey());
String credential = Pems.pem(api.generateKeyForClient(PREFIX).getPrivateKey());
assertClientCreated(PREFIX, credential);
}
@Test
public void testListCookbooks() throws Exception {
Set<String> cookbookNames = chefApi.listCookbooks();
Set<String> cookbookNames = api.listCookbooks();
assertFalse(cookbookNames.isEmpty());
for (String cookbook : cookbookNames) {
for (String version : chefApi.getVersionsOfCookbook(cookbook)) {
CookbookVersion cookbookO = chefApi.getCookbook(cookbook, version);
for (String version : api.getVersionsOfCookbook(cookbook)) {
CookbookVersion cookbookO = api.getCookbook(cookbook, version);
for (Resource resource : ImmutableList.<Resource> builder().addAll(cookbookO.getDefinitions())
.addAll(cookbookO.getFiles()).addAll(cookbookO.getLibraries()).addAll(cookbookO.getSuppliers())
.addAll(cookbookO.getRecipes()).addAll(cookbookO.getResources()).addAll(cookbookO.getRootFiles())
.addAll(cookbookO.getTemplates()).build()) {
try {
InputStream stream = chefApi.getResourceContents(resource);
InputStream stream = api.getResourceContents(resource);
byte[] md5 = asByteSource(stream).hash(md5()).asBytes();
assertEquals(md5, resource.getChecksum());
} catch (NullPointerException e) {
@ -180,95 +151,75 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = "testCreateNewCookbook")
public void testUpdateCookbook() throws Exception {
CookbookVersion cookbook = chefApi.getCookbook(PREFIX, "0.0.0");
assertNotNull(chefApi.updateCookbook(PREFIX, "0.0.0", cookbook));
CookbookVersion cookbook = api.getCookbook(PREFIX, "0.0.0");
assertNotNull(api.updateCookbook(PREFIX, "0.0.0", cookbook));
}
@Test(dependsOnMethods = { "testCreateNewCookbook", "testUpdateCookbook" })
public void testDeleteCookbook() throws Exception {
assertNotNull(chefApi.deleteCookbook(PREFIX, "0.0.0"));
}
@Test(expectedExceptions = AuthorizationException.class)
public void testValidatorCannotListClients() throws Exception {
for (String client : validatorClient.listClients()) {
assertNotNull(validatorClient.getClient(client));
}
}
@Test(expectedExceptions = AuthorizationException.class)
public void testValidatorCannotDeleteClient() throws Exception {
validatorClient.deleteClient(PREFIX);
}
@Test
public void testValidatorCreateClient() throws Exception {
chefApi.deleteClient(VALIDATOR_PREFIX);
String credential = Pems.pem(validatorClient.createClient(VALIDATOR_PREFIX).getPrivateKey());
assertClientCreated(VALIDATOR_PREFIX, credential);
assertNotNull(api.deleteCookbook(PREFIX, "0.0.0"));
}
@Test
public void testCreateClient() throws Exception {
chefApi.deleteClient(PREFIX);
String credential = Pems.pem(chefApi.createClient(PREFIX).getPrivateKey());
api.deleteClient(PREFIX);
String credential = Pems.pem(api.createClient(PREFIX).getPrivateKey());
assertClientCreated(PREFIX, credential);
}
@Test
public void testCreateAdminClient() throws Exception {
chefApi.deleteClient(ADMIN_PREFIX);
String credential = Pems.pem(chefApi.createClient(ADMIN_PREFIX, CreateClientOptions.Builder.admin())
.getPrivateKey());
api.deleteClient(ADMIN_PREFIX);
String credential = Pems.pem(api.createClient(ADMIN_PREFIX, CreateClientOptions.Builder.admin()).getPrivateKey());
assertClientCreated(ADMIN_PREFIX, credential);
}
@Test
public void testClientExists() throws Exception {
assertNotNull(chefApi.clientExists(validatorIdentity));
assertNotNull(api.clientExists(identity));
}
@Test
public void testListNodes() throws Exception {
Set<String> nodes = chefApi.listNodes();
Set<String> nodes = api.listNodes();
assertNotNull(nodes);
}
@Test(dependsOnMethods = "testCreateRole")
public void testCreateNode() throws Exception {
chefApi.deleteNode(PREFIX);
chefApi.createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]"), "_default"));
node = chefApi.getNode(PREFIX);
api.deleteNode(PREFIX);
api.createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]"), "_default"));
node = api.getNode(PREFIX);
// TODO check recipes
assertNotNull(node);
Set<String> nodes = chefApi.listNodes();
Set<String> nodes = api.listNodes();
assert nodes.contains(PREFIX) : String.format("node %s not in %s", PREFIX, nodes);
}
@Test(dependsOnMethods = "testCreateNode")
public void testNodeExists() throws Exception {
assertNotNull(chefApi.nodeExists(PREFIX));
assertNotNull(api.nodeExists(PREFIX));
}
@Test(dependsOnMethods = "testNodeExists")
public void testUpdateNode() throws Exception {
for (String nodename : chefApi.listNodes()) {
Node node = chefApi.getNode(nodename);
chefApi.updateNode(node);
for (String nodename : api.listNodes()) {
Node node = api.getNode(nodename);
api.updateNode(node);
}
}
@Test
public void testListRoles() throws Exception {
Set<String> roles = chefApi.listRoles();
Set<String> roles = api.listRoles();
assertNotNull(roles);
}
@Test
public void testCreateRole() throws Exception {
chefApi.deleteRole(PREFIX);
chefApi.createRole(new Role(PREFIX, Collections.singleton("recipe[java]")));
role = chefApi.getRole(PREFIX);
api.deleteRole(PREFIX);
api.createRole(new Role(PREFIX, Collections.singleton("recipe[java]")));
role = api.getRole(PREFIX);
assertNotNull(role);
assertEquals(role.getName(), PREFIX);
assertEquals(role.getRunList(), Collections.singleton("recipe[java]"));
@ -276,37 +227,37 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = "testCreateRole")
public void testRoleExists() throws Exception {
assertNotNull(chefApi.roleExists(PREFIX));
assertNotNull(api.roleExists(PREFIX));
}
@Test(dependsOnMethods = "testRoleExists")
public void testUpdateRole() throws Exception {
for (String rolename : chefApi.listRoles()) {
Role role = chefApi.getRole(rolename);
chefApi.updateRole(role);
for (String rolename : api.listRoles()) {
Role role = api.getRole(rolename);
api.updateRole(role);
}
}
@Test
public void testListDatabags() throws Exception {
Set<String> databags = chefApi.listDatabags();
Set<String> databags = api.listDatabags();
assertNotNull(databags);
}
@Test
public void testCreateDatabag() throws Exception {
chefApi.deleteDatabag(PREFIX);
chefApi.createDatabag(PREFIX);
api.deleteDatabag(PREFIX);
api.createDatabag(PREFIX);
}
@Test(dependsOnMethods = "testCreateDatabag")
public void testDatabagExists() throws Exception {
assertNotNull(chefApi.databagExists(PREFIX));
assertNotNull(api.databagExists(PREFIX));
}
@Test(dependsOnMethods = "testCreateDatabagItem")
public void testListDatabagItems() throws Exception {
Set<String> databagItems = chefApi.listDatabagItems(PREFIX);
Set<String> databagItems = api.listDatabagItems(PREFIX);
assertNotNull(databagItems);
}
@ -314,14 +265,14 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
public void testCreateDatabagItem() throws Exception {
Properties config = new Properties();
config.setProperty("foo", "bar");
chefApi.deleteDatabagItem(PREFIX, PREFIX);
databagItem = chefApi.createDatabagItem(PREFIX, new DatabagItem("config", context.utils().json().toJson(config)));
api.deleteDatabagItem(PREFIX, PREFIX);
databagItem = api.createDatabagItem(PREFIX, new DatabagItem("config", json.toJson(config)));
assertNotNull(databagItem);
assertEquals(databagItem.getId(), "config");
// The databagItem json contains extra keys: (the name and the type if the
// item)
Properties props = context.utils().json().fromJson(databagItem.toString(), Properties.class);
Properties props = json.fromJson(databagItem.toString(), Properties.class);
for (Object key : config.keySet()) {
assertTrue(props.containsKey(key));
assertEquals(config.get(key), props.get(key));
@ -330,20 +281,20 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = "testCreateDatabagItem")
public void testDatabagItemExists() throws Exception {
assertNotNull(chefApi.databagItemExists(PREFIX, PREFIX));
assertNotNull(api.databagItemExists(PREFIX, PREFIX));
}
@Test(dependsOnMethods = "testDatabagItemExists")
public void testUpdateDatabagItem() throws Exception {
for (String databagItemId : chefApi.listDatabagItems(PREFIX)) {
DatabagItem databagItem = chefApi.getDatabagItem(PREFIX, databagItemId);
chefApi.updateDatabagItem(PREFIX, databagItem);
for (String databagItemId : api.listDatabagItems(PREFIX)) {
DatabagItem databagItem = api.getDatabagItem(PREFIX, databagItemId);
api.updateDatabagItem(PREFIX, databagItem);
}
}
@Test
public void testListSearchIndexes() throws Exception {
Set<String> indexes = chefApi.listSearchIndexes();
Set<String> indexes = api.listSearchIndexes();
assertNotNull(indexes);
assert indexes.contains("node") : indexes;
assert indexes.contains("client") : indexes;
@ -352,7 +303,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test
public void testSearchNodes() throws Exception {
SearchResult<? extends Node> results = chefApi.searchNodes();
SearchResult<? extends Node> results = api.searchNodes();
assertNotNull(results);
}
@ -361,7 +312,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
Predicate<SearchOptions> waitForIndex = retry(new Predicate<SearchOptions>() {
@Override
public boolean apply(SearchOptions input) {
SearchResult<? extends Node> results = chefApi.searchNodes(input);
SearchResult<? extends Node> results = api.searchNodes(input);
assertNotNull(results);
if (results.size() > 0) {
assertEquals(results.size(), 1);
@ -380,7 +331,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test
public void testSearchClients() throws Exception {
SearchResult<? extends Client> results = chefApi.searchClients();
SearchResult<? extends Client> results = api.searchClients();
assertNotNull(results);
}
@ -389,7 +340,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
Predicate<SearchOptions> waitForIndex = retry(new Predicate<SearchOptions>() {
@Override
public boolean apply(SearchOptions input) {
SearchResult<? extends Client> results = chefApi.searchClients(input);
SearchResult<? extends Client> results = api.searchClients(input);
assertNotNull(results);
if (results.size() > 0) {
assertEquals(results.size(), 1);
@ -408,7 +359,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test
public void testSearchRoles() throws Exception {
SearchResult<? extends Role> results = chefApi.searchRoles();
SearchResult<? extends Role> results = api.searchRoles();
assertNotNull(results);
}
@ -417,7 +368,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
Predicate<SearchOptions> waitForIndex = retry(new Predicate<SearchOptions>() {
@Override
public boolean apply(SearchOptions input) {
SearchResult<? extends Role> results = chefApi.searchRoles(input);
SearchResult<? extends Role> results = api.searchRoles(input);
assertNotNull(results);
if (results.size() > 0) {
assertEquals(results.size(), 1);
@ -436,7 +387,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = { "testListSearchIndexes", "testDatabagItemExists" })
public void testSearchDatabag() throws Exception {
SearchResult<? extends DatabagItem> results = chefApi.searchDatabag(PREFIX);
SearchResult<? extends DatabagItem> results = api.searchDatabag(PREFIX);
assertNotNull(results);
}
@ -445,7 +396,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
Predicate<SearchOptions> waitForIndex = retry(new Predicate<SearchOptions>() {
@Override
public boolean apply(SearchOptions input) {
SearchResult<? extends DatabagItem> results = chefApi.searchDatabag(PREFIX, input);
SearchResult<? extends DatabagItem> results = api.searchDatabag(PREFIX, input);
assertNotNull(results);
if (results.size() > 0) {
assertEquals(results.size(), 1);
@ -464,15 +415,15 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(expectedExceptions = ResourceNotFoundException.class, dependsOnMethods = "testListSearchIndexes")
public void testSearchDatabagNotFound() throws Exception {
SearchResult<? extends DatabagItem> results = chefApi.searchDatabag("whoopie");
SearchResult<? extends DatabagItem> results = api.searchDatabag("whoopie");
assertNotNull(results);
}
@Test
public void testCreateEnvironment() {
chefApi.deleteEnvironment(PREFIX);
chefApi.createEnvironment(new Environment(PREFIX, PREFIX));
Environment env = chefApi.getEnvironment(PREFIX);
api.deleteEnvironment(PREFIX);
api.createEnvironment(new Environment(PREFIX, PREFIX));
Environment env = api.getEnvironment(PREFIX);
assertNotNull(env);
assertEquals(env.getName(), PREFIX);
assertEquals(env.getDescription(), PREFIX);
@ -480,23 +431,23 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = "testCreateEnvironment")
public void testListEnvironment() {
Set<String> envList = chefApi.listEnvironments();
Set<String> envList = api.listEnvironments();
assertNotNull(envList);
assertTrue(envList.contains(PREFIX));
}
@Test(dependsOnMethods = "testCreateEnvironment")
public void testSearchEnvironments() throws Exception {
SearchResult<? extends Environment> results = chefApi.searchEnvironments();
SearchResult<? extends Environment> results = api.searchEnvironments();
assertNotNull(results);
}
@Test(dependsOnMethods = {"testListSearchIndexes", "testCreateEnvironment"})
@Test(dependsOnMethods = { "testListSearchIndexes", "testCreateEnvironment" })
public void testSearchEnvironmentsWithOptions() throws Exception {
Predicate<SearchOptions> waitForIndex = retry(new Predicate<SearchOptions>() {
@Override
public boolean apply(SearchOptions input) {
SearchResult<? extends Environment> results = chefApi.searchEnvironments(input);
SearchResult<? extends Environment> results = api.searchEnvironments(input);
assertNotNull(results);
if (results.size() > 0) {
assertEquals(results.size(), 1);
@ -513,22 +464,22 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
assertTrue(waitForIndex.apply(options));
}
@Test
public void testListCookbookVersionsWithChefService() throws Exception {
Iterable<? extends CookbookVersion> cookbooks = chefService.listCookbookVersions();
assertNotNull(cookbooks);
}
@AfterClass(groups = { "live", "integration" })
@Override
public void tearDownContext() {
chefApi.deleteClient(PREFIX);
chefApi.deleteClient(ADMIN_PREFIX);
chefApi.deleteClient(VALIDATOR_PREFIX);
chefApi.deleteNode(PREFIX);
chefApi.deleteRole(PREFIX);
chefApi.deleteDatabag(PREFIX);
chefApi.deleteEnvironment(PREFIX);
try {
Closeables.close(validatorContext, true);
} catch (IOException e) {
throw propagate(e);
}
super.tearDownContext();
public void tearDown() {
api.deleteClient(PREFIX);
api.deleteClient(ADMIN_PREFIX);
api.deleteNode(PREFIX);
api.deleteRole(PREFIX);
api.deleteDatabag(PREFIX);
api.deleteEnvironment(PREFIX);
super.tearDown();
}
private void assertClientCreated(String identity, String credential) {
@ -536,14 +487,14 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
overrides.setProperty(provider + ".identity", identity);
overrides.setProperty(provider + ".credential", credential);
C clientContext = createContext(overrides, setupModules());
A clientApi = create(overrides, setupModules());
try {
Client client = getChefApi(clientContext).getClient(identity);
Client client = clientApi.getClient(identity);
assertNotNull(client);
} finally {
try {
Closeables.close(clientContext, true);
Closeables.close(clientApi, true);
} catch (IOException e) {
throw propagate(e);
}

View File

@ -18,31 +18,35 @@
*/
package org.jclouds.chef.internal;
import static org.jclouds.reflect.Types2.checkBound;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.jclouds.Context;
import org.jclouds.apis.BaseContextLiveTest;
import org.jclouds.apis.BaseApiLiveTest;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefService;
import org.jclouds.json.Json;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.google.common.reflect.TypeToken;
import com.google.inject.Injector;
import com.google.inject.Module;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live")
public abstract class BaseChefContextLiveTest<C extends Context> extends BaseContextLiveTest<C> {
public abstract class BaseChefLiveTest<A extends ChefApi> extends BaseApiLiveTest<A> {
public BaseChefContextLiveTest() {
provider = "chef";
}
protected abstract ChefApi getChefApi(C context);
protected Injector injector;
protected ChefService chefService;
protected Json json;
/**
* the credential is a path to the pem file.
@ -54,6 +58,19 @@ public abstract class BaseChefContextLiveTest<C extends Context> extends BaseCon
return overrides;
}
@Override
protected void initialize() {
super.initialize();
chefService = injector.getInstance(ChefService.class);
json = injector.getInstance(Json.class);
}
@Override
protected A create(Properties props, Iterable<Module> modules) {
injector = newBuilder().modules(modules).overrides(props).buildInjector();
return injector.getInstance(resolveApiClass());
}
protected String setCredentialFromPemFile(Properties overrides, String identity, String key) {
String val = null;
String credentialFromFile = null;
@ -71,4 +88,11 @@ public abstract class BaseChefContextLiveTest<C extends Context> extends BaseCon
return credentialFromFile;
}
@SuppressWarnings("unchecked")
private Class<A> resolveApiClass() {
return Class.class.cast(checkBound(new TypeToken<A>(getClass()) {
private static final long serialVersionUID = 1L;
}).getRawType());
}
}

View File

@ -18,14 +18,11 @@
*/
package org.jclouds.chef.internal;
import static org.jclouds.reflect.Reflection2.typeToken;
import java.util.Map;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefApiMetadata;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.config.ChefBootstrapModule;
import org.jclouds.chef.config.ChefParserModule;
import org.jclouds.chef.config.ChefRestClientModule;
@ -39,7 +36,6 @@ import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
import com.google.inject.Module;
/**
@ -48,7 +44,7 @@ import com.google.inject.Module;
*/
@Test(groups = "live")
@Deprecated
public class BaseStubbedOhaiLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class BaseStubbedOhaiLiveTest extends BaseChefLiveTest<ChefApi> {
@ConfiguresOhai
static class TestOhaiModule extends OhaiModule {
@ -67,14 +63,4 @@ public class BaseStubbedOhaiLiveTest extends BaseChefContextLiveTest<ChefContext
ImmutableSet.<Class<? extends Module>> of(ChefRestClientModule.class, ChefParserModule.class,
ChefBootstrapModule.class, TestOhaiModule.class)).build();
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -18,16 +18,14 @@
*/
package org.jclouds.chef.strategy.internal;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.testng.annotations.BeforeClass;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code CleanupStaleNodesAndClientsImpl} strategies
@ -35,17 +33,16 @@ import com.google.common.reflect.TypeToken;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "CleanupStaleNodesAndClientsImplLiveTest")
public class CleanupStaleNodesAndClientsImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class CleanupStaleNodesAndClientsImplLiveTest extends BaseChefLiveTest<ChefApi> {
private CreateNodeAndPopulateAutomaticAttributesImpl creater;
private CleanupStaleNodesAndClientsImpl strategy;
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
this.creater = context.utils().injector().getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = context.utils().injector().getInstance(CleanupStaleNodesAndClientsImpl.class);
public void initialize() {
super.initialize();
this.creater = injector.getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = injector.getInstance(CleanupStaleNodesAndClientsImpl.class);
}
@Test
@ -54,25 +51,15 @@ public class CleanupStaleNodesAndClientsImplLiveTest extends BaseChefContextLive
creater.execute(prefix, ImmutableSet.<String> of());
// http://tickets.corp.opscode.com/browse/PL-522
// assert chef.nodeExists(prefix);
assert context.getApi().getNode(prefix) != null;
assertNotNull(api.getNode(prefix));
strategy.execute(prefix, 10);
assert context.getApi().getNode(prefix) != null;
assertNotNull(api.getNode(prefix));
Thread.sleep(1000);
strategy.execute(prefix, 1);
assert context.getApi().getNode(prefix) == null;
assertNull(api.getNode(prefix));
} finally {
context.getApi().deleteNode(prefix);
api.deleteNode(prefix);
}
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -18,21 +18,17 @@
*/
package org.jclouds.chef.strategy.internal;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.domain.Node;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.jclouds.ohai.config.OhaiModule.CurrentUserProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code CreateNodeAndPopulateAutomaticAttributesImpl}
@ -41,40 +37,30 @@ import com.google.common.reflect.TypeToken;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "CreateNodeAndPopulateAutomaticAttributesImplLiveTest")
public class CreateNodeAndPopulateAutomaticAttributesImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class CreateNodeAndPopulateAutomaticAttributesImplLiveTest extends BaseChefLiveTest<ChefApi> {
private CurrentUserProvider currentUserProvider;
private CreateNodeAndPopulateAutomaticAttributesImpl strategy;
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
this.currentUserProvider = context.utils().injector().getInstance(CurrentUserProvider.class);
public void initialize() {
super.initialize();
this.currentUserProvider = injector.getInstance(CurrentUserProvider.class);
this.strategy = injector.getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
}
@Test
public void testExecute() {
Set<String> runList = ImmutableSet.of("role[" + prefix + "]");
try {
context.utils().injector().getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class)
.execute(prefix, runList);
Node node = context.getApi().getNode(prefix);
strategy.execute(prefix, runList);
Node node = api.getNode(prefix);
assertEquals(node.getName(), prefix);
assertEquals(node.getRunList(), runList);
assertEquals(node.getAutomatic().get("current_user").toString(), currentUserProvider.get().toString());
} finally {
context.getApi().deleteNode(prefix);
api.deleteNode(prefix);
}
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -18,16 +18,14 @@
*/
package org.jclouds.chef.strategy.internal;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.testng.annotations.BeforeClass;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code DeleteAllApisAndNodesInListImpl} strategies
@ -35,19 +33,16 @@ import com.google.common.reflect.TypeToken;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "DeleteAllApisAndNodesInListImplLiveTest")
public class DeleteAllApisAndNodesInListImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class DeleteAllApisAndNodesInListImplLiveTest extends BaseChefLiveTest<ChefApi> {
private DeleteAllNodesInListImpl strategy;
private CreateNodeAndPopulateAutomaticAttributesImpl creater;
private ChefApi chef;
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
this.creater = context.utils().injector().getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = context.utils().injector().getInstance(DeleteAllNodesInListImpl.class);
this.chef = context.getApi();
public void initialize() {
super.initialize();
this.creater = injector.getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = injector.getInstance(DeleteAllNodesInListImpl.class);
}
@Test
@ -57,26 +52,17 @@ public class DeleteAllApisAndNodesInListImplLiveTest extends BaseChefContextLive
creater.execute(prefix + 1, ImmutableSet.<String> of());
// http://tickets.corp.opscode.com/browse/PL-522
// assert chef.nodeExists(prefix);
assert chef.getNode(prefix) != null;
assert chef.getNode(prefix + 1) != null;
// assert api.nodeExists(prefix);
assertNotNull(api.getNode(prefix));
assertNotNull(api.getNode(prefix + 1));
strategy.execute(ImmutableSet.of(prefix, prefix + 1));
assert chef.getNode(prefix) == null;
assert chef.getNode(prefix + 1) == null;
assertNull(api.getNode(prefix));
assertNull(api.getNode(prefix + 1));
} finally {
chef.deleteNode(prefix);
chef.deleteNode(prefix + 1);
api.deleteNode(prefix);
api.deleteNode(prefix + 1);
}
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -19,19 +19,15 @@
package org.jclouds.chef.strategy.internal;
import static com.google.common.collect.Iterables.size;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertEquals;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code GetNodesImpl} strategies
@ -39,29 +35,26 @@ import com.google.common.reflect.TypeToken;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "GetNodesImplLiveTest")
public class GetNodesImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class GetNodesImplLiveTest extends BaseChefLiveTest<ChefApi> {
private ListNodesImpl strategy;
private CreateNodeAndPopulateAutomaticAttributesImpl creater;
private ChefApi chef;
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
this.creater = context.utils().injector().getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = context.utils().injector().getInstance(ListNodesImpl.class);
this.chef = context.getApi();
public void initialize() {
super.initialize();
this.creater = injector.getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class);
this.strategy = injector.getInstance(ListNodesImpl.class);
creater.execute(prefix, ImmutableSet.<String> of());
creater.execute(prefix + 1, ImmutableSet.<String> of());
}
@AfterClass(groups = { "integration", "live" })
@Override
protected void tearDownContext() {
chef.deleteNode(prefix);
chef.deleteNode(prefix + 1);
super.tearDownContext();
protected void tearDown() {
api.deleteNode(prefix);
api.deleteNode(prefix + 1);
super.tearDown();
}
@Test
@ -86,14 +79,4 @@ public class GetNodesImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
assertEquals(size(strategy.execute(ImmutableSet.of(prefix, prefix + 1))), 2);
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -18,21 +18,17 @@
*/
package org.jclouds.chef.strategy.internal;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.domain.Node;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.jclouds.ohai.config.OhaiModule.CurrentUserProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code UpdateAutomaticAttributesOnNodeImpl} strategies
@ -40,39 +36,30 @@ import com.google.common.reflect.TypeToken;
* @author Adrian Cole
*/
@Test(groups = "live", testName = "UpdateAutomaticAttributesOnNodeImplLiveTest")
public class UpdateAutomaticAttributesOnNodeImplLiveTest extends BaseChefContextLiveTest<ChefContext> {
public class UpdateAutomaticAttributesOnNodeImplLiveTest extends BaseChefLiveTest<ChefApi> {
private CurrentUserProvider currentUserProvider;
private UpdateAutomaticAttributesOnNodeImpl strategy;
@BeforeClass(groups = { "integration", "live" })
@Override
public void setupContext() {
super.setupContext();
this.currentUserProvider = context.utils().injector().getInstance(CurrentUserProvider.class);
public void initialize() {
super.initialize();
this.currentUserProvider = injector.getInstance(CurrentUserProvider.class);
this.strategy = injector.getInstance(UpdateAutomaticAttributesOnNodeImpl.class);
}
@Test
public void testExecute() {
Set<String> runList = ImmutableSet.of("role[" + prefix + "]");
try {
context.getApi().createNode(new Node(prefix, runList, "_default"));
context.utils().injector().getInstance(UpdateAutomaticAttributesOnNodeImpl.class).execute(prefix);
Node node = context.getApi().getNode(prefix);
api.createNode(new Node(prefix, runList, "_default"));
strategy.execute(prefix);
Node node = api.getNode(prefix);
assertEquals(node.getName(), prefix);
assertEquals(node.getRunList(), runList);
assertEquals(node.getAutomatic().get("current_user").toString(), currentUserProvider.get().toString());
} finally {
context.getApi().deleteNode(prefix);
api.deleteNode(prefix);
}
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}

View File

@ -18,31 +18,23 @@
*/
package org.jclouds.chef.test;
import static com.google.common.base.Throwables.propagate;
import static org.jclouds.reflect.Reflection2.typeToken;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.io.IOException;
import java.util.Properties;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.domain.DatabagItem;
import org.jclouds.chef.internal.BaseChefContextLiveTest;
import org.testng.annotations.AfterClass;
import org.jclouds.chef.internal.BaseChefLiveTest;
import org.testng.annotations.Test;
import com.google.common.io.Closeables;
import com.google.common.reflect.TypeToken;
/**
* Tests behavior of {@code TransientChefApi}
*
* @author Adrian Cole
*/
@Test(groups = { "integration" })
public class TransientChefApiIntegrationTest extends BaseChefContextLiveTest<ChefContext> {
public class TransientChefApiIntegrationTest extends BaseChefLiveTest<ChefApi> {
public static final String PREFIX = System.getProperty("user.name") + "-jcloudstest";
private DatabagItem databagItem;
@ -55,57 +47,37 @@ public class TransientChefApiIntegrationTest extends BaseChefContextLiveTest<Che
return new Properties();
}
@AfterClass(groups = { "integration", "live" })
@Override
public void tearDownContext() {
try {
Closeables.close(context, true);
} catch (IOException e) {
throw propagate(e);
}
}
public void testCreateDatabag() {
context.getApi().deleteDatabag(PREFIX);
context.getApi().createDatabag(PREFIX);
api.deleteDatabag(PREFIX);
api.createDatabag(PREFIX);
}
@Test(dependsOnMethods = "testCreateDatabag")
public void testDatabagExists() {
assertNotNull(context.getApi().databagExists(PREFIX));
assertNotNull(api.databagExists(PREFIX));
}
@Test(dependsOnMethods = { "testDatabagExists" })
public void testCreateDatabagItem() {
Properties config = new Properties();
config.setProperty("foo", "bar");
databagItem = context.getApi().createDatabagItem(PREFIX,
new DatabagItem("config", context.utils().json().toJson(config)));
databagItem = api.createDatabagItem(PREFIX, new DatabagItem("config", json.toJson(config)));
assertNotNull(databagItem);
assertEquals(databagItem.getId(), "config");
assertEquals(config, context.utils().json().fromJson(databagItem.toString(), Properties.class));
assertEquals(config, json.fromJson(databagItem.toString(), Properties.class));
}
@Test(dependsOnMethods = "testCreateDatabagItem")
public void testDatabagItemExists() {
assertNotNull(context.getApi().databagItemExists(PREFIX, PREFIX));
assertNotNull(api.databagItemExists(PREFIX, PREFIX));
}
@Test(dependsOnMethods = "testDatabagItemExists")
public void testUpdateDatabagItem() {
for (String databagItemId : context.getApi().listDatabagItems(PREFIX)) {
DatabagItem databagItem = context.getApi().getDatabagItem(PREFIX, databagItemId);
context.getApi().updateDatabagItem(PREFIX, databagItem);
for (String databagItemId : api.listDatabagItems(PREFIX)) {
DatabagItem databagItem = api.getDatabagItem(PREFIX, databagItemId);
api.updateDatabagItem(PREFIX, databagItem);
}
}
@Override
protected ChefApi getChefApi(ChefContext context) {
return context.getApi();
}
@Override
protected TypeToken<ChefContext> contextType() {
return typeToken(ChefContext.class);
}
}