Search in sources :

Example 16 with GitPatchManagementServiceImpl

use of org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl in project fuse-karaf by jboss-fuse.

the class PatchServiceImplTest method bundleUpdatesInPatch.

@Test
@SuppressWarnings("unchecked")
public void bundleUpdatesInPatch() throws Exception {
    BundleContext context = mock(BundleContext.class);
    Bundle bundle0 = mock(Bundle.class);
    when(bundle0.getBundleContext()).thenReturn(context);
    when(context.getProperty("karaf.home")).thenReturn("target/bundleUpdatesInPatch");
    when(context.getProperty("karaf.base")).thenReturn("target/bundleUpdatesInPatch");
    when(context.getProperty("karaf.data")).thenReturn("target/bundleUpdatesInPatch/data");
    when(context.getProperty("karaf.etc")).thenReturn("target/bundleUpdatesInPatch/etc");
    when(context.getProperty("karaf.name")).thenReturn("root");
    when(context.getProperty("karaf.instances")).thenReturn("instances");
    when(context.getProperty("karaf.default.repository")).thenReturn("system");
    when(context.getProperty("fuse.patch.location")).thenReturn(null);
    when(context.getBundle(0)).thenReturn(bundle0);
    PatchServiceImpl service = new PatchServiceImpl();
    Method m = service.getClass().getDeclaredMethod("bundleUpdatesInPatch", Patch.class, Bundle[].class, Map.class, PatchServiceImpl.BundleVersionHistory.class, Map.class, PatchKind.class, Map.class, List.class);
    m.setAccessible(true);
    Field f = service.getClass().getDeclaredField("helper");
    f.setAccessible(true);
    f.set(service, new OSGiPatchHelper(new File("target/bundleUpdatesInPatch"), context) {

        @Override
        public String[] getBundleIdentity(String url) throws IOException {
            Artifact a = Utils.mvnurlToArtifact(url, false);
            return a == null ? null : new String[] { a.getArtifactId(), a.getVersion() };
        }
    });
    PatchData pd = new PatchData("patch-x");
    // for these two, bundle.getLocation() will return matching location
    pd.getBundles().add("mvn:io.fabric8/pax-romana/1.0.1");
    pd.getBundles().add("mvn:io.fabric8/pax-hellenica/1.0.1/jar");
    // for these two, bundle.getLocation() will return non-matching location
    pd.getBundles().add("mvn:io.fabric8/pax-bohemia/1.0.1");
    pd.getBundles().add("mvn:io.fabric8/pax-pomerania/1.0.1/jar");
    // for these two, bundle.getLocation() will return matching location
    pd.getBundles().add("mvn:io.fabric8/pax-avaria/1.0.1/jar/uber");
    pd.getBundles().add("mvn:io.fabric8/pax-mazovia/1.0.1//uber");
    // for these two, bundle.getLocation() will return non-matching location
    pd.getBundles().add("mvn:io.fabric8/pax-novgorod/1.0.1/jar/uber");
    pd.getBundles().add("mvn:io.fabric8/pax-castile/1.0.1//uber");
    f = pd.getClass().getDeclaredField("versionRanges");
    f.setAccessible(true);
    f.set(pd, new HashMap<>());
    Patch patch = new Patch(pd, null);
    Bundle[] bundles = new Bundle[8];
    bundles[0] = bundle("mvn:io.fabric8/pax-romana/1.0.0");
    bundles[1] = bundle("mvn:io.fabric8/pax-hellenica/1.0.0/jar");
    bundles[2] = bundle("mvn:io.fabric8/pax-bohemia/1.0.0/jar");
    bundles[3] = bundle("mvn:io.fabric8/pax-pomerania/1.0.0");
    bundles[4] = bundle("mvn:io.fabric8/pax-avaria/1.0.0/jar/uber");
    bundles[5] = bundle("mvn:io.fabric8/pax-mazovia/1.0.0//uber");
    bundles[6] = bundle("mvn:io.fabric8/pax-novgorod/1.0.0//uber");
    bundles[7] = bundle("mvn:io.fabric8/pax-castile/1.0.0/jar/uber");
    Object _list = m.invoke(service, patch, bundles, new HashMap<>(), new PatchServiceImpl.BundleVersionHistory(new HashMap<String, Patch>()), new HashMap<>(), PatchKind.NON_ROLLUP, new HashMap<>(), null);
    List<BundleUpdate> list = (List<BundleUpdate>) _list;
    assertThat(list.size(), equalTo(8));
    assertThat(list.get(0).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-romana/1.0.0"));
    assertThat(list.get(1).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-hellenica/1.0.0/jar"));
    assertThat(list.get(2).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-bohemia/1.0.0/jar"));
    assertThat(list.get(3).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-pomerania/1.0.0"));
    assertThat(list.get(4).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-avaria/1.0.0/jar/uber"));
    assertThat(list.get(5).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-mazovia/1.0.0//uber"));
    assertThat(list.get(6).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-novgorod/1.0.0//uber"));
    assertThat(list.get(7).getPreviousLocation(), equalTo("mvn:io.fabric8/pax-castile/1.0.0/jar/uber"));
    assertThat(list.get(0).getNewLocation(), equalTo("mvn:io.fabric8/pax-romana/1.0.1"));
    assertThat(list.get(1).getNewLocation(), equalTo("mvn:io.fabric8/pax-hellenica/1.0.1/jar"));
    assertThat(list.get(2).getNewLocation(), equalTo("mvn:io.fabric8/pax-bohemia/1.0.1"));
    assertThat(list.get(3).getNewLocation(), equalTo("mvn:io.fabric8/pax-pomerania/1.0.1/jar"));
    assertThat(list.get(4).getNewLocation(), equalTo("mvn:io.fabric8/pax-avaria/1.0.1/jar/uber"));
    assertThat(list.get(5).getNewLocation(), equalTo("mvn:io.fabric8/pax-mazovia/1.0.1//uber"));
    assertThat(list.get(6).getNewLocation(), equalTo("mvn:io.fabric8/pax-novgorod/1.0.1/jar/uber"));
    assertThat(list.get(7).getNewLocation(), equalTo("mvn:io.fabric8/pax-castile/1.0.1//uber"));
    // ---
    Repository repository = mock(Repository.class);
    File tmp = new File("target/bundleUpdatesInPatch/" + UUID.randomUUID().toString());
    tmp.mkdirs();
    File startupProperties = new File(tmp, "etc/startup.properties");
    FileUtils.copyFile(new File("src/test/resources/uber-startup.properties"), startupProperties);
    when(repository.getWorkTree()).thenReturn(tmp);
    Git fork = mock(Git.class);
    when(fork.getRepository()).thenReturn(repository);
    GitPatchManagementServiceImpl gitPatchManagementService = new GitPatchManagementServiceImpl(context);
    m = gitPatchManagementService.getClass().getDeclaredMethod("updateFileReferences", Git.class, PatchData.class, List.class);
    m.setAccessible(true);
    m.invoke(gitPatchManagementService, fork, pd, list);
    try (FileReader reader = new FileReader(startupProperties)) {
        Properties startup = new Properties();
        startup.load(reader);
        assertTrue(startup.containsKey("io/fabric8/pax-romana/1.0.1/pax-romana-1.0.1.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-hellenica/1.0.1/pax-hellenica-1.0.1.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-bohemia/1.0.1/pax-bohemia-1.0.1.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-pomerania/1.0.1/pax-pomerania-1.0.1.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-avaria/1.0.1/pax-avaria-1.0.1-uber.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-mazovia/1.0.1/pax-mazovia-1.0.1-uber.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-novgorod/1.0.1/pax-novgorod-1.0.1-uber.jar"));
        assertTrue(startup.containsKey("io/fabric8/pax-castile/1.0.1/pax-castile-1.0.1-uber.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-romana/1.0.0/pax-romana-1.0.0.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-hellenica/1.0.0/pax-hellenica-1.0.0.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-bohemia/1.0.0/pax-bohemia-1.0.0.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-pomerania/1.0.0/pax-pomerania-1.0.0.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-avaria/1.0.0/pax-avaria-1.0.0-uber.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-mazovia/1.0.0/pax-mazovia-1.0.0-uber.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-novgorod/1.0.0/pax-novgorod-1.0.0-uber.jar"));
        assertFalse(startup.containsKey("io/fabric8/pax-castile/1.0.0/pax-castile-1.0.0-uber.jar"));
    }
}
Also used : HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Properties(java.util.Properties) Field(java.lang.reflect.Field) List(java.util.List) LinkedList(java.util.LinkedList) FileReader(java.io.FileReader) BundleUpdate(org.jboss.fuse.patch.management.BundleUpdate) PatchData(org.jboss.fuse.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) Method(java.lang.reflect.Method) IOException(java.io.IOException) Artifact(org.jboss.fuse.patch.management.Artifact) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) JarFile(java.util.jar.JarFile) File(java.io.File) Patch(org.jboss.fuse.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 17 with GitPatchManagementServiceImpl

use of org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl in project fuse-karaf by jboss-fuse.

the class PatchServiceImplTest method testPatch.

@Test
public void testPatch() throws Exception {
    ComponentContext componentContext = mock(ComponentContext.class);
    BundleContext bundleContext = mock(BundleContext.class);
    Bundle sysBundle = mock(Bundle.class);
    BundleContext sysBundleContext = mock(BundleContext.class);
    Bundle bundle = mock(Bundle.class);
    Bundle bundle2 = mock(Bundle.class);
    FrameworkWiring wiring = mock(FrameworkWiring.class);
    GitPatchRepository repository = mock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    when(bundleContext.getBundle(0)).thenReturn(sysBundle);
    when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
    when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
    when(repository.getManagedPatch(anyString())).thenReturn(null);
    when(repository.findOrCreateMainGitRepository()).thenReturn(null);
    when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
    when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.getCanonicalPath());
    when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
    when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
    when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
    when(sysBundleContext.getProperty("karaf.data")).thenReturn(karaf.getCanonicalPath() + "/data");
    when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
    PatchManagement pm = mockManagementService(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    PatchServiceImpl service = new PatchServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    try {
        service.download(new URL("file:" + storage + "/temp/f00.zip"));
        fail("Should have thrown exception on non existent patch file.");
    } catch (Exception ignored) {
    }
    Iterable<Patch> patches = service.download(patch132.toURI().toURL());
    assertNotNull(patches);
    Iterator<Patch> it = patches.iterator();
    assertTrue(it.hasNext());
    Patch patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.3.2", 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.3.2", itb.next());
    assertNull(patch.getResult());
    // 
    // Simulate the patch
    // 
    when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
    when(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).thenReturn(null);
    when(bundle.getSymbolicName()).thenReturn("my-bsn");
    when(bundle.getVersion()).thenReturn(new Version("1.3.1"));
    when(bundle.getLocation()).thenReturn("location");
    when(bundle.getBundleId()).thenReturn(123L);
    BundleStartLevel bsl = mock(BundleStartLevel.class);
    when(bsl.getStartLevel()).thenReturn(30);
    when(bundle.adapt(BundleStartLevel.class)).thenReturn(bsl);
    when(bundle.getState()).thenReturn(1);
    when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
    PatchResult result = service.install(patch, true);
    assertNotNull(result);
    assertTrue(result.isSimulation());
    // 
    // Recreate a new service and verify the downloaded patch is still available
    // 
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    when(bundleContext.getBundle(0)).thenReturn(sysBundle);
    when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
    when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
    when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.toString());
    when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
    when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
    when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
    when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
    when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
    when(repository.getManagedPatch(anyString())).thenReturn(null);
    when(repository.findOrCreateMainGitRepository()).thenReturn(null);
    service = new PatchServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    patches = service.getPatches();
    assertNotNull(patches);
    it = patches.iterator();
    assertTrue(it.hasNext());
    patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.3.2", patch.getPatchData().getId());
    assertNotNull(patch.getPatchData().getBundles());
    assertEquals(1, patch.getPatchData().getBundles().size());
    itb = patch.getPatchData().getBundles().iterator();
    assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
    assertNull(patch.getResult());
    // 
    // Install the patch
    // 
    when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
    when(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).thenReturn(null);
    when(bundle.getSymbolicName()).thenReturn("my-bsn");
    when(bundle.getVersion()).thenReturn(new Version("1.3.1"));
    when(bundle.getLocation()).thenReturn("location");
    when(bundle.getHeaders()).thenReturn(new Hashtable<String, String>());
    when(bundle.getBundleId()).thenReturn(123L);
    bundle.update(any(InputStream.class));
    when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
    when(bundle.getState()).thenReturn(Bundle.INSTALLED);
    when(bundle.getRegisteredServices()).thenReturn(null);
    when(bundle.adapt(BundleStartLevel.class)).thenReturn(bsl);
    when(bsl.getStartLevel()).thenReturn(30);
    when(sysBundleContext.getBundle(0)).thenReturn(sysBundle);
    when(sysBundle.adapt(FrameworkWiring.class)).thenReturn(wiring);
    when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
    bundle.start();
    doAnswer(invocationOnMock -> {
        ((FrameworkListener) invocationOnMock.getArgument(1)).frameworkEvent(null);
        return invocationOnMock.getMock();
    }).when(wiring).refreshBundles(any(), any(FrameworkListener.class));
    result = service.install(patch, false);
    assertNotNull(result);
    assertSame(result, patch.getResult());
    assertFalse(patch.getResult().isSimulation());
    // 
    // Recreate a new service and verify the downloaded patch is still available and installed
    // 
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    when(bundleContext.getBundle(0)).thenReturn(sysBundle);
    when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
    when(repository.getManagedPatch(anyString())).thenReturn(null);
    when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
    when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.toString());
    when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
    when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
    when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
    when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
    when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
    service = new PatchServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    patches = service.getPatches();
    assertNotNull(patches);
    it = patches.iterator();
    assertTrue(it.hasNext());
    patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.3.2", patch.getPatchData().getId());
    assertNotNull(patch.getPatchData().getBundles());
    assertEquals(1, patch.getPatchData().getBundles().size());
    itb = patch.getPatchData().getBundles().iterator();
    assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
    assertNotNull(patch.getResult());
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(java.net.URL) PatchException(org.jboss.fuse.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) Version(org.osgi.framework.Version) PatchManagement(org.jboss.fuse.patch.management.PatchManagement) PatchResult(org.jboss.fuse.patch.management.PatchResult) FrameworkListener(org.osgi.framework.FrameworkListener) Patch(org.jboss.fuse.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 18 with GitPatchManagementServiceImpl

use of org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceIT method initializationPerformedNoFuseVersion.

@Test
public void initializationPerformedNoFuseVersion() throws IOException, GitAPIException {
    pm = new GitPatchManagementServiceImpl(bundleContext);
    pm.start();
    try {
        pm.ensurePatchManagementInitialized();
        fail("Should fail, because versions can't be determined");
    } catch (PatchException e) {
        assertTrue(e.getMessage().contains("Can't find"));
    }
}
Also used : GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) Test(org.junit.Test)

Example 19 with GitPatchManagementServiceImpl

use of org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceIT method disabledPatchManagement.

@Test
public void disabledPatchManagement() throws IOException, GitAPIException {
    System.setProperty("patching.disabled", "true");
    pm = new GitPatchManagementServiceImpl(bundleContext);
    pm.start();
    assertFalse(pm.isEnabled());
    System.setProperty("patching.disabled", "");
}
Also used : GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) Test(org.junit.Test)

Example 20 with GitPatchManagementServiceImpl

use of org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceIT method initializationPerformedNoBaselineDistribution.

@Test
public void initializationPerformedNoBaselineDistribution() throws IOException, GitAPIException {
    freshKarafStandaloneDistro();
    pm = new GitPatchManagementServiceImpl(bundleContext);
    pm.start();
    try {
        pm.ensurePatchManagementInitialized();
        fail("Should fail, because no baseline distribution is found");
    } catch (PatchException e) {
        assertTrue(e.getMessage().contains("Can't find baseline distribution"));
    }
}
Also used : GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) Test(org.junit.Test)

Aggregations

GitPatchManagementServiceImpl (org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl)31 Test (org.junit.Test)25 GitPatchRepository (org.jboss.fuse.patch.management.impl.GitPatchRepository)22 File (java.io.File)18 Git (org.eclipse.jgit.api.Git)18 ObjectId (org.eclipse.jgit.lib.ObjectId)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 Map (java.util.Map)8 Ref (org.eclipse.jgit.lib.Ref)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 IOException (java.io.IOException)5 Patch (org.jboss.fuse.patch.management.Patch)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 FileReader (java.io.FileReader)4 LinkedList (java.util.LinkedList)4 Properties (java.util.Properties)4 FeaturesProcessing (org.apache.karaf.features.internal.model.processing.FeaturesProcessing)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 PatchData (org.jboss.fuse.patch.management.PatchData)4 PatchManagement (org.jboss.fuse.patch.management.PatchManagement)4