PageRenderTime 1195ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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