Add more tests to ConfigParametersYamlTests

Including for BROOKLYN-328 and BROOKLYN-329


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

Branch: refs/heads/master
Commit: bbe8e7a3d856a6291548a2b974222a9bd25c159a
Parents: ea87091
Author: Aled Sage <[email protected]>
Authored: Wed Aug 10 21:13:31 2016 +0100
Committer: Aled Sage <[email protected]>
Committed: Thu Aug 11 20:37:25 2016 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/ConfigParametersYamlTest.java | 263 ++++++++++++++++++-
 1 file changed, 261 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bbe8e7a3/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
----------------------------------------------------------------------
diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
index 3350686..4f9ead8 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
@@ -21,14 +21,20 @@ package org.apache.brooklyn.camp.brooklyn;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
-import java.io.StringReader;
 import java.util.Map;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
+import org.apache.brooklyn.test.Asserts;
+import org.apache.brooklyn.util.core.internal.ssh.ExecCmdAsserts;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.api.client.repackaged.com.google.common.base.Joiner;
@@ -39,6 +45,13 @@ public class ConfigParametersYamlTest extends 
AbstractYamlTest {
     @SuppressWarnings("unused")
     private static final Logger LOG = 
LoggerFactory.getLogger(ConfigParametersYamlTest.class);
 
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        RecordingSshTool.clear();
+    }
+    
     @Test
     public void testConfigParametersListedInType() throws Exception {
         addCatalogItems(
@@ -47,7 +60,7 @@ public class ConfigParametersYamlTest extends 
AbstractYamlTest {
                 "  items:",
                 "  - id: entity-with-keys",
                 "    item:",
-                "      type: org.apache.brooklyn.core.test.entity.TestEntity",
+                "      type: "+TestEntity.class.getName(),
                 "      brooklyn.parameters:",
                 "      - name: testConfigParametersListedInType.mykey",
                 "        description: myDescription",
@@ -73,4 +86,250 @@ public class ConfigParametersYamlTest extends 
AbstractYamlTest {
         // Check get default value
         assertEquals(entity.config().get(key), ImmutableMap.of("myDefaultKey", 
"myDefaultVal"));
     }
+    
+    @Test
+    public void testConfigParameterDefault() throws Exception {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+TestEntity.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: my.param.key",
+                "        type: string",
+                "        default: myDefaultVal",
+                "      brooklyn.config:",
+                "        my.other.key: $brooklyn:config(\"my.param.key\")");
+
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: sub-entity",
+                "    item:",
+                "      type: entity-with-keys");
+        
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: template",
+                "  items:",
+                "  - id: wrapper-entity",
+                "    item:",
+                "      services:",
+                "      - type: entity-with-keys");
+
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: entity-with-keys");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myDefaultVal");
+        }
+        
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: entity-with-keys",
+                    "  brooklyn.config:",
+                    "    my.param.key: myOverrideVal");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myOverrideVal");
+        }
+        
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: sub-entity");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myDefaultVal");
+        }
+        
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: sub-entity",
+                    "  brooklyn.config:",
+                    "    my.param.key: myOverrideVal");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myOverrideVal");
+        }
+        
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: wrapper-entity");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myDefaultVal");
+        }
+        
+        {
+            String yaml = Joiner.on("\n").join(
+                    "services:",
+                    "- type: wrapper-entity",
+                    "  brooklyn.config:",
+                    "    my.param.key: myOverrideVal");
+            Entity app = createStartWaitAndLogApplication(yaml);
+            TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+            
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myOverrideVal");
+        }
+    }
+    
+    @Test
+    public void testSubTypeUsesDefaultsFromSuper() throws Exception {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+TestEntity.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: my.param.key",
+                "        type: string",
+                "        default: myDefaultVal",
+                "      brooklyn.config:",
+                "        my.other.key: $brooklyn:config(\"my.param.key\")");
+
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: sub-entity",
+                "    item:",
+                "      type: entity-with-keys",
+                "      brooklyn.config:",
+                "        my.sub.key: $brooklyn:config(\"my.param.key\")");
+        
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- type: sub-entity");
+        Entity app = createStartWaitAndLogApplication(yaml);
+        TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+        
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myDefaultVal");
+        
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.sub.key")), 
"myDefaultVal");
+    }
+
+    // TODO: fails; it presumably gets the config key defined in java, rather 
than the brooklyn.parameters key
+    // See https://issues.apache.org/jira/browse/BROOKLYN-328
+    @Test(groups={"WIP", "Broken"})
+    public void testConfigParameterOverridingJavaConfig() throws Exception {
+        String confName = TestEntity.CONF_OBJECT.getName();
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+TestEntity.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: "+confName,
+                "        type: java.lang.Object",
+                "        default: myDefaultObj",
+                "      brooklyn.config:",
+                "        my.other.obj: $brooklyn:config(\""+confName+"\")");
+
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- type: entity-with-keys");
+        Entity app = createStartWaitAndLogApplication(yaml);
+        TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+        
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.obj")),
 "myDefaultObj");
+    }
+    
+    // TODO: fails; times out getting config. Problem is that scopeRoot() 
resolves to entity-with-keys!
+    // Presumably because it is resolved from inside the entity-with-keys?
+    // https://issues.apache.org/jira/browse/BROOKLYN-329
+    @Test(groups={"WIP", "Broken"})
+    public void testConfigParameterPassedFromOuterConfigParameter() throws 
Exception {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+TestEntity.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: my.param.key",
+                "        type: string",
+                "        default: myDefaultVal",
+                "      brooklyn.config:",
+                "        my.other.key: $brooklyn:config(\"my.param.key\")");
+
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: wrapper-entity",
+                "    item:",
+                "      brooklyn.parameters:",
+                "      - name: my.param.key",
+                "        type: string",
+                "        default: myDefaultValInOuter",
+                "      type: entity-with-keys",
+                "      brooklyn.config:",
+                "        my.param.key: 
$brooklyn:scopeRoot().config(\"my.param.key\")");
+        
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- type: wrapper-entity");
+        
+        Entity app = createStartWaitAndLogApplication(yaml);
+        final TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+        Asserts.assertReturnsEventually(new Runnable() {
+            public void run() {
+                
assertEquals(entity.config().get(ConfigKeys.newStringConfigKey("my.other.key")),
 "myDefaultValInOuter");
+            }},
+            Asserts.DEFAULT_LONG_TIMEOUT);
+    }
+    
+    @Test
+    public void testSubTypeUsesDefaultsFromSuperInConfigMerging() throws 
Exception {
+        RecordingSshTool.setCustomResponse(".*myCommand.*", new 
RecordingSshTool.CustomResponse(0, "myResponse", null));
+        
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+VanillaSoftwareProcess.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: my.param.key",
+                "        type: string",
+                "        default: myDefaultVal",
+                "      brooklyn.config:",
+                "        shell.env:",
+                "          KEY_IN_SUPER: $brooklyn:config(\"my.param.key\")",
+                "        launch.command: myLaunchCmd");
+
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: sub-entity",
+                "    item:",
+                "      type: entity-with-keys",
+                "      brooklyn.config:",
+                "        shell.env:",
+                "          KEY_IN_SUB: myBoringVal");
+        
+        String yaml = Joiner.on("\n").join(
+                "location:",
+                "  localhost:",
+                "    sshToolClass: "+RecordingSshTool.class.getName(),
+                "services:",
+                "- type: sub-entity");
+        Entity app = createStartWaitAndLogApplication(yaml);
+        
+        ExecCmd cmd = 
ExecCmdAsserts.findExecContaining(RecordingSshTool.getExecCmds(), 
"myLaunchCmd");
+        assertEquals(cmd.env.get("KEY_IN_SUPER"), "myDefaultVal", "cmd="+cmd);
+        assertEquals(cmd.env.get("KEY_IN_SUB"), "myBoringVal", "cmd="+cmd);
+    }
 }

Reply via email to