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