brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [5/6] brooklyn-server git commit: jclouds stubbing: support different providers
Date Tue, 23 May 2017 14:23:43 GMT
jclouds stubbing: support different providers


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/57ded182
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/57ded182
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/57ded182

Branch: refs/heads/master
Commit: 57ded1822c2cecfb0be0b55a7dc528cd9d7300d9
Parents: 8a832de
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon May 22 14:06:31 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue May 23 14:45:33 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/AbstractJcloudsStubYamlTest.java   |   9 +-
 .../jclouds/AbstractJcloudsStubbedUnitTest.java |  27 ++-
 .../jclouds/JcloudsStubTemplateBuilder.java     | 204 ++++++++++++++-----
 3 files changed, 185 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/57ded182/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractJcloudsStubYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractJcloudsStubYamlTest.java
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractJcloudsStubYamlTest.java
index 380c132..4854160 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractJcloudsStubYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractJcloudsStubYamlTest.java
@@ -105,13 +105,17 @@ public abstract class AbstractJcloudsStubYamlTest extends AbstractJcloudsStubbed
     }
 
     protected void addStubbedLocationToCatalog() {
+        String locationSpec = getLocationSpec();
+        String provider = getProvider();
+        String regionOrEndpoint = getRegion();
+        
         addCatalogItems(
                 "brooklyn.catalog:",
                 "  id: " + LOCATION_CATALOG_ID,
                 "  version: 1.0.0",
                 "  itemType: location",
                 "  item:",
-                "    type: " + LOCATION_SPEC,
+                "    type: " + locationSpec,
                 "    brooklyn.config:",
                 "      identity: myidentity",
                 "      credential: mycredential",
@@ -124,6 +128,9 @@ public abstract class AbstractJcloudsStubYamlTest extends AbstractJcloudsStubbed
                 "        $brooklyn:object:",
                 "          type: "+JcloudsStubTemplateBuilder.class.getName(),
                 "          factoryMethod.name: create",
+                "          factoryMethod.args:",
+                "          - " + provider,
+                "          - " + regionOrEndpoint,
                 "      " + JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS_PREDICATE.getName()
+ ":",
                 "        $brooklyn:object:",
                 "          type: "+Predicates.class.getName(),

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/57ded182/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java
b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java
index 37231be..83c6b7e 100644
--- a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java
+++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java
@@ -18,8 +18,13 @@
  */
 package org.apache.brooklyn.location.jclouds;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.util.Map;
 
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
 import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.BasicNodeCreator;
@@ -33,7 +38,6 @@ import org.slf4j.LoggerFactory;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 
-import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableMap;
 
@@ -94,7 +98,7 @@ public abstract class AbstractJcloudsStubbedUnitTest extends AbstractJcloudsLive
         this.computeServiceRegistry = new StubbedComputeServiceRegistry(nodeCreator, false);
         final Map<Object, Object> defaults = ImmutableMap.builder()
                 .put(JcloudsLocationConfig.COMPUTE_SERVICE_REGISTRY, computeServiceRegistry)
-                .put(JcloudsLocationConfig.TEMPLATE_BUILDER, JcloudsStubTemplateBuilder.create())
+                .put(JcloudsLocationConfig.TEMPLATE_BUILDER, JcloudsStubTemplateBuilder.create(getProvider(),
getRegion()))
                 .put(JcloudsLocationConfig.ACCESS_IDENTITY, "stub-identity")
                 .put(JcloudsLocationConfig.ACCESS_CREDENTIAL, "stub-credential")
                 .put(SshMachineLocation.SSH_TOOL_CLASS, RecordingSshTool.class.getName())
@@ -125,4 +129,23 @@ public abstract class AbstractJcloudsStubbedUnitTest extends AbstractJcloudsLive
     protected NodeCreator newNodeCreator() {
         return new BasicNodeCreator();
     }
+    
+    protected String getProvider() {
+        LocationSpec<?> spec = mgmt().getLocationRegistry().getLocationSpec(getLocationSpec()).get();
+        return getRequiredConfig(spec, JcloudsLocation.CLOUD_PROVIDER);
+    }
+    
+    protected String getRegion() {
+        LocationSpec<? extends Location> spec = mgmt().getLocationRegistry().getLocationSpec(getLocationSpec()).get();
+        return getRequiredConfig(spec, JcloudsLocation.CLOUD_REGION_ID);
+    }
+    
+    protected String getRequiredConfig(LocationSpec<?> spec, ConfigKey<String>
key) {
+        String result = (String) spec.getConfig().get(key);
+        if (result != null) {
+            return result;
+        }
+        result = (String) spec.getFlags().get(key.getName());
+        return checkNotNull(result, "config "+key.getName());
+    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/57ded182/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsStubTemplateBuilder.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsStubTemplateBuilder.java
b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsStubTemplateBuilder.java
index 0ac5fb2..e12f5cb 100644
--- a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsStubTemplateBuilder.java
+++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsStubTemplateBuilder.java
@@ -18,16 +18,25 @@
  */
 package org.apache.brooklyn.location.jclouds;
 
-import com.google.common.base.Functions;
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
-import com.google.common.base.Objects.ToStringHelper;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import org.jclouds.compute.domain.*;
+import static org.jclouds.ec2.compute.domain.EC2HardwareBuilder.t2_micro;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.inject.Provider;
+
+import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.HardwareBuilder;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.ImageBuilder;
+import org.jclouds.compute.domain.OperatingSystem;
+import org.jclouds.compute.domain.OsFamily;
+import org.jclouds.compute.domain.Processor;
+import org.jclouds.compute.domain.TemplateBuilder;
+import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
 import org.jclouds.compute.options.TemplateOptions;
 import org.jclouds.compute.strategy.GetImageStrategy;
 import org.jclouds.compute.suppliers.ImageCacheSupplier;
@@ -38,47 +47,57 @@ import org.jclouds.domain.LoginCredentials;
 import org.jclouds.ec2.compute.domain.RegionAndName;
 import org.jclouds.ec2.compute.functions.ImagesToRegionAndIdMap;
 import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl;
+import org.jclouds.ec2.compute.options.EC2TemplateOptions;
 import org.jclouds.ec2.domain.RootDeviceType;
 import org.jclouds.ec2.domain.VirtualizationType;
+import org.jclouds.googlecomputeengine.compute.options.GoogleComputeEngineTemplateOptions;
 import org.jclouds.rest.AuthorizationException;
+import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
 
-import javax.inject.Provider;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicReference;
-
-import static org.jclouds.ec2.compute.domain.EC2HardwareBuilder.*;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import com.google.common.base.Functions;
+import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.Predicates;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 
 public class JcloudsStubTemplateBuilder {
-    protected final Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("aws-ec2").description("aws-ec2").build();
-    protected final Location jcloudsDomainLocation = new LocationBuilder().scope(LocationScope.REGION).id("us-east-1").description("us-east-1")
-            .parent(provider).build();
-
-    protected final Hardware HARDWARE_SUPPORTING_BOGUS = t2_micro().id("supporting-bogus")
-            .supportsImageIds(ImmutableSet.of("us-east-1/bogus-image"))
-            .virtualizationType(VirtualizationType.PARAVIRTUAL)
-            .rootDeviceType(RootDeviceType.EBS)
-            .build();
+    protected final Location provider;
+    protected final Location jcloudsDomainLocation;
 
+    protected final String providerName;
+    protected final String regionName;
+    
     public static TemplateBuilder create() {
         return new JcloudsStubTemplateBuilder().createTemplateBuilder();
     }
 
+    public static TemplateBuilder create(String providerName, String regionName) {
+        return new JcloudsStubTemplateBuilder(providerName, regionName).createTemplateBuilder();
+    }
+
+    public JcloudsStubTemplateBuilder() {
+        this("aws-ec2", "us-east-1");
+    }
+
+    public JcloudsStubTemplateBuilder(String providerName, String regionName) {
+        this.providerName = providerName;
+        this.regionName = regionName;
+        this.provider = new LocationBuilder().scope(LocationScope.PROVIDER).id(providerName).description(providerName).build();
+        this.jcloudsDomainLocation = new LocationBuilder().scope(LocationScope.REGION).id(this.regionName).description(this.regionName)
+                .parent(provider).build();
+    }
+
     public TemplateBuilder createTemplateBuilder() {
-        final Supplier<Set<? extends Image>> images = Suppliers.<Set<?
extends Image>> ofInstance(ImmutableSet.of(
-                new ImageBuilder().providerId("ebs-image-provider").name("image")
-                        .id("us-east-1/bogus-image").location(jcloudsDomainLocation)
-                        .userMetadata(ImmutableMap.of("rootDeviceType", RootDeviceType.EBS.value()))
-                        .operatingSystem(new OperatingSystem(OsFamily.UBUNTU, null, "1.0",
VirtualizationType.PARAVIRTUAL.value(), "ubuntu", true))
-                        .description("description").version("1.0").defaultCredentials(LoginCredentials.builder().user("root").build())
-                        .status(Image.Status.AVAILABLE)
-                        .build()));
+        final Supplier<Set<? extends Image>> images = Suppliers.<Set<?
extends Image>> ofInstance(ImmutableSet.of(getImage()));
         ImmutableMap<RegionAndName, Image> imageMap = (ImmutableMap<RegionAndName,
Image>) ImagesToRegionAndIdMap.imagesToMap(images.get());
         Supplier<LoadingCache<RegionAndName, ? extends Image>> imageCache = Suppliers.<LoadingCache<RegionAndName,
? extends Image>> ofInstance(
                 CacheBuilder.newBuilder().<RegionAndName, Image>build(CacheLoader.from(Functions.forMap(imageMap))));
-        JcloudsStubTemplateBuilder jcloudsStubTemplateBuilder = new JcloudsStubTemplateBuilder();
-        return jcloudsStubTemplateBuilder.newTemplateBuilder(images, imageCache);
+        return newTemplateBuilder(images, imageCache);
     }
 
     /**
@@ -87,10 +106,9 @@ public class JcloudsStubTemplateBuilder {
      */
     @SuppressWarnings("unchecked")
     protected TemplateBuilder newTemplateBuilder(Supplier<Set<? extends Image>>
images, Supplier<LoadingCache<RegionAndName, ? extends Image>> imageCache) {
-
         Provider<TemplateOptions> optionsProvider = mock(Provider.class);
         Provider<TemplateBuilder> templateBuilderProvider = mock(Provider.class);
-        TemplateOptions defaultOptions = mock(TemplateOptions.class);
+        TemplateOptions defaultOptions = newTemplateOptions();
         final GetImageStrategy getImageStrategy = mock(GetImageStrategy.class);
 
         when(optionsProvider.get()).thenReturn(defaultOptions);
@@ -98,7 +116,7 @@ public class JcloudsStubTemplateBuilder {
         Supplier<Set<? extends Location>> locations = Suppliers.<Set<?
extends Location>> ofInstance(ImmutableSet
                 .of(jcloudsDomainLocation));
         Supplier<Set<? extends Hardware>> sizes = Suppliers.<Set<? extends
Hardware>> ofInstance(ImmutableSet
-                .of(HARDWARE_SUPPORTING_BOGUS));
+                .of(getHardware()));
 
 
 //        AtomicReference<AuthorizationException> authException = new AtomicReference<AuthorizationException>(new
AuthorizationException());
@@ -111,18 +129,100 @@ public class JcloudsStubTemplateBuilder {
             }
         };
 
-        return new EC2TemplateBuilderImpl(
-                locations,
-                new ImageCacheSupplier(images, 60, authException, imageLoader),
-                sizes,
-                Suppliers.ofInstance(jcloudsDomainLocation),
-                optionsProvider,
-                templateBuilderProvider,
-                imageCache) {
-            @Override
-            protected ToStringHelper string() {
-                return super.string().add("type", "Stubbed-TemplateBuilder");
-            }
-        };
+        switch (providerName) {
+        case "aws-ec2" :
+        case "ec2" :
+            return new EC2TemplateBuilderImpl(
+                    locations,
+                    new ImageCacheSupplier(images, 60, authException, imageLoader),
+                    sizes,
+                    Suppliers.ofInstance(jcloudsDomainLocation),
+                    optionsProvider,
+                    templateBuilderProvider,
+                    imageCache) {
+                @Override
+                protected ToStringHelper string() {
+                    return super.string().add("type", "Stubbed-TemplateBuilder");
+                }
+            };
+        default:
+            return new TemplateBuilderImpl(
+                    locations,
+                    new ImageCacheSupplier(images, 60, authException, imageLoader),
+                    sizes,
+                    Suppliers.ofInstance(jcloudsDomainLocation),
+                    optionsProvider,
+                    templateBuilderProvider) {
+                @Override
+                protected ToStringHelper string() {
+                    return super.string().add("type", "Stubbed-TemplateBuilder");
+                }
+            };
+        }
+    }
+    
+    protected TemplateOptions newTemplateOptions() {
+        switch (providerName) {
+        case "aws-ec2" :
+            return new AWSEC2TemplateOptions();
+        case "ec2" :
+            return new EC2TemplateOptions();
+        case "google-compute-engine" :
+            return new GoogleComputeEngineTemplateOptions();
+            //return mock(GoogleComputeEngineTemplateOptions.class);
+        case "azurecompute" :
+            return new org.jclouds.azurecompute.compute.options.AzureComputeTemplateOptions();
+        case "azurecompute-arm" :
+            return new org.jclouds.azurecompute.arm.compute.options.AzureTemplateOptions();
+        case "softlayer" :
+            return new SoftLayerTemplateOptions();
+        default:
+            throw new UnsupportedOperationException("Unsupported stubbed TemplateOptions
for provider "+providerName);
+        }
+    }
+    
+    protected Image getImage() {
+        switch (providerName) {
+        case "aws-ec2" :
+        case "ec2" :
+            return new ImageBuilder().providerId("ebs-image-provider").name("image")
+                    .id(regionName+"/bogus-image").location(jcloudsDomainLocation)
+                    .userMetadata(ImmutableMap.of("rootDeviceType", RootDeviceType.EBS.value()))
+                    .operatingSystem(new OperatingSystem(OsFamily.UBUNTU, null, "1.0", VirtualizationType.PARAVIRTUAL.value(),
"ubuntu", true))
+                    .description("description").version("1.0").defaultCredentials(LoginCredentials.builder().user("root").build())
+                    .status(Image.Status.AVAILABLE)
+                    .build();
+        case "google-compute-engine" :
+            return new ImageBuilder().providerId("gce-image-provider").name("image")
+                    .id(regionName+"/bogus-image").location(jcloudsDomainLocation)
+                    .operatingSystem(new OperatingSystem(OsFamily.UBUNTU, null, "1.0", VirtualizationType.PARAVIRTUAL.value(),
"ubuntu", true))
+                    .description("description").version("1.0").defaultCredentials(LoginCredentials.builder().user("root").build())
+                    .status(Image.Status.AVAILABLE)
+                    .build();
+        default:
+            throw new UnsupportedOperationException("Unsupported stubbed Image for provider
"+providerName);
+        }
+    }
+    
+    protected Hardware getHardware() {
+        switch (providerName) {
+        case "aws-ec2" :
+        case "ec2" :
+            return t2_micro().id("supporting-bogus")
+                    .supportsImageIds(ImmutableSet.of("us-east-1/bogus-image"))
+                    .virtualizationType(VirtualizationType.PARAVIRTUAL)
+                    .rootDeviceType(RootDeviceType.EBS)
+                    .build();
+        case "google-compute-engine" :
+            return new HardwareBuilder()
+                    .providerId(providerName)
+                    .id("n1-standard-1")
+                    .ram(3750)
+                    .processor(new Processor(1, 1234))
+                    .supportsImage(Predicates.alwaysTrue())
+                    .build();
+        default:
+            throw new UnsupportedOperationException("Unsupported stubbed Hardware for provider
"+providerName);
+        }
     }
 }


Mime
View raw message