Repository: brooklyn-server
Updated Branches:
  refs/heads/master 377454ed8 -> 6287271b8


Test jclouds TemplateOptions coercions


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

Branch: refs/heads/master
Commit: 610c720f73476b6a63770916f72cc9d8da75c334
Parents: 1b23cb6
Author: Aled Sage <[email protected]>
Authored: Mon May 22 14:08:34 2017 +0100
Committer: Aled Sage <[email protected]>
Committed: Tue May 23 14:45:33 2017 +0100

----------------------------------------------------------------------
 .../JcloudsTemplateOptionsYamlTest.java         | 118 +++++++++++++++++++
 .../JcloudsTemplateOptionsStubbedTest.java      | 102 ++++++++++++++++
 2 files changed, 220 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/610c720f/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsTemplateOptionsYamlTest.java
----------------------------------------------------------------------
diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsTemplateOptionsYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsTemplateOptionsYamlTest.java
new file mode 100644
index 0000000..943266b
--- /dev/null
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsTemplateOptionsYamlTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.apache.brooklyn.camp.brooklyn;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.NoSuchElementException;
+
+import org.apache.brooklyn.api.entity.Application;
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import 
org.apache.brooklyn.camp.brooklyn.JcloudsCustomizerInstantiationYamlDslTest.RecordingLocationCustomizer;
+import org.apache.brooklyn.camp.brooklyn.spi.creation.CampTypePlanTransformer;
+import org.apache.brooklyn.core.entity.trait.Startable;
+import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
+import org.apache.brooklyn.entity.machine.MachineEntity;
+import org.jclouds.compute.options.TemplateOptions;
+import 
org.jclouds.googlecomputeengine.compute.options.GoogleComputeEngineTemplateOptions;
+import org.jclouds.googlecomputeengine.domain.Instance.ServiceAccount;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+
+/**
+ * Tests that jcouds TemplateOptions are constructed properly from yaml 
blueprints.
+ */
+@Test
+public class JcloudsTemplateOptionsYamlTest extends 
AbstractJcloudsStubYamlTest {
+
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        RecordingLocationCustomizer.clear();
+        super.setUp();
+    }
+
+    @AfterMethod(alwaysRun=true)
+    @Override
+    public void tearDown() throws Exception {
+        try {
+            super.tearDown();
+        } finally {
+            RecordingLocationCustomizer.clear();
+        }
+    }
+    
+    @Override
+    protected String getLocationSpec() {
+        return "jclouds:google-compute-engine:us-central1-a";
+    }
+    
+    @Test
+    public void testCoercesStronglyTypedTemplateOption() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "location: " + LOCATION_CATALOG_ID,
+                "services:\n" +
+                "- type: " + MachineEntity.class.getName(),
+                "  brooklyn.config:",
+                "    onbox.base.dir.skipResolution: true",
+                "    sshMonitoring.enabled: false",
+                "    metrics.usage.retrieve: false",
+                "    enabled: true",
+                "    provisioning.properties:",
+                "      templateOptions:",
+                "        serviceAccounts:",
+                "        - email: myemail",
+                "          scopes:",
+                "          - myscope1",
+                "          - myscope2",
+                "      customizer:",
+                "        $brooklyn:object:",
+                "          type: " + 
RecordingLocationCustomizer.class.getName(),
+                "          object.fields:",
+                "            enabled: $brooklyn:config(\"enabled\")");
+
+        EntitySpec<?> spec = 
managementContext.getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT,
 yaml, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class);
+        Entity app = managementContext.getEntityManager().createEntity(spec);
+
+        app.invoke(Startable.START, ImmutableMap.<String, Object>of()).get();
+
+        GoogleComputeEngineTemplateOptions options = 
(GoogleComputeEngineTemplateOptions) findTemplateOptionsInCustomizerArgs();
+        assertEquals(options.serviceAccounts(), ImmutableList.of(
+                ServiceAccount.create("myemail", ImmutableList.of("myscope1", 
"myscope2"))));
+    }
+    
+    private TemplateOptions findTemplateOptionsInCustomizerArgs() {
+        for (RecordingLocationCustomizer.CallParams call : 
RecordingLocationCustomizer.calls) {
+            Optional<?> templateOptions = Iterables.tryFind(call.args, 
Predicates.instanceOf(TemplateOptions.class));
+            if (templateOptions.isPresent()) {
+                return (TemplateOptions) templateOptions.get();
+            }
+        }
+        throw new NoSuchElementException();
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/610c720f/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsTemplateOptionsStubbedTest.java
----------------------------------------------------------------------
diff --git 
a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsTemplateOptionsStubbedTest.java
 
b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsTemplateOptionsStubbedTest.java
new file mode 100644
index 0000000..a65b759
--- /dev/null
+++ 
b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsTemplateOptionsStubbedTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.apache.brooklyn.location.jclouds;
+
+import static org.testng.Assert.assertEquals;
+
+import 
org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.AbstractNodeCreator;
+import 
org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.NodeCreator;
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.jclouds.compute.domain.NodeMetadata.Status;
+import org.jclouds.compute.domain.NodeMetadataBuilder;
+import org.jclouds.compute.domain.Template;
+import org.jclouds.compute.options.TemplateOptions;
+import org.jclouds.domain.LoginCredentials;
+import 
org.jclouds.googlecomputeengine.compute.options.GoogleComputeEngineTemplateOptions;
+import org.jclouds.googlecomputeengine.domain.Instance.ServiceAccount;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+public class JcloudsTemplateOptionsStubbedTest extends 
AbstractJcloudsStubbedUnitTest {
+
+    @SuppressWarnings("unused")
+    private static final Logger log = 
LoggerFactory.getLogger(JcloudsImageChoiceStubbedLiveTest.class);
+    
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        initNodeCreatorAndJcloudsLocation(newNodeCreator(), ImmutableMap.of());
+    }
+    
+    /**
+     * For overriding.
+     */
+    protected String getLocationSpec() {
+        return "jclouds:google-compute-engine:us-central1-a";
+    }
+    
+
+    @Override
+    protected NodeCreator newNodeCreator() {
+        return new AbstractNodeCreator() {
+            @Override protected NodeMetadata newNode(String group, Template 
template) {
+                NodeMetadata result = new NodeMetadataBuilder()
+                        .id("myid")
+                        
.credentials(LoginCredentials.builder().identity("myuser").credential("mypassword").build())
+                        .loginPort(22)
+                        .status(Status.RUNNING)
+                        .publicAddresses(ImmutableList.of("173.194.32.123"))
+                        .privateAddresses(ImmutableList.of("172.168.10.11"))
+                        .build();
+                return result;
+            }
+        };
+    }
+
+    @Test
+    public void testTemplateOption() throws Exception {
+        ServiceAccount serviceAccount = ServiceAccount.create("myemail", 
ImmutableList.of("myscope1"));
+        
+        RecordingLocationCustomizer customizer = new 
RecordingLocationCustomizer();
+        obtainMachine(ImmutableMap.builder()
+                .put(JcloudsLocation.JCLOUDS_LOCATION_CUSTOMIZERS, 
ImmutableList.of(customizer))
+                .put(JcloudsLocation.TEMPLATE_OPTIONS, ImmutableMap.of(
+                        "serviceAccounts", ImmutableList.of(serviceAccount)))
+                .build());
+        
+        GoogleComputeEngineTemplateOptions options = 
(GoogleComputeEngineTemplateOptions) customizer.templateOptions;
+        assertEquals(options.serviceAccounts(), 
ImmutableList.of(serviceAccount));
+    }
+
+    private static class RecordingLocationCustomizer extends 
BasicJcloudsLocationCustomizer {
+        TemplateOptions templateOptions;
+        
+        @Override
+        public void customize(JcloudsLocation location, ComputeService 
computeService, TemplateOptions templateOptions) {
+            this.templateOptions = templateOptions;
+        }
+    }
+}

Reply via email to