PageRenderTime 16ms CodeModel.GetById 28ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

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