Search in sources :

Example 1 with TestDownloadManager

use of org.apache.karaf.features.internal.support.TestDownloadManager in project karaf by apache.

the class DeployerTest method testUpdateSimpleFeature.

@Test
public void testUpdateSimpleFeature() throws Exception {
    IMocksControl c = EasyMock.createControl();
    final String dataDir = "data1";
    TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir);
    RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
    repo.load(true);
    Feature f100 = repo.getFeatures()[0];
    Feature f101 = repo.getFeatures()[1];
    Deployer.DeployCallback callback = c.createMock(Deployer.DeployCallback.class);
    BundleInstallSupport installSupport = c.createMock(BundleInstallSupportImpl.class);
    Deployer deployer = new Deployer(manager, resolver, installSupport, callback);
    final TestBundle bundleA = createTestBundle(1L, Bundle.ACTIVE, dataDir, "a100");
    callback.print(EasyMock.anyString(), EasyMock.anyBoolean());
    EasyMock.expectLastCall().anyTimes();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED);
    EasyMock.expectLastCall();
    installSupport.stopBundle(EasyMock.eq(bundleA), anyInt());
    EasyMock.expectLastCall().andStubAnswer(() -> {
        bundleA.state = Bundle.RESOLVED;
        return null;
    });
    installSupport.updateBundle(EasyMock.eq(bundleA), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            URL loc = getClass().getResource(dataDir + "/" + "a101" + ".mf");
            Manifest man = new Manifest(loc.openStream());
            Hashtable<String, String> headers = new Hashtable<>();
            for (Map.Entry<Object, Object> attr : man.getMainAttributes().entrySet()) {
                headers.put(attr.getKey().toString(), attr.getValue().toString());
            }
            bundleA.update(headers);
            return null;
        }
    });
    installSupport.startBundle(EasyMock.eq(bundleA));
    EasyMock.expectLastCall();
    installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.saveState(EasyMock.anyObject());
    EasyMock.expectLastCall();
    installSupport.installConfigs(f101);
    EasyMock.expectLastCall();
    installSupport.installLibraries(f101);
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED);
    EasyMock.expectLastCall();
    installSupport.resolveBundles(EasyMock.eq(Collections.singleton(bundleA)), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED);
    EasyMock.expectLastCall();
    installSupport.refreshPackages(EasyMock.eq(Collections.singleton(bundleA)));
    EasyMock.expectLastCall();
    callback.callListeners(FeatureEventMatcher.eq(new FeatureEvent(FeatureEvent.EventType.FeatureUninstalled, f100, FeaturesService.ROOT_REGION, false)));
    EasyMock.expectLastCall();
    callback.callListeners(FeatureEventMatcher.eq(new FeatureEvent(FeatureEvent.EventType.FeatureInstalled, f101, FeaturesService.ROOT_REGION, false)));
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED);
    EasyMock.expectLastCall();
    c.replay();
    Deployer.DeploymentState dstate = new Deployer.DeploymentState();
    dstate.state = new State();
    addToMapSet(dstate.state.installedFeatures, ROOT_REGION, f100.getId());
    addToMapSet(dstate.state.managedBundles, ROOT_REGION, 1L);
    dstate.bundles = new HashMap<>();
    dstate.bundles.put(1L, bundleA);
    dstate.bundlesPerRegion = new HashMap<>();
    addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, 1L);
    dstate.features = new HashMap<>();
    dstate.features.put(f100.getId(), f100);
    dstate.features.put(f101.getId(), f101);
    dstate.filtersPerRegion = new HashMap<>();
    dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
    Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
    request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
    request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
    request.globalRepository = null;
    request.options = EnumSet.noneOf(Option.class);
    request.overrides = Collections.emptySet();
    request.stateChanges = Collections.emptyMap();
    request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
    request.requirements = new HashMap<>();
    addToMapSet(request.requirements, ROOT_REGION, f101.getName() + "/" + new VersionRange(f101.getVersion(), true));
    deployer.deploy(dstate, request);
    c.verify();
}
Also used : TestBundle(org.apache.karaf.features.internal.support.TestBundle) VersionRange(org.apache.felix.utils.version.VersionRange) Feature(org.apache.karaf.features.Feature) URL(java.net.URL) IMocksControl(org.easymock.IMocksControl) FeatureEvent(org.apache.karaf.features.FeatureEvent) Hashtable(java.util.Hashtable) Manifest(java.util.jar.Manifest) TestDownloadManager(org.apache.karaf.features.internal.support.TestDownloadManager) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Test(org.junit.Test)

Example 2 with TestDownloadManager

use of org.apache.karaf.features.internal.support.TestDownloadManager in project karaf by apache.

the class DeployerTest method doTestPrereqOnPrereq.

@SuppressWarnings("unchecked")
private void doTestPrereqOnPrereq(int scenario) throws Exception {
    IMocksControl c = EasyMock.createControl();
    String dataDir = "data3";
    TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir);
    RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
    repo.load(true);
    Map<String, Bundle> bundles = new HashMap<>();
    bundles.put("a100", createTestBundle(1, Bundle.ACTIVE, dataDir, "a100"));
    bundles.put("b100", createTestBundle(2, Bundle.ACTIVE, dataDir, "b100"));
    bundles.put("c100", createTestBundle(3, Bundle.ACTIVE, dataDir, "c100"));
    Deployer.DeploymentState dstate = new Deployer.DeploymentState();
    dstate.state = new State();
    dstate.bundles = new HashMap<>();
    dstate.bundlesPerRegion = new HashMap<>();
    dstate.features = new HashMap<>();
    for (Feature f : repo.getFeatures()) {
        dstate.features.put(f.getId(), f);
    }
    dstate.filtersPerRegion = new HashMap<>();
    dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
    Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
    request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
    request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
    request.globalRepository = null;
    request.options = EnumSet.noneOf(Option.class);
    request.overrides = Collections.emptySet();
    request.stateChanges = Collections.emptyMap();
    request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
    MyDeployCallback callback = new MyDeployCallback(dstate);
    BundleInstallSupport installSupport = c.createMock(BundleInstallSupportImpl.class);
    Capture<String> capture = Capture.newInstance();
    installSupport.installBundle(EasyMock.anyString(), EasyMock.capture(capture), anyObject(InputStream.class));
    EasyMock.expectLastCall().andAnswer(() -> bundles.get(capture.getValue())).atLeastOnce();
    installSupport.installConfigs(EasyMock.anyObject());
    EasyMock.expectLastCall().atLeastOnce();
    installSupport.installLibraries(EasyMock.anyObject());
    EasyMock.expectLastCall().atLeastOnce();
    installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
    expectLastCall().atLeastOnce();
    installSupport.resolveBundles(anyObject(Set.class), anyObject(Map.class), anyObject(Map.class));
    expectLastCall().atLeastOnce();
    Deployer deployer = new Deployer(manager, resolver, installSupport, callback);
    c.replay();
    for (int i = 1; i <= 4; i++) {
        request.requirements = new HashMap<>();
        addToMapSet(request.requirements, ROOT_REGION, "demo-" + scenario + "-c");
        Set<String> prereqs = new HashSet<>();
        while (true) {
            try {
                deployer.deploy(callback.dstate, request);
                break;
            } catch (Deployer.PartialDeploymentException e) {
                if (!prereqs.containsAll(e.getMissing())) {
                    prereqs.addAll(e.getMissing());
                } else {
                    throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) MapUtils.addToMapSet(org.apache.karaf.features.internal.util.MapUtils.addToMapSet) HashMap(java.util.HashMap) Feature(org.apache.karaf.features.Feature) IMocksControl(org.easymock.IMocksControl) HashSet(java.util.HashSet) Bundle(org.osgi.framework.Bundle) TestBundle(org.apache.karaf.features.internal.support.TestBundle) InputStream(java.io.InputStream) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) TestDownloadManager(org.apache.karaf.features.internal.support.TestDownloadManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TestDownloadManager

use of org.apache.karaf.features.internal.support.TestDownloadManager in project karaf by apache.

the class DeployerTest method testPrerequisite.

@Test
public void testPrerequisite() throws Exception {
    IMocksControl c = EasyMock.createControl();
    String dataDir = "data2";
    TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir);
    RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
    repo.load(true);
    Feature f1 = repo.getFeatures()[0];
    Feature f2 = repo.getFeatures()[1];
    Bundle serviceBundle1 = createTestBundle(1, Bundle.ACTIVE, dataDir, "a100");
    Bundle serviceBundle2 = createTestBundle(2, Bundle.ACTIVE, dataDir, "b100");
    Deployer.DeployCallback callback = c.createMock(Deployer.DeployCallback.class);
    BundleInstallSupport installSupport = c.createMock(BundleInstallSupportImpl.class);
    Deployer deployer = new Deployer(manager, resolver, installSupport, callback);
    callback.print(EasyMock.anyString(), EasyMock.anyBoolean());
    EasyMock.expectLastCall().anyTimes();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED);
    EasyMock.expectLastCall();
    installSupport.installBundle(EasyMock.eq(ROOT_REGION), EasyMock.eq("a100"), EasyMock.anyObject());
    EasyMock.expectLastCall().andReturn(serviceBundle1);
    installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.saveState(EasyMock.anyObject());
    EasyMock.expectLastCall();
    installSupport.installConfigs(EasyMock.anyObject());
    EasyMock.expectLastCall();
    installSupport.installLibraries(EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED);
    EasyMock.expectLastCall();
    installSupport.resolveBundles(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED);
    EasyMock.expectLastCall();
    callback.callListeners(EasyMock.<FeatureEvent>anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED);
    EasyMock.expectLastCall();
    c.replay();
    Deployer.DeploymentState dstate = new Deployer.DeploymentState();
    dstate.state = new State();
    dstate.bundles = new HashMap<>();
    dstate.bundlesPerRegion = new HashMap<>();
    dstate.features = new HashMap<>();
    dstate.features.put(f1.getId(), f1);
    dstate.features.put(f2.getId(), f2);
    dstate.filtersPerRegion = new HashMap<>();
    dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
    Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
    request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
    request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
    request.globalRepository = null;
    request.options = EnumSet.noneOf(Option.class);
    request.overrides = Collections.emptySet();
    request.stateChanges = Collections.emptyMap();
    request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
    request.requirements = new HashMap<>();
    addToMapSet(request.requirements, ROOT_REGION, f2.getName());
    try {
        deployer.deploy(dstate, request);
        fail("Should have thrown an exception");
    } catch (Deployer.PartialDeploymentException e) {
    // ok
    }
    c.verify();
    c.reset();
    callback.print(EasyMock.anyString(), EasyMock.anyBoolean());
    EasyMock.expectLastCall().anyTimes();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED);
    EasyMock.expectLastCall();
    installSupport.installBundle(EasyMock.eq(ROOT_REGION), EasyMock.eq("b100"), EasyMock.anyObject());
    EasyMock.expectLastCall().andReturn(serviceBundle2);
    installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.saveState(EasyMock.anyObject());
    EasyMock.expectLastCall();
    installSupport.installConfigs(f2);
    EasyMock.expectLastCall();
    installSupport.installLibraries(f2);
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED);
    EasyMock.expectLastCall();
    installSupport.resolveBundles(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED);
    EasyMock.expectLastCall();
    callback.callListeners(EasyMock.<FeatureEvent>anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED);
    EasyMock.expectLastCall();
    EasyMock.replay(callback);
    dstate = new Deployer.DeploymentState();
    dstate.state = new State();
    addToMapSet(dstate.state.installedFeatures, ROOT_REGION, f1.getId());
    dstate.state.stateFeatures.put(ROOT_REGION, Collections.singletonMap(f1.getId(), "Started"));
    addToMapSet(dstate.state.managedBundles, ROOT_REGION, serviceBundle1.getBundleId());
    dstate.bundles = new HashMap<>();
    dstate.bundles.put(serviceBundle1.getBundleId(), serviceBundle1);
    dstate.bundlesPerRegion = new HashMap<>();
    addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, serviceBundle1.getBundleId());
    dstate.features = new HashMap<>();
    dstate.features.put(f1.getId(), f1);
    dstate.features.put(f2.getId(), f2);
    dstate.filtersPerRegion = new HashMap<>();
    dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
    request = new Deployer.DeploymentRequest();
    request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
    request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
    request.globalRepository = null;
    request.options = EnumSet.noneOf(Option.class);
    request.overrides = Collections.emptySet();
    request.stateChanges = Collections.emptyMap();
    request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
    request.requirements = new HashMap<>();
    addToMapSet(request.requirements, ROOT_REGION, f2.getName());
    deployer.deploy(dstate, request);
    EasyMock.verify(callback);
}
Also used : Bundle(org.osgi.framework.Bundle) TestBundle(org.apache.karaf.features.internal.support.TestBundle) Feature(org.apache.karaf.features.Feature) IMocksControl(org.easymock.IMocksControl) TestDownloadManager(org.apache.karaf.features.internal.support.TestDownloadManager) Test(org.junit.Test)

Example 4 with TestDownloadManager

use of org.apache.karaf.features.internal.support.TestDownloadManager in project karaf by apache.

the class DeployerTest method testUpdateServiceBundle.

@Test
public void testUpdateServiceBundle() throws Exception {
    IMocksControl c = EasyMock.createControl();
    String dataDir = "data1";
    TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir);
    RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
    repo.load(true);
    Feature f1 = repo.getFeatures()[0];
    Bundle serviceBundle = createTestBundle(1, Bundle.ACTIVE, dataDir, "a100");
    Deployer.DeployCallback callback = c.createMock(Deployer.DeployCallback.class);
    BundleInstallSupport installSupport = c.createMock(BundleInstallSupportImpl.class);
    Deployer deployer = new Deployer(manager, resolver, installSupport, callback);
    callback.print(EasyMock.anyString(), EasyMock.anyBoolean());
    EasyMock.expectLastCall().anyTimes();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED);
    EasyMock.expectLastCall();
    installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.saveState(EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED);
    EasyMock.expectLastCall();
    installSupport.installConfigs(f1);
    EasyMock.expectLastCall();
    installSupport.installLibraries(f1);
    EasyMock.expectLastCall();
    installSupport.resolveBundles(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED);
    EasyMock.expectLastCall();
    callback.callListeners(EasyMock.<FeatureEvent>anyObject());
    EasyMock.expectLastCall();
    callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED);
    EasyMock.expectLastCall();
    c.replay();
    Deployer.DeploymentState dstate = new Deployer.DeploymentState();
    dstate.state = new State();
    dstate.bundles = new HashMap<>();
    dstate.bundles.put(serviceBundle.getBundleId(), serviceBundle);
    dstate.bundlesPerRegion = new HashMap<>();
    addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, serviceBundle.getBundleId());
    dstate.features = Collections.singletonMap(f1.getId(), f1);
    dstate.filtersPerRegion = new HashMap<>();
    dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
    Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
    request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
    request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
    request.globalRepository = null;
    request.options = EnumSet.noneOf(Option.class);
    request.overrides = Collections.emptySet();
    request.stateChanges = Collections.emptyMap();
    request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
    request.requirements = new HashMap<>();
    addToMapSet(request.requirements, ROOT_REGION, f1.getName());
    deployer.deploy(dstate, request);
    c.verify();
}
Also used : Bundle(org.osgi.framework.Bundle) TestBundle(org.apache.karaf.features.internal.support.TestBundle) Feature(org.apache.karaf.features.Feature) IMocksControl(org.easymock.IMocksControl) TestDownloadManager(org.apache.karaf.features.internal.support.TestDownloadManager) Test(org.junit.Test)

Example 5 with TestDownloadManager

use of org.apache.karaf.features.internal.support.TestDownloadManager in project karaf by apache.

the class FeaturesDependenciesTest method doTestFeatureDependency.

private void doTestFeatureDependency(String[] features, String[] bundles) throws Exception {
    RepositoryImpl repo = new RepositoryImpl(getClass().getResource("data8/features.xml").toURI());
    Map<String, Set<String>> requirements = new HashMap<>();
    for (String feature : features) {
        addToMapSet(requirements, "root", feature);
    }
    Map<String, Set<String>> expected = new HashMap<>();
    for (String bundle : bundles) {
        addToMapSet(expected, "root", bundle);
    }
    SubsystemResolver resolver = new SubsystemResolver(this.resolver, new TestDownloadManager(getClass(), "data8"));
    resolver.prepare(Arrays.asList(repo.getFeatures()), requirements, Collections.emptyMap());
    resolver.resolve(Collections.emptySet(), FeaturesService.DEFAULT_FEATURE_RESOLUTION_RANGE, null, null, null);
    verify(resolver, expected);
}
Also used : Set(java.util.Set) MapUtils.addToMapSet(org.apache.karaf.features.internal.util.MapUtils.addToMapSet) HashMap(java.util.HashMap) TestDownloadManager(org.apache.karaf.features.internal.support.TestDownloadManager) RepositoryImpl(org.apache.karaf.features.internal.service.RepositoryImpl)

Aggregations

TestDownloadManager (org.apache.karaf.features.internal.support.TestDownloadManager)17 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Set (java.util.Set)13 MapUtils.addToMapSet (org.apache.karaf.features.internal.util.MapUtils.addToMapSet)13 RepositoryImpl (org.apache.karaf.features.internal.service.RepositoryImpl)12 Feature (org.apache.karaf.features.Feature)6 TestBundle (org.apache.karaf.features.internal.support.TestBundle)5 IMocksControl (org.easymock.IMocksControl)5 Bundle (org.osgi.framework.Bundle)4 VersionRange (org.apache.felix.utils.version.VersionRange)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 Manifest (java.util.jar.Manifest)1