diff options
author | Jon Bratseth <bratseth@oath.com> | 2020-07-18 12:42:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-18 12:42:23 +0200 |
commit | 332b14ea43b2f3dbc9c53829120202167c8db514 (patch) | |
tree | 056fed28b1e3d722edbd2332225803bfde9e41aa | |
parent | 4acc25ece51eee3de6eb89283c90f4c47e66323b (diff) | |
parent | 7fca48252b72672a1505c9519193ae376eb562eb (diff) |
Merge pull request #13923 from vespa-engine/gjoranv/unit-test-platform-bundles
Gjoranv/unit test platform bundles
2 files changed, 90 insertions, 6 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/core/config/PlatformBundleLoader.java b/container-core/src/main/java/com/yahoo/container/core/config/PlatformBundleLoader.java index f922bc0cb25..1f788823753 100644 --- a/container-core/src/main/java/com/yahoo/container/core/config/PlatformBundleLoader.java +++ b/container-core/src/main/java/com/yahoo/container/core/config/PlatformBundleLoader.java @@ -10,8 +10,13 @@ import java.util.Set; import java.util.logging.Logger; /** - * Installs all platform bundles, using the {@link DiskBundleInstaller}. + * Used to install the bundles that are added as platform bundles by the config-model. + * * All platform bundles reside on disk, and they are never uninstalled. + * Platform bundles are allowed to pre-install other bundles on disk via the + * X-JDisc-Preinstall-Bundle manifest header. + * + * Attempts to install additional bundles, after the first call, should be a NOP. * * @author gjoranv */ @@ -21,27 +26,41 @@ public class PlatformBundleLoader { private final Osgi osgi; private final DiskBundleInstaller installer; + private Set<Bundle> installedBundles; + private boolean hasLoadedBundles = false; + public PlatformBundleLoader(Osgi osgi) { + this(osgi, new DiskBundleInstaller()); + } + + PlatformBundleLoader(Osgi osgi, DiskBundleInstaller installer) { this.osgi = osgi; - installer = new DiskBundleInstaller(); + this.installer = installer; } public void useBundles(List<FileReference> fileReferences) { - Set<Bundle> installedBundles = install(fileReferences); + if (hasLoadedBundles) { + log.fine(() -> "Platform bundles have already been installed." + + "\nInstalled bundles: " + installedBundles + + "\nGiven files: " + fileReferences); + return; + } + installedBundles = install(fileReferences); BundleStarter.startBundles(installedBundles); + hasLoadedBundles = true; } private Set<Bundle> install(List<FileReference> bundlesToInstall) { - var installedBundles = new LinkedHashSet<Bundle>(); + var allInstalled = new LinkedHashSet<Bundle>(); for (FileReference reference : bundlesToInstall) { try { - installedBundles.addAll(installBundleFromDisk(reference)); + allInstalled.addAll(installBundleFromDisk(reference)); } catch(Exception e) { throw new RuntimeException("Could not install bundle '" + reference + "'", e); } } - return installedBundles; + return allInstalled; } private List<Bundle> installBundleFromDisk(FileReference reference) { diff --git a/container-core/src/test/java/com/yahoo/container/core/config/PlatformBundleLoaderTest.java b/container-core/src/test/java/com/yahoo/container/core/config/PlatformBundleLoaderTest.java new file mode 100644 index 00000000000..338577c6f4c --- /dev/null +++ b/container-core/src/test/java/com/yahoo/container/core/config/PlatformBundleLoaderTest.java @@ -0,0 +1,65 @@ +package com.yahoo.container.core.config; + +import com.yahoo.config.FileReference; +import com.yahoo.osgi.Osgi; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.Bundle; + +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author gjoranv + */ +public class PlatformBundleLoaderTest { + + private static final FileReference BUNDLE_1_REF = new FileReference("bundle-1"); + private static final Bundle BUNDLE_1 = new TestBundle(BUNDLE_1_REF.value()); + private static final FileReference BUNDLE_2_REF = new FileReference("bundle-2"); + private static final Bundle BUNDLE_2 = new TestBundle(BUNDLE_2_REF.value()); + + private PlatformBundleLoader bundleLoader; + private TestOsgi osgi; + + @Before + public void setup() { + osgi = new TestOsgi(testBundles()); + bundleLoader = new PlatformBundleLoader(osgi, new TestBundleInstaller()); + } + + @Test + public void bundles_are_installed_and_started() { + bundleLoader.useBundles(List.of(BUNDLE_1_REF)); + assertEquals(1, osgi.getInstalledBundles().size()); + + // The bundle is installed and started + TestBundle installedBundle = (TestBundle)osgi.getInstalledBundles().get(0); + assertEquals(BUNDLE_1.getSymbolicName(), installedBundle.getSymbolicName()); + assertTrue(installedBundle.started); + } + + @Test + public void bundles_cannot_be_added_by_later_calls() { + bundleLoader.useBundles(List.of(BUNDLE_1_REF)); + bundleLoader.useBundles(List.of(BUNDLE_2_REF)); // Should be a NOP + + assertEquals(1, osgi.getInstalledBundles().size()); + assertEquals(BUNDLE_1.getSymbolicName(), osgi.getInstalledBundles().get(0).getSymbolicName()); + } + + private static Map<String, Bundle> testBundles() { + return Map.of(BUNDLE_1_REF.value(), BUNDLE_1, + BUNDLE_2_REF.value(), BUNDLE_2); + } + + static class TestBundleInstaller extends DiskBundleInstaller { + @Override + public List<Bundle> installBundles(FileReference reference, Osgi osgi) { + return osgi.install(reference.value()); + } + } +} |