PageRenderTime 1206ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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