PageRenderTime 43ms CodeModel.GetById 24ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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