This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit ee10c427443719526af7bd21f1526b6aea7ecc35
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed May 26 14:50:19 2021 +0100

    expand tests for bundle installation
    
    checks a few more things and cases; adds failing case for gap left by the 
recent change
---
 .../camp/brooklyn/catalog/CatalogScanOsgiTest.java | 112 +++++++++++++++------
 1 file changed, 79 insertions(+), 33 deletions(-)

diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogScanOsgiTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogScanOsgiTest.java
index 00a34b2..8dc74c5 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogScanOsgiTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogScanOsgiTest.java
@@ -18,19 +18,25 @@
  */
 package org.apache.brooklyn.camp.brooklyn.catalog;
 
+import java.util.function.Predicate;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.stream.InputStreamSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import static org.testng.Assert.assertEquals;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.Map;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
 import org.apache.brooklyn.api.mgmt.ManagementContext;
@@ -50,7 +56,6 @@ import org.apache.brooklyn.util.os.Os;
 import org.apache.brooklyn.util.osgi.OsgiTestResources;
 import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.Strings;
-import org.apache.tools.ant.taskdefs.Zip;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
@@ -58,6 +63,8 @@ import com.google.common.collect.Iterables;
 
 public class CatalogScanOsgiTest extends AbstractYamlTest {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(CatalogScanOsgiTest.class);
+
     @Override protected boolean disableOsgi() { return false; }
     
     @Test(groups="Broken")  // AH think not going to support this; see notes 
in BasicBrooklynCatalog.scanAnnotationsInBundle
@@ -158,58 +165,97 @@ public class CatalogScanOsgiTest extends AbstractYamlTest 
{
     }
 
     @Test
-    public void testAddSnapshotBom() throws IOException {
-        installBom(bomSnapshot("aaa"));
-        checkInstalled("aaa");
+    public void testAddOverwritingSnapshotBoms() throws IOException {
+        installBom(bom("aaa", "2-SNAPSHOT", 1), 
ResultCode.INSTALLED_NEW_BUNDLE);
+        checkInstalled("aaa", 1);
+
+        installBom(bom("aaa", "2-SNAPSHOT", 2), 
ResultCode.UPDATED_EXISTING_BUNDLE);
+        checkInstalled("aaa", 2);
+
+        // fixed by ea4518ed7ee018d7af2996ec8da44ac3780c4469 - previously the 
bundle would be ignored
+        installBom(bom("aaa", "2-SNAPSHOT", 1), 
ResultCode.UPDATED_EXISTING_BUNDLE);
+        checkInstalled("aaa", 1);
 
-        installBom(bomSnapshot("aaa"));
-        checkInstalled("aaa");
+        // but this fails with ea4518ed7ee018d7af2996ec8da44ac3780c4469 - now 
the bundle is reinstalled
+        installBom(bom("aaa", "2-SNAPSHOT", 1), 
ResultCode.IGNORING_BUNDLE_AREADY_INSTALLED);
+        checkInstalled("aaa", 1);
+    }
 
+    @Test
+    public void testAddOverwritingSnapshotAndNonSnapshotBoms() throws 
IOException {
         installBom(bomSnapshot("bbb"));
-        checkInstalled("bbb");
+        checkInstalled("bbb", null);
 
-        installBom(bomSnapshot("ccc"));
-        checkInstalled("ccc");
+        // overwrites snapshot
+        installBom(bom("bbb", "1.0", 1));
+        checkInstalled("bbb", 1);
 
-        installBom(bomSnapshot("ddd"));
-        checkInstalled("ddd");
+        // same one can be installed
+        installBom(bom("bbb", "1.0", 1));
+        checkInstalled("bbb", 1);
 
-        installBom(bomSnapshot("bbb"));
-        checkInstalled("bbb");
+        // different one at non-snapshot version cannot be reinstalled
+        Asserts.assertFailsWith(() -> installBom(bom("bbb", "1.0", 2)), e -> {
+            Asserts.expectedFailureContainsIgnoreCase(e, "already installed", 
"bbb");
+            return true;
+        });
 
-        installBom(bomSnapshot("aaa"));
-        checkInstalled("aaa");
+        // new snapshot can be installed but is not preferred
+        installBom(bom("bbb", "3.0-SNAPSHOT", 2));
+        checkInstalled("bbb", 1);
+
+    }
+
+    public void installBom(String bom) {
+        installBom(bom, null);
     }
 
-    public void installBom(String bom) throws IOException {
-        File f = Os.newTempFile(this.getClass(), "zip");
-        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
-        out.putNextEntry(new ZipEntry("catalog.bom"));
-        Streams.copy(new 
ByteArrayInputStream(bom.getBytes(StandardCharsets.UTF_8)), out);
-        out.closeEntry();
-        Streams.closeQuietly(out);
+    public void installBom(String bom, ResultCode expectedResultCode) {
+        try {
+            File f = Os.newTempFile(this.getClass(), "zip");
+            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
+            out.putNextEntry(new ZipEntry("catalog.bom"));
+            Streams.copy(new 
ByteArrayInputStream(bom.getBytes(StandardCharsets.UTF_8)), out);
+            out.closeEntry();
+            Streams.closeQuietly(out);
 
-        ((ManagementContextInternal) 
mgmt()).getOsgiManager().get().install(InputStreamSource.of("test:"+f, 
f)).checkNoError();
+            OsgiBundleInstallationResult result = ((ManagementContextInternal) 
mgmt()).getOsgiManager().get().install(InputStreamSource.of("test:" + f, 
f)).getWithError();
+            LOG.info("Installed "+result.getVersionedName()+": 
"+result.getCode()+" - "+result.getMessage());
+            if (expectedResultCode!=null) 
Asserts.assertEquals(result.getCode(), expectedResultCode);
+        } catch (Exception e) {
+            throw Exceptions.propagate(e);
+        }
     }
 
-    public void checkInstalled(String name) throws IOException {
-        RegisteredType snapshot;
-        snapshot = mgmt().getTypeRegistry().get("snapshot");
-        Asserts.assertNotNull(snapshot);
-        Asserts.assertEquals(snapshot.getDisplayName(), name);
+    public void checkInstalled(String name, Integer fooConfig) throws 
IOException {
+        RegisteredType t = mgmt().getTypeRegistry().get(name);
+        Asserts.assertNotNull(t, "Could not find type '"+name+"'");
+        Asserts.assertEquals(t.getDisplayName(), name);
+        EntitySpec spec = (EntitySpec) mgmt().getTypeRegistry().create(t, 
null, null);
+        Object realFooConfig = 
spec.getConfig().get(ConfigKeys.newConfigKey(Object.class, "foo"));
+        Asserts.assertEquals(realFooConfig, fooConfig);
     }
 
     static String bomSnapshot(String name) {
+        return bom(name, "2.0-SNAPSHOT");
+    }
+    static String bom(String name, String version) {
         return Strings.lines("brooklyn.catalog:",
-            "    version: 2.0-SNAPSHOT",
-            "    bundle: snappy",
+            "    version: "+version,
+            "    bundle: "+name,
             "    items:",
             "    - item:",
-            "        id: snapshot",
+            "        id: "+name,
             "        name: "+name,
             "        displayName: "+name,
             "        type: "+BasicEntity.class.getName());
     }
 
+    static String bom(String name, String version, Integer fooConfig) {
+        return Strings.lines(bom(name, version),
+                "        brooklyn.config:",
+                "          foo: "+fooConfig);
+    }
+
     
 }

Reply via email to