PageRenderTime 28ms CodeModel.GetById 8ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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