You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ad...@apache.org on 2014/11/16 22:39:31 UTC
[1/2] jclouds git commit: JCLOUDS-774 cleanup derived location
suppliers and backfill test for inconsistent region and zone mappings.
Repository: jclouds
Updated Branches:
refs/heads/master 90a6bb19f -> 6040f749b
JCLOUDS-774 cleanup derived location suppliers and backfill test for inconsistent region and zone mappings.
Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/d8cb6958
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/d8cb6958
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/d8cb6958
Branch: refs/heads/master
Commit: d8cb6958f0b531435de484279aeca9edb5d12016
Parents: 90a6bb1
Author: Adrian Cole <ad...@gmail.com>
Authored: Sun Nov 16 10:27:50 2014 -0800
Committer: Adrian Cole <ad...@gmail.com>
Committed: Sun Nov 16 13:38:48 2014 -0800
----------------------------------------------------------------------
.../RegionIdsFromRegionIdToURIKeySet.java | 22 +++----
.../ZoneIdToURIFromJoinOnRegionIdToURI.java | 29 ++++----
.../ZoneIdsFromRegionIdToZoneIdsValues.java | 27 +++-----
.../derived/ZoneIdsFromZoneIdToURIKeySet.java | 17 ++---
.../ZoneIdToURIFromJoinOnRegionIdToURITest.java | 69 ++++++++++++++++++++
5 files changed, 110 insertions(+), 54 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds/blob/d8cb6958/core/src/main/java/org/jclouds/location/suppliers/derived/RegionIdsFromRegionIdToURIKeySet.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/location/suppliers/derived/RegionIdsFromRegionIdToURIKeySet.java b/core/src/main/java/org/jclouds/location/suppliers/derived/RegionIdsFromRegionIdToURIKeySet.java
index eb5de4a..75aff2c 100644
--- a/core/src/main/java/org/jclouds/location/suppliers/derived/RegionIdsFromRegionIdToURIKeySet.java
+++ b/core/src/main/java/org/jclouds/location/suppliers/derived/RegionIdsFromRegionIdToURIKeySet.java
@@ -16,39 +16,35 @@
*/
package org.jclouds.location.suppliers.derived;
+import static com.google.common.base.Throwables.propagate;
+
import java.lang.reflect.UndeclaredThrowableException;
import java.net.URI;
import java.util.Map;
import java.util.Set;
-import javax.inject.Singleton;
-
import org.jclouds.location.Region;
import org.jclouds.location.suppliers.RegionIdsSupplier;
import com.google.common.base.Supplier;
-import com.google.common.base.Throwables;
import com.google.inject.Inject;
-/**
- * as opposed to via properties, lets look up regions via api, as they are more likely to change
- */
-@Singleton
-public class RegionIdsFromRegionIdToURIKeySet implements RegionIdsSupplier {
+/** As opposed to via properties, lets look up regions via api, as they are more likely to change. */
+public final class RegionIdsFromRegionIdToURIKeySet implements RegionIdsSupplier {
- private final Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier;
+ private final Supplier<Map<String, Supplier<URI>>> regionIdToURIs;
@Inject
- protected RegionIdsFromRegionIdToURIKeySet(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier) {
- this.regionIdToURISupplier = regionIdToURISupplier;
+ RegionIdsFromRegionIdToURIKeySet(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURIs) {
+ this.regionIdToURIs = regionIdToURIs;
}
@Override
public Set<String> get() {
try {
- return regionIdToURISupplier.get().keySet();
+ return regionIdToURIs.get().keySet();
} catch (UndeclaredThrowableException e) {
- throw Throwables.propagate(e.getCause());
+ throw propagate(e.getCause());
}
}
}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/d8cb6958/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURI.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURI.java b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURI.java
index ee5115e..f630cd5 100644
--- a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURI.java
+++ b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURI.java
@@ -16,13 +16,14 @@
*/
package org.jclouds.location.suppliers.derived;
+import static com.google.common.base.Preconditions.checkState;
+
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;
-import javax.inject.Singleton;
import org.jclouds.location.Region;
import org.jclouds.location.Zone;
@@ -32,28 +33,30 @@ import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
-@Singleton
-public class ZoneIdToURIFromJoinOnRegionIdToURI implements ZoneIdToURISupplier {
+public final class ZoneIdToURIFromJoinOnRegionIdToURI implements ZoneIdToURISupplier {
- private final Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier;
- private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier;
+ private final Supplier<Map<String, Supplier<URI>>> regionIdToURIs;
+ private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds;
@Inject
- protected ZoneIdToURIFromJoinOnRegionIdToURI(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier,
- @Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier) {
- this.regionIdToURISupplier = regionIdToURISupplier;
- this.regionIdToZoneIdsSupplier = regionIdToZoneIdsSupplier;
+ ZoneIdToURIFromJoinOnRegionIdToURI(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURIs,
+ @Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds) {
+ this.regionIdToURIs = regionIdToURIs;
+ this.regionIdToZoneIds = regionIdToZoneIds;
}
@Override
public Map<String, Supplier<URI>> get() {
- Builder<String, Supplier<URI>> builder = ImmutableMap.<String, Supplier<URI>> builder();
- for (Entry<String, Supplier<URI>> regionToURI : regionIdToURISupplier.get().entrySet()) {
- for (String zone : regionIdToZoneIdsSupplier.get().get(regionToURI.getKey()).get()) {
+ Map<String, Supplier<Set<String>>> regionIdToZoneIds = this.regionIdToZoneIds.get();
+ Builder<String, Supplier<URI>> builder = ImmutableMap.builder();
+ for (Entry<String, Supplier<URI>> regionToURI : regionIdToURIs.get().entrySet()) {
+ Supplier<Set<String>> zoneIds = regionIdToZoneIds.get(regionToURI.getKey());
+ checkState(zoneIds != null, "region %s is not in the configured region to zone mappings: %s",
+ regionToURI.getKey(), regionIdToZoneIds);
+ for (String zone : zoneIds.get()) {
builder.put(zone, regionToURI.getValue());
}
}
return builder.build();
}
-
}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/d8cb6958/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromRegionIdToZoneIdsValues.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromRegionIdToZoneIdsValues.java b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromRegionIdToZoneIdsValues.java
index 25764af..cd54ca8 100644
--- a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromRegionIdToZoneIdsValues.java
+++ b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromRegionIdToZoneIdsValues.java
@@ -16,40 +16,33 @@
*/
package org.jclouds.location.suppliers.derived;
-import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
-import javax.inject.Singleton;
import org.jclouds.location.Zone;
import org.jclouds.location.suppliers.ZoneIdsSupplier;
import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-/**
- * as opposed to via properties, lets look up zones via api, as they are more likely to change
- */
-@Singleton
-public class ZoneIdsFromRegionIdToZoneIdsValues implements ZoneIdsSupplier {
+/** As opposed to via properties, lets look up zones via api, as they are more likely to change. */
+public final class ZoneIdsFromRegionIdToZoneIdsValues implements ZoneIdsSupplier {
- private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier;
+ private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds;
@Inject
- protected ZoneIdsFromRegionIdToZoneIdsValues(
- @Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier) {
- this.regionIdToZoneIdsSupplier = regionIdToZoneIdsSupplier;
+ ZoneIdsFromRegionIdToZoneIdsValues(@Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds) {
+ this.regionIdToZoneIds = regionIdToZoneIds;
}
@Override
public Set<String> get() {
- Collection<Supplier<Set<String>>> zones = regionIdToZoneIdsSupplier.get().values();
- return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(zones, Suppliers
- .<Set<String>> supplierFunction())));
+ ImmutableSet.Builder<String> result = ImmutableSet.builder();
+ for (Supplier<Set<String>> zone : regionIdToZoneIds.get().values()) {
+ result.addAll(zone.get());
+ }
+ return result.build();
}
-
}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/d8cb6958/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromZoneIdToURIKeySet.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromZoneIdToURIKeySet.java b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromZoneIdToURIKeySet.java
index a60f4c2..ceaf7fb 100644
--- a/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromZoneIdToURIKeySet.java
+++ b/core/src/main/java/org/jclouds/location/suppliers/derived/ZoneIdsFromZoneIdToURIKeySet.java
@@ -20,29 +20,24 @@ import java.net.URI;
import java.util.Map;
import java.util.Set;
-import javax.inject.Singleton;
-
import org.jclouds.location.Zone;
import org.jclouds.location.suppliers.ZoneIdsSupplier;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
-/**
- * as opposed to via properties, lets look up zones via api, as they are more likely to change
- */
-@Singleton
-public class ZoneIdsFromZoneIdToURIKeySet implements ZoneIdsSupplier {
+/** As opposed to via properties, lets look up zones via api, as they are more likely to change. */
+public final class ZoneIdsFromZoneIdToURIKeySet implements ZoneIdsSupplier {
- private final Supplier<Map<String, Supplier<URI>>> zoneIdToURISupplier;
+ private final Supplier<Map<String, Supplier<URI>>> zoneIdToURIs;
@Inject
- protected ZoneIdsFromZoneIdToURIKeySet(@Zone Supplier<Map<String, Supplier<URI>>> zoneIdToURISupplier) {
- this.zoneIdToURISupplier = zoneIdToURISupplier;
+ ZoneIdsFromZoneIdToURIKeySet(@Zone Supplier<Map<String, Supplier<URI>>> zoneIdToURIs) {
+ this.zoneIdToURIs = zoneIdToURIs;
}
@Override
public Set<String> get() {
- return zoneIdToURISupplier.get().keySet();
+ return zoneIdToURIs.get().keySet();
}
}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/d8cb6958/core/src/test/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURITest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURITest.java b/core/src/test/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURITest.java
new file mode 100644
index 0000000..6407ee7
--- /dev/null
+++ b/core/src/test/java/org/jclouds/location/suppliers/derived/ZoneIdToURIFromJoinOnRegionIdToURITest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.location.suppliers.derived;
+
+import static org.testng.Assert.assertEquals;
+
+import java.net.URI;
+import java.util.Map;
+import java.util.Set;
+
+import org.testng.annotations.Test;
+
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
+@Test
+public class ZoneIdToURIFromJoinOnRegionIdToURITest {
+
+ public void zoneToRegionMappingsValid() {
+ Map<String, Supplier<URI>> regionIdToURIs = Maps.newLinkedHashMap();
+ regionIdToURIs.put("us-east-1", Suppliers.ofInstance(URI.create("ec2.us-east-1.amazonaws.com")));
+ regionIdToURIs.put("eu-central-1", Suppliers.ofInstance(URI.create("ec2.eu-central-1.amazonaws.com")));
+ Map<String, Supplier<Set<String>>> regionIdToZoneIds = Maps.newLinkedHashMap();
+ regionIdToZoneIds.put("us-east-1", supplyZoneIds("us-east-1a", "us-east-1b"));
+ regionIdToZoneIds.put("eu-central-1", supplyZoneIds("eu-central-1a"));
+
+ Map<String, Supplier<URI>> result = new ZoneIdToURIFromJoinOnRegionIdToURI(Suppliers.ofInstance(regionIdToURIs),
+ Suppliers.ofInstance(regionIdToZoneIds)).get();
+
+ assertEquals(result.size(), 3);
+ assertEquals(result.get("us-east-1a"), regionIdToURIs.get("us-east-1"));
+ assertEquals(result.get("us-east-1b"), regionIdToURIs.get("us-east-1"));
+ assertEquals(result.get("eu-central-1a"), regionIdToURIs.get("eu-central-1"));
+ }
+
+ @Test(expectedExceptions = IllegalStateException.class,
+ expectedExceptionsMessageRegExp = "region eu-central-1 is not in the configured region to zone mappings: .*")
+ public void zoneToRegionMappingsInconsistentOnKeys() {
+ Map<String, Supplier<URI>> regionIdToURIs = Maps.newLinkedHashMap();
+ regionIdToURIs.put("us-east-1", Suppliers.ofInstance(URI.create("ec2.us-east-1.amazonaws.com")));
+ regionIdToURIs.put("eu-central-1", Suppliers.ofInstance(URI.create("ec2.eu-central-1.amazonaws.com")));
+ Map<String, Supplier<Set<String>>> regionIdToZoneIds = Maps.newLinkedHashMap();
+ regionIdToZoneIds.put("us-east-1", supplyZoneIds("us-east-1a", "us-east-1b"));
+ // missing regionIdToZoneIds mapping for eu-central-1
+
+ new ZoneIdToURIFromJoinOnRegionIdToURI(Suppliers.ofInstance(regionIdToURIs),
+ Suppliers.ofInstance(regionIdToZoneIds)).get();
+ }
+
+ private static Supplier<Set<String>> supplyZoneIds(String... zoneIds) {
+ return Suppliers.<Set<String>>ofInstance(ImmutableSet.copyOf(zoneIds));
+ }
+}
[2/2] jclouds git commit: Revert
428b2bd2ea2d73354e15ffad52516105b15576cd as this hides inconsistency between
regions and zones.
Posted by ad...@apache.org.
Revert 428b2bd2ea2d73354e15ffad52516105b15576cd as this hides inconsistency between regions and zones.
Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/6040f749
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/6040f749
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/6040f749
Branch: refs/heads/master
Commit: 6040f749bd8acd316024150b140b9af25fb6bb8f
Parents: d8cb695
Author: Adrian Cole <ad...@gmail.com>
Authored: Sun Nov 16 12:26:35 2014 -0800
Committer: Adrian Cole <ad...@gmail.com>
Committed: Sun Nov 16 13:39:02 2014 -0800
----------------------------------------------------------------------
apis/ec2/pom.xml | 12 ++
.../DescribeAvailabilityZonesInRegion.java | 57 ++------
.../ec2/internal/BaseEC2ApiMockTest.java | 133 +++++++++++++++++++
...scribeAvailabilityZonesInRegionMockTest.java | 75 +++++++++++
.../DescribeAvailabilityZonesInRegionTest.java | 122 -----------------
5 files changed, 232 insertions(+), 167 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds/blob/6040f749/apis/ec2/pom.xml
----------------------------------------------------------------------
diff --git a/apis/ec2/pom.xml b/apis/ec2/pom.xml
index 2d39916..4fe3493 100644
--- a/apis/ec2/pom.xml
+++ b/apis/ec2/pom.xml
@@ -91,6 +91,18 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.squareup.okhttp</groupId>
+ <artifactId>mockwebserver</artifactId>
+ <scope>test</scope>
+ <exclusions>
+ <!-- Already provided by jclouds-sshj -->
+ <exclusion>
+ <groupId>org.bouncycastle</groupId>
+ <artifactId>bcprov-jdk15on</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
<build>
<plugins>
http://git-wip-us.apache.org/repos/asf/jclouds/blob/6040f749/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java
----------------------------------------------------------------------
diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java
index 5a2b846..67cb55d 100644
--- a/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java
+++ b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java
@@ -19,74 +19,41 @@ package org.jclouds.ec2.suppliers;
import java.util.Map;
import java.util.Set;
-import javax.annotation.Resource;
import javax.inject.Inject;
-import javax.inject.Singleton;
import org.jclouds.ec2.EC2Api;
import org.jclouds.ec2.domain.AvailabilityZoneInfo;
import org.jclouds.ec2.features.AvailabilityZoneAndRegionApi;
-import org.jclouds.http.HttpResponseException;
import org.jclouds.location.Region;
import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier;
-import org.jclouds.logging.Logger;
-import org.jclouds.util.Suppliers2;
-import com.google.common.base.Function;
import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Maps;
-@Singleton
-public class DescribeAvailabilityZonesInRegion implements RegionIdToZoneIdsSupplier {
- @Resource
- protected Logger logger = Logger.NULL;
-
- private final AvailabilityZoneAndRegionApi client;
+public final class DescribeAvailabilityZonesInRegion implements RegionIdToZoneIdsSupplier {
+ private final EC2Api api;
private final Supplier<Set<String>> regions;
@Inject
- public DescribeAvailabilityZonesInRegion(EC2Api client, @Region Supplier<Set<String>> regions) {
- this.client = client.getAvailabilityZoneAndRegionApi().get();
+ DescribeAvailabilityZonesInRegion(EC2Api api, @Region Supplier<Set<String>> regions) {
+ this.api = api;
this.regions = regions;
}
@Override
public Map<String, Supplier<Set<String>>> get() {
- Builder<String, Set<String>> map = ImmutableMap.builder();
- HttpResponseException exception = null;
- // TODO: this should be parallel
+ AvailabilityZoneAndRegionApi zoneApi = api.getAvailabilityZoneAndRegionApi().get();
+ Builder<String, Supplier<Set<String>>> map = ImmutableMap.builder();
for (String region : regions.get()) {
- try {
- ImmutableSet<String> zones = ImmutableSet.copyOf(Iterables.transform(client
- .describeAvailabilityZonesInRegion(region), new Function<AvailabilityZoneInfo, String>() {
-
- @Override
- public String apply(AvailabilityZoneInfo arg0) {
- return arg0.getZone();
- }
-
- }));
- if (!zones.isEmpty())
- map.put(region, zones);
- } catch (HttpResponseException e) {
- // TODO: this should be in retry handler, not here.
- if (e.getMessage().contains("Unable to tunnel through proxy")) {
- exception = e;
- logger.error(e, "Could not describe availability zones in Region: %s", region);
- } else {
- throw e;
- }
+ ImmutableSet.Builder<String> zoneBuilder = ImmutableSet.builder();
+ for (AvailabilityZoneInfo zone : zoneApi.describeAvailabilityZonesInRegion(region)) {
+ zoneBuilder.add(zone.getZone());
}
+ map.put(region, Suppliers.<Set<String>>ofInstance(zoneBuilder.build()));
}
- ImmutableMap<String, Set<String>> result = map.build();
- if (result.isEmpty() && exception != null) {
- throw exception;
- }
- return Maps.transformValues(result, Suppliers2.<Set<String>> ofInstanceFunction());
+ return map.build();
}
-
}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/6040f749/apis/ec2/src/test/java/org/jclouds/ec2/internal/BaseEC2ApiMockTest.java
----------------------------------------------------------------------
diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/internal/BaseEC2ApiMockTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/internal/BaseEC2ApiMockTest.java
new file mode 100644
index 0000000..7834ba0
--- /dev/null
+++ b/apis/ec2/src/test/java/org/jclouds/ec2/internal/BaseEC2ApiMockTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.ec2.internal;
+
+import static com.google.common.base.Throwables.propagate;
+import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
+import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
+import static javax.ws.rs.core.MediaType.APPLICATION_XML;
+import static org.jclouds.util.Strings2.toStringAndClose;
+import static org.testng.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.jclouds.Constants;
+import org.jclouds.ContextBuilder;
+import org.jclouds.concurrent.config.ExecutorServiceModule;
+import org.jclouds.ec2.EC2Api;
+import org.jclouds.ec2.EC2ApiMetadata;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import com.google.inject.Module;
+import com.squareup.okhttp.mockwebserver.MockResponse;
+import com.squareup.okhttp.mockwebserver.MockWebServer;
+import com.squareup.okhttp.mockwebserver.RecordedRequest;
+
+/**
+ * Tests need to run {@code singleThreaded = true) as otherwise tests will clash on the regionToServers field.
+ * Sharing the regionToServers field means less code to write.
+ */
+public class BaseEC2ApiMockTest {
+ protected static final String DEFAULT_REGION = "us-east-1";
+
+ // Example keys from http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
+ private static final String ACCESS_KEY = "AKIAIOSFODNN7EXAMPLE";
+ private static final String SECRET_KEY = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
+
+ private Map<String, MockWebServer> regionToServers = Maps.newLinkedHashMap();
+
+ protected EC2Api api() {
+ return builder().buildApi(EC2Api.class);
+ }
+
+ protected ContextBuilder builder() {
+ Properties overrides = new Properties();
+ overrides.setProperty(Constants.PROPERTY_MAX_RETRIES, "1");
+ return ContextBuilder.newBuilder(new EC2ApiMetadata()).credentials(ACCESS_KEY, SECRET_KEY)
+ .endpoint("http://localhost:" + regionToServers.get(DEFAULT_REGION).getPort()).overrides(overrides)
+ .modules(modules);
+ }
+
+ private final Set<Module> modules = ImmutableSet.<Module>of(new ExecutorServiceModule(sameThreadExecutor()));
+
+ @BeforeMethod
+ public void start() throws IOException {
+ MockWebServer server = new MockWebServer();
+ server.play();
+ regionToServers.put(DEFAULT_REGION, server);
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void stop() throws IOException {
+ for (MockWebServer server : regionToServers.values()) {
+ server.shutdown();
+ }
+ }
+
+ protected void enqueue(String region, MockResponse response) {
+ regionToServers.get(region).enqueue(response);
+ }
+
+ protected void enqueueRegions(String... regions) throws IOException {
+ StringBuilder describeRegionsResponse = new StringBuilder();
+ describeRegionsResponse.append("<DescribeRegionsResponse>");
+ for (String region : regions) {
+ describeRegionsResponse.append("<item>");
+ describeRegionsResponse.append("<regionName>").append(region).append("</regionName>");
+ if (!regionToServers.containsKey(region)) {
+ MockWebServer server = new MockWebServer();
+ server.play();
+ regionToServers.put(region, server);
+ }
+ String regionEndpoint = "http://localhost:" + regionToServers.get(region).getPort();
+ describeRegionsResponse.append("<regionEndpoint>").append(regionEndpoint).append("</regionEndpoint>");
+ describeRegionsResponse.append("</item>");
+ }
+ describeRegionsResponse.append("</DescribeRegionsResponse>");
+ enqueue(DEFAULT_REGION,
+ new MockResponse().addHeader(CONTENT_TYPE, APPLICATION_XML).setBody(describeRegionsResponse.toString()));
+ }
+
+ protected void enqueueXml(String region, String resource) {
+ enqueue(region,
+ new MockResponse().addHeader(CONTENT_TYPE, APPLICATION_XML).setBody(stringFromResource(resource)));
+ }
+
+ protected String stringFromResource(String resourceName) {
+ try {
+ return toStringAndClose(getClass().getResourceAsStream(resourceName));
+ } catch (IOException e) {
+ throw propagate(e);
+ }
+ }
+
+ /** Stripping out authorization, ensures the following post params were sent. */
+ protected RecordedRequest assertPosted(String region, String postParams) throws InterruptedException {
+ RecordedRequest request = regionToServers.get(region).takeRequest();
+ assertEquals(request.getMethod(), "POST");
+ assertEquals(request.getPath(), "/");
+ assertEquals(new String(request.getBody(), Charsets.UTF_8).replaceAll("&Signature.*", ""), postParams);
+ return request;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/6040f749/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionMockTest.java
----------------------------------------------------------------------
diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionMockTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionMockTest.java
new file mode 100644
index 0000000..1170224
--- /dev/null
+++ b/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionMockTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.ec2.suppliers;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.jclouds.ec2.internal.BaseEC2ApiMockTest;
+import org.jclouds.rest.AuthorizationException;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableSet;
+import com.squareup.okhttp.mockwebserver.MockResponse;
+
+@Test(groups = "unit", testName = "DescribeAvailabilityZonesInRegionMockTest", singleThreaded = true)
+public class DescribeAvailabilityZonesInRegionMockTest extends BaseEC2ApiMockTest {
+
+ public void onlySendsRequestsToConfiguredRegions() throws Exception {
+ enqueueRegions("us-east-1");
+ enqueueXml("us-east-1", "/availabilityZones.xml");
+
+ Map<String, Supplier<Set<String>>> result = new DescribeAvailabilityZonesInRegion(api(),
+ supplyRegionIds("us-east-1")).get();
+
+ assertEquals(result.size(), 1);
+ assertEquals(result.get("us-east-1").get(),
+ ImmutableSet.of("us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"));
+
+ assertPosted("us-east-1", "Action=DescribeRegions");
+ assertPosted("us-east-1", "Action=DescribeAvailabilityZones");
+ }
+
+ public void failsOnAuthorizationErrorToAnyRegion() throws Exception {
+ enqueueRegions("us-east-1", "eu-central-1");
+ enqueueXml("us-east-1", "/availabilityZones.xml");
+ enqueue("eu-central-1", new MockResponse().setResponseCode(401));
+
+ DescribeAvailabilityZonesInRegion supplier = new DescribeAvailabilityZonesInRegion(api(),
+ supplyRegionIds("us-east-1", "eu-central-1"));
+
+ try {
+ supplier.get();
+ fail();
+ } catch (AuthorizationException e){
+
+ }
+
+ assertPosted("us-east-1", "Action=DescribeRegions");
+ assertPosted("us-east-1", "Action=DescribeAvailabilityZones");
+ assertPosted("eu-central-1", "Action=DescribeAvailabilityZones");
+ }
+
+ private static Supplier<Set<String>> supplyRegionIds(String... regionIds) {
+ return Suppliers.<Set<String>>ofInstance(ImmutableSet.copyOf(regionIds));
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds/blob/6040f749/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java
----------------------------------------------------------------------
diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java
deleted file mode 100644
index 8d00242..0000000
--- a/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java
+++ /dev/null
@@ -1,122 +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.ec2.suppliers;
-
-import static org.easymock.EasyMock.expect;
-import static org.easymock.classextension.EasyMock.createControl;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.fail;
-
-import java.util.Map;
-import java.util.Set;
-
-import org.easymock.classextension.IMocksControl;
-import org.jclouds.ec2.EC2Api;
-import org.jclouds.ec2.domain.AvailabilityZoneInfo;
-import org.jclouds.ec2.features.AvailabilityZoneAndRegionApi;
-import org.jclouds.http.HttpCommand;
-import org.jclouds.http.HttpResponseException;
-import org.testng.annotations.Test;
-
-import com.google.common.base.Optional;
-import com.google.common.base.Suppliers;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Maps;
-
-/**
- * A test for {@link DescribeAvailabilityZonesInRegion}.
- */
-public class DescribeAvailabilityZonesInRegionTest {
- @Test
- public void testDescribeAvailabilityZonesInRegion_BestEffort() {
- IMocksControl control = createControl();
- EC2Api client = control.createMock(EC2Api.class);
- AvailabilityZoneAndRegionApi regionClient = control.createMock(AvailabilityZoneAndRegionApi.class);
- AvailabilityZoneInfo info1 = control.createMock(AvailabilityZoneInfo.class);
- AvailabilityZoneInfo info2 = control.createMock(AvailabilityZoneInfo.class);
- HttpCommand command = control.createMock(HttpCommand.class);
- HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...",
- command, null);
-
- expect(client.getAvailabilityZoneAndRegionApi()).andStubReturn((Optional) Optional.of(regionClient));
- expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion1")).andReturn(
- ImmutableSet.of(info1));
- expect(regionClient.describeAvailabilityZonesInRegion("inaccessibleRegion")).andThrow(exception);
- expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion2")).andReturn(
- ImmutableSet.of(info2));
- expect(info1.getZone()).andStubReturn("zone1");
- expect(info2.getZone()).andStubReturn("zone2");
-
- Set<String> regions = ImmutableSet.of("accessibleRegion1", "inaccessibleRegion", "accessibleRegion2");
- control.replay();
-
- Map<String, Set<String>> expectedResult = ImmutableMap.<String, Set<String>> builder().put("accessibleRegion1",
- ImmutableSet.of("zone1")).put("accessibleRegion2", ImmutableSet.of("zone2")).build();
-
- DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers
- .ofInstance(regions));
- assertEquals(Maps.transformValues(regionIdToZoneId.get(), Suppliers.<Set<String>> supplierFunction()),
- expectedResult);
- control.verify();
- }
-
- @Test
- public void testDescribeAvailabilityZonesInRegion_RethrowIfNoneFound() {
- IMocksControl control = createControl();
- EC2Api client = control.createMock(EC2Api.class);
- AvailabilityZoneAndRegionApi regionClient = control.createMock(AvailabilityZoneAndRegionApi.class);
- HttpCommand command = control.createMock(HttpCommand.class);
- HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...",
- command, null);
-
- expect(client.getAvailabilityZoneAndRegionApi()).andStubReturn((Optional) Optional.of(regionClient));
- expect(regionClient.describeAvailabilityZonesInRegion("inaccessibleRegion")).andThrow(exception);
-
- Set<String> regions = ImmutableSet.of("inaccessibleRegion");
- control.replay();
-
- DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers
- .ofInstance(regions));
- try {
- regionIdToZoneId.get();
- fail("expected exception");
- } catch (HttpResponseException e) {
- assertEquals(e, exception);
- }
- control.verify();
- }
-
- @Test
- public void testDescribeAvailabilityZonesInRegion_NoZones() {
- IMocksControl control = createControl();
- EC2Api client = control.createMock(EC2Api.class);
- AvailabilityZoneAndRegionApi regionClient = control.createMock(AvailabilityZoneAndRegionApi.class);
-
- expect(client.getAvailabilityZoneAndRegionApi()).andStubReturn((Optional) Optional.of(regionClient));
- expect(regionClient.describeAvailabilityZonesInRegion("emptyRegion")).andReturn(
- ImmutableSet.<AvailabilityZoneInfo> of());
-
- Set<String> regions = ImmutableSet.of("emptyRegion");
- control.replay();
-
- DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers
- .ofInstance(regions));
- assertEquals(regionIdToZoneId.get(), ImmutableMap.<String, String> of());
- control.verify();
- }
-}