2010-10-17 01:24:22 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
|
|
|
|
*
|
|
|
|
* ====================================================================
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
* ====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jclouds.aws;
|
|
|
|
|
|
|
|
import static org.testng.Assert.assertEquals;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.jclouds.blobstore.BlobStoreContext;
|
|
|
|
import org.jclouds.blobstore.BlobStoreContextFactory;
|
|
|
|
import org.jclouds.compute.ComputeServiceContext;
|
|
|
|
import org.jclouds.compute.ComputeServiceContextFactory;
|
|
|
|
import org.jclouds.compute.RunNodesException;
|
|
|
|
import org.jclouds.compute.domain.NodeMetadata;
|
|
|
|
import org.jclouds.domain.Credentials;
|
|
|
|
import org.jclouds.rest.config.CredentialStoreModule;
|
2010-12-30 17:40:23 -05:00
|
|
|
import org.jclouds.util.Strings2;
|
2010-10-17 01:24:22 -04:00
|
|
|
import org.testng.annotations.BeforeTest;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that credentials stored in the blobstore can be reused across compute contexts.
|
|
|
|
* <p/>
|
|
|
|
* This test is in aws only because it happens to have both blobstore and compute
|
|
|
|
*
|
|
|
|
* TODO create a blobstore and compute integration module
|
|
|
|
*
|
|
|
|
* @author Adrian Cole
|
|
|
|
*/
|
|
|
|
@Test(testName = "jclouds.CredentialsStoredInBlobStoreTest")
|
|
|
|
public class CredentialsStoredInBlobStoreTest {
|
|
|
|
|
|
|
|
private BlobStoreContext blobContext;
|
|
|
|
private Map<String, InputStream> credentialsMap;
|
|
|
|
|
|
|
|
@BeforeTest
|
|
|
|
void setupCredentialContainerAndMap() {
|
|
|
|
blobContext = new BlobStoreContextFactory().createContext("transient", "foo", "bar");
|
|
|
|
blobContext.getBlobStore().createContainerInLocation(null, "credentials");
|
|
|
|
credentialsMap = blobContext.createInputStreamMap("credentials");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testWeCanUseBlobStoreToStoreCredentialsAcrossContexts() throws RunNodesException, IOException {
|
|
|
|
|
|
|
|
ComputeServiceContext computeContext = new ComputeServiceContextFactory().createContext("stub", "foo", "bar",
|
2010-10-28 02:45:30 -04:00
|
|
|
ImmutableSet.of(new CredentialStoreModule(credentialsMap)));
|
2010-10-17 01:24:22 -04:00
|
|
|
|
|
|
|
Set<? extends NodeMetadata> nodes = computeContext.getComputeService().runNodesWithTag("foo", 10);
|
|
|
|
|
|
|
|
verifyCredentialsFromNodesAreInContext(nodes, computeContext);
|
|
|
|
computeContext.close();
|
|
|
|
|
|
|
|
// recreate the compute context with the same map and ensure it still works!
|
2010-10-28 02:45:30 -04:00
|
|
|
computeContext = new ComputeServiceContextFactory().createContext("stub", "foo", "bar", Collections
|
|
|
|
.singleton(new CredentialStoreModule(credentialsMap)));
|
2010-10-17 01:24:22 -04:00
|
|
|
|
|
|
|
verifyCredentialsFromNodesAreInContext(nodes, computeContext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void verifyCredentialsFromNodesAreInContext(Set<? extends NodeMetadata> nodes,
|
2010-10-28 02:45:30 -04:00
|
|
|
ComputeServiceContext computeContext) throws IOException {
|
2010-10-17 01:24:22 -04:00
|
|
|
// verify each node's credential is in the map.
|
|
|
|
assertEquals(computeContext.credentialStore().size(), 10);
|
|
|
|
for (NodeMetadata node : nodes) {
|
2010-10-28 02:45:30 -04:00
|
|
|
assertEquals(computeContext.credentialStore().get("node#" + node.getId()), node.getCredentials());
|
2010-10-17 01:24:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify the credentials are in the backing store and of a known json format
|
|
|
|
assertEquals(credentialsMap.size(), 10);
|
|
|
|
for (Entry<String, InputStream> entry : credentialsMap.entrySet()) {
|
|
|
|
Credentials credentials = computeContext.credentialStore().get(entry.getKey());
|
2010-12-30 17:40:23 -05:00
|
|
|
assertEquals(Strings2.toStringAndClose(entry.getValue()), String.format(
|
2010-10-28 02:45:30 -04:00
|
|
|
"{\"identity\":\"%s\",\"credential\":\"%s\"}", credentials.identity, credentials.credential));
|
2010-10-17 01:24:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|