From af771506b97cf996dd7c25053c07e1cc8f9eba99 Mon Sep 17 00:00:00 2001 From: gjoranv Date: Sat, 5 Oct 2019 00:06:55 +0200 Subject: Allow duplicate bsn+version for a given set of bundles. - Set 'org.osgi.framework.bsnversion' to 'managed' - Add bundle collision/event hook to handle duplicates and their lifecycle. - Add unit tests for duplicate bundles. o Add bundle 'l1-dup' which is a duplicate of the existing 'l1', but returns a different value from its implemented class. o Add bundle 'ml-dup' that is exactly the same as 'ml' --- .../core/BundleCollisionHookIntegrationTest.java | 88 ++++++++++++++++++++++ .../jdisc/core/FelixFrameworkIntegrationTest.java | 5 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java (limited to 'jdisc_core_test/integration_test/src') diff --git a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java new file mode 100644 index 00000000000..58c6c1ba8d6 --- /dev/null +++ b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java @@ -0,0 +1,88 @@ +package com.yahoo.jdisc.core; + +import com.yahoo.jdisc.test.TestDriver; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleException; + +import java.util.Collections; + +import static com.yahoo.jdisc.core.FelixFrameworkIntegrationTest.startBundle; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * Note that the '-dup' bundles are necessary, because felix ignores duplicate bundles with the same location. + * + * @author gjoranv + */ +public class BundleCollisionHookIntegrationTest { + + private FelixFramework felix; + + @Before + public void startFelix() throws Exception { + felix = TestDriver.newOsgiFramework(); + felix.start(); + } + + @After + public void stopFelix() throws Exception { + felix.stop(); + } + + @Test + public void duplicate_bundles_cannot_be_installed_in_general() throws Exception { + startBundle(felix, "cert-l1.jar"); + try { + startBundle(felix, "cert-l1-dup.jar"); + fail("Expected exception due to duplicate bundles"); + } catch (BundleException e) { + assertTrue(e.getMessage().contains("Bundle symbolic name and version are not unique")); + } + } + + @Test + public void duplicate_bundles_can_be_installed_if_bsn_version_is_whitelisted() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + felix.allowDuplicateBundles(Collections.singleton(bundleL)); + + Bundle bundleLdup = startBundle(felix, "cert-l1-dup.jar"); + assertNotEquals(bundleL.getBundleId(), bundleLdup.getBundleId()); + } + + @Test + public void duplicates_whitelist_is_updated_when_bundles_are_uninstalled() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + felix.allowDuplicateBundles(Collections.singleton(bundleL)); + + startBundle(felix, "cert-l1-dup.jar"); + + // Remove the original bundle -> will also remove the allowed duplicate + bundleL.uninstall(); + + // Trigger error by trying to re-install the bundle while the duplicate remains + try { + startBundle(felix, "cert-l1.jar"); + fail("Expected exception due to duplicate bundles"); + } catch (BundleException e) { + assertTrue(e.getMessage().contains("Bundle symbolic name and version are not unique")); + } + + } + + @Test + public void duplicates_whitelist_can_be_updated_with_additional_bundles() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + Bundle bundleMl = startBundle(felix, "cert-ml.jar"); + felix.allowDuplicateBundles(Collections.singleton(bundleL)); + felix.allowDuplicateBundles(Collections.singleton(bundleMl)); + + startBundle(felix, "cert-l1-dup.jar"); + startBundle(felix, "cert-ml-dup.jar"); + } + +} diff --git a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/FelixFrameworkIntegrationTest.java b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/FelixFrameworkIntegrationTest.java index e8551b44a3c..9b7453d2d6b 100644 --- a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/FelixFrameworkIntegrationTest.java +++ b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/FelixFrameworkIntegrationTest.java @@ -111,8 +111,11 @@ public class FelixFrameworkIntegrationTest { Bundle bundleL = startBundle(felix, "cert-l1.jar"); Bundle bundleM = startBundle(felix, "cert-ml.jar"); assertEquals(1, callClass(bundleM, "com.yahoo.jdisc.bundle.m.CertificateM")); + + // Switch from l1 to l2 (identical bundles, except for bsn) bundleL.uninstall(); startBundle(felix, "cert-l2.jar"); + felix.refreshPackages(); assertEquals(2, callClass(bundleM, "com.yahoo.jdisc.bundle.m.CertificateM")); felix.stop(); @@ -185,7 +188,7 @@ public class FelixFrameworkIntegrationTest { "com.yahoo.vespa.jdisc_core.cert-q-frag"); } - private static Bundle startBundle(FelixFramework felix, String bundleLocation) throws BundleException { + static Bundle startBundle(FelixFramework felix, String bundleLocation) throws BundleException { List lst = felix.installBundle(bundleLocation); assertEquals(1, lst.size()); felix.startBundles(lst, false); -- cgit v1.2.3