diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinder.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinder.java index ba237c40b5..086fa3da89 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinder.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinder.java @@ -31,12 +31,10 @@ public class DeregisterLoadBalancerRequestBinder extends BaseProfitBricksRequest @Override protected String createPayload(LoadBalancer.Request.DeregisterPayload payload) { - requestBuilder.append("") - .append(""); + requestBuilder.append(""); for (String s : payload.serverIds()) requestBuilder.append(format("%s", s)); requestBuilder.append(format("%s", payload.id())) - .append("") .append(""); return requestBuilder.toString(); diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinder.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinder.java index 21f1d84907..3741dadaeb 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinder.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinder.java @@ -32,14 +32,12 @@ public class RegisterLoadBalancerRequestBinder extends BaseProfitBricksRequestBi @Override protected String createPayload(LoadBalancer.Request.RegisterPayload payload) { requestBuilder - .append("").append("") + .append("") .append(format("%s", payload.id())); for (String s : payload.serverIds()) requestBuilder.append(format("%s", s)); - requestBuilder - .append("") - .append(""); + requestBuilder.append(""); return requestBuilder.toString().replaceAll("\\s+", ""); } diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/config/ProfitBricksComputeServiceContextModule.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/config/ProfitBricksComputeServiceContextModule.java index 2b45d18108..b3fe313aa5 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/config/ProfitBricksComputeServiceContextModule.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/config/ProfitBricksComputeServiceContextModule.java @@ -16,10 +16,15 @@ */ package org.jclouds.profitbricks.compute.config; +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; +import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PERIOD; import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_MAX_PERIOD; import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PREDICATE_DATACENTER; +import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PREDICATE_SNAPSHOT; import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_TIMEOUT; +import static org.jclouds.util.Predicates2.retry; import java.util.concurrent.TimeUnit; @@ -49,11 +54,8 @@ import org.jclouds.profitbricks.compute.function.LocationToLocation; import org.jclouds.profitbricks.compute.function.ProvisionableToImage; import org.jclouds.profitbricks.compute.function.ServerToNodeMetadata; import org.jclouds.profitbricks.compute.function.StorageToVolume; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; import org.jclouds.profitbricks.domain.ProvisioningState; import org.jclouds.profitbricks.domain.Provisionable; -import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; @@ -101,10 +103,26 @@ public class ProfitBricksComputeServiceContextModule extends @Provides @Singleton @Named(POLL_PREDICATE_DATACENTER) - Predicate provideWaitDataCenterUntilAvailablePredicate( + Predicate provideDataCenterAvailablePredicate( final ProfitBricksApi api, ComputeConstants constants) { - return Predicates2.retry(new ProvisioningStatusPollingPredicate( - api, ProvisioningStatusAware.DATACENTER, ProvisioningState.AVAILABLE), + return retry(new DataCenterProvisioningStatePredicate( + api, ProvisioningState.AVAILABLE), + constants.pollTimeout(), constants.pollPeriod(), constants.pollMaxPeriod(), TimeUnit.SECONDS); + } + + @Provides + @Named(TIMEOUT_NODE_RUNNING) + Predicate provideServerRunningPredicate(final ProfitBricksApi api, ComputeConstants constants) { + return retry(new ServerStatusPredicate( + api, Server.Status.RUNNING), + constants.pollTimeout(), constants.pollPeriod(), constants.pollMaxPeriod(), TimeUnit.SECONDS); + } + + @Provides + @Named(TIMEOUT_NODE_SUSPENDED) + Predicate provideServerSuspendedPredicate(final ProfitBricksApi api, ComputeConstants constants) { + return retry(new ServerStatusPredicate( + api, Server.Status.SHUTOFF), constants.pollTimeout(), constants.pollPeriod(), constants.pollMaxPeriod(), TimeUnit.SECONDS); } @@ -117,6 +135,69 @@ public class ProfitBricksComputeServiceContextModule extends return provisioningManager; } + @Provides + @Singleton + @Named(POLL_PREDICATE_SNAPSHOT) + Predicate provideSnapshotAvailablePredicate(final ProfitBricksApi api, ComputeConstants constants) { + return retry(new SnapshotProvisioningStatePredicate( + api, ProvisioningState.AVAILABLE), + constants.pollTimeout(), constants.pollPeriod(), constants.pollMaxPeriod(), TimeUnit.SECONDS); + } + + static class DataCenterProvisioningStatePredicate implements Predicate { + + private final ProfitBricksApi api; + private final ProvisioningState expectedState; + + public DataCenterProvisioningStatePredicate(ProfitBricksApi api, ProvisioningState expectedState) { + this.api = checkNotNull(api, "api must not be null"); + this.expectedState = checkNotNull(expectedState, "expectedState must not be null"); + } + + @Override + public boolean apply(String input) { + checkNotNull(input, "datacenter id"); + return api.dataCenterApi().getDataCenterState(input) == expectedState; + } + + } + + static class ServerStatusPredicate implements Predicate { + + private final ProfitBricksApi api; + private final Server.Status expectedStatus; + + public ServerStatusPredicate(ProfitBricksApi api, Server.Status expectedStatus) { + this.api = checkNotNull(api, "api must not be null"); + this.expectedStatus = checkNotNull(expectedStatus, "expectedStatus must not be null"); + } + + @Override + public boolean apply(String input) { + checkNotNull(input, "server id"); + return api.serverApi().getServer(input).status() == expectedStatus; + } + + } + + static class SnapshotProvisioningStatePredicate implements Predicate { + + private final ProfitBricksApi api; + private final ProvisioningState expectedState; + + public SnapshotProvisioningStatePredicate(ProfitBricksApi api, ProvisioningState expectedState) { + this.api = checkNotNull(api, "api must not be null"); + this.expectedState = checkNotNull(expectedState, "expectedState must not be null"); + } + + @Override + public boolean apply(String input) { + checkNotNull(input, "snapshot id"); + return api.snapshotApi().getSnapshot(input).state() == expectedState; + } + + } + @Singleton public static class ComputeConstants { diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicate.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicate.java deleted file mode 100644 index 41c3e932d5..0000000000 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicate.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.profitbricks.compute.internal; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.profitbricks.ProfitBricksApi; -import org.jclouds.profitbricks.domain.ProvisioningState; - -import com.google.common.base.Predicate; -import org.jclouds.rest.ResourceNotFoundException; - -/** - * A custom predicate for waiting until a virtual resource satisfies the given expected status - *

- * Performing api requests on a datacenter that is not {@link ProvisioningState#AVAILABLE} is not allowed. On some - * cases, the API user gets blocked from further requests, and will then need to contact tech support for api lock - * release. - */ -public class ProvisioningStatusPollingPredicate implements Predicate { - - private final ProfitBricksApi api; - private final ProvisioningStatusAware domain; - private final ProvisioningState expect; - - public ProvisioningStatusPollingPredicate(ProfitBricksApi api, ProvisioningStatusAware domain, ProvisioningState expect) { - this.api = checkNotNull(api, "API null"); - this.expect = checkNotNull(expect, "Expected state null"); - this.domain = checkNotNull(domain, "Domain null"); - } - - @Override - public boolean apply(String input) { - checkNotNull(input, "Virtual item id can't be null."); - try { - switch (domain) { - case DATACENTER: - return expect == api.dataCenterApi().getDataCenterState(input); - case SERVER: - return expect == api.serverApi().getServer(input).state(); - case STORAGE: - return expect == api.storageApi().getStorage(input).state(); - case NIC: - return expect == api.nicApi().getNic(input).state(); - case SNAPSHOT: - return expect == api.snapshotApi().getSnapshot(input).state(); - default: - throw new IllegalArgumentException("Unknown domain '" + domain + "'"); - } - } catch (ResourceNotFoundException ex) { - // After provisioning, a node might still not be "fetchable" via API - return false; - } - } - -} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/config/ProfitBricksComputeProperties.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/config/ProfitBricksComputeProperties.java index 19b0e53c99..8f5840d87f 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/config/ProfitBricksComputeProperties.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/config/ProfitBricksComputeProperties.java @@ -19,6 +19,7 @@ package org.jclouds.profitbricks.config; public class ProfitBricksComputeProperties { public static final String POLL_PREDICATE_DATACENTER = "jclouds.profitbricks.predicate.datacenter"; + public static final String POLL_PREDICATE_SNAPSHOT = "jclouds.profitbricks.predicate.snapshot"; public static final String POLL_TIMEOUT = "jclouds.profitbricks.poll.timeout"; public static final String POLL_PERIOD = "jclouds.profitbricks.operation.poll.initial-period"; diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/Location.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/Location.java index 6102a42a25..834fa866cf 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/Location.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/domain/Location.java @@ -52,4 +52,9 @@ public enum Location { return location; return UNRECOGNIZED; } + + @Override + public String toString() { + return id; + } } diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java index a81293e421..ca9841d8ad 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/IpBlockApi.java @@ -17,14 +17,17 @@ package org.jclouds.profitbricks.features; import java.util.List; + import javax.inject.Named; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; + import org.jclouds.Fallbacks; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Location; import org.jclouds.profitbricks.http.filters.ProfitBricksSoapMessageEnvelope; import org.jclouds.profitbricks.http.parser.RequestIdOnlyResponseHandler; import org.jclouds.profitbricks.http.parser.ipblock.IpBlockListResponseHandler; @@ -58,7 +61,7 @@ public interface IpBlockApi { @Named("publicipblock:reserve") @Payload("{blockSize}{location}") @XMLResponseParser(IpBlockResponseHandler.class) - IpBlock reservePublicIpBlock(@PayloadParam("blockSize") String blockSize, @PayloadParam("location") String location); + IpBlock reservePublicIpBlock(@PayloadParam("blockSize") int blockSize, @PayloadParam("location") Location location); @POST @Named("publicipblock:addip") diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/LoadBalancerApi.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/LoadBalancerApi.java index 3d371836b4..6cf8e23206 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/LoadBalancerApi.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/LoadBalancerApi.java @@ -21,7 +21,9 @@ import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; + import java.util.List; + import org.jclouds.Fallbacks; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.profitbricks.binder.loadbalancer.CreateLoadBalancerRequestBinder; @@ -31,6 +33,7 @@ import org.jclouds.profitbricks.binder.loadbalancer.UpdateLoadBalancerRequestBin import org.jclouds.profitbricks.domain.LoadBalancer; import org.jclouds.profitbricks.http.filters.ProfitBricksSoapMessageEnvelope; import org.jclouds.profitbricks.http.parser.RequestIdOnlyResponseHandler; +import org.jclouds.profitbricks.http.parser.loadbalancer.LoadBalancerIdOnlyResponseHandler; import org.jclouds.profitbricks.http.parser.loadbalancer.LoadBalancerListResponseHandler; import org.jclouds.profitbricks.http.parser.loadbalancer.LoadBalancerResponseHandler; import org.jclouds.rest.annotations.Fallback; @@ -62,7 +65,7 @@ public interface LoadBalancerApi { @POST @Named("loadbalancer:create") @MapBinder(CreateLoadBalancerRequestBinder.class) - @XMLResponseParser(RequestIdOnlyResponseHandler.class) + @XMLResponseParser(LoadBalancerIdOnlyResponseHandler.class) String createLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.CreatePayload payload); @POST @@ -72,10 +75,10 @@ public interface LoadBalancerApi { LoadBalancer registerLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.RegisterPayload payload); @POST - @Named("loadbalancer:Deregister") + @Named("loadbalancer:deregister") @MapBinder(DeregisterLoadBalancerRequestBinder.class) - @XMLResponseParser(LoadBalancerResponseHandler.class) - LoadBalancer deregisterLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.DeregisterPayload payload); + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String deregisterLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.DeregisterPayload payload); @POST @Named("loadbalancer:delete") @@ -85,6 +88,6 @@ public interface LoadBalancerApi { @POST @Named("loadbalancer:update") @MapBinder(UpdateLoadBalancerRequestBinder.class) - @XMLResponseParser(LoadBalancerResponseHandler.class) - LoadBalancer updateLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.UpdatePayload payload); + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String updateLoadBalancer(@PayloadParam("loadbalancer") LoadBalancer.Request.UpdatePayload payload); } diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/NicApi.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/NicApi.java index 57e425ff8c..c9459352a3 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/NicApi.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/features/NicApi.java @@ -37,8 +37,12 @@ import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.POST; + import java.util.List; +import org.jclouds.profitbricks.http.parser.RequestIdOnlyResponseHandler; +import org.jclouds.profitbricks.http.parser.nic.NicIdOnlyResponseHandler; + @RequestFilters({BasicAuthentication.class, ProfitBricksSoapMessageEnvelope.class}) @Consumes(MediaType.TEXT_XML) @Produces(MediaType.TEXT_XML) @@ -54,8 +58,8 @@ public interface NicApi { @POST @Named("nic:create") @MapBinder(CreateNicRequestBinder.class) - @XMLResponseParser(NicResponseHandler.class) - Nic createNic(@PayloadParam("nic") Nic.Request.CreatePayload payload); + @XMLResponseParser(NicIdOnlyResponseHandler.class) + String createNic(@PayloadParam("nic") Nic.Request.CreatePayload payload); @POST @Named("nic:get") @@ -67,14 +71,14 @@ public interface NicApi { @POST @Named("nic:update") @MapBinder(UpdateNicRequestBinder.class) - @XMLResponseParser(NicResponseHandler.class) - Nic updateNic(@PayloadParam("nic") Nic.Request.UpdatePayload payload); + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String updateNic(@PayloadParam("nic") Nic.Request.UpdatePayload payload); @POST @Named("nic:setInternetAccess") @MapBinder(SetInternetAccessBinder.class) - @XMLResponseParser(NicResponseHandler.class) - Nic setInternetAccess(@PayloadParam("nic") Nic.Request.SetInternetAccessPayload payload); + @XMLResponseParser(RequestIdOnlyResponseHandler.class) + String setInternetAccess(@PayloadParam("nic") Nic.Request.SetInternetAccessPayload payload); @POST @Named("nic:delete") diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandler.java new file mode 100644 index 0000000000..a239336e07 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandler.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.loadbalancer; + +import org.jclouds.profitbricks.http.parser.BaseProfitBricksResponseHandler; +import org.xml.sax.SAXException; + +/** + * Handler for parsing SOAP response where loadBalancerId is the only usable value. + * + * Other properties available (which are ignored): requestId, dataCenterId, dataCenterVersion + */ +public class LoadBalancerIdOnlyResponseHandler extends BaseProfitBricksResponseHandler { + + private String loadBalancerId; + + LoadBalancerIdOnlyResponseHandler() { + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + setPropertyOnEndTag(qName); + clearTextBuffer(); + } + + @Override + protected void setPropertyOnEndTag(String qName) { + if ("loadBalancerId".equals(qName)) + loadBalancerId = textToStringValue(); + } + + @Override + public String getResult() { + return loadBalancerId; + } + +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandler.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandler.java new file mode 100644 index 0000000000..8f8ba03ab8 --- /dev/null +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandler.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.nic; + +import org.jclouds.profitbricks.http.parser.BaseProfitBricksResponseHandler; +import org.xml.sax.SAXException; + +/** + * Handler for parsing SOAP response where nicId is the only usable value. + * + * Other properties available (which are ignored): requestId, dataCenterId, dataCenterVersion + */ +public class NicIdOnlyResponseHandler extends BaseProfitBricksResponseHandler { + + private String nicId; + + NicIdOnlyResponseHandler() { + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + setPropertyOnEndTag(qName); + clearTextBuffer(); + } + + @Override + protected void setPropertyOnEndTag(String qName) { + if ("nicId".equals(qName)) + nicId = textToStringValue(); + } + + @Override + public String getResult() { + return nicId; + } + +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/BaseProfitBricksLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/BaseProfitBricksLiveTest.java index e310b6e207..524149ad2a 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/BaseProfitBricksLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/BaseProfitBricksLiveTest.java @@ -16,30 +16,171 @@ */ package org.jclouds.profitbricks; -import java.util.concurrent.TimeUnit; +import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; +import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; +import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PREDICATE_DATACENTER; +import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PREDICATE_SNAPSHOT; +import static org.testng.Assert.assertTrue; + +import java.util.List; +import java.util.Objects; +import java.util.Properties; import org.jclouds.apis.BaseApiLiveTest; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.domain.Location; +import org.jclouds.profitbricks.domain.Server; import org.jclouds.profitbricks.domain.ProvisioningState; -import org.jclouds.util.Predicates2; +import org.jclouds.profitbricks.features.DataCenterApi; +import org.jclouds.profitbricks.features.ServerApi; import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.Module; +import com.google.inject.TypeLiteral; +import com.google.inject.name.Names; + +import org.jclouds.profitbricks.domain.Nic; +import org.jclouds.profitbricks.features.NicApi; public abstract class BaseProfitBricksLiveTest extends BaseApiLiveTest { - protected Predicate dcWaitingPredicate; + public static final Location testLocation = Location.US_LAS; + + private Predicate dataCenterAvailable; + private Predicate snapshotAvailable; + private Predicate serverRunning; + private Predicate serverSuspended; public BaseProfitBricksLiveTest() { provider = "profitbricks"; } @Override - protected void initialize() { - super.initialize(); - this.dcWaitingPredicate = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.DATACENTER, ProvisioningState.AVAILABLE), - 2l * 60l, 2l, TimeUnit.SECONDS); + protected ProfitBricksApi create(Properties props, Iterable modules) { + Injector injector = newBuilder().modules(modules).overrides(props).buildInjector(); + dataCenterAvailable = injector.getInstance(Key.get(new TypeLiteral>() { + }, Names.named(POLL_PREDICATE_DATACENTER))); + snapshotAvailable = injector.getInstance(Key.get(new TypeLiteral>() { + }, Names.named(POLL_PREDICATE_SNAPSHOT))); + serverRunning = injector.getInstance(Key.get(new TypeLiteral>() { + }, Names.named(TIMEOUT_NODE_RUNNING))); + serverSuspended = injector.getInstance(Key.get(new TypeLiteral>() { + }, Names.named(TIMEOUT_NODE_SUSPENDED))); + + return injector.getInstance(ProfitBricksApi.class); } + protected void assertDataCenterAvailable(DataCenter dataCenter) { + assertDataCenterAvailable(dataCenter.id()); + } + + protected void assertDataCenterAvailable(String dataCenterId) { + assertTrue(dataCenterAvailable.apply(dataCenterId), + String.format("Datacenter %s wasn't available in the configured timeout", dataCenterId)); + } + + protected void assertSnapshotAvailable(String snapshotId){ + assertTrue(snapshotAvailable.apply(snapshotId), + String.format("Snapshot %s wasn't available in the configured timeout", snapshotId)); + } + + protected void assertNodeRunning(String serverId) { + assertTrue(serverRunning.apply(serverId), String.format("Server %s did not start in the configured timeout", serverId)); + } + + protected void assertNodeSuspended(String serverId) { + assertTrue(serverSuspended.apply(serverId), String.format("Server %s did not stop in the configured timeout", serverId)); + } + + protected DataCenter findOrCreateDataCenter(final String name) { + DataCenterApi dataCenterApi = api.dataCenterApi(); + + return FluentIterable.from(dataCenterApi.getAllDataCenters()).firstMatch(new Predicate() { + + @Override + public boolean apply(DataCenter input) { + boolean match = Objects.equals(input.name(), name); + if (match && input.location() == testLocation) + assertDataCenterAvailable(input); + + return match; + } + }).or(new Supplier() { + + @Override + public DataCenter get() { + DataCenter dataCenter = api.dataCenterApi().createDataCenter( + DataCenter.Request.creatingPayload(name, testLocation)); + assertDataCenterAvailable(dataCenter); + + return api.dataCenterApi().getDataCenter(dataCenter.id()); + } + }); + } + + protected Server findOrCreateServer(final DataCenter dataCenter) { + return FluentIterable.from(dataCenter.servers()).firstMatch(new Predicate() { + + @Override + public boolean apply(Server input) { + return input.state() == ProvisioningState.AVAILABLE; + } + }).or(new Supplier() { + + @Override + public Server get() { + ServerApi serverApi = api.serverApi(); + String name = String.format("server-%d", dataCenter.servers().size()); + String createdServerId = serverApi.createServer( + Server.Request.creatingBuilder() + .dataCenterId(dataCenter.id()) + .name(name) + .cores(1) + .ram(256) + .build()); + assertDataCenterAvailable(dataCenter); + assertNodeRunning(createdServerId); + + return serverApi.getServer(createdServerId); + } + }); + } + + protected Nic findOrCreateNic(final DataCenter dataCenter) { + final NicApi nicApi = api.nicApi(); + final List nics = nicApi.getAllNics(); + + return FluentIterable.from(nics).firstMatch(new Predicate() { + + @Override + public boolean apply(Nic input) { + return Objects.equals(input.dataCenterId(), dataCenter.id()) + && input.state() == ProvisioningState.AVAILABLE; + } + }).or(new Supplier() { + + @Override + public Nic get() { + Server server = findOrCreateServer(dataCenter); + String name = String.format("%s-nic-%d", server.name(), nics.size()); + String nicId = nicApi.createNic(Nic.Request.creatingBuilder() + .name(name) + .lanId(1) + .serverId(server.id()) + .build()); + assertDataCenterAvailable(dataCenter); + + return nicApi.getNic(nicId); + } + }); + } + + protected void destroyDataCenter(final DataCenter dataCenter) { + boolean success = api.dataCenterApi().deleteDataCenter(dataCenter.id()); + assertTrue(success, "DataCenter wasn't deleted"); + } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinderTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinderTest.java index b1fa5c3639..c690fb3e28 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinderTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/DeregisterLoadBalancerRequestBinderTest.java @@ -16,11 +16,12 @@ */ package org.jclouds.profitbricks.binder.loadbalancer; -import com.google.common.collect.Lists; -import java.util.List; -import org.jclouds.profitbricks.domain.LoadBalancer; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; + +import com.google.common.collect.ImmutableList; + +import org.jclouds.profitbricks.domain.LoadBalancer; import org.testng.annotations.Test; @Test(groups = "unit", testName = "DeregisterLoadBalancerRequestBinderTest") @@ -29,13 +30,9 @@ public class DeregisterLoadBalancerRequestBinderTest { @Test public void testDeregisterPayload() { DeregisterLoadBalancerRequestBinder binder = new DeregisterLoadBalancerRequestBinder(); - List serverIds = Lists.newArrayList(); - serverIds.add("1"); - serverIds.add("2"); - LoadBalancer.Request.DeregisterPayload payload = LoadBalancer.Request.createDeregisteringPayload("load-balancer-id", serverIds); - - String actual = binder.createPayload(payload); + String actual = binder.createPayload(LoadBalancer.Request.createDeregisteringPayload( + "load-balancer-id", ImmutableList.of("1", "2"))); assertNotNull(actual, "Binder returned null payload"); assertEquals(expectedPayload, actual); @@ -43,10 +40,8 @@ public class DeregisterLoadBalancerRequestBinderTest { private final String expectedPayload = (" \n" - + " " + " 1\n" + " 2\n" + " load-balancer-id\n" - + " " + " ").replaceAll("\\s+", ""); } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinderTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinderTest.java index db42403e64..8f2e5a9789 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinderTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/binder/loadbalancer/RegisterLoadBalancerRequestBinderTest.java @@ -16,11 +16,12 @@ */ package org.jclouds.profitbricks.binder.loadbalancer; -import com.google.common.collect.Lists; -import java.util.List; -import org.jclouds.profitbricks.domain.LoadBalancer; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; + +import org.jclouds.profitbricks.domain.LoadBalancer; + +import com.google.common.collect.ImmutableList; import org.testng.annotations.Test; @Test(groups = "unit", testName = "RegisterLoadBalancerRequestBinderTest") @@ -29,13 +30,8 @@ public class RegisterLoadBalancerRequestBinderTest { @Test public void testRegisterPayload() { RegisterLoadBalancerRequestBinder binder = new RegisterLoadBalancerRequestBinder(); - List serverIds = Lists.newArrayList(); - serverIds.add("1"); - serverIds.add("2"); - - LoadBalancer.Request.RegisterPayload payload = LoadBalancer.Request.createRegisteringPaylod("load-balancer-id", serverIds); - - String actual = binder.createPayload(payload); + String actual = binder.createPayload(LoadBalancer.Request.createRegisteringPaylod( + "load-balancer-id", ImmutableList.of("1", "2"))); assertNotNull(actual, "Binder returned null payload"); assertEquals(expectedPayload, actual); @@ -43,10 +39,8 @@ public class RegisterLoadBalancerRequestBinderTest { private final String expectedPayload = (" \n" - + " " + " load-balancer-id\n" + " 1\n" + " 2\n" - + " " + " ").replaceAll("\\s+", ""); } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapterLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapterLiveTest.java deleted file mode 100644 index 83540a57c4..0000000000 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapterLiveTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.profitbricks.compute; - -import org.jclouds.compute.domain.NodeMetadata; -import org.testng.annotations.Test; - -import org.jclouds.compute.internal.BaseComputeServiceLiveTest; -import org.jclouds.sshj.config.SshjSshClientModule; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.inject.Module; -import org.jclouds.compute.domain.ExecResponse; -import org.jclouds.logging.config.LoggingModule; -import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; - -@Test(groups = "live", singleThreaded = true, testName = "ProfitBricksComputeServiceAdapterLiveTest") -public class ProfitBricksComputeServiceAdapterLiveTest extends BaseComputeServiceLiveTest { - - public ProfitBricksComputeServiceAdapterLiveTest() { - provider = "profitbricks"; - } - - @Override - protected Module getSshModule() { - return new SshjSshClientModule(); - } - - @Override - protected LoggingModule getLoggingModule() { - return new SLF4JLoggingModule(); - } - - @Override - public void testOptionToNotBlock() throws Exception { - // ProfitBricks implementation intentionally blocks until the node is 'AVAILABLE' - } - - @Override - protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet tags) { - // ProfitBricks doesn't support tags - } - - @Override - protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap userMetadata) { - // ProfitBricks doesn't support user metadata - } - - @Override - protected void checkResponseEqualsHostname(ExecResponse execResponse, NodeMetadata node1) { - // ProfitBricks doesn't support hostname - } - - @Override - protected void checkOsMatchesTemplate(NodeMetadata node) { - // Not enough description from API to match template - } - -} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceLiveTest.java new file mode 100644 index 0000000000..2599f713c7 --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceLiveTest.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.compute; + +import static org.jclouds.profitbricks.BaseProfitBricksLiveTest.testLocation; +import static org.jclouds.profitbricks.config.ProfitBricksComputeProperties.POLL_PREDICATE_DATACENTER; + +import java.util.Objects; + +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; + +import org.jclouds.compute.domain.NodeMetadata; +import org.testng.annotations.Test; + +import org.jclouds.compute.internal.BaseComputeServiceLiveTest; +import org.jclouds.sshj.config.SshjSshClientModule; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Key; +import com.google.inject.Module; +import com.google.inject.TypeLiteral; +import com.google.inject.name.Names; + +import org.jclouds.compute.domain.ExecResponse; +import org.jclouds.logging.config.LoggingModule; +import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; +import org.jclouds.profitbricks.ProfitBricksApi; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.features.DataCenterApi; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeGroups; + +@Test(groups = "live", singleThreaded = true, testName = "ProfitBricksComputeServiceLiveTest") +public class ProfitBricksComputeServiceLiveTest extends BaseComputeServiceLiveTest { + + private static final String TEST_DC_NAME = "computeServiceLiveTest" + System.currentTimeMillis(); + + private DataCenter dataCenter; + + public ProfitBricksComputeServiceLiveTest() { + provider = "profitbricks"; + } + + @BeforeGroups(groups = {"integration", "live"}) + @Override + public void setupContext() { + super.setupContext(); + + final DataCenterApi api = getDataCenterApi(); + final Predicate predicate = getDataCenterPredicate(); + dataCenter = FluentIterable.from(api.getAllDataCenters()).firstMatch(new Predicate() { + + @Override + public boolean apply(DataCenter input) { + boolean match = Objects.equals(input.name(), TEST_DC_NAME); + if (match && input.location() == testLocation) + return predicate.apply(input.id()); + return match; + } + }).or(new Supplier() { + + @Override + public DataCenter get() { + DataCenter dataCenter = api.createDataCenter( + DataCenter.Request.creatingPayload(TEST_DC_NAME, testLocation)); + predicate.apply(dataCenter.id()); + + return api.getDataCenter(dataCenter.id()); + } + }); + } + + @AfterClass(groups = {"integration", "live"}, alwaysRun = true) + @Override + protected void tearDownContext() { + super.tearDownContext(); + if (dataCenter != null) + getDataCenterApi().deleteDataCenter(dataCenter.id()); + } + + private Predicate getDataCenterPredicate() { + return client.getContext().utils().injector().getInstance(Key.get(new TypeLiteral>() { + }, Names.named(POLL_PREDICATE_DATACENTER))); + } + + private DataCenterApi getDataCenterApi() { + return client.getContext().unwrapApi(ProfitBricksApi.class).dataCenterApi(); + } + + @Override + protected Module getSshModule() { + return new SshjSshClientModule(); + } + + @Override + protected LoggingModule getLoggingModule() { + return new SLF4JLoggingModule(); + } + + @Override + public void testOptionToNotBlock() throws Exception { + // ProfitBricks implementation intentionally blocks until the node is 'AVAILABLE' + } + + @Override + protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet tags) { + // ProfitBricks doesn't support tags + } + + @Override + protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap userMetadata) { + // ProfitBricks doesn't support user metadata + } + + @Override + protected void checkResponseEqualsHostname(ExecResponse execResponse, NodeMetadata node1) { + // ProfitBricks doesn't support hostname + } + + @Override + protected void checkOsMatchesTemplate(NodeMetadata node) { + // Not enough description from API to match template + } + +} diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicateTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/config/StatusPredicateTest.java similarity index 68% rename from providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicateTest.java rename to providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/config/StatusPredicateTest.java index 690412ac7d..9c2b228649 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusPollingPredicateTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/config/StatusPredicateTest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jclouds.profitbricks.compute.internal; +package org.jclouds.profitbricks.compute.config; import static org.jclouds.profitbricks.internal.BaseProfitBricksMockTest.mockWebServer; import static org.testng.Assert.assertEquals; @@ -22,7 +22,12 @@ import static org.testng.Assert.assertEquals; import java.util.concurrent.TimeUnit; import org.jclouds.profitbricks.ProfitBricksApi; +import org.jclouds.profitbricks.compute.config.ProfitBricksComputeServiceContextModule.DataCenterProvisioningStatePredicate; +import org.jclouds.profitbricks.compute.config.ProfitBricksComputeServiceContextModule.ServerStatusPredicate; +import org.jclouds.profitbricks.compute.config.ProfitBricksComputeServiceContextModule.SnapshotProvisioningStatePredicate; import org.jclouds.profitbricks.domain.ProvisioningState; +import org.jclouds.profitbricks.domain.Server; +import org.jclouds.profitbricks.domain.Snapshot; import org.jclouds.profitbricks.internal.BaseProfitBricksMockTest; import org.jclouds.util.Predicates2; import org.testng.annotations.Test; @@ -31,12 +36,12 @@ import com.google.common.base.Predicate; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; + /** - * Tests for the {@link ProvisioningStatusPollingPredicate} class. - *

+ * Test class for {@link DataCenterProvisioningStatePredicate} and {@link ServerStatusPredicate} */ @Test(groups = "unit", testName = "ProvisioningStatusPollingPredicateTest") -public class ProvisioningStatusPollingPredicateTest extends BaseProfitBricksMockTest { +public class StatusPredicateTest extends BaseProfitBricksMockTest { @Test public void testDataCenterPredicate() throws Exception { @@ -56,7 +61,7 @@ public class ProvisioningStatusPollingPredicateTest extends BaseProfitBricksMock ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); Predicate waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(pbApi, ProvisioningStatusAware.DATACENTER, ProvisioningState.AVAILABLE), + new DataCenterProvisioningStatePredicate(pbApi, ProvisioningState.AVAILABLE), 30l, 1l, TimeUnit.SECONDS); String id = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; @@ -89,48 +94,15 @@ public class ProvisioningStatusPollingPredicateTest extends BaseProfitBricksMock ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); Predicate waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(pbApi, ProvisioningStatusAware.SERVER, ProvisioningState.AVAILABLE), + new ServerStatusPredicate(pbApi, Server.Status.RUNNING), 30l, 1l, TimeUnit.SECONDS); String id = "qwertyui-qwer-qwer-qwer-qwertyyuiiop"; try { waitUntilAvailable.apply(id); - ProvisioningState finalState = pbApi.serverApi().getServer(id).state(); + Server remoteServer = pbApi.serverApi().getServer(id); + assertEquals(remoteServer.status(), Server.Status.RUNNING); assertRequestHasCommonProperties(server.takeRequest()); - assertEquals(finalState, ProvisioningState.AVAILABLE); - } finally { - pbApi.close(); - server.shutdown(); - } - } - - @Test - public void testStoragePredicate() throws Exception { - MockWebServer server = mockWebServer(); - - byte[] payloadInProcess = payloadFromResource("/storage/storage-state-inprocess.xml"); - byte[] payloadAvailable = payloadFromResource("/storage/storage.xml"); - - // wait 3 times - server.enqueue(new MockResponse().setBody(payloadInProcess)); - server.enqueue(new MockResponse().setBody(payloadInProcess)); - server.enqueue(new MockResponse().setBody(payloadInProcess)); - server.enqueue(new MockResponse().setBody(payloadAvailable)); - - server.enqueue(new MockResponse().setBody(payloadAvailable)); - - ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); - - Predicate waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(pbApi, ProvisioningStatusAware.STORAGE, ProvisioningState.AVAILABLE), - 30l, 1l, TimeUnit.SECONDS); - - String id = "qswdefrg-qaws-qaws-defe-rgrgdsvcxbrh"; - try { - waitUntilAvailable.apply(id); - ProvisioningState finalState = pbApi.storageApi().getStorage(id).state(); - assertRequestHasCommonProperties(server.takeRequest()); - assertEquals(finalState, ProvisioningState.AVAILABLE); } finally { pbApi.close(); server.shutdown(); @@ -155,15 +127,15 @@ public class ProvisioningStatusPollingPredicateTest extends BaseProfitBricksMock ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); Predicate waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(pbApi, ProvisioningStatusAware.SNAPSHOT, ProvisioningState.AVAILABLE), + new SnapshotProvisioningStatePredicate(pbApi, ProvisioningState.AVAILABLE), 30l, 1l, TimeUnit.SECONDS); String id = "qswdefrg-qaws-qaws-defe-rgrgdsvcxbrh"; try { waitUntilAvailable.apply(id); - ProvisioningState finalState = pbApi.snapshotApi().getSnapshot(id).state(); + Snapshot snapshot = pbApi.snapshotApi().getSnapshot(id); + assertEquals(snapshot.state(), ProvisioningState.AVAILABLE); assertRequestHasCommonProperties(server.takeRequest()); - assertEquals(finalState, ProvisioningState.AVAILABLE); } finally { pbApi.close(); server.shutdown(); diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DataCenterApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DataCenterApiLiveTest.java index 6bcf053ca4..33a086c135 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DataCenterApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DataCenterApiLiveTest.java @@ -20,6 +20,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; import java.util.List; @@ -28,12 +29,10 @@ import org.jclouds.profitbricks.domain.DataCenter; import org.jclouds.profitbricks.domain.Location; import org.jclouds.profitbricks.domain.ProvisioningState; -import static org.testng.Assert.assertTrue; - import org.testng.annotations.AfterClass; import org.testng.annotations.Test; -@Test(groups = "live", testName = "DataCenterApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "DataCenterApiLiveTest") public class DataCenterApiLiveTest extends BaseProfitBricksLiveTest { private String dcId; @@ -45,7 +44,7 @@ public class DataCenterApiLiveTest extends BaseProfitBricksLiveTest { ); assertNotNull(dc); - dcWaitingPredicate.apply(dc.id()); + assertDataCenterAvailable(dc); dcId = dc.id(); } @@ -87,7 +86,7 @@ public class DataCenterApiLiveTest extends BaseProfitBricksLiveTest { ); assertNotNull(dataCenter); - dcWaitingPredicate.apply(dcId); + assertDataCenterAvailable(dataCenter); DataCenter fetchedDc = api.dataCenterApi().getDataCenter(dcId); @@ -118,10 +117,7 @@ public class DataCenterApiLiveTest extends BaseProfitBricksLiveTest { @AfterClass(alwaysRun = true) public void testDeleteDataCenter() { - if (dcId != null) { - boolean result = api.dataCenterApi().deleteDataCenter(dcId); - - assertTrue(result, "Created test data center was not deleted."); - } + boolean result = api.dataCenterApi().deleteDataCenter(dcId); + assertTrue(result, "Created test data center was not deleted."); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DrivesApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DrivesApiLiveTest.java index 2f3c76745f..ff4dbb4fc4 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DrivesApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/DrivesApiLiveTest.java @@ -16,51 +16,48 @@ */ package org.jclouds.profitbricks.features; -import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; -import java.util.List; - import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.DataCenter; import org.jclouds.profitbricks.domain.Drive; import org.jclouds.profitbricks.domain.Image; import org.jclouds.profitbricks.domain.Server; import org.testng.annotations.Test; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import com.google.common.base.Predicate; import com.google.common.collect.Iterables; -@Test(groups = "live", testName = "DrivesApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "DrivesApiLiveTest") public class DrivesApiLiveTest extends BaseProfitBricksLiveTest { - public String serverId; - public String imageId; + private DataCenter dataCenter; + private Server server; + private Image image; - @Override - protected void initialize() { - super.initialize(); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("drivesApiLiveTest" + System.currentTimeMillis()); + server = findOrCreateServer(dataCenter); + image = Iterables.tryFind(api.imageApi().getAllImages(), new Predicate() { - List servers = api.serverApi().getAllServers(); - assertFalse(servers.isEmpty(), "At least one server is required to run drives test."); - - Server server = Iterables.getFirst(servers, null); - assertNotNull(server); - - this.serverId = server.id(); - - List images = api.imageApi().getAllImages(); - assertFalse(images.isEmpty(), "At least one image is required to run drives test."); - - Image image = Iterables.getFirst(images, null); - assertNotNull(image); - - this.imageId = image.id(); + @Override + public boolean apply(Image input) { + return input.location() == dataCenter.location() + && input.type() == Image.Type.CDROM; + } + }).get(); } @Test public void addRomDriveToServerTest() { - String requestId = api.drivesApi().addRomDriveToServer(Drive.Request.AddRomDriveToServerPayload.builder() - .serverId(serverId) - .imageId("05cadf29-6c12-11e4-beeb-52540066fee9") + assertDataCenterAvailable(dataCenter); + String requestId = api.drivesApi().addRomDriveToServer( + Drive.Request.AddRomDriveToServerPayload.builder() + .serverId(server.id()) + .imageId(image.id()) .deviceNumber("0") .build()); assertNotNull(requestId); @@ -68,8 +65,13 @@ public class DrivesApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "addRomDriveToServerTest") public void removeRomDriveFromServerTest() { - String requestId = api.drivesApi().removeRomDriveFromServer(imageId, serverId); - + assertDataCenterAvailable(dataCenter); + String requestId = api.drivesApi().removeRomDriveFromServer(image.id(), server.id()); assertNotNull(requestId); } + + @AfterClass(alwaysRun = true) + public void cleanUp() { + destroyDataCenter(dataCenter); + } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/FirewallApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/FirewallApiLiveTest.java index 772667a203..c5b63f2cbc 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/FirewallApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/FirewallApiLiveTest.java @@ -16,79 +16,58 @@ */ package org.jclouds.profitbricks.features; -import com.google.common.collect.Iterables; - -import java.util.List; - -import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.domain.Firewall; -import org.jclouds.profitbricks.domain.Nic; - import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; -import java.util.concurrent.TimeUnit; +import java.util.List; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; +import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.domain.Firewall; +import org.jclouds.profitbricks.domain.Nic; import org.jclouds.profitbricks.domain.Firewall.Protocol; -import org.jclouds.profitbricks.domain.ProvisioningState; -import org.jclouds.util.Predicates2; import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; -@Test(groups = "live", testName = "FirewallApiLiveTest", singleThreaded = true) + +@Test(groups = "live", testName = "FirewallApiLiveTest") public class FirewallApiLiveTest extends BaseProfitBricksLiveTest { - private Predicate waitUntilAvailable; + private DataCenter dataCenter; private Nic nic; private Firewall createdFirewall; - private Firewall.Rule createdFirewallRule; - @Override - protected void initialize() { - super.initialize(); - List nics = api.nicApi().getAllNics(); - assertFalse(nics.isEmpty(), "Must atleast have 1 NIC available for firewall testing."); - - this.nic = Iterables.tryFind(nics, new Predicate() { - - @Override - public boolean apply(Nic input) { - return input.state() == ProvisioningState.AVAILABLE; - } - }).orNull(); - - assertNotNull(nic, "No available NIC for firewall testing was found."); - - this.waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.NIC, ProvisioningState.AVAILABLE), - 2l * 60l, 2l, TimeUnit.SECONDS); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("firewallApiLiveTest" + System.currentTimeMillis()); + nic = findOrCreateNic(dataCenter); } @Test public void testAddFirewallRuleToNic() { + assertDataCenterAvailable(dataCenter); Firewall firewall = api.firewallApi().addFirewallRuleToNic( - Firewall.Request.createAddRulePayload(nic.id(), ImmutableList.of( + Firewall.Request.createAddRulePayload( + nic.id(), ImmutableList.of( Firewall.Rule.builder() .name("test-rule-tcp") .protocol(Protocol.TCP) .build() - )) + ) + ) ); assertNotNull(firewall); - assertNotNull(firewall.rules()); + assertFalse(firewall.rules().isEmpty()); + assertDataCenterAvailable(dataCenter); - waitUntilAvailable.apply(nic.id()); createdFirewall = firewall; - createdFirewallRule = Iterables.getOnlyElement(firewall.rules()); } @Test(dependsOnMethods = "testAddFirewallRuleToNic") @@ -109,37 +88,52 @@ public class FirewallApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testAddFirewallRuleToNic") public void testActivateFirewall() { - boolean result = api.firewallApi().activateFirewall(ImmutableList.of(createdFirewall.id())); - - waitUntilAvailable.apply(nic.id()); - + assertDataCenterAvailable(dataCenter); + boolean result = api.firewallApi().activateFirewall( + ImmutableList.of(createdFirewall.id())); + assertDataCenterAvailable(dataCenter); assertTrue(result); + + Firewall firewall = api.firewallApi().getFirewall(createdFirewall.id()); + assertTrue(firewall.active(), "Firewall wasn't activated"); } @Test(dependsOnMethods = "testActivateFirewall") void testDeactivateFirewall() { - boolean result = api.firewallApi().deactivateFirewall(ImmutableList.of(createdFirewall.id())); - - waitUntilAvailable.apply(nic.id()); - + assertDataCenterAvailable(dataCenter); + boolean result = api.firewallApi().deactivateFirewall( + ImmutableList.of(createdFirewall.id())); + assertDataCenterAvailable(dataCenter); assertTrue(result); + + Firewall firewall = api.firewallApi().getFirewall(createdFirewall.id()); + assertFalse(firewall.active(), "Firewall wasn't deactivated"); } - @Test(dependsOnMethods = "testActivateFirewall") + @Test(dependsOnMethods = "testDeactivateFirewall") void testRemoveFirewallRule() { - boolean result = api.firewallApi().removeFirewallRules(ImmutableList.of(createdFirewallRule.id())); + assertDataCenterAvailable(dataCenter); + for (Firewall.Rule rule : createdFirewall.rules()) { + boolean result = api.firewallApi().removeFirewallRules( + ImmutableList.of(rule.id())); - waitUntilAvailable.apply(nic.id()); + assertTrue(result); + assertDataCenterAvailable(dataCenter); - assertTrue(result); + } + Firewall firewall = api.firewallApi().getFirewall(createdFirewall.id()); + assertTrue(firewall.rules().isEmpty(), "Not all rules removed"); + } + + @Test(dependsOnMethods = "testRemoveFirewallRule") + public void testDeleteFirewall() { + assertDataCenterAvailable(dataCenter); + boolean result = api.firewallApi().deleteFirewall(ImmutableList.of(createdFirewall.id())); + assertTrue(result, "Created firewall was not deleted."); } @AfterClass(alwaysRun = true) - public void testDeleteFirewall() { - if (createdFirewall != null) { - boolean result = api.firewallApi().deleteFirewall(ImmutableList.of(createdFirewall.id())); - - assertTrue(result, "Created firewall was not deleted."); - } + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ImageApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ImageApiLiveTest.java index 2299a0c301..b71cd8d748 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ImageApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ImageApiLiveTest.java @@ -16,20 +16,24 @@ */ package org.jclouds.profitbricks.features; -import com.google.common.collect.Iterables; -import java.util.List; -import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.domain.Image; import static org.testng.Assert.assertEquals; + import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; + +import java.util.List; + +import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.Image; import org.testng.annotations.Test; -@Test(groups = "live", testName = "ImageApiLiveTest", singleThreaded = true) +import com.google.common.collect.Iterables; + +@Test(groups = "live", testName = "ImageApiLiveTest") public class ImageApiLiveTest extends BaseProfitBricksLiveTest { - private String imageId; + private Image image; @Test public void testGetAllImages() { @@ -37,22 +41,22 @@ public class ImageApiLiveTest extends BaseProfitBricksLiveTest { assertNotNull(images); assertFalse(images.isEmpty(), "No images found."); - imageId = Iterables.getFirst(images, null).id(); + + image = Iterables.getFirst(images, null); + assertNotNull(image); } @Test(dependsOnMethods = "testGetAllImages") public void testGetImage() { - Image image = api.imageApi().getImage(imageId); + Image fetchedImage = api.imageApi().getImage(image.id()); - assertNotNull(image); - assertEquals(image.id(), imageId); + assertNotNull(fetchedImage); + assertEquals(fetchedImage, image); } @Test public void testGetNonExistingImage() { String id = "random-non-existing-id"; - Image image = api.imageApi().getImage(id); - - assertNull(image, "Should've just returned null"); + assertNull(api.imageApi().getImage(id), "Should've just returned null"); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java index e7c2eda498..12670cd22c 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiLiveTest.java @@ -16,45 +16,46 @@ */ package org.jclouds.profitbricks.features; -import com.google.common.collect.Iterables; -import java.util.List; -import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.domain.IpBlock; -import org.jclouds.profitbricks.domain.Location; -import org.jclouds.profitbricks.domain.Nic; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.util.List; + +import com.google.common.collect.Iterables; + +import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.domain.IpBlock; +import org.jclouds.profitbricks.domain.Nic; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -@Test(groups = "live", testName = "IpBlockApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "IpBlockApiLiveTest") public class IpBlockApiLiveTest extends BaseProfitBricksLiveTest { - private String nicid; + private DataCenter dataCenter; + private Nic nic; + private IpBlock newIpBlock; - @Override - public void initialize() { - super.initialize(); - - List nics = api.nicApi().getAllNics(); - - assertFalse(nics.isEmpty(), "At least one NIC is requred to test IpBlocks"); - - Nic nic = Iterables.getFirst(nics, null); - - nicid = nic.id(); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("ipBlockApiLiveTest" + System.currentTimeMillis()); + nic = findOrCreateNic(dataCenter); } @Test public void testReservePublicIpBlock() { - newIpBlock = api.ipBlockApi().reservePublicIpBlock("2", Location.US_LAS.getId()); + assertDataCenterAvailable(dataCenter); + newIpBlock = api.ipBlockApi().reservePublicIpBlock(1, testLocation); assertNotNull(newIpBlock); - assertNotNull(newIpBlock.ips()); assertFalse(newIpBlock.ips().isEmpty()); } - @Test + @Test(dependsOnMethods = "testReservePublicIpBlock") public void testGetAllIpBlocks() { List ipBlocks = api.ipBlockApi().getAllIpBlock(); @@ -71,22 +72,40 @@ public class IpBlockApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testReservePublicIpBlock") public void testAddPublicIpToNic() { - String requestId = api.ipBlockApi().addPublicIpToNic(newIpBlock.ips().get(0), nicid); + assertDataCenterAvailable(dataCenter); + String ipToAdd = Iterables.getFirst(newIpBlock.ips(), null); + String requestId = api.ipBlockApi().addPublicIpToNic( + ipToAdd, nic.id()); assertNotNull(requestId); + assertDataCenterAvailable(dataCenter); + List ips = api.nicApi().getNic(nic.id()).ips(); + assertTrue(ips.contains(ipToAdd), "NIC didn't contain added public ip"); } @Test(dependsOnMethods = "testAddPublicIpToNic") public void testRemovePublicIpFromNic() { - String requestId = api.ipBlockApi().removePublicIpFromNic(newIpBlock.ips().get(0), nicid); + assertDataCenterAvailable(dataCenter); + String ipToRemove = Iterables.getFirst(newIpBlock.ips(), null); + String requestId = api.ipBlockApi().removePublicIpFromNic( + ipToRemove, nic.id()); assertNotNull(requestId); + assertDataCenterAvailable(dataCenter); + List ips = api.nicApi().getNic(nic.id()).ips(); + assertFalse(ips.contains(ipToRemove), "NIC still contains removed public ip"); } @Test(dependsOnMethods = "testRemovePublicIpFromNic") public void testReleasePublicIpBlock() { + assertDataCenterAvailable(dataCenter); String requestId = api.ipBlockApi().releasePublicIpBlock(newIpBlock.id()); assertNotNull(requestId); } + + @AfterClass(alwaysRun = true) + public void cleanUp() { + destroyDataCenter(dataCenter); + } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java index e948e767bf..234f163bb8 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/IpBlockApiMockTest.java @@ -119,12 +119,12 @@ public class IpBlockApiMockTest extends BaseProfitBricksMockTest { ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); IpBlockApi api = pbApi.ipBlockApi(); - String blockSize = "2"; + int blockSize = 2; Location location = Location.US_LAS; String content = "" + blockSize + "" + location.getId() + ""; try { - IpBlock ipBlock = api.reservePublicIpBlock(blockSize, location.getId()); + IpBlock ipBlock = api.reservePublicIpBlock(blockSize, location); assertRequestHasCommonProperties(server.takeRequest(), content); assertNotNull(ipBlock); } finally { diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiLiveTest.java index e293e1cc78..0780f78932 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiLiveTest.java @@ -16,61 +16,58 @@ */ package org.jclouds.profitbricks.features; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; import java.util.List; +import java.util.Objects; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import org.assertj.core.util.Lists; import org.jclouds.profitbricks.BaseProfitBricksLiveTest; import org.jclouds.profitbricks.domain.DataCenter; import org.jclouds.profitbricks.domain.LoadBalancer; import org.jclouds.profitbricks.domain.LoadBalancer.Algorithm; import org.jclouds.profitbricks.domain.Server; -import org.testng.Assert; import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @Test(groups = "unit", testName = "LoadBalancerApiLiveTest") public class LoadBalancerApiLiveTest extends BaseProfitBricksLiveTest { - private String dataCenterId; + private DataCenter dataCenter; + private Server server; + private String loadBalancerId; - private String serverId; - @Override - protected void initialize() { - super.initialize(); - List dataCenters = api.dataCenterApi().getAllDataCenters(); - assertFalse(dataCenters.isEmpty(), "At least 1 datacenter has to be available for loadbalancer testing."); - - dataCenterId = Iterables.getFirst(dataCenters, null).id(); - - List servers = api.serverApi().getAllServers(); - assertFalse(servers.isEmpty(), "At least 1 server has to be available for loadbalancer testing."); - - serverId = Iterables.getFirst(servers, null).id(); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("loadBalancerApiLiveTest" + System.currentTimeMillis()); + server = findOrCreateServer(dataCenter); } @Test public void testCreateLoadBalancer() { - List serverIds = com.google.common.collect.Lists.newArrayList(); - serverIds.add("server-ids"); - - LoadBalancer.Request.CreatePayload payload = LoadBalancer.Request.creatingBuilder() - .dataCenterId(dataCenterId) + assertDataCenterAvailable(dataCenter); + String createdId = api.loadBalancerApi().createLoadBalancer( + LoadBalancer.Request.creatingBuilder() + .dataCenterId(dataCenter.id()) .name("testName") .algorithm(Algorithm.ROUND_ROBIN) - .ip("0.0.0.1") + .ip("192.168.0.200") .lanId(1) - .serverIds(serverIds) - .build(); + .build() + ); - String requestId = api.loadBalancerApi().createLoadBalancer(payload); - - assertNotNull(requestId); + assertNotNull(createdId); + assertDataCenterAvailable(dataCenter); + this.loadBalancerId = createdId; } @Test(dependsOnMethods = "testCreateLoadBalancer") @@ -85,50 +82,76 @@ public class LoadBalancerApiLiveTest extends BaseProfitBricksLiveTest { LoadBalancer loadBalancer = api.loadBalancerApi().getLoadBalancer(loadBalancerId); assertNotNull(loadBalancer); + assertEquals(loadBalancer.id(), loadBalancerId); } - @Test(dependsOnMethods = "testCreateLoadBalancer") + @Test(dependsOnMethods = "testGetLoadBalancer") public void testRegisterLoadBalancer() { - List serverIds = Lists.newArrayList(); - serverIds.add(serverId); - - LoadBalancer.Request.RegisterPayload payload = LoadBalancer.Request - .createRegisteringPaylod(loadBalancerId, serverIds); - - LoadBalancer loadBalancer = api.loadBalancerApi().registerLoadBalancer(payload); + assertDataCenterAvailable(dataCenter); + LoadBalancer loadBalancer = api.loadBalancerApi().registerLoadBalancer( + LoadBalancer.Request + .createRegisteringPaylod(loadBalancerId, ImmutableList.of(server.id())) + ); assertNotNull(loadBalancer); + assertDataCenterAvailable(dataCenter); + Optional balancedServer = Iterables.tryFind(loadBalancer.balancedServers(), new Predicate() { + + @Override + public boolean apply(Server t) { + return Objects.equals(t.id(), server.id()); + } + }); + assertTrue(balancedServer.isPresent(), "Server input wasn't registered to loadbalancer"); } @Test(dependsOnMethods = "testRegisterLoadBalancer") public void testDeregisterLoadBalancer() { - List serverIds = Lists.newArrayList(); - serverIds.add(serverId); + assertDataCenterAvailable(dataCenter); + String requestId = api.loadBalancerApi().deregisterLoadBalancer( + LoadBalancer.Request + .createDeregisteringPayload(loadBalancerId, ImmutableList.of(server.id())) + ); - LoadBalancer.Request.DeregisterPayload payload = LoadBalancer.Request - .createDeregisteringPayload(loadBalancerId, serverIds); + assertNotNull(requestId); + assertDataCenterAvailable(dataCenter); + LoadBalancer loadBalancer = api.loadBalancerApi().getLoadBalancer(loadBalancerId); + Optional balancedServer = Iterables.tryFind(loadBalancer.balancedServers(), new Predicate() { - LoadBalancer loadBalancer = api.loadBalancerApi().deregisterLoadBalancer(payload); - - assertNotNull(loadBalancer); + @Override + public boolean apply(Server t) { + return Objects.equals(t.id(), loadBalancerId); + } + }); + assertFalse(balancedServer.isPresent(), "Server input wasn't deregistered from loadbalancer"); } - @Test(dependsOnMethods = "testCreateLoadBalancer") + @Test(dependsOnMethods = "testDeregisterLoadBalancer") public void testUpdateLoadBalancer() { - LoadBalancer.Request.UpdatePayload payload = LoadBalancer.Request.updatingBuilder() + assertDataCenterAvailable(dataCenter); + String newName = "whatever"; + String requestId = api.loadBalancerApi().updateLoadBalancer( + LoadBalancer.Request.updatingBuilder() .id(loadBalancerId) - .name("whatever") - .build(); + .name(newName) + .build() + ); - LoadBalancer loadBalancer = api.loadBalancerApi().updateLoadBalancer(payload); + assertNotNull(requestId); + assertDataCenterAvailable(dataCenter); + LoadBalancer loadBalancer = api.loadBalancerApi().getLoadBalancer(loadBalancerId); + assertEquals(loadBalancer.name(), newName); + } - assertNotNull(loadBalancer); + @Test(dependsOnMethods = "testUpdateLoadBalancer") + public void testDeleteLoadBalancer() { + assertDataCenterAvailable(dataCenter); + boolean result = api.loadBalancerApi().deleteLoadBalancer(loadBalancerId); + assertTrue(result, "Test load balancer wasn't deleted"); } @AfterClass(alwaysRun = true) - public void testDeleteLoadBalancer() { - boolean result = api.loadBalancerApi().deleteLoadBalancer(loadBalancerId); - - Assert.assertTrue(result); + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiMockTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiMockTest.java index c4f26aabb6..e4e8fd7ed6 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiMockTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/LoadBalancerApiMockTest.java @@ -23,9 +23,11 @@ import static org.testng.AssertJUnit.assertTrue; import java.util.List; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; + import org.jclouds.profitbricks.ProfitBricksApi; import org.jclouds.profitbricks.domain.LoadBalancer; import org.jclouds.profitbricks.domain.LoadBalancer.Algorithm; @@ -142,7 +144,7 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { try { List serverIds = Lists.newArrayList(); serverIds.add("server-ids"); - String requestId = api.createLoadBalancer(LoadBalancer.Request.creatingBuilder() + String loadBalancerId = api.createLoadBalancer(LoadBalancer.Request.creatingBuilder() .dataCenterId("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee") .name("load-balancer-name") .algorithm(Algorithm.ROUND_ROBIN) @@ -152,7 +154,7 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { .build()); assertRequestHasCommonProperties(server.takeRequest(), content); - + assertEquals(loadBalancerId, "1234-1234-1234-1234"); } finally { pbApi.close(); server.shutdown(); @@ -162,7 +164,7 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { @Test public void testUpdateLoadBalancer() throws Exception { MockWebServer server = mockWebServer(); - server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-create.xml"))); + server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-update.xml"))); ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); LoadBalancerApi api = pbApi.loadBalancerApi(); @@ -187,10 +189,10 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { .ip("192.168.0.1") .build(); - LoadBalancer loadBalancer = api.updateLoadBalancer(toUpdate); + String requestId = api.updateLoadBalancer(toUpdate); assertRequestHasCommonProperties(server.takeRequest(), content); - assertNotNull(loadBalancer); + assertEquals(requestId, "request-id"); } finally { pbApi.close(); server.shutdown(); @@ -207,11 +209,9 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { LoadBalancerApi api = pbApi.loadBalancerApi(); String content = "" - + "" + "1234" + "1" + "2" - + "" + ""; try { @@ -225,6 +225,7 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { assertRequestHasCommonProperties(server.takeRequest(), content); assertNotNull(loadBalancer); + assertEquals(loadBalancer.id(), "load-balancer-id"); } finally { pbApi.close(); server.shutdown(); @@ -234,31 +235,25 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { @Test public void testDeregisterLoadBalancer() throws Exception { MockWebServer server = mockWebServer(); - server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-update.xml"))); + server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-deregister.xml"))); ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); LoadBalancerApi api = pbApi.loadBalancerApi(); String content = "" - + "" + "1" + "2" + "load-balancer-id" - + "" + ""; try { - List serverIds = Lists.newArrayList(); - serverIds.add("1"); - serverIds.add("2"); - LoadBalancer.Request.DeregisterPayload payload = LoadBalancer.Request - .createDeregisteringPayload("load-balancer-id", serverIds); + .createDeregisteringPayload("load-balancer-id", ImmutableList.of("1", "2")); - LoadBalancer loadBalancer = api.deregisterLoadBalancer(payload); + String requestId = api.deregisterLoadBalancer(payload); assertRequestHasCommonProperties(server.takeRequest(), content); - assertNotNull(loadBalancer); + assertEquals(requestId, "request-id"); } finally { pbApi.close(); server.shutdown(); @@ -268,7 +263,7 @@ public class LoadBalancerApiMockTest extends BaseProfitBricksMockTest { @Test public void testDeleteLoadBalancer() throws Exception { MockWebServer server = mockWebServer(); - server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-register.xml"))); + server.enqueue(new MockResponse().setBody(payloadFromResource("/loadbalancer/loadbalancer-delete.xml"))); ProfitBricksApi pbApi = api(server.getUrl(rootUrl)); LoadBalancerApi api = pbApi.loadBalancerApi(); diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiLiveTest.java index fbbef8b713..5253d1f768 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiLiveTest.java @@ -17,68 +17,47 @@ package org.jclouds.profitbricks.features; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; -import com.google.common.collect.Iterables; - import java.util.List; -import java.util.concurrent.TimeUnit; import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; +import org.jclouds.profitbricks.domain.DataCenter; import org.jclouds.profitbricks.domain.Nic; -import org.jclouds.profitbricks.domain.ProvisioningState; import org.jclouds.profitbricks.domain.Server; -import org.jclouds.util.Predicates2; - import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import org.testng.annotations.BeforeClass; -import com.google.common.base.Predicate; - -@Test(groups = "live", testName = "NicApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "NicApiLiveTest") public class NicApiLiveTest extends BaseProfitBricksLiveTest { - private Predicate waitUntilAvailable; + private DataCenter dataCenter; private Server server; - private Nic createdNic; - @Override - protected void initialize() { - super.initialize(); - List servers = api.serverApi().getAllServers(); - assertFalse(servers.isEmpty(), "Must atleast have 1 server available for NIC testing."); + private String createdNicId; - this.server = Iterables.tryFind(servers, new Predicate() { - - @Override - public boolean apply(Server input) { - return input.state() == ProvisioningState.AVAILABLE; - } - }).orNull(); - - this.waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.NIC, ProvisioningState.AVAILABLE), - 2l * 60l, 2l, TimeUnit.SECONDS); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("nicApiLiveTest-" + System.currentTimeMillis()); + server = findOrCreateServer(dataCenter); } @Test public void testCreateNic() { - Nic.Request.CreatePayload payload = Nic.Request.creatingBuilder() + assertDataCenterAvailable(dataCenter); + String nicId = api.nicApi().createNic(Nic.Request.creatingBuilder() .name("name nr1") .dhcpActive(true) .serverId(server.id()) .lanId(1) - .build(); + .build()); - Nic nic = api.nicApi().createNic(payload); - assertNotNull(nic); + assertNotNull(nicId); + assertDataCenterAvailable(dataCenter); - waitUntilAvailable.apply(nic.id()); - this.createdNic = nic; + this.createdNicId = nicId; } @Test(dependsOnMethods = "testCreateNic") @@ -90,47 +69,56 @@ public class NicApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testCreateNic") public void testGetNic() { - Nic nic = api.nicApi().getNic(createdNic.id()); + Nic nic = api.nicApi().getNic(createdNicId); assertNotNull(nic); - assertEquals(nic.id(), createdNic.id()); + assertEquals(nic.id(), createdNicId); } - @Test(dependsOnMethods = "testCreateNic") + @Test(dependsOnMethods = "testGetNic") public void testUpdateNic() { - Nic.Request.UpdatePayload payload = Nic.Request.updatingBuilder() + assertDataCenterAvailable(dataCenter); + String newName = "name nr2"; + String requestId = api.nicApi().updateNic( + Nic.Request.updatingBuilder() .name("name nr2") - .id(createdNic.id()) - .build(); + .id(createdNicId) + .build() + ); - Nic updatedNic = api.nicApi().updateNic(payload); - assertNotNull(updatedNic); - waitUntilAvailable.apply(payload.id()); + assertNotNull(requestId); + assertDataCenterAvailable(dataCenter); - updatedNic = api.nicApi().getNic(payload.id()); - - assertEquals(updatedNic.name(), payload.name()); + Nic nic = api.nicApi().getNic(createdNicId); + assertEquals(nic.name(), newName); } @Test(dependsOnMethods = "testUpdateNic") public void testSetInternetAccess() { + assertDataCenterAvailable(dataCenter); - Nic.Request.SetInternetAccessPayload payload = Nic.Request.setInternetAccessBuilder() - .dataCenterId(createdNic.dataCenterId()) + String requestId = api.nicApi().setInternetAccess(Nic.Request.setInternetAccessBuilder() + .dataCenterId(dataCenter.id()) .lanId(1) .internetAccess(true) - .build(); + .build() + ); + assertDataCenterAvailable(dataCenter); + assertNotNull(requestId); - Nic result = api.nicApi().setInternetAccess(payload); - assertNotNull(result); + Nic nic = api.nicApi().getNic(createdNicId); + assertTrue(nic.internetAccess(), "Expected nic to have internet access"); + } + + @Test(dependsOnMethods = "testSetInternetAccess") + public void testDeleteNic() { + assertDataCenterAvailable(dataCenter); + boolean result = api.nicApi().deleteNic(createdNicId); + assertTrue(result, "Created test NIC was not deleted."); } @AfterClass(alwaysRun = true) - public void testDeleteNic() { - if (createdNic != null) { - boolean result = api.nicApi().deleteNic(createdNic.id()); - - assertTrue(result, "Created test NIC was not deleted."); - } + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiMockTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiMockTest.java index 2129b2501f..940efa71de 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiMockTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/NicApiMockTest.java @@ -16,17 +16,20 @@ */ package org.jclouds.profitbricks.features; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; -import java.util.List; -import org.jclouds.profitbricks.ProfitBricksApi; -import org.jclouds.profitbricks.domain.Nic; -import org.jclouds.profitbricks.internal.BaseProfitBricksMockTest; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; + +import java.util.List; + +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; + +import org.jclouds.profitbricks.ProfitBricksApi; +import org.jclouds.profitbricks.domain.Nic; +import org.jclouds.profitbricks.internal.BaseProfitBricksMockTest; import org.testng.annotations.Test; @Test(groups = "unit", testName = "NicApiMockTest") @@ -111,7 +114,7 @@ public class NicApiMockTest extends BaseProfitBricksMockTest { + ""; try { - Nic nic = api.createNic( + String nicId = api.createNic( Nic.Request.creatingBuilder() .ip("192.168.0.1") .name("nic-name") @@ -121,7 +124,7 @@ public class NicApiMockTest extends BaseProfitBricksMockTest { .build()); assertRequestHasCommonProperties(server.takeRequest(), content); - assertNotNull(nic.id()); + assertNotNull(nicId); } finally { pbApi.close(); @@ -147,7 +150,7 @@ public class NicApiMockTest extends BaseProfitBricksMockTest { + "" + ""; try { - Nic nic = api.updateNic(Nic.Request.updatingBuilder() + String requestId = api.updateNic(Nic.Request.updatingBuilder() .id("nic-id") .ip("10.0.0.1") .name("nic-name") @@ -155,7 +158,7 @@ public class NicApiMockTest extends BaseProfitBricksMockTest { .lanId(1) .build()); assertRequestHasCommonProperties(server.takeRequest(), content); - assertNotNull(nic); + assertEquals(requestId, "request-id"); } finally { pbApi.close(); server.shutdown(); @@ -176,13 +179,13 @@ public class NicApiMockTest extends BaseProfitBricksMockTest { + "true" + ""; try { - Nic nic = api.setInternetAccess(Nic.Request.setInternetAccessBuilder() + String requestId = api.setInternetAccess(Nic.Request.setInternetAccessBuilder() .dataCenterId("datacenter-id") .lanId(1) .internetAccess(true) .build()); assertRequestHasCommonProperties(server.takeRequest(), content); - assertNotNull(nic); + assertEquals(requestId, "request-id"); } finally { pbApi.close(); server.shutdown(); diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ServerApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ServerApiLiveTest.java index 6ceffcd150..be1a60c005 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ServerApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/ServerApiLiveTest.java @@ -16,46 +16,35 @@ */ package org.jclouds.profitbricks.features; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import java.util.List; -import java.util.concurrent.TimeUnit; -import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; -import org.jclouds.profitbricks.domain.DataCenter; -import org.jclouds.profitbricks.domain.ProvisioningState; -import org.jclouds.profitbricks.domain.Server; -import org.jclouds.util.Predicates2; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; + +import java.util.List; + +import org.jclouds.profitbricks.BaseProfitBricksLiveTest; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.domain.ProvisioningState; +import org.jclouds.profitbricks.domain.Server; import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -@Test(groups = "live", testName = "ServerApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "ServerApiLiveTest") public class ServerApiLiveTest extends BaseProfitBricksLiveTest { - private Predicate waitUntilAvailable; private DataCenter dataCenter; private String createdServerId; - @Override - protected void initialize() { - super.initialize(); - List dataCenters = api.dataCenterApi().getAllDataCenters(); - assertFalse(dataCenters.isEmpty(), "Must atleast have 1 datacenter available for server testing."); - - this.dataCenter = Iterables.getFirst(dataCenters, null); - - this.waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.SERVER, ProvisioningState.AVAILABLE), - 2l * 60l, 2l, TimeUnit.SECONDS); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("serverApiLiveTest-" + System.currentTimeMillis()); } @Test public void testCreateServer() { + assertDataCenterAvailable(dataCenter); String serverId = api.serverApi().createServer( Server.Request.creatingBuilder() .dataCenterId(dataCenter.id()) @@ -65,6 +54,9 @@ public class ServerApiLiveTest extends BaseProfitBricksLiveTest { .build()); assertNotNull(serverId); + assertDataCenterAvailable(dataCenter); + assertNodeRunning(serverId); + this.createdServerId = serverId; } @@ -84,15 +76,9 @@ public class ServerApiLiveTest extends BaseProfitBricksLiveTest { assertFalse(servers.isEmpty()); } - @Test(dependsOnMethods = "testCreateServer") - public void testWaitUntilAvailable() { - boolean available = waitUntilAvailable.apply(createdServerId); - - assertTrue(available); - } - - @Test(dependsOnMethods = "testWaitUntilAvailable") + @Test(dependsOnMethods = "testGetServer") public void testUpdateServer() { + assertDataCenterAvailable(dataCenter); String requestId = api.serverApi().updateServer( Server.Request.updatingBuilder() .id(createdServerId) @@ -102,7 +88,7 @@ public class ServerApiLiveTest extends BaseProfitBricksLiveTest { .build()); assertNotNull(requestId); - waitUntilAvailable.apply(createdServerId); + assertDataCenterAvailable(dataCenter); Server server = api.serverApi().getServer(createdServerId); assertEquals(server.state(), ProvisioningState.AVAILABLE); @@ -112,11 +98,8 @@ public class ServerApiLiveTest extends BaseProfitBricksLiveTest { public void testStopServer() { String requestId = api.serverApi().stopServer(createdServerId); assertNotNull(requestId); + assertNodeSuspended(createdServerId); - Predicate waitUntilInactive = Predicates2.retry(new ProvisioningStatusPollingPredicate( - api, ProvisioningStatusAware.SERVER, ProvisioningState.INACTIVE), 2l * 60l, 2l, TimeUnit.SECONDS); - - waitUntilInactive.apply(createdServerId); Server server = api.serverApi().getServer(createdServerId); assertEquals(server.status(), Server.Status.SHUTOFF); } @@ -125,20 +108,22 @@ public class ServerApiLiveTest extends BaseProfitBricksLiveTest { public void testStartServer() { String requestId = api.serverApi().startServer(createdServerId); assertNotNull(requestId); - - waitUntilAvailable.apply(createdServerId); + assertNodeRunning(createdServerId); Server server = api.serverApi().getServer(createdServerId); assertEquals(server.status(), Server.Status.RUNNING); } - @AfterClass(alwaysRun = true) + @Test(dependsOnMethods = "testStartServer") public void testDeleteServer() { - if (createdServerId != null) { - boolean result = api.serverApi().deleteServer(createdServerId); + assertDataCenterAvailable(dataCenter); + boolean result = api.serverApi().deleteServer(createdServerId); + assertTrue(result, "Created test server was not deleted."); + } - assertTrue(result, "Created test server was not deleted."); - } + @AfterClass(alwaysRun = true) + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/SnapshotApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/SnapshotApiLiveTest.java index 500d218023..10a3f38564 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/SnapshotApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/SnapshotApiLiveTest.java @@ -16,59 +16,78 @@ */ package org.jclouds.profitbricks.features; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.util.List; + import org.jclouds.profitbricks.BaseProfitBricksLiveTest; import org.jclouds.profitbricks.domain.OsType; import org.jclouds.profitbricks.domain.Snapshot; -import org.testng.annotations.Test; - -import java.util.List; -import java.util.concurrent.TimeUnit; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; -import org.jclouds.profitbricks.domain.ProvisioningState; import org.jclouds.profitbricks.domain.Storage; -import org.jclouds.util.Predicates2; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; - -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import org.testng.annotations.Test; +import org.jclouds.profitbricks.domain.DataCenter; +import org.jclouds.profitbricks.domain.ProvisioningState; import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; -@Test(groups = "live", testName = "SnapshotApiLiveTest", singleThreaded = true) +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; + +@Test(groups = "live", testName = "SnapshotApiLiveTest") public class SnapshotApiLiveTest extends BaseProfitBricksLiveTest { - protected Predicate snapshotWaitingPredicate; - private String snapshotId; - private String storageId; + private DataCenter dataCenter; + private Storage storage; - @Override - protected void initialize() { - super.initialize(); + private String createdSnapshotId; - initializeWaitPredicate(); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("snapshotApiLiveTest-" + System.currentTimeMillis()); + storage = FluentIterable.from(dataCenter.storages()).firstMatch(new Predicate() { - List storages = api.storageApi().getAllStorages(); - assertFalse(storages.isEmpty(), "Must atleast have 1 storage available for snapshot testing."); + @Override + public boolean apply(Storage input) { + return input.state() == ProvisioningState.AVAILABLE + && input.size() <= 10f; + } + }).or(new Supplier() { - storageId = Iterables.getFirst(storages, null).id(); + @Override + public Storage get() { + StorageApi storageApi = api.storageApi(); + String name = String.format("server-%d", dataCenter.servers().size()); + String createdStorageId = storageApi.createStorage( + Storage.Request.creatingBuilder() + .dataCenterId(dataCenter.id()) + .name(name) + .size(2f) + .build() + ); + assertDataCenterAvailable(dataCenter); + + return storageApi.getStorage(createdStorageId); + } + }); } @Test public void testCreateSnapshot() { - Snapshot snapshot = api.snapshotApi().createSnapshot(Snapshot.Request.creatingBuilder() - .storageId(storageId) + assertDataCenterAvailable(dataCenter); + Snapshot snapshot = api.snapshotApi().createSnapshot( + Snapshot.Request.creatingBuilder() + .storageId(storage.id()) .description("my description") .name("test snapshot") .build()); assertNotNull(snapshot); + assertSnapshotAvailable(snapshot.id()); - snapshotWaitingPredicate.apply(snapshot.id()); - - snapshotId = snapshot.id(); + createdSnapshotId = snapshot.id(); } @Test(dependsOnMethods = "testCreateSnapshot") @@ -81,20 +100,22 @@ public class SnapshotApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testCreateSnapshot") public void testGetSnapshot() { - Snapshot snapshot = api.snapshotApi().getSnapshot(snapshotId); + Snapshot snapshot = api.snapshotApi().getSnapshot(createdSnapshotId); assertNotNull(snapshot); - assertEquals(snapshot.id(), snapshotId); + assertEquals(snapshot.id(), createdSnapshotId); } - @Test(dependsOnMethods = "testCreateSnapshot") + @Test(dependsOnMethods = "testGetSnapshot") public void testUpdateSnapshot() { - + assertSnapshotAvailable(createdSnapshotId); String newName = "new name"; + String newDescription = "new description"; - api.snapshotApi().updateSnapshot(Snapshot.Request.updatingBuilder() - .id(snapshotId) - .description("new description") + String requestId = api.snapshotApi().updateSnapshot( + Snapshot.Request.updatingBuilder() + .id(createdSnapshotId) + .description(newDescription) .name(newName) .bootable(true) .osType(OsType.LINUX) @@ -107,30 +128,26 @@ public class SnapshotApiLiveTest extends BaseProfitBricksLiveTest { .isRamHotPlug(true) .isRamHotUnPlug(true) .build()); - - Snapshot snapshot = api.snapshotApi().getSnapshot(snapshotId); - - assertNotNull(snapshot); - assertEquals(snapshot.name(), newName); + assertNotNull(requestId); } - @Test(dependsOnMethods = "testCreateSnapshot") + @Test(dependsOnMethods = "testUpdateSnapshot") public void testRollbackSnapshot() { - String result = api.snapshotApi().rollbackSnapshot(Snapshot.Request.createRollbackPayload(snapshotId, storageId)); + assertSnapshotAvailable(createdSnapshotId); + String requestid = api.snapshotApi().rollbackSnapshot( + Snapshot.Request.createRollbackPayload(createdSnapshotId, storage.id())); + assertNotNull(requestid); + } - assertNotNull(result); + @Test(dependsOnMethods = "testRollbackSnapshot", alwaysRun = true) + public void testDeleteSnapshot() { + assertSnapshotAvailable(createdSnapshotId); + boolean result = api.snapshotApi().deleteSnapshot(createdSnapshotId); + assertTrue(result, "Created snapshot wasn't deleted"); } @AfterClass(alwaysRun = true) - public void testDeleteSnapshot() { - boolean result = api.snapshotApi().deleteSnapshot(snapshotId); - - assertTrue(result); - } - - private void initializeWaitPredicate() { - this.snapshotWaitingPredicate = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.SNAPSHOT, ProvisioningState.AVAILABLE), - 2l * 60l, 2l, TimeUnit.SECONDS); + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/StorageApiLiveTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/StorageApiLiveTest.java index 08ffba2882..cdaac80b81 100644 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/StorageApiLiveTest.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/features/StorageApiLiveTest.java @@ -22,44 +22,28 @@ import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import java.util.List; -import java.util.concurrent.TimeUnit; import org.jclouds.profitbricks.BaseProfitBricksLiveTest; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusAware; -import org.jclouds.profitbricks.compute.internal.ProvisioningStatusPollingPredicate; import org.jclouds.profitbricks.domain.DataCenter; -import org.jclouds.profitbricks.domain.ProvisioningState; import org.jclouds.profitbricks.domain.Server; import org.jclouds.profitbricks.domain.Storage; import org.jclouds.rest.InsufficientResourcesException; -import org.jclouds.util.Predicates2; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import org.testng.annotations.BeforeClass; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; - -@Test(groups = "live", testName = "StorageApiLiveTest", singleThreaded = true) +@Test(groups = "live", testName = "StorageApiLiveTest") public class StorageApiLiveTest extends BaseProfitBricksLiveTest { - private Predicate waitUntilAvailable; private DataCenter dataCenter; private Server server; + private String createdStorageId; - @Override - protected void initialize() { - super.initialize(); - List dataCenters = api.dataCenterApi().getAllDataCenters(); - assertFalse(dataCenters.isEmpty(), "Must atleast have 1 datacenter available for storage testing."); - - dataCenter = Iterables.getFirst(dataCenters, null); - if (dataCenter != null) - dataCenter = api.dataCenterApi().getDataCenter(dataCenter.id()); // fetch individual to load more properties - - this.waitUntilAvailable = Predicates2.retry( - new ProvisioningStatusPollingPredicate(api, ProvisioningStatusAware.STORAGE, ProvisioningState.AVAILABLE), - 6l * 60l, 2l, TimeUnit.SECONDS); + @BeforeClass + public void setupTest() { + dataCenter = findOrCreateDataCenter("storageApiLiveTest" + System.currentTimeMillis()); + server = findOrCreateServer(dataCenter); } @Test(expectedExceptions = InsufficientResourcesException.class) @@ -74,6 +58,7 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { @Test public void testCreateStorage() { + assertDataCenterAvailable(dataCenter); String storageId = api.storageApi().createStorage( Storage.Request.creatingBuilder() .dataCenterId(dataCenter.id()) @@ -82,6 +67,8 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { .build()); assertNotNull(storageId); + assertDataCenterAvailable(dataCenter); + createdStorageId = storageId; } @@ -102,14 +89,8 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { } @Test(dependsOnMethods = "testCreateStorage") - public void testWaitUntilAvailable() { - boolean available = waitUntilAvailable.apply(createdStorageId); - - assertTrue(available); - } - - @Test(dependsOnMethods = "testWaitUntilAvailable") public void testUpdateStorage() { + assertDataCenterAvailable(dataCenter); String requestId = api.storageApi().updateStorage( Storage.Request.updatingBuilder() .id(createdStorageId) @@ -118,7 +99,7 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { .build()); assertNotNull(requestId); - waitUntilAvailable.apply(createdStorageId); + assertDataCenterAvailable(dataCenter); Storage storage = api.storageApi().getStorage(createdStorageId); assertEquals(storage.size(), 5f); @@ -127,9 +108,7 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testUpdateStorage") public void testConnectStorage() { - server = Iterables.getFirst(dataCenter.servers(), null); - assertNotNull(server, "No server to attach to."); - + assertDataCenterAvailable(dataCenter); String requestId = api.storageApi().connectStorageToServer( Storage.Request.connectingBuilder() .storageId(createdStorageId) @@ -138,7 +117,7 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { ); assertNotNull(requestId); - waitUntilAvailable.apply(createdStorageId); + assertDataCenterAvailable(dataCenter); Storage storage = api.storageApi().getStorage(createdStorageId); assertTrue(storage.serverIds().contains(server.id())); @@ -146,22 +125,26 @@ public class StorageApiLiveTest extends BaseProfitBricksLiveTest { @Test(dependsOnMethods = "testConnectStorage") public void testDisconnectStorage() { + assertDataCenterAvailable(dataCenter); String requestId = api.storageApi() .disconnectStorageFromServer(createdStorageId, server.id()); assertNotNull(requestId); - waitUntilAvailable.apply(createdStorageId); + assertDataCenterAvailable(dataCenter); Storage storage = api.storageApi().getStorage(createdStorageId); assertFalse(storage.serverIds().contains(server.id())); } - @AfterClass(alwaysRun = true) + @Test(dependsOnMethods = "testDisconnectStorage") public void testDeleteStorage() { - if (createdStorageId != null) { - boolean result = api.storageApi().deleteStorage(createdStorageId); + assertDataCenterAvailable(dataCenter); + boolean result = api.storageApi().deleteStorage(createdStorageId); + assertTrue(result, "Created test storage was not deleted"); + } - assertTrue(result, "Created test storage was not delete."); - } + @AfterClass(alwaysRun = true) + public void cleanUp() { + destroyDataCenter(dataCenter); } } diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandlerTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandlerTest.java new file mode 100644 index 0000000000..9aa18ef52c --- /dev/null +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/loadbalancer/LoadBalancerIdOnlyResponseHandlerTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.profitbricks.http.parser.loadbalancer; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.http.functions.ParseSax; +import org.jclouds.profitbricks.http.parser.BaseResponseHandlerTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "LoadBalancerIdOnlyResponseHandlerTest") +public class LoadBalancerIdOnlyResponseHandlerTest extends BaseResponseHandlerTest { + + @Override + protected ParseSax createParser() { + return factory.create(injector.getInstance(LoadBalancerIdOnlyResponseHandler.class)); + } + + @Test + public void testParseResponseFromCreateLoadBalancer() { + ParseSax parser = createParser(); + + String loadBalancerId = parser.parse(payloadFromResource("/loadbalancer/loadbalancer-create.xml")); + + assertEquals("1234-1234-1234-1234", loadBalancerId); + } +} diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusAware.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandlerTest.java similarity index 50% rename from providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusAware.java rename to providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandlerTest.java index bd00a3d7cd..23b9c4ce43 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/internal/ProvisioningStatusAware.java +++ b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/http/parser/nic/NicIdOnlyResponseHandlerTest.java @@ -14,14 +14,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jclouds.profitbricks.compute.internal; +package org.jclouds.profitbricks.http.parser.nic; -/** - * An enumeration of ProfitBricks domain classes containing a property 'ProvisioningState'. - * - * @see ProvisioningStatusPollingPredicate - */ -public enum ProvisioningStatusAware { +import static org.testng.Assert.assertEquals; + +import org.jclouds.http.functions.ParseSax; +import org.jclouds.profitbricks.http.parser.BaseResponseHandlerTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "NicIdOnlyResponseHandlerTest") +public class NicIdOnlyResponseHandlerTest extends BaseResponseHandlerTest { + + @Override + protected ParseSax createParser() { + return factory.create(injector.getInstance(NicIdOnlyResponseHandler.class)); + } + + @Test + public void testParseResponseFromCreateNic() { + ParseSax parser = createParser(); + String nicId = parser.parse(payloadFromResource("/nic/nic-create.xml")); + assertEquals("nic-id", nicId); + } - DATACENTER, SERVER, STORAGE, NIC, SNAPSHOT; } diff --git a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-create.xml b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-create.xml index 5c1afb0c53..58b97c1c52 100644 --- a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-create.xml +++ b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-create.xml @@ -1,17 +1,13 @@ - - - - - + + + + + + request-id aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee - load-balancer-name - ROUND_ROBIN - 192.168.0.1 - 1 - server-ids - - - - \ No newline at end of file + 10 + 1234-1234-1234-1234 + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-deregister.xml b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-deregister.xml index 86ef94cbde..b149be8d81 100644 --- a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-deregister.xml +++ b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-deregister.xml @@ -1,14 +1,12 @@ - - - - - - 1 - 2 - load-balancer-id - - - - \ No newline at end of file + + + + + + request-id + datacenter-id + 10 + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-register.xml b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-register.xml index a32c65a0fd..c3388839d2 100644 --- a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-register.xml +++ b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-register.xml @@ -1,13 +1,19 @@ - - - - - - 1234 - load-balancer-id - - - - \ No newline at end of file + + + + + + datacenter-id + 4 + load-balancer-id + 1 + + true + balanced-nic-id + server-id + server-name + + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-update.xml b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-update.xml index 87c1f02b0b..0da544db8a 100644 --- a/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-update.xml +++ b/providers/profitbricks/src/test/resources/loadbalancer/loadbalancer-update.xml @@ -1,15 +1,12 @@ - - - - - - aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee - load-balancer-name - ROUND_ROBIN - 192.168.0.1 - - - - \ No newline at end of file + + + + + + request-id + datacenter-id + 10 + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/logback-test.xml b/providers/profitbricks/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..902e5cd401 --- /dev/null +++ b/providers/profitbricks/src/test/resources/logback-test.xml @@ -0,0 +1,74 @@ + + + + + %m%n + + + + + target/test-data/jclouds.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-wire.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-compute.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-ssh.log + + + %d %-5p [%c] [%thread] %m%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/providers/profitbricks/src/test/resources/nic/nic-delete.xml b/providers/profitbricks/src/test/resources/nic/nic-delete.xml index 0b5cc8310e..281bd09d06 100644 --- a/providers/profitbricks/src/test/resources/nic/nic-delete.xml +++ b/providers/profitbricks/src/test/resources/nic/nic-delete.xml @@ -1,9 +1,12 @@ - - - - - - nic-id - - - \ No newline at end of file + + + + + + request-id + datacenter-id + datacenter-version + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/nic/nic-internetaccess.xml b/providers/profitbricks/src/test/resources/nic/nic-internetaccess.xml index 6594894eaf..3468ee4b75 100644 --- a/providers/profitbricks/src/test/resources/nic/nic-internetaccess.xml +++ b/providers/profitbricks/src/test/resources/nic/nic-internetaccess.xml @@ -1,11 +1,12 @@ - - - - - - datacenter-id - 1 - internet-access - - - \ No newline at end of file + + + + + + request-id + datacenter-id + 8 + + + + \ No newline at end of file diff --git a/providers/profitbricks/src/test/resources/nic/nic-update.xml b/providers/profitbricks/src/test/resources/nic/nic-update.xml index 451914cc7b..70ce539de7 100644 --- a/providers/profitbricks/src/test/resources/nic/nic-update.xml +++ b/providers/profitbricks/src/test/resources/nic/nic-update.xml @@ -1,15 +1,12 @@ - - - - - - - nic-id - ip - nic-name - true - 1 - - - - \ No newline at end of file + + + + + + request-id + datacenter-id + 2 + + + + \ No newline at end of file