PageRenderTime 848ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/cloudsigma/src/test/java/org/jclouds/cloudsigma/CloudSigmaClientLiveTest.java

http://github.com/jclouds/jclouds
Java | 443 lines | 347 code | 65 blank | 31 comment | 22 complexity | f0fc802ba2830da8794937791485593f MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.cloudsigma;
  18. import static java.util.concurrent.TimeUnit.SECONDS;
  19. import static org.jclouds.util.Predicates2.retry;
  20. import static org.testng.Assert.assertEquals;
  21. import static org.testng.Assert.assertNotNull;
  22. import java.io.IOException;
  23. import java.util.Set;
  24. import java.util.logging.Logger;
  25. import org.jclouds.cloudsigma.domain.ClaimType;
  26. import org.jclouds.cloudsigma.domain.CreateDriveRequest;
  27. import org.jclouds.cloudsigma.domain.DriveData;
  28. import org.jclouds.cloudsigma.domain.DriveInfo;
  29. import org.jclouds.cloudsigma.domain.DriveStatus;
  30. import org.jclouds.cloudsigma.domain.DriveType;
  31. import org.jclouds.cloudsigma.domain.IDEDevice;
  32. import org.jclouds.cloudsigma.domain.Model;
  33. import org.jclouds.cloudsigma.domain.ProfileInfo;
  34. import org.jclouds.cloudsigma.domain.Server;
  35. import org.jclouds.cloudsigma.domain.ServerInfo;
  36. import org.jclouds.cloudsigma.domain.ServerStatus;
  37. import org.jclouds.cloudsigma.domain.StaticIPInfo;
  38. import org.jclouds.cloudsigma.domain.VLANInfo;
  39. import org.jclouds.cloudsigma.options.CloneDriveOptions;
  40. import org.jclouds.cloudsigma.predicates.DriveClaimed;
  41. import org.jclouds.cloudsigma.util.Servers;
  42. import org.jclouds.compute.domain.ExecResponse;
  43. import org.jclouds.compute.internal.BaseComputeServiceContextLiveTest;
  44. import org.jclouds.domain.LoginCredentials;
  45. import org.jclouds.predicates.SocketOpen;
  46. import org.jclouds.ssh.SshClient;
  47. import org.jclouds.sshj.config.SshjSshClientModule;
  48. import org.testng.annotations.AfterGroups;
  49. import org.testng.annotations.BeforeGroups;
  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 CloudSigmaApi}
  60. *
  61. * @author Adrian Cole
  62. */
  63. @Test(groups = "live", singleThreaded = true, testName = "CloudSigmaClientLiveTest")
  64. public class CloudSigmaClientLiveTest extends BaseComputeServiceContextLiveTest {
  65. public CloudSigmaClientLiveTest() {
  66. provider = "cloudsigma";
  67. }
  68. protected long driveSize = 8 * 1024 * 1024 * 1024l;
  69. protected int maxDriveImageTime = 300;
  70. protected String vncPassword = "Il0veVNC";
  71. protected CloudSigmaApi client;
  72. protected Predicate<HostAndPort> socketTester;
  73. protected Predicate<DriveInfo> driveNotClaimed;
  74. protected String imageId;
  75. @BeforeGroups(groups = { "integration", "live" })
  76. @Override
  77. public void setupContext() {
  78. super.setupContext();
  79. client = view.utils().injector().getInstance(CloudSigmaApi.class);
  80. driveNotClaimed = retry(Predicates.not(new DriveClaimed(client)), maxDriveImageTime, 1, SECONDS);
  81. SocketOpen socketOpten = context.utils().injector().getInstance(SocketOpen.class);
  82. socketTester = retry(socketOpten, maxDriveImageTime, 1, SECONDS);
  83. if (template == null || template.getImageId() == null) {
  84. imageId = view.getComputeService().templateBuilder().build().getImage().getId();
  85. }
  86. }
  87. @Test
  88. public void testGetProfileInfo() throws Exception {
  89. ProfileInfo profile = client.getProfileInfo();
  90. assertNotNull(profile);
  91. }
  92. @Test
  93. public void testListVLANs() throws Exception {
  94. Set<String> vlans = client.listVLANs();
  95. assertNotNull(vlans);
  96. }
  97. @Test
  98. public void testListVLANInfo() throws Exception {
  99. Set<? extends VLANInfo> vlans = client.listVLANInfo();
  100. assertNotNull(vlans);
  101. }
  102. @Test
  103. public void testGetVLAN() throws Exception {
  104. for (String vlanUUID : client.listVLANs()) {
  105. assert !"".equals(vlanUUID);
  106. assertNotNull(client.getVLANInfo(vlanUUID));
  107. }
  108. }
  109. @Test
  110. public void testListStaticIPs() throws Exception {
  111. Set<String> ips = client.listStaticIPs();
  112. assertNotNull(ips);
  113. }
  114. @Test
  115. public void testListStaticIPInfo() throws Exception {
  116. Set<? extends StaticIPInfo> ips = client.listStaticIPInfo();
  117. assertNotNull(ips);
  118. }
  119. @Test
  120. public void testGetStaticIP() throws Exception {
  121. for (String ipUUID : client.listStaticIPs()) {
  122. assert !"".equals(ipUUID);
  123. assertNotNull(client.getStaticIPInfo(ipUUID));
  124. }
  125. }
  126. @Test
  127. public void testListServers() throws Exception {
  128. Set<String> servers = client.listServers();
  129. assertNotNull(servers);
  130. }
  131. @Test
  132. public void testListServerInfo() throws Exception {
  133. Set<? extends ServerInfo> servers = client.listServerInfo();
  134. assertNotNull(servers);
  135. }
  136. @Test
  137. public void testGetServer() throws Exception {
  138. for (String serverUUID : client.listServers()) {
  139. assert !"".equals(serverUUID);
  140. assertNotNull(client.getServerInfo(serverUUID));
  141. }
  142. }
  143. @Test
  144. public void testListDrives() throws Exception {
  145. Set<String> drives = client.listDrives();
  146. assertNotNull(drives);
  147. }
  148. @Test
  149. public void testListDriveInfo() throws Exception {
  150. Set<? extends DriveInfo> drives = client.listDriveInfo();
  151. assertNotNull(drives);
  152. }
  153. @Test
  154. public void testGetDrive() throws Exception {
  155. for (String driveUUID : client.listStandardDrives()) {
  156. assert !"".equals(driveUUID);
  157. DriveInfo drive = client.getDriveInfo(driveUUID);
  158. assertNotNull(drive);
  159. assert !drive.getType().equals(DriveType.UNRECOGNIZED) : drive;
  160. if (drive.getType() == DriveType.DISK && drive.getDriveType().contains("preinstalled"))
  161. System.out.println(drive.getName());
  162. }
  163. }
  164. protected String prefix = System.getProperty("user.name") + ".test";
  165. protected DriveInfo drive;
  166. @Test
  167. public void testCreateDrive() throws Exception {
  168. drive = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(driveSize).build());
  169. checkCreatedDrive();
  170. DriveInfo newInfo = client.getDriveInfo(drive.getUuid());
  171. checkDriveMatchesGet(newInfo);
  172. }
  173. protected void checkDriveMatchesGet(DriveInfo newInfo) {
  174. assertEquals(newInfo.getUuid(), drive.getUuid());
  175. assertEquals(newInfo.getType(), DriveType.DISK);
  176. }
  177. protected void checkCreatedDrive() {
  178. assertNotNull(drive.getUuid());
  179. assertNotNull(drive.getUser());
  180. assertEquals(drive.getName(), prefix);
  181. assertEquals(drive.getSize(), driveSize);
  182. assertEquals(drive.getStatus(), DriveStatus.ACTIVE);
  183. // for some reason, these occasionally return as 4096,1
  184. // assertEquals(info.getReadBytes(), 0l);
  185. // assertEquals(info.getWriteBytes(), 0l);
  186. // assertEquals(info.getReadRequests(), 0l);
  187. // assertEquals(info.getWriteRequests(), 0l);
  188. assertEquals(drive.getEncryptionCipher(), "aes-xts-plain");
  189. assertEquals(drive.getType(), null);
  190. }
  191. @Test(dependsOnMethods = "testCreateDrive")
  192. public void testSetDriveData() throws Exception {
  193. DriveInfo drive2 = client.setDriveData(
  194. drive.getUuid(),
  195. new DriveData.Builder().claimType(ClaimType.SHARED).name("rediculous")
  196. .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"))
  197. .use(ImmutableSet.of("networking", "security", "gateway")).build());
  198. assertNotNull(drive2.getUuid(), drive.getUuid());
  199. assertEquals(drive2.getName(), "rediculous");
  200. assertEquals(drive2.getClaimType(), ClaimType.SHARED);
  201. assertEquals(drive2.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"));
  202. assertEquals(drive2.getUse(), ImmutableSet.of("networking", "security", "gateway"));
  203. drive = drive2;
  204. }
  205. @Test
  206. public void testCreateAndDestroyVLAN() throws Exception {
  207. VLANInfo vlan = client.createVLAN(prefix);
  208. String id = vlan.getUuid();
  209. try {
  210. vlan = client.getVLANInfo(vlan.getUuid());
  211. assertEquals(vlan.getName(), prefix);
  212. String prefix2 = prefix + "2";
  213. vlan = client.renameVLAN(vlan.getUuid(), prefix2);
  214. assertEquals(vlan.getName(), prefix2);
  215. } finally {
  216. client.destroyVLAN(id);
  217. }
  218. }
  219. @Test(dependsOnMethods = "testSetDriveData")
  220. public void testCreateAndDestroyStaticIP() throws Exception {
  221. StaticIPInfo ip = client.createStaticIP();
  222. StaticIPInfo ip2 = client.createStaticIP();
  223. Server server = null;
  224. try {
  225. ip = client.getStaticIPInfo(ip.getAddress());
  226. assertNotNull(ip);
  227. Logger.getAnonymousLogger().info("preparing drive");
  228. prepareDrive();
  229. Server serverRequest = Servers.smallWithStaticIP(prefix, drive.getUuid(), vncPassword, ip.getAddress())
  230. .build();
  231. Logger.getAnonymousLogger().info("starting server");
  232. server = client.createServer(serverRequest);
  233. assertEquals(server.getNics().get(0).getDhcp(), ip.getAddress());
  234. client.stopServer(server.getUuid());
  235. server = client.setServerConfiguration(server.getUuid(), Servers.changeIP(server, ip2.getAddress()));
  236. assertEquals(server.getNics().get(0).getDhcp(), ip2.getAddress());
  237. } finally {
  238. if (server != null) {
  239. client.destroyServer(server.getUuid());
  240. client.destroyDrive(drive.getUuid());
  241. }
  242. client.destroyStaticIP(ip.getAddress());
  243. client.destroyStaticIP(ip2.getAddress());
  244. }
  245. }
  246. protected ServerInfo server;
  247. @Test(dependsOnMethods = "testCreateAndDestroyStaticIP")
  248. public void testCreateAndStartServer() throws Exception {
  249. Logger.getAnonymousLogger().info("preparing drive");
  250. prepareDrive();
  251. Server serverRequest = Servers.small(prefix, drive.getUuid(), vncPassword).build();
  252. Logger.getAnonymousLogger().info("starting server");
  253. server = client.createServer(serverRequest);
  254. client.startServer(server.getUuid());
  255. server = client.getServerInfo(server.getUuid());
  256. checkStartedServer();
  257. Server newInfo = client.getServerInfo(server.getUuid());
  258. checkServerMatchesGet(newInfo);
  259. }
  260. protected void checkServerMatchesGet(Server newInfo) {
  261. assertEquals(newInfo.getUuid(), server.getUuid());
  262. }
  263. protected void checkStartedServer() {
  264. System.out.println(new Gson().toJson(server));
  265. assertNotNull(server.getUuid());
  266. assertNotNull(server.getUser());
  267. assertEquals(server.getName(), prefix);
  268. assertEquals(server.isPersistent(), true);
  269. assertEquals(server.getDevices(),
  270. ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid(drive.getUuid()).build()));
  271. assertEquals(server.getBootDeviceIds(), ImmutableSet.of("ide:0:0"));
  272. assertEquals(server.getNics().get(0).getDhcp(), server.getVnc().getIp());
  273. assertEquals(server.getNics().get(0).getModel(), Model.E1000);
  274. assertEquals(server.getStatus(), ServerStatus.ACTIVE);
  275. }
  276. @Test(dependsOnMethods = "testCreateAndStartServer")
  277. public void testConnectivity() throws Exception {
  278. Logger.getAnonymousLogger().info("awaiting vnc");
  279. assert socketTester.apply(HostAndPort.fromParts(server.getVnc().getIp(), 5900)) : server;
  280. Logger.getAnonymousLogger().info("awaiting ssh");
  281. assert socketTester.apply(HostAndPort.fromParts(server.getNics().get(0).getDhcp(), 22)) : server;
  282. doConnectViaSsh(server, getSshCredentials(server));
  283. }
  284. @Test(dependsOnMethods = "testConnectivity")
  285. public void testLifeCycle() throws Exception {
  286. client.stopServer(server.getUuid());
  287. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  288. client.startServer(server.getUuid());
  289. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  290. client.resetServer(server.getUuid());
  291. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
  292. client.shutdownServer(server.getUuid());
  293. // behavior on shutdown depends on how your server OS is set up to respond
  294. // to an ACPI power
  295. // button signal
  296. assert (client.getServerInfo(server.getUuid()).getStatus() == ServerStatus.ACTIVE || client.getServerInfo(
  297. server.getUuid()).getStatus() == ServerStatus.STOPPED);
  298. }
  299. @Test(dependsOnMethods = "testLifeCycle")
  300. public void testSetServerConfiguration() throws Exception {
  301. client.stopServer(server.getUuid());
  302. assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
  303. ServerInfo server2 = client.setServerConfiguration(
  304. server.getUuid(),
  305. Server.Builder.fromServer(server).name("rediculous")
  306. .use(ImmutableSet.of("networking", "security", "gateway")).build());
  307. assertNotNull(server2.getUuid(), server.getUuid());
  308. assertEquals(server2.getName(), "rediculous");
  309. checkUse(server2);
  310. server = server2;
  311. }
  312. protected void checkUse(ServerInfo server2) {
  313. // bug where use aren't updated
  314. assertEquals(server2.getUse(), ImmutableSet.<String> of());
  315. }
  316. @Test(dependsOnMethods = "testSetServerConfiguration")
  317. public void testDestroyServer() throws Exception {
  318. client.destroyServer(server.getUuid());
  319. assertEquals(client.getServerInfo(server.getUuid()), null);
  320. }
  321. @Test(dependsOnMethods = "testDestroyServer")
  322. public void testDestroyDrive() throws Exception {
  323. client.destroyDrive(drive.getUuid());
  324. assertEquals(client.getDriveInfo(drive.getUuid()), null);
  325. }
  326. protected void doConnectViaSsh(Server server, LoginCredentials creds) throws IOException {
  327. SshClient ssh = Guice.createInjector(new SshjSshClientModule()).getInstance(SshClient.Factory.class)
  328. .create(HostAndPort.fromParts(server.getVnc().getIp(), 22), creds);
  329. try {
  330. ssh.connect();
  331. ExecResponse hello = ssh.exec("echo hello");
  332. assertEquals(hello.getOutput().trim(), "hello");
  333. System.err.println(ssh.exec("df -k").getOutput());
  334. System.err.println(ssh.exec("mount").getOutput());
  335. System.err.println(ssh.exec("uname -a").getOutput());
  336. } finally {
  337. if (ssh != null)
  338. ssh.disconnect();
  339. }
  340. }
  341. @AfterGroups(groups = "live")
  342. @Override
  343. protected void tearDownContext() {
  344. if (server != null)
  345. client.destroyServer(server.getUuid());
  346. if (server != null)
  347. client.destroyDrive(drive.getUuid());
  348. super.tearDownContext();
  349. }
  350. @Test
  351. public void testListStandardDrives() throws Exception {
  352. Set<String> drives = client.listStandardDrives();
  353. assertNotNull(drives);
  354. }
  355. @Test
  356. public void testListStandardCds() throws Exception {
  357. Set<String> drives = client.listStandardCds();
  358. assertNotNull(drives);
  359. }
  360. @Test
  361. public void testListStandardImages() throws Exception {
  362. Set<String> drives = client.listStandardImages();
  363. assertNotNull(drives);
  364. }
  365. protected LoginCredentials getSshCredentials(Server server) {
  366. return LoginCredentials.builder().user("root").password(vncPassword).build();
  367. }
  368. protected void prepareDrive() {
  369. client.destroyDrive(drive.getUuid());
  370. drive = client.cloneDrive(imageId, drive.getName(),
  371. new CloneDriveOptions().size(driveSize).tags("cat:mouse", "monkey:banana"));
  372. // Block until the async clone operation has completed.
  373. assert driveNotClaimed.apply(drive) : client.getDriveInfo(drive.getUuid());
  374. DriveInfo clonedDrive = client.getDriveInfo(drive.getUuid());
  375. System.err.println("after prepare" + clonedDrive);
  376. assertEquals(clonedDrive.getTags(), ImmutableSet.of("cat:mouse", "monkey:banana"));
  377. }
  378. }