PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/loadbalancer/src/test/java/org/jclouds/loadbalancer/BaseLoadBalancerServiceLiveTest.java

https://github.com/alasdairhodge/jclouds
Java | 181 lines | 131 code | 26 blank | 24 comment | 16 complexity | f1e9cd8abecae8cc24920843198040c7 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.loadbalancer;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static org.testng.Assert.assertNotNull;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import java.util.Set;
  26. import java.util.concurrent.ExecutionException;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.TimeoutException;
  29. import org.jclouds.Constants;
  30. import org.jclouds.compute.BaseVersionedServiceLiveTest;
  31. import org.jclouds.compute.ComputeServiceContext;
  32. import org.jclouds.compute.ComputeServiceContextFactory;
  33. import org.jclouds.compute.RunNodesException;
  34. import org.jclouds.compute.domain.NodeMetadata;
  35. import org.jclouds.compute.domain.Template;
  36. import org.jclouds.compute.predicates.NodePredicates;
  37. import org.jclouds.loadbalancer.domain.LoadBalancerMetadata;
  38. import org.jclouds.logging.log4j.config.Log4JLoggingModule;
  39. import org.jclouds.net.IPSocket;
  40. import org.jclouds.predicates.RetryablePredicate;
  41. import org.jclouds.predicates.SocketOpen;
  42. import org.jclouds.ssh.SshClient;
  43. import org.testng.annotations.AfterTest;
  44. import org.testng.annotations.BeforeGroups;
  45. import org.testng.annotations.Test;
  46. import com.google.common.collect.ImmutableSet;
  47. import com.google.inject.Guice;
  48. import com.google.inject.Module;
  49. /**
  50. *
  51. * @author Adrian Cole
  52. */
  53. @Test(groups = "live", sequential = true)
  54. public abstract class BaseLoadBalancerServiceLiveTest extends BaseVersionedServiceLiveTest {
  55. protected SshClient.Factory sshFactory;
  56. protected String group;
  57. protected RetryablePredicate<IPSocket> socketTester;
  58. protected Set<? extends NodeMetadata> nodes;
  59. protected Template template;
  60. protected Map<String, String> keyPair;
  61. protected LoadBalancerMetadata loadbalancer;
  62. protected LoadBalancerServiceContext context;
  63. protected String computeProvider;
  64. protected String computeIdentity;
  65. protected String computeCredential;
  66. protected String computeEndpoint;
  67. protected String computeApiversion;
  68. protected ComputeServiceContext computeContext;
  69. @Override
  70. protected void setupCredentials() {
  71. super.setupCredentials();
  72. computeProvider = checkNotNull(System.getProperty("test." + provider + ".compute.provider"), "test." + provider
  73. + ".compute.provider");
  74. computeIdentity = checkNotNull(System.getProperty("test." + provider + ".compute.identity"), "test." + provider
  75. + ".compute.identity");
  76. computeCredential = System.getProperty("test." + provider + ".compute.credential");
  77. computeEndpoint = System.getProperty("test." + provider + ".compute.endpoint");
  78. computeApiversion = System.getProperty("test." + provider + ".compute.apiversion");
  79. }
  80. protected Properties setupComputeProperties() {
  81. Properties overrides = new Properties();
  82. overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
  83. overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
  84. overrides.setProperty(computeProvider + ".identity", computeIdentity);
  85. if (computeCredential != null)
  86. overrides.setProperty(computeProvider + ".credential", computeCredential);
  87. if (computeEndpoint != null)
  88. overrides.setProperty(computeProvider + ".endpoint", computeEndpoint);
  89. if (computeApiversion != null)
  90. overrides.setProperty(computeProvider + ".apiversion", computeApiversion);
  91. return overrides;
  92. }
  93. @BeforeGroups(groups = { "integration", "live" })
  94. public void setupClient() throws InterruptedException, ExecutionException, TimeoutException, IOException {
  95. setServiceDefaults();
  96. if (group == null)
  97. group = checkNotNull(provider, "provider");
  98. setupCredentials();
  99. initializeContext();
  100. initializeComputeContext();
  101. buildSocketTester();
  102. }
  103. public void setServiceDefaults() {
  104. }
  105. private void initializeContext() throws IOException {
  106. if (context != null)
  107. context.close();
  108. context = new LoadBalancerServiceContextFactory(setupRestProperties()).createContext(provider,
  109. ImmutableSet.of(new Log4JLoggingModule()), setupProperties());
  110. }
  111. private void initializeComputeContext() throws IOException {
  112. if (computeContext != null)
  113. computeContext.close();
  114. computeContext = new ComputeServiceContextFactory(setupRestProperties()).createContext(computeProvider,
  115. ImmutableSet.of(new Log4JLoggingModule(), getSshModule()), setupComputeProperties());
  116. }
  117. protected void buildSocketTester() {
  118. SocketOpen socketOpen = Guice.createInjector(getSshModule()).getInstance(SocketOpen.class);
  119. socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
  120. }
  121. abstract protected Module getSshModule();
  122. @BeforeGroups(groups = { "integration", "live" }, dependsOnMethods = "setupClient")
  123. public void createNodes() throws RunNodesException {
  124. try {
  125. nodes = computeContext.getComputeService().createNodesInGroup(group, 2);
  126. } catch (RunNodesException e) {
  127. nodes = e.getSuccessfulNodes();
  128. throw e;
  129. }
  130. }
  131. @Test(enabled = true)
  132. public void testLoadBalanceNodesMatching() throws Exception {
  133. // create load balancers
  134. loadbalancer = context.getLoadBalancerService().createLoadBalancerInLocation(null, group, "HTTP", 80, 80, nodes);
  135. assertNotNull(loadbalancer);
  136. validateNodesInLoadBalancer();
  137. }
  138. // TODO create a LoadBalancerService method for this.
  139. protected abstract void validateNodesInLoadBalancer();
  140. @Test(enabled = true, dependsOnMethods = "testLoadBalanceNodesMatching")
  141. public void testDestroyLoadBalancers() throws Exception {
  142. context.getLoadBalancerService().destroyLoadBalancer(loadbalancer.getId());
  143. }
  144. @AfterTest
  145. protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException {
  146. if (loadbalancer != null) {
  147. context.getLoadBalancerService().destroyLoadBalancer(loadbalancer.getId());
  148. }
  149. if (nodes != null) {
  150. computeContext.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group));
  151. }
  152. computeContext.close();
  153. context.close();
  154. }
  155. }