tests for NodeClient

This commit is contained in:
danikov 2011-11-16 09:53:27 +00:00
parent adbcee9596
commit e1547f5853
8 changed files with 529 additions and 0 deletions

View File

@ -0,0 +1,171 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudloadbalancers.features;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
import org.jclouds.cloudloadbalancers.CloudLoadBalancersAsyncClient;
import org.jclouds.cloudloadbalancers.CloudLoadBalancersClient;
import org.jclouds.cloudloadbalancers.CloudLoadBalancersContextBuilder;
import org.jclouds.cloudloadbalancers.domain.NodeAttributes;
import org.jclouds.cloudloadbalancers.domain.NodeAttributes.Builder;
import org.jclouds.cloudloadbalancers.domain.NodeRequest;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.cloudloadbalancers.functions.UnwrapNode;
import org.jclouds.cloudloadbalancers.functions.UnwrapNodes;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ReleasePayloadAndReturn;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test;
import com.google.inject.TypeLiteral;
/**
* Tests behavior of {@code NodeAsyncClient}
*
* @author Dan Lo Bianco
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "NodeAsyncClientTest")
public class NodeAsyncClientTest extends BaseCloudLoadBalancersAsyncClientTest<NodeAsyncClient> {
public void testListNodes() throws SecurityException, NoSuchMethodException, IOException {
Method method = NodeAsyncClient.class.getMethod("listNodes", int.class);
HttpRequest httpRequest = processor.createRequest(method, 2);
assertRequestLineEquals(httpRequest,
"GET https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/2/nodes HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNodes.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testGetNode() throws SecurityException, NoSuchMethodException, IOException {
Method method = NodeAsyncClient.class.getMethod("getNode", int.class, int.class);
HttpRequest httpRequest = processor.createRequest(method, 2, 3);
assertRequestLineEquals(httpRequest,
"GET https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/2/nodes/3 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testCreateNodeWithType() throws SecurityException, NoSuchMethodException, IOException {
Method method = NodeAsyncClient.class.getMethod("createNode", int.class, NodeRequest.class);
HttpRequest httpRequest = processor.createRequest(method, 3, NodeRequest.builder().
address("192.168.1.1").port(8080).build());
assertRequestLineEquals(httpRequest,
"POST https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/3/nodes HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(
httpRequest,
"{\"node\":{\"address\":\"192.168.1.1\",\"port\":8080,\"condition\":\"ENABLED\"}}",
"application/json", false);
assertResponseParserClassEquals(method, httpRequest, UnwrapNode.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testModifyNode() throws SecurityException, NoSuchMethodException, IOException {
Method method = NodeAsyncClient.class.getMethod("modifyNode", int.class, int.class,
NodeAttributes.class);
HttpRequest httpRequest = processor.createRequest(method, 7, 8, Builder.condition(Condition.DISABLED).weight(13));
assertRequestLineEquals(httpRequest,
"PUT https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/7/nodes/8 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, "{\"node\":{\"condition\":\"DISABLED\",\"weight\":13}}", "application/json", false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testRemoveLoadBalancer() throws SecurityException, NoSuchMethodException, IOException {
Method method = NodeAsyncClient.class.getMethod("removeNode", int.class, int.class);
HttpRequest httpRequest = processor.createRequest(method, 4, 9);
assertRequestLineEquals(httpRequest,
"DELETE https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers/4/nodes/9 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: */*\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<NodeAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<NodeAsyncClient>>() {
};
}
protected String provider = "cloudloadbalancers";
@Override
public RestContextSpec<CloudLoadBalancersClient, CloudLoadBalancersAsyncClient> createContextSpec() {
return new RestContextFactory(getProperties()).createContextSpec(provider, "user", "password", new Properties());
}
@Override
protected Properties getProperties() {
Properties overrides = new Properties();
overrides.setProperty(PROPERTY_REGIONS, "US");
overrides.setProperty(PROPERTY_API_VERSION, "1");
overrides.setProperty(provider + ".endpoint", "https://auth");
overrides.setProperty(provider + ".contextbuilder", CloudLoadBalancersContextBuilder.class.getName());
return overrides;
}
}

View File

@ -0,0 +1,141 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudloadbalancers.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Logger;
import org.jclouds.cloudloadbalancers.domain.LoadBalancer;
import org.jclouds.cloudloadbalancers.domain.LoadBalancerRequest;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.NodeAttributes;
import org.jclouds.cloudloadbalancers.domain.NodeRequest;
import org.jclouds.cloudloadbalancers.domain.VirtualIP.Type;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code NodeClient}
*
* @author Dan Lo Bianco
*/
@Test(groups = "live", singleThreaded = true, testName = "NodeClientLiveTest")
public class NodeClientLiveTest extends BaseCloudLoadBalancersClientLiveTest {
private Map<LoadBalancer, Set<Node>> nodes = new HashMap<LoadBalancer, Set<Node>>();
@BeforeGroups(groups = "live")
protected void setup() {
assertTrue(client.getConfiguredRegions().size() > 0, "Need to have some regions!");
Logger.getAnonymousLogger().info("running against regions "+client.getConfiguredRegions());
for (String region : client.getConfiguredRegions()) {
Logger.getAnonymousLogger().info("starting lb in region " + region);
LoadBalancer lb = client.getLoadBalancerClient(region).createLoadBalancer(
LoadBalancerRequest.builder().name(prefix + "-" + region).protocol("HTTP").port(80).virtualIPType(
Type.PUBLIC).node(NodeRequest.builder().address("192.168.1.1").port(8080).build()).build());
nodes.put(lb, new HashSet<Node>());
assert loadBalancerActive.apply(lb) : lb;
}
}
@AfterGroups(groups = "live")
protected void tearDown() {
for (Entry<LoadBalancer, Set<Node>> entry : nodes.entrySet()) {
LoadBalancer lb = entry.getKey();
for (Node n : entry.getValue()) {
client.getNodeClient(lb.getRegion()).removeNode(lb.getId(), n.getId());
assertEquals(client.getNodeClient(lb.getRegion()).
getNode(lb.getId(), n.getId()), null);
}
client.getLoadBalancerClient(lb.getRegion()).removeLoadBalancer(lb.getId());
assert loadBalancerDeleted.apply(lb) : lb;
}
super.tearDown();
}
public void testListNodes() throws Exception {
for (LoadBalancer lb : nodes.keySet()) {
Set<Node> response = client.getNodeClient(lb.getRegion()).listNodes(lb.getId());
assert null != response;
assertTrue(response.size() >= 0);
for (Node n : response) {
assert n.getId() != -1 : n;
assert n.getCondition() != null : n;
assert n.getAddress() != null : n;
assert n.getPort() != -1 : n;
assert n.getStatus() != null : n;
assert n.getWeight() != -1 : n;
Node getDetails = client.getNodeClient(lb.getRegion()).getNode(lb.getId(), n.getId());
System.out.println(n.toString());
try {
assertEquals(getDetails.getId(), n.getId());
assertEquals(getDetails.getCondition(), n.getCondition());
assertEquals(getDetails.getAddress(), n.getAddress());
assertEquals(getDetails.getPort(), n.getPort());
assertEquals(getDetails.getStatus(), n.getStatus());
assertEquals(getDetails.getWeight(), n.getWeight());
} catch (AssertionError e) {
throw new AssertionError(String.format("%s\n%s - %s", e.getMessage(),getDetails, n));
}
}
}
}
public void testCreateNode() throws Exception {
for (LoadBalancer lb : nodes.keySet()) {
String region = lb.getRegion();
Logger.getAnonymousLogger().info("starting node on loadbalancer "+lb.getId()+" in region "+region);
Node n = client.getNodeClient(region).createNode(lb.getId(),
NodeRequest.builder().address("192.168.1.1").port(8080).build());
assertEquals(lb.getStatus(), Node.Status.ONLINE);
nodes.get(lb).add(n);
assertEquals(client.getNodeClient(region).getNode(lb.getId(), n.getId()).getStatus(), Node.Status.ONLINE);
Node newNode = client.getNodeClient(region).getNode(lb.getId(), n.getId());
assertEquals(newNode.getStatus(), Node.Status.ONLINE);
}
}
@Test(dependsOnMethods = "testCreateNode")
public void testModifyNode() throws Exception {
for (Entry<LoadBalancer, Set<Node>> entry : nodes.entrySet()) {
for (Node n : entry.getValue()) {
String region = entry.getKey().getRegion();
client.getNodeClient(region).modifyNode(entry.getKey().getId(), n.getId(),
NodeAttributes.Builder.weight(23));
assertEquals(client.getNodeClient(region)
.getNode(entry.getKey().getId(), n.getId()).getStatus(), Node.Status.ONLINE);
Node newNode = client.getNodeClient(region).getNode(entry.getKey().getId(), n.getId());
assertEquals(newNode.getStatus(), Node.Status.ONLINE);
assertEquals(newNode.getWeight(), (Integer)23);
}
}
}
}

View File

@ -0,0 +1,53 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudloadbalancers.functions;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.Node.Status;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.http.HttpResponse;
import org.jclouds.json.BaseItemParserTest;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.inject.Injector;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "unit")
public class UnwrapNodeTest extends BaseItemParserTest<Node> {
@Override
public String resource() {
return "/getnode.json";
}
@Override
public Node expected() {
return Node.builder().id(410).address("10.1.1.1").port(80)
.condition(Condition.ENABLED).status(Status.ONLINE).weight(12).build();
}
@Override
protected Function<HttpResponse, Node> parser(Injector i) {
return i.getInstance(UnwrapNode.class);
}
}

View File

@ -0,0 +1,61 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudloadbalancers.functions;
import java.util.Set;
import org.jclouds.cloudloadbalancers.domain.Node;
import org.jclouds.cloudloadbalancers.domain.Node.Status;
import org.jclouds.cloudloadbalancers.domain.internal.BaseNode.Condition;
import org.jclouds.http.HttpResponse;
import org.jclouds.json.BaseSetParserTest;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "unit")
public class UnwrapNodesTest extends BaseSetParserTest<Node> {
@Override
public String resource() {
return "/listnodes.json";
}
@Override
public Set<Node> expected() {
return ImmutableSet.of(
Node.builder().id(410).address("10.1.1.1").port(80).
condition(Condition.ENABLED).weight(3).status(Status.ONLINE).build(),
Node.builder().id(411).address("10.1.1.2").port(80).
condition(Condition.ENABLED).weight(8).status(Status.ONLINE).build(),
Node.builder().id(412).address("10.1.1.3").port(80).
condition(Condition.DISABLED).weight(12).status(Status.ONLINE).build());
}
@Override
protected Function<HttpResponse, Set<Node>> parser(Injector i) {
return i.getInstance(UnwrapNodes.class);
}
}

View File

@ -0,0 +1,9 @@
{"node": {
"id":410,
"address":"10.1.1.1",
"port":80,
"condition":"ENABLED",
"status":"ONLINE",
"weight":12
}
}

View File

@ -0,0 +1,28 @@
{
"nodes": [
{
"id":"410",
"address":"10.1.1.1",
"port":80,
"condition":"ENABLED",
"status":"ONLINE",
"weight":3
},
{
"id":"411",
"address":"10.1.1.2",
"port":80,
"condition":"ENABLED",
"status":"ONLINE",
"weight":8
},
{
"id":"412",
"address":"10.1.1.3",
"port":80,
"condition":"DISABLED",
"status":"ONLINE",
"weight":12
}
]
}

View File

@ -0,0 +1,33 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rackspace.cloudloadbalancers;
import org.jclouds.cloudloadbalancers.features.NodeClientLiveTest;
import org.testng.annotations.Test;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "live", singleThreaded = true)
public class CloudLoadBalancersUKNodeClientLiveTest extends NodeClientLiveTest {
public CloudLoadBalancersUKNodeClientLiveTest() {
provider = "cloudloadbalancers-uk";
}
}

View File

@ -0,0 +1,33 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rackspace.cloudloadbalancers;
import org.jclouds.cloudloadbalancers.features.LoadBalancerClientLiveTest;
import org.testng.annotations.Test;
/**
*
* @author Dan Lo Bianco
*/
@Test(groups = "live", singleThreaded = true)
public class CloudLoadBalancersUSLoadBalancerClientLiveTest extends LoadBalancerClientLiveTest {
public CloudLoadBalancersUSLoadBalancerClientLiveTest() {
provider = "cloudloadbalancers-us";
}
}