Search in sources :

Example 81 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImpl method checkStandaloneChild.

/**
 * Check if this is installation in @{link {@link io.fabric8.patch.management.EnvType#STANDALONE_CHILD}}
 * - in this case the patch has to be installed in root first
 * @param patches
 */
private void checkStandaloneChild(Collection<Patch> patches) {
    if (patchManagement.isStandaloneChild()) {
        for (Patch patch : patches) {
            if (patch.getResult() == null) {
                throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
            } else {
                List<String> bases = patch.getResult().getKarafBases();
                boolean isInstalledInRoot = false;
                for (String base : bases) {
                    String[] coords = base.split("\\s*\\|\\s*");
                    if (coords.length == 2 && coords[1].trim().equals(System.getProperty("karaf.home"))) {
                        isInstalledInRoot = true;
                    }
                }
                if (!isInstalledInRoot) {
                    throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
                }
            }
        }
    }
}
Also used : PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch)

Example 82 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImplTest method createMockServiceImpl.

/*
     * Create a mock patch service implementation with a provided patch storage location
     */
private ServiceImpl createMockServiceImpl(File patches) throws IOException {
    ComponentContext componentContext = createMock(ComponentContext.class);
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(bundle.getVersion()).andReturn(new Version(1, 2, 0)).anyTimes();
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(bundleContext.getBundle()).andReturn(bundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(patches.toString()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(patches.getParent() + "/data").anyTimes();
    try {
        expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
        expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
        expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
        expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
        expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
        expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    } catch (GitAPIException | IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = new GitPatchManagementServiceImpl(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    return service;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) ComponentContext(org.osgi.service.component.ComponentContext) Version(org.osgi.framework.Version) Bundle(org.osgi.framework.Bundle) PatchManagement(io.fabric8.patch.management.PatchManagement) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext)

Example 83 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImplTest method testPatchWithVersionRanges.

@Test
public void testPatchWithVersionRanges() throws Exception {
    ComponentContext componentContext = createMock(ComponentContext.class);
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    Bundle bundle2 = createMock(Bundle.class);
    FrameworkWiring wiring = createMock(FrameworkWiring.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
    expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
    expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
    expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = mockManagementService(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    Iterable<Patch> patches = service.download(patch140.toURI().toURL());
    assertNotNull(patches);
    Iterator<Patch> it = patches.iterator();
    assertTrue(it.hasNext());
    Patch patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.4.0", patch.getPatchData().getId());
    assertNotNull(patch.getPatchData().getBundles());
    assertEquals(1, patch.getPatchData().getBundles().size());
    Iterator<String> itb = patch.getPatchData().getBundles().iterator();
    assertEquals("mvn:foo/my-bsn/1.4.0", itb.next());
    assertNull(patch.getResult());
    verify(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    // 
    // Simulate the patch
    // 
    reset(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
    expect(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).andReturn(null);
    expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
    expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
    expect(bundle.getLocation()).andReturn("location").anyTimes();
    expect(bundle.getBundleId()).andReturn(123L);
    BundleStartLevel bsl = createMock(BundleStartLevel.class);
    expect(bsl.getStartLevel()).andReturn(30).anyTimes();
    expect(bundle.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bundle.getState()).andReturn(1);
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, bsl);
    PatchResult result = service.install(patch, true);
    assertNotNull(result);
    assertNull(patch.getResult());
    assertEquals(1, result.getBundleUpdates().size());
    assertTrue(result.isSimulation());
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Version(org.osgi.framework.Version) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) Patch(io.fabric8.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 84 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImplTest method testCheckPrerequisitesMultiplePatches.

@Test
public void testCheckPrerequisitesMultiplePatches() throws IOException {
    ServiceImpl service = createMockServiceImpl(getDirectoryForResource("prereq/patch1.patch"));
    Collection<Patch> patches = new LinkedList<Patch>();
    patches.add(service.getPatch("patch3"));
    // this should not throw a PatchException
    service.checkPrerequisites(patches);
    patches.add(service.getPatch("patch2"));
    try {
        service.checkPrerequisites(patches);
        fail("Should not pass check if one of the patches is missing a requirement");
    } catch (PatchException e) {
    // graciously do nothing, this is OK
    }
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 85 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImplTest method testCheckPrerequisitesSatisfied.

@Test
public void testCheckPrerequisitesSatisfied() throws IOException {
    ServiceImpl service = createMockServiceImpl(getDirectoryForResource("prereq/patch3.patch"));
    Patch patch = service.getPatch("patch3");
    assertNotNull(patch);
    // this should not throw a PatchException
    service.checkPrerequisites(patch);
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Patch(io.fabric8.patch.management.Patch) Test(org.junit.Test)

Aggregations

File (java.io.File)54 Test (org.junit.Test)43 Git (org.eclipse.jgit.api.Git)35 PatchException (io.fabric8.patch.management.PatchException)34 Patch (io.fabric8.patch.management.Patch)30 IOException (java.io.IOException)28 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)27 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)26 HashMap (java.util.HashMap)18 RevCommit (org.eclipse.jgit.revwalk.RevCommit)18 LinkedList (java.util.LinkedList)17 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)17 ObjectId (org.eclipse.jgit.lib.ObjectId)17 Bundle (org.osgi.framework.Bundle)17 Version (org.osgi.framework.Version)15 PatchResult (io.fabric8.patch.management.PatchResult)13 BundleUpdate (io.fabric8.patch.management.BundleUpdate)11 PatchData (io.fabric8.patch.management.PatchData)11 ArrayList (java.util.ArrayList)11