PageRenderTime 85ms CodeModel.GetById 66ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/apis/nova/src/test/java/org/jclouds/openstack/nova/live/compute/ComputeBase.java

https://github.com/regularfry/jclouds
Java | 178 lines | 131 code | 25 blank | 22 comment | 17 complexity | 5345b83748c0cae3cd7d37c33ec77a51 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.openstack.nova.live.compute;
 20
 21import static com.google.common.base.Predicates.and;
 22import static com.google.common.base.Predicates.not;
 23import static com.google.common.collect.Sets.filter;
 24import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
 25import static org.jclouds.compute.predicates.NodePredicates.all;
 26import static org.jclouds.compute.predicates.NodePredicates.inGroup;
 27import static org.jclouds.openstack.nova.live.PropertyHelper.setupKeyPair;
 28import static org.jclouds.openstack.nova.live.PropertyHelper.setupOverrides;
 29import static org.jclouds.openstack.nova.live.PropertyHelper.setupProperties;
 30import static org.testng.Assert.assertEquals;
 31
 32import java.io.IOException;
 33import java.net.URISyntaxException;
 34import java.util.Map;
 35import java.util.Properties;
 36import java.util.Set;
 37import java.util.concurrent.ExecutionException;
 38import java.util.concurrent.TimeUnit;
 39import java.util.concurrent.TimeoutException;
 40
 41import com.google.common.collect.Iterables;
 42import org.jclouds.compute.ComputeService;
 43import org.jclouds.compute.ComputeServiceContext;
 44import org.jclouds.compute.ComputeServiceContextFactory;
 45import org.jclouds.compute.RunNodesException;
 46import org.jclouds.compute.domain.ComputeMetadata;
 47import org.jclouds.compute.domain.NodeMetadata;
 48import org.jclouds.compute.domain.NodeState;
 49import org.jclouds.compute.domain.TemplateBuilder;
 50import org.jclouds.compute.options.TemplateOptions;
 51import org.jclouds.domain.Credentials;
 52import org.jclouds.domain.Location;
 53import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
 54import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
 55import org.jclouds.net.IPSocket;
 56import org.jclouds.predicates.RetryablePredicate;
 57import org.jclouds.predicates.SocketOpen;
 58import org.jclouds.ssh.SshException;
 59import org.jclouds.sshj.SshjSshClient;
 60import org.jclouds.sshj.config.SshjSshClientModule;
 61import org.testng.annotations.BeforeTest;
 62
 63import com.google.common.collect.ImmutableSet;
 64import com.google.inject.Guice;
 65import com.google.inject.Module;
 66
 67/**
 68 * @author Victor Galkin
 69 */
 70public class ComputeBase {
 71   protected ComputeServiceContext context;
 72   protected ComputeService computeService;
 73
 74   protected String provider = "nova";
 75
 76
 77   protected Map<String, String> keyPair;
 78   protected Properties overrides;
 79   protected String testImageId;
 80
 81   @BeforeTest
 82   public void before() throws InterruptedException, ExecutionException, TimeoutException, IOException {
 83      Properties properties = setupProperties(this.getClass());
 84      setupOverrides(properties);
 85      overrides = properties;
 86      keyPair = setupKeyPair(properties);
 87      testImageId = properties.getProperty("test.nova.image.id");
 88      initializeContextAndComputeService(properties);
 89
 90   }
 91
 92   @SuppressWarnings("unused")
 93   private RetryablePredicate<IPSocket> buildSocket() {
 94      SocketOpen socketOpen = Guice.createInjector(getSshModule()).getInstance(SocketOpen.class);
 95      return new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
 96   }
 97
 98   private Module getSshModule() {
 99      return new SshjSshClientModule();
100   }
101
102   protected TemplateBuilder getDefaultTemplateBuilder() {
103      return computeService.templateBuilder().imageId(testImageId).options(getDefaultTemplateOptions());
104   }
105
106   private TemplateOptions getDefaultTemplateOptions() {
107      return TemplateOptions.Builder.blockUntilRunning(false);
108      //.installPrivateKey(Payloads.newStringPayload(keyPair.get("private")));
109   }
110
111   protected NodeMetadata getDefaultNodeImmediately(String group) throws RunNodesException {
112      for (ComputeMetadata node : computeService.listNodes()) {
113         if (((NodeMetadata) node).getGroup() != null)
114            if (((NodeMetadata) node).getGroup().equals(group))
115               if (((NodeMetadata) node).getState().equals(NodeState.PENDING)
116                     || ((NodeMetadata) node).getState().equals(NodeState.RUNNING)) return (NodeMetadata) node;
117      }
118      return createDefaultNode(group);
119   }
120
121   protected NodeMetadata createDefaultNode(TemplateOptions options, String group) throws RunNodesException {
122      return computeService.createNodesInGroup(group, 1, getDefaultTemplateBuilder().options(options).build())
123            .iterator().next();
124   }
125
126   protected NodeMetadata createDefaultNode(String group) throws RunNodesException {
127      return createDefaultNode(getDefaultTemplateOptions(), group);
128   }
129
130
131   protected void initializeContextAndComputeService(Properties properties) throws IOException {
132      if (context != null)
133         context.close();
134      context = new ComputeServiceContextFactory().createContext(provider, ImmutableSet.of(
135            new SLF4JLoggingModule(), getSshModule()), properties);
136      computeService = context.getComputeService();
137   }
138
139   protected String awaitForStartup(String nodeId) throws InterruptedException {
140      while (true) {
141         NodeMetadata metadata = computeService.getNodeMetadata(nodeId);
142         Set<String> addresses = metadata.getPublicAddresses();
143         System.out.println(addresses);
144         System.out.println(metadata.getState());
145         if (metadata.getState() == NodeState.RUNNING && addresses != null && !addresses.isEmpty())
146            return addresses.iterator().next();
147         Thread.sleep(1000);
148      }
149   }
150
151   protected Set<? extends NodeMetadata> getFreshNodes(String group) {
152      return filter(computeService.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED)));
153   }
154
155   protected void awaitForSshPort(String address, Credentials credentials) throws URISyntaxException {
156      IPSocket socket = new IPSocket(address, 22);
157
158      SshjSshClient ssh = new SshjSshClient(
159            new BackoffLimitedRetryHandler(), socket, 10000, credentials.identity, null, credentials.credential.getBytes());
160      while (true) {
161         try {
162            System.out.println("ping: " + socket);
163            ssh.connect();
164            return;
165         } catch (SshException ignore) {
166         }
167      }
168   }
169
170   protected void assertLocationSameOrChild(Location test, Location expected) {
171      if (!test.equals(expected)) {
172         assertEquals(test.getParent().getId(), expected.getId());
173      } else {
174         assertEquals(test, expected);
175      }
176   }
177
178}