PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mattstep/jclouds
Java | 354 lines | 272 code | 51 blank | 31 comment | 8 complexity | 9256a372e5aa321b5ddf304b1026622f 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 java.util.concurrent.TimeUnit.SECONDS;
  21. import static org.jclouds.util.Predicates2.retry;
  22. import static org.testng.Assert.assertEquals;
  23. import static org.testng.Assert.assertNotNull;
  24. import java.io.IOException;
  25. import java.util.Set;
  26. import java.util.logging.Logger;
  27. import org.jclouds.compute.domain.ExecResponse;
  28. import org.jclouds.compute.internal.BaseComputeServiceContextLiveTest;
  29. import org.jclouds.domain.LoginCredentials;
  30. import org.jclouds.elasticstack.domain.ClaimType;
  31. import org.jclouds.elasticstack.domain.CreateDriveRequest;
  32. import org.jclouds.elasticstack.domain.DriveData;
  33. import org.jclouds.elasticstack.domain.DriveInfo;
  34. import org.jclouds.elasticstack.domain.DriveStatus;
  35. import org.jclouds.elasticstack.domain.IDEDevice;
  36. import org.jclouds.elasticstack.domain.ImageConversionType;
  37. import org.jclouds.elasticstack.domain.Model;
  38. import org.jclouds.elasticstack.domain.Server;
  39. import org.jclouds.elasticstack.domain.ServerInfo;
  40. import org.jclouds.elasticstack.domain.ServerStatus;
  41. import org.jclouds.elasticstack.predicates.DriveClaimed;
  42. import org.jclouds.elasticstack.util.Servers;
  43. import org.jclouds.io.Payloads;
  44. import org.jclouds.predicates.SocketOpen;
  45. import org.jclouds.rest.RestContext;
  46. import org.jclouds.ssh.SshClient;
  47. import org.jclouds.sshj.config.SshjSshClientModule;
  48. import org.jclouds.util.Strings2;
  49. import org.testng.annotations.AfterGroups;
  50. import org.testng.annotations.BeforeClass;
  51. import org.testng.annotations.Test;
  52. import com.google.common.base.Predicate;
  53. import com.google.common.base.Predicates;
  54. import com.google.common.collect.ImmutableMap;
  55. import com.google.common.collect.ImmutableSet;
  56. import com.google.common.net.HostAndPort;
  57. import com.google.gson.Gson;
  58. import com.google.inject.Guice;
  59. /**
  60. * Tests behavior of {@code ElasticStackClient}
  61. *
  62. * @author Adrian Cole
  63. */
  64. @Test(groups = "live", singleThreaded = true, testName = "ElasticStackClientLiveTest")
  65. public class ElasticStackClientLiveTest extends BaseComputeServiceContextLiveTest {
  66. public ElasticStackClientLiveTest() {
  67. provider = "elasticstack";
  68. }
  69. protected long driveSize = 1 * 1024 * 1024 * 1024l;
  70. protected int maxDriveImageTime = 360;
  71. protected String vncPassword = "Il0veVNC";
  72. protected ElasticStackClient client;
  73. protected RestContext<ElasticStackClient, ElasticStackAsyncClient> cloudStackContext;
  74. protected Predicate<HostAndPort> socketTester;
  75. protected Predicate<DriveInfo> driveNotClaimed;
  76. protected String imageId;
  77. @Override
  78. @BeforeClass(groups = { "integration", "live" })
  79. public void setupContext() {
  80. super.setupContext();
  81. imageId = view.getComputeService().templateBuilder().build().getImage().getId();
  82. client = view.unwrap(ElasticStackApiMetadata.CONTEXT_TOKEN).getApi();
  83. driveNotClaimed = retry(Predicates.not(new DriveClaimed(client)), maxDriveImageTime, 1, SECONDS);
  84. SocketOpen socketOpen = context.utils().injector().getInstance(SocketOpen.class);
  85. socketTester = retry(socketOpen, maxDriveImageTime, 1, SECONDS);
  86. }
  87. @Test
  88. public void testListServers() throws Exception {
  89. Set<String> servers = client.listServers();
  90. assertNotNull(servers);
  91. }
  92. @Test
  93. public void testListServerInfo() throws Exception {
  94. Set<? extends ServerInfo> servers = client.listServerInfo();
  95. assertNotNull(servers);
  96. }
  97. @Test
  98. public void testGetServer() throws Exception {
  99. for (String serverUUID : client.listServers()) {
  100. assert !"".equals(serverUUID);
  101. assertNotNull(client.getServerInfo(serverUUID));
  102. }
  103. }
  104. @Test
  105. public void testListDrives() throws Exception {
  106. Set<String> drives = client.listDrives();
  107. assertNotNull(drives);
  108. }
  109. @Test
  110. public void testListDriveInfo() throws Exception {
  111. Set<? extends DriveInfo> drives = client.listDriveInfo();
  112. assertNotNull(drives);
  113. }
  114. @Test
  115. public void testGetDrive() throws Exception {
  116. for (String driveUUID : client.listDrives()) {
  117. assert !"".equals(driveUUID) : driveUUID;
  118. assert client.getDriveInfo(driveUUID) != null : driveUUID;
  119. }
  120. }
  121. protected String prefix = System.getProperty("user.name") + ".test";
  122. protected DriveInfo drive;
  123. @Test
  124. public void testCreateDrive() throws Exception {
  125. drive = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(driveSize).build());
  126. checkCreatedDrive();
  127. DriveInfo newInfo = client.getDriveInfo(drive.getUuid());
  128. checkDriveMatchesGet(newInfo);
  129. }
  130. protected void checkDriveMatchesGet(DriveInfo newInfo) {
  131. assertEquals(newInfo.getUuid(), drive.getUuid());
  132. }
  133. protected void checkCreatedDrive() {
  134. assertNotNull(drive.getUuid());
  135. assertNotNull(drive.getUser());
  136. assertEquals(drive.getName(), prefix);
  137. assertEquals(drive.getSize(), driveSize);
  138. assertEquals(drive.getStatus(), DriveStatus.ACTIVE);
  139. // for some reason, these occasionally return as 4096,1
  140. // assertEquals(info.getReadBytes(), 0l);
  141. // assertEquals(info.getWriteBytes(), 0l);
  142. // assertEquals(info.getReadRequests(), 0l);
  143. // assertEquals(info.getWriteRequests(), 0l);
  144. assertEquals(drive.getEncryptionCipher(), "aes-xts-plain");
  145. }
  146. @Test(dependsOnMethods = "testCreateDrive")
  147. public void testSetDriveData() throws Exception {
  148. DriveInfo drive2 = client.setDriveData(drive.getUuid(), new DriveData.Builder().claimType(ClaimType.SHARED).name(
  149. "rediculous").readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")).tags(
  150. ImmutableSet.of("networking", "security", "gateway")).userMetadata(ImmutableMap.of("foo", "bar"))
  151. .build());
  152. assertNotNull(drive2.getUuid(), drive.getUuid());
  153. assertEquals(drive2.getName(), "rediculous");
  154. assertEquals(drive2.getClaimType(), ClaimType.SHARED);
  155. assertEquals(drive2.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"));
  156. assertEquals(drive2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
  157. assertEquals(drive2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
  158. drive = drive2;
  159. }
  160. protected ServerInfo server;
  161. @Test(dependsOnMethods = "testSetDriveData")
  162. public void testCreateAndStartServer() throws Exception {
  163. Logger.getAnonymousLogger().info("preparing drive");
  164. prepareDrive();
  165. Server serverRequest = Servers.small(prefix, drive.getUuid(), vncPassword).build();
  166. Logger.getAnonymousLogger().info("starting server");
  167. server = client.createServer(serverRequest);
  168. client.startServer(server.getUuid());
  169. server = client.getServerInfo(server.getUuid());
  170. checkStartedServer();
  171. Server newInfo = client.getServerInfo(server.getUuid());
  172. checkServerMatchesGet(newInfo);
  173. }
  174. protected void checkServerMatchesGet(Server newInfo) {
  175. assertEquals(newInfo.getUuid(), server.getUuid());
  176. }
  177. protected void checkStartedServer() {
  178. System.out.println(new Gson().toJson(server));
  179. assertNotNull(server.getUuid());
  180. assertNotNull(server.getUser());
  181. assertEquals(server.getName(), prefix);
  182. assertEquals(server.isPersistent(), true);
  183. assertEquals(server.getDevices(), ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid(drive.getUuid())
  184. .build()));
  185. assertEquals(server.getBootDeviceIds(), ImmutableSet.of("ide:0:0"));
  186. assertEquals(server.getNics().get(0).getDhcp(), server.getVnc().getIp());
  187. assertEquals(server.getNics().get(0).getModel(), Model.E1000);
  188. assertEquals(server.getStatus(), ServerStatus.ACTIVE);
  189. }
  190. @Test(dependsOnMethods = "testCreateAndStartServer")
  191. public void testConnectivity() throws Exception {
  192. HostAndPort vncsocket = HostAndPort.fromParts(server.getVnc().getIp(), 5900);
  193. Logger.getAnonymousLogger().info("awaiting vnc: " + vncsocket);
  194. assert socketTester.apply(vncsocket) : server;
  195. HostAndPort sshsocket = HostAndPort.fromParts(server.getNics().get(0).getDhcp(), 22);
  196. Logger.getAnonymousLogger().info("awaiting ssh: " + sshsocket);
  197. assert socketTester.apply(sshsocket) : server;
  198. doConnectViaSsh(server, getSshCredentials(server));
  199. }
  200. @Test(dependsOnMethods = "testConnectivity")
  201. public void testLifeCycle() throws Exception {
  202. client.stopServer(server.getUuid());
  203. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  204. client.startServer(server.getUuid());
  205. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  206. client.resetServer(server.getUuid());
  207. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  208. client.shutdownServer(server.getUuid());
  209. // behavior on shutdown depends on how your server OS is set up to respond to an ACPI power
  210. // button signal
  211. assert (client.getServerInfo(server.getUuid()).getStatus() == ServerStatus.ACTIVE || client.getServerInfo(
  212. server.getUuid()).getStatus() == ServerStatus.STOPPED);
  213. }
  214. @Test(dependsOnMethods = "testLifeCycle")
  215. public void testSetServerConfiguration() throws Exception {
  216. client.stopServer(server.getUuid());
  217. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  218. ServerInfo server2 = client.setServerConfiguration(server.getUuid(), Server.Builder.fromServer(server).name(
  219. "rediculous").tags(ImmutableSet.of("networking", "security", "gateway")).userMetadata(
  220. ImmutableMap.of("foo", "bar")).build());
  221. assertNotNull(server2.getUuid(), server.getUuid());
  222. assertEquals(server2.getName(), "rediculous");
  223. checkTagsAndMetadata(server2);
  224. server = server2;
  225. }
  226. protected void checkTagsAndMetadata(ServerInfo server2) {
  227. assertEquals(server2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
  228. assertEquals(server2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
  229. }
  230. @Test(dependsOnMethods = "testSetServerConfiguration")
  231. public void testDestroyServer() throws Exception {
  232. client.destroyServer(server.getUuid());
  233. assertEquals(client.getServerInfo(server.getUuid()), null);
  234. }
  235. @Test(dependsOnMethods = "testDestroyServer")
  236. public void testDestroyDrive() throws Exception {
  237. client.destroyDrive(drive.getUuid());
  238. assertEquals(client.getDriveInfo(drive.getUuid()), null);
  239. }
  240. protected void doConnectViaSsh(Server server, LoginCredentials creds) throws IOException {
  241. SshClient ssh = Guice.createInjector(new SshjSshClientModule()).getInstance(SshClient.Factory.class).create(
  242. HostAndPort.fromParts(server.getVnc().getIp(), 22), creds);
  243. try {
  244. ssh.connect();
  245. ExecResponse hello = ssh.exec("echo hello");
  246. assertEquals(hello.getOutput().trim(), "hello");
  247. System.err.println(ssh.exec("df -k").getOutput());
  248. System.err.println(ssh.exec("mount").getOutput());
  249. System.err.println(ssh.exec("uname -a").getOutput());
  250. } finally {
  251. if (ssh != null)
  252. ssh.disconnect();
  253. }
  254. }
  255. @AfterGroups(groups = "live")
  256. @Override
  257. protected void tearDownContext() {
  258. try {
  259. client.destroyServer(server.getUuid());
  260. } catch (Exception e) {
  261. // no need to check null or anything as we swallow all
  262. }
  263. try {
  264. client.destroyDrive(drive.getUuid());
  265. } catch (Exception e) {
  266. }
  267. super.tearDownContext();
  268. }
  269. private DriveInfo drive2;
  270. private DriveInfo drive3;
  271. public void testWeCanReadAndWriteToDrive() throws IOException {
  272. drive2 = client.createDrive(new CreateDriveRequest.Builder().name(prefix + "2").size(1 * 1024 * 1024l).build());
  273. client.writeDrive(drive2.getUuid(), Payloads.newStringPayload("foo"));
  274. assertEquals(Strings2.toString(client.readDrive(drive2.getUuid(), 0, 3)), "foo");
  275. }
  276. @Test(dependsOnMethods = "testWeCanReadAndWriteToDrive")
  277. public void testWeCopyADriveContentsViaGzip() throws IOException {
  278. try {
  279. drive3 = client
  280. .createDrive(new CreateDriveRequest.Builder().name(prefix + "3").size(1 * 1024 * 1024l).build());
  281. System.err.println("before image; drive 2" + client.getDriveInfo(drive2.getUuid()));
  282. System.err.println("before image; drive 3" + client.getDriveInfo(drive3.getUuid()));
  283. client.imageDrive(drive2.getUuid(), drive3.getUuid());
  284. assert driveNotClaimed.apply(drive3) : client.getDriveInfo(drive3.getUuid());
  285. assert driveNotClaimed.apply(drive2) : client.getDriveInfo(drive2.getUuid());
  286. System.err.println("after image; drive 2" + client.getDriveInfo(drive2.getUuid()));
  287. System.err.println("after image; drive 3" + client.getDriveInfo(drive3.getUuid()));
  288. assertEquals(Strings2.toString(client.readDrive(drive3.getUuid(), 0, 3)), "foo");
  289. } finally {
  290. client.destroyDrive(drive2.getUuid());
  291. client.destroyDrive(drive3.getUuid());
  292. }
  293. }
  294. protected LoginCredentials getSshCredentials(Server server) {
  295. return LoginCredentials.builder().user("toor").password(server.getVnc().getPassword()).build();
  296. }
  297. protected void prepareDrive() {
  298. System.err.println("before prepare" + client.getDriveInfo(drive.getUuid()));
  299. client.imageDrive(imageId, drive.getUuid(), ImageConversionType.GUNZIP);
  300. assert driveNotClaimed.apply(drive) : client.getDriveInfo(drive.getUuid());
  301. System.err.println("after prepare" + client.getDriveInfo(drive.getUuid()));
  302. }
  303. }