PageRenderTime 143ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/elasticstack/src/test/java/org/jclouds/elasticstack/ElasticStackClientLiveTest.java

https://github.com/regularfry/jclouds
Java | 385 lines | 300 code | 54 blank | 31 comment | 16 complexity | 777dc9d7de2d875f3e620b087b3a0172 MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.elasticstack;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static org.testng.Assert.assertEquals;
  22. import static org.testng.Assert.assertNotNull;
  23. import java.io.IOException;
  24. import java.util.Properties;
  25. import java.util.Set;
  26. import java.util.concurrent.TimeUnit;
  27. import java.util.logging.Logger;
  28. import org.jclouds.Constants;
  29. import org.jclouds.compute.ComputeServiceContextFactory;
  30. import org.jclouds.compute.domain.ExecResponse;
  31. import org.jclouds.domain.Credentials;
  32. import org.jclouds.elasticstack.domain.ClaimType;
  33. import org.jclouds.elasticstack.domain.CreateDriveRequest;
  34. import org.jclouds.elasticstack.domain.DriveData;
  35. import org.jclouds.elasticstack.domain.DriveInfo;
  36. import org.jclouds.elasticstack.domain.DriveStatus;
  37. import org.jclouds.elasticstack.domain.IDEDevice;
  38. import org.jclouds.elasticstack.domain.ImageConversionType;
  39. import org.jclouds.elasticstack.domain.Model;
  40. import org.jclouds.elasticstack.domain.Server;
  41. import org.jclouds.elasticstack.domain.ServerInfo;
  42. import org.jclouds.elasticstack.domain.ServerStatus;
  43. import org.jclouds.elasticstack.predicates.DriveClaimed;
  44. import org.jclouds.elasticstack.util.Servers;
  45. import org.jclouds.io.Payloads;
  46. import org.jclouds.logging.log4j.config.Log4JLoggingModule;
  47. import org.jclouds.net.IPSocket;
  48. import org.jclouds.predicates.InetSocketAddressConnect;
  49. import org.jclouds.predicates.RetryablePredicate;
  50. import org.jclouds.rest.RestContext;
  51. import org.jclouds.ssh.SshClient;
  52. import org.jclouds.sshj.config.SshjSshClientModule;
  53. import org.jclouds.util.Strings2;
  54. import org.testng.annotations.AfterGroups;
  55. import org.testng.annotations.BeforeGroups;
  56. import org.testng.annotations.Test;
  57. import com.google.common.base.Predicate;
  58. import com.google.common.base.Predicates;
  59. import com.google.common.collect.ImmutableMap;
  60. import com.google.common.collect.ImmutableSet;
  61. import com.google.gson.Gson;
  62. import com.google.inject.Guice;
  63. import com.google.inject.Module;
  64. /**
  65. * Tests behavior of {@code ElasticStackClient}
  66. *
  67. * @author Adrian Cole
  68. */
  69. @Test(groups = "live")
  70. public class ElasticStackClientLiveTest {
  71. protected long driveSize = 1 * 1024 * 1024 * 1024l;
  72. protected int maxDriveImageTime = 360;
  73. protected String vncPassword = "Il0veVNC";
  74. protected ElasticStackClient client;
  75. protected RestContext<ElasticStackClient, ElasticStackAsyncClient> context;
  76. protected Predicate<IPSocket> socketTester;
  77. protected String bootDrive = "38df0986-4d85-4b76-b502-3878ffc80161";
  78. protected String provider = "elasticstack";
  79. protected String identity;
  80. protected String credential;
  81. protected String endpoint;
  82. protected String apiversion;
  83. protected Predicate<DriveInfo> driveNotClaimed;
  84. protected void setupCredentials() {
  85. identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
  86. credential = System.getProperty("test." + provider + ".credential");
  87. endpoint = System.getProperty("test." + provider + ".endpoint");
  88. apiversion = System.getProperty("test." + provider + ".apiversion");
  89. }
  90. protected Properties setupProperties() {
  91. Properties overrides = new Properties();
  92. overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
  93. overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
  94. overrides.setProperty(provider + ".identity", identity);
  95. if (credential != null)
  96. overrides.setProperty(provider + ".credential", credential);
  97. if (endpoint != null)
  98. overrides.setProperty(provider + ".endpoint", endpoint);
  99. if (apiversion != null)
  100. overrides.setProperty(provider + ".apiversion", apiversion);
  101. return overrides;
  102. }
  103. @BeforeGroups(groups = "live")
  104. public void setupClient() {
  105. setupCredentials();
  106. Properties overrides = setupProperties();
  107. context = new ComputeServiceContextFactory().createContext(provider,
  108. ImmutableSet.<Module> of(new Log4JLoggingModule()), overrides).getProviderSpecificContext();
  109. client = context.getApi();
  110. driveNotClaimed = new RetryablePredicate<DriveInfo>(Predicates.not(new DriveClaimed(client)), maxDriveImageTime,
  111. 1, TimeUnit.SECONDS);
  112. socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), maxDriveImageTime, 1,
  113. TimeUnit.SECONDS);
  114. }
  115. @Test
  116. public void testListServers() throws Exception {
  117. Set<String> servers = client.listServers();
  118. assertNotNull(servers);
  119. }
  120. @Test
  121. public void testListServerInfo() throws Exception {
  122. Set<? extends ServerInfo> servers = client.listServerInfo();
  123. assertNotNull(servers);
  124. }
  125. @Test
  126. public void testGetServer() throws Exception {
  127. for (String serverUUID : client.listServers()) {
  128. assert !"".equals(serverUUID);
  129. assertNotNull(client.getServerInfo(serverUUID));
  130. }
  131. }
  132. @Test
  133. public void testListDrives() throws Exception {
  134. Set<String> drives = client.listDrives();
  135. assertNotNull(drives);
  136. }
  137. @Test
  138. public void testListDriveInfo() throws Exception {
  139. Set<? extends DriveInfo> drives = client.listDriveInfo();
  140. assertNotNull(drives);
  141. }
  142. @Test
  143. public void testGetDrive() throws Exception {
  144. for (String driveUUID : client.listDrives()) {
  145. assert !"".equals(driveUUID) : driveUUID;
  146. assert client.getDriveInfo(driveUUID) != null : driveUUID;
  147. }
  148. }
  149. protected String prefix = System.getProperty("user.name") + ".test";
  150. protected DriveInfo drive;
  151. @Test
  152. public void testCreateDrive() throws Exception {
  153. drive = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(driveSize).build());
  154. checkCreatedDrive();
  155. DriveInfo newInfo = client.getDriveInfo(drive.getUuid());
  156. checkDriveMatchesGet(newInfo);
  157. }
  158. protected void checkDriveMatchesGet(DriveInfo newInfo) {
  159. assertEquals(newInfo.getUuid(), drive.getUuid());
  160. }
  161. protected void checkCreatedDrive() {
  162. assertNotNull(drive.getUuid());
  163. assertNotNull(drive.getUser());
  164. assertEquals(drive.getName(), prefix);
  165. assertEquals(drive.getSize(), driveSize);
  166. assertEquals(drive.getStatus(), DriveStatus.ACTIVE);
  167. // for some reason, these occasionally return as 4096,1
  168. // assertEquals(info.getReadBytes(), 0l);
  169. // assertEquals(info.getWriteBytes(), 0l);
  170. // assertEquals(info.getReadRequests(), 0l);
  171. // assertEquals(info.getWriteRequests(), 0l);
  172. assertEquals(drive.getEncryptionCipher(), "aes-xts-plain");
  173. }
  174. @Test(dependsOnMethods = "testCreateDrive")
  175. public void testSetDriveData() throws Exception {
  176. DriveInfo drive2 = client.setDriveData(drive.getUuid(), new DriveData.Builder().claimType(ClaimType.SHARED).name(
  177. "rediculous").readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")).tags(
  178. ImmutableSet.of("networking", "security", "gateway")).userMetadata(ImmutableMap.of("foo", "bar"))
  179. .build());
  180. assertNotNull(drive2.getUuid(), drive.getUuid());
  181. assertEquals(drive2.getName(), "rediculous");
  182. assertEquals(drive2.getClaimType(), ClaimType.SHARED);
  183. assertEquals(drive2.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"));
  184. assertEquals(drive2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
  185. assertEquals(drive2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
  186. drive = drive2;
  187. }
  188. protected ServerInfo server;
  189. @Test(dependsOnMethods = "testSetDriveData")
  190. public void testCreateAndStartServer() throws Exception {
  191. Logger.getAnonymousLogger().info("preparing drive");
  192. prepareDrive();
  193. Server serverRequest = Servers.small(prefix, drive.getUuid(), vncPassword).build();
  194. Logger.getAnonymousLogger().info("starting server");
  195. server = client.createServer(serverRequest);
  196. client.startServer(server.getUuid());
  197. server = client.getServerInfo(server.getUuid());
  198. checkStartedServer();
  199. Server newInfo = client.getServerInfo(server.getUuid());
  200. checkServerMatchesGet(newInfo);
  201. }
  202. protected void checkServerMatchesGet(Server newInfo) {
  203. assertEquals(newInfo.getUuid(), server.getUuid());
  204. }
  205. protected void checkStartedServer() {
  206. System.out.println(new Gson().toJson(server));
  207. assertNotNull(server.getUuid());
  208. assertNotNull(server.getUser());
  209. assertEquals(server.getName(), prefix);
  210. assertEquals(server.isPersistent(), true);
  211. assertEquals(server.getDevices(), ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid(drive.getUuid())
  212. .build()));
  213. assertEquals(server.getBootDeviceIds(), ImmutableSet.of("ide:0:0"));
  214. assertEquals(server.getNics().get(0).getDhcp(), server.getVnc().getIp());
  215. assertEquals(server.getNics().get(0).getModel(), Model.E1000);
  216. assertEquals(server.getStatus(), ServerStatus.ACTIVE);
  217. }
  218. @Test(dependsOnMethods = "testCreateAndStartServer")
  219. public void testConnectivity() throws Exception {
  220. IPSocket vncsocket = new IPSocket(server.getVnc().getIp(), 5900);
  221. Logger.getAnonymousLogger().info("awaiting vnc: " + vncsocket);
  222. assert socketTester.apply(vncsocket) : server;
  223. IPSocket sshsocket = new IPSocket(server.getNics().get(0).getDhcp(), 22);
  224. Logger.getAnonymousLogger().info("awaiting ssh: " + sshsocket);
  225. assert socketTester.apply(sshsocket) : server;
  226. doConnectViaSsh(server, getSshCredentials(server));
  227. }
  228. @Test(dependsOnMethods = "testConnectivity")
  229. public void testLifeCycle() throws Exception {
  230. client.stopServer(server.getUuid());
  231. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  232. client.startServer(server.getUuid());
  233. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  234. client.resetServer(server.getUuid());
  235. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  236. client.shutdownServer(server.getUuid());
  237. // behavior on shutdown depends on how your server OS is set up to respond to an ACPI power
  238. // button signal
  239. assert (client.getServerInfo(server.getUuid()).getStatus() == ServerStatus.ACTIVE || client.getServerInfo(
  240. server.getUuid()).getStatus() == ServerStatus.STOPPED);
  241. }
  242. @Test(dependsOnMethods = "testLifeCycle")
  243. public void testSetServerConfiguration() throws Exception {
  244. client.stopServer(server.getUuid());
  245. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  246. ServerInfo server2 = client.setServerConfiguration(server.getUuid(), Server.Builder.fromServer(server).name(
  247. "rediculous").tags(ImmutableSet.of("networking", "security", "gateway")).userMetadata(
  248. ImmutableMap.of("foo", "bar")).build());
  249. assertNotNull(server2.getUuid(), server.getUuid());
  250. assertEquals(server2.getName(), "rediculous");
  251. checkTagsAndMetadata(server2);
  252. server = server2;
  253. }
  254. protected void checkTagsAndMetadata(ServerInfo server2) {
  255. assertEquals(server2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
  256. assertEquals(server2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
  257. }
  258. @Test(dependsOnMethods = "testSetServerConfiguration")
  259. public void testDestroyServer() throws Exception {
  260. client.destroyServer(server.getUuid());
  261. assertEquals(client.getServerInfo(server.getUuid()), null);
  262. }
  263. @Test(dependsOnMethods = "testDestroyServer")
  264. public void testDestroyDrive() throws Exception {
  265. client.destroyDrive(drive.getUuid());
  266. assertEquals(client.getDriveInfo(drive.getUuid()), null);
  267. }
  268. protected void doConnectViaSsh(Server server, Credentials creds) throws IOException {
  269. SshClient ssh = Guice.createInjector(new SshjSshClientModule()).getInstance(SshClient.Factory.class).create(
  270. new IPSocket(server.getVnc().getIp(), 22), creds);
  271. try {
  272. ssh.connect();
  273. ExecResponse hello = ssh.exec("echo hello");
  274. assertEquals(hello.getOutput().trim(), "hello");
  275. System.err.println(ssh.exec("df -k").getOutput());
  276. System.err.println(ssh.exec("mount").getOutput());
  277. System.err.println(ssh.exec("uname -a").getOutput());
  278. } finally {
  279. if (ssh != null)
  280. ssh.disconnect();
  281. }
  282. }
  283. @AfterGroups(groups = "live")
  284. protected void tearDown() {
  285. try {
  286. client.destroyServer(server.getUuid());
  287. } catch (Exception e) {
  288. // no need to check null or anything as we swallow all
  289. }
  290. try {
  291. client.destroyDrive(drive.getUuid());
  292. } catch (Exception e) {
  293. }
  294. if (context != null)
  295. context.close();
  296. }
  297. private DriveInfo drive2;
  298. private DriveInfo drive3;
  299. public void testWeCanReadAndWriteToDrive() throws IOException {
  300. drive2 = client.createDrive(new CreateDriveRequest.Builder().name(prefix + "2").size(1 * 1024 * 1024l).build());
  301. client.writeDrive(drive2.getUuid(), Payloads.newStringPayload("foo"));
  302. assertEquals(Strings2.toStringAndClose(client.readDrive(drive2.getUuid(), 0, 3).getInput()), "foo");
  303. }
  304. @Test(dependsOnMethods = "testWeCanReadAndWriteToDrive")
  305. public void testWeCopyADriveContentsViaGzip() throws IOException {
  306. try {
  307. drive3 = client
  308. .createDrive(new CreateDriveRequest.Builder().name(prefix + "3").size(1 * 1024 * 1024l).build());
  309. System.err.println("before image; drive 2" + client.getDriveInfo(drive2.getUuid()));
  310. System.err.println("before image; drive 3" + client.getDriveInfo(drive3.getUuid()));
  311. client.imageDrive(drive2.getUuid(), drive3.getUuid());
  312. assert driveNotClaimed.apply(drive3) : client.getDriveInfo(drive3.getUuid());
  313. assert driveNotClaimed.apply(drive2) : client.getDriveInfo(drive2.getUuid());
  314. System.err.println("after image; drive 2" + client.getDriveInfo(drive2.getUuid()));
  315. System.err.println("after image; drive 3" + client.getDriveInfo(drive3.getUuid()));
  316. assertEquals(Strings2.toStringAndClose(client.readDrive(drive3.getUuid(), 0, 3).getInput()), "foo");
  317. } finally {
  318. client.destroyDrive(drive2.getUuid());
  319. client.destroyDrive(drive3.getUuid());
  320. }
  321. }
  322. protected Credentials getSshCredentials(Server server) {
  323. return new Credentials("toor", server.getVnc().getPassword());
  324. }
  325. protected void prepareDrive() {
  326. System.err.println("before prepare" + client.getDriveInfo(drive.getUuid()));
  327. client.imageDrive(bootDrive, drive.getUuid(), ImageConversionType.GUNZIP);
  328. assert driveNotClaimed.apply(drive) : client.getDriveInfo(drive.getUuid());
  329. System.err.println("after prepare" + client.getDriveInfo(drive.getUuid()));
  330. }
  331. }