Search in sources :

Example 16 with GitPatchRepository

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

the class PatchServiceImplTest method testLoadWithoutRanges.

@Test
public void testLoadWithoutRanges() throws IOException, GitAPIException {
    BundleContext bundleContext = mock(BundleContext.class);
    ComponentContext componentContext = mock(ComponentContext.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 = new GitPatchManagementServiceImpl(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    PatchServiceImpl service = new PatchServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    PatchData pd = PatchData.load(getClass().getClassLoader().getResourceAsStream("test1.patch"));
    assertEquals(2, pd.getBundles().size());
    assertTrue(pd.getRequirements().isEmpty());
}
Also used : GitPatchManagementServiceImpl(org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl) PatchData(org.jboss.fuse.patch.management.PatchData) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) PatchManagement(org.jboss.fuse.patch.management.PatchManagement) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 17 with GitPatchRepository

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

the class PatchServiceImplTest method testPatchWithVersionRanges.

@Test
public void testPatchWithVersionRanges() 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);
    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());
    // 
    // 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);
    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) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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) Patch(org.jboss.fuse.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 18 with GitPatchRepository

use of org.jboss.fuse.patch.management.impl.GitPatchRepository 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 19 with GitPatchRepository

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

the class GitPatchManagementServiceIT method commitNonRollupPatch.

@Test
public void commitNonRollupPatch() throws IOException, GitAPIException {
    freshKarafStandaloneDistro();
    GitPatchRepository repository = patchManagement();
    PatchManagement management = (PatchManagement) pm;
    preparePatchZip("src/test/resources/content/patch1", "target/karaf/patches/source/patch-1.zip", false);
    List<PatchData> patches = management.fetchPatches(new File("target/karaf/patches/source/patch-1.zip").toURI().toURL());
    Patch patch = management.trackPatch(patches.get(0));
    Git fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
    ObjectId master1 = fork.getRepository().resolve(GitPatchRepository.HISTORY_BRANCH);
    String tx = management.beginInstallation(PatchKind.NON_ROLLUP);
    management.install(tx, patch, null);
    management.commitInstallation(tx);
    repository.closeRepository(fork, true);
    fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
    ObjectId master2 = fork.getRepository().resolve(GitPatchRepository.HISTORY_BRANCH);
    assertThat(master1, not(equalTo(master2)));
    assertThat(fork.tagList().call().size(), equalTo(3));
    assertTrue(repository.containsTag(fork, "patch-management"));
    assertTrue(repository.containsTag(fork, "baseline-7.0.0"));
    assertTrue(repository.containsTag(fork, "patch-my-patch-1"));
    assertThat("Baseline should not change", repository.findCurrentBaseline(fork).getTagName(), equalTo("baseline-7.0.0"));
    String binStart = FileUtils.readFileToString(new File(karafHome, "bin/start"), "UTF-8");
    assertTrue("bin/start should be patched by patch-1", binStart.contains("echo \"started\""));
    // we had conflict, so expect the backup
    String oldBinStart = FileUtils.readFileToString(new File(karafHome, "patches/my-patch-1.backup/bin/start"), "UTF-8");
    assertTrue("bin/start should be backed up", oldBinStart.contains("echo \"This is user's change\""));
}
Also used : Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) File(java.io.File) Test(org.junit.Test)

Example 20 with GitPatchRepository

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

the class GitPatchManagementServiceIT method rollbackInstalledNonRollupPatch.

@Test
public void rollbackInstalledNonRollupPatch() throws IOException, GitAPIException {
    freshKarafStandaloneDistro();
    GitPatchRepository repository = patchManagement();
    PatchManagement management = (PatchManagement) pm;
    preparePatchZip("src/test/resources/content/patch1", "target/karaf/patches/source/patch-1.zip", false);
    List<PatchData> patches = management.fetchPatches(new File("target/karaf/patches/source/patch-1.zip").toURI().toURL());
    Patch patch = management.trackPatch(patches.get(0));
    Git fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
    ObjectId master1 = fork.getRepository().resolve(GitPatchRepository.HISTORY_BRANCH);
    String tx = management.beginInstallation(PatchKind.NON_ROLLUP);
    management.install(tx, patch, null);
    management.commitInstallation(tx);
    management.rollback(patch.getPatchData());
    repository.closeRepository(fork, true);
    fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
    ObjectId master2 = fork.getRepository().resolve(GitPatchRepository.HISTORY_BRANCH);
    assertThat(master1, not(equalTo(master2)));
    assertThat(fork.tagList().call().size(), equalTo(2));
    assertTrue(repository.containsTag(fork, "patch-management"));
    assertTrue(repository.containsTag(fork, "baseline-7.0.0"));
    assertFalse(repository.containsTag(fork, "patch-my-patch-1"));
    String binStart = FileUtils.readFileToString(new File(karafHome, "bin/start"), "UTF-8");
    assertTrue("bin/start should be at previous version", binStart.contains("echo \"This is user's change\""));
}
Also used : Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) GitPatchRepository(org.jboss.fuse.patch.management.impl.GitPatchRepository) File(java.io.File) Test(org.junit.Test)

Aggregations

GitPatchRepository (org.jboss.fuse.patch.management.impl.GitPatchRepository)28 Test (org.junit.Test)25 Git (org.eclipse.jgit.api.Git)24 File (java.io.File)23 GitPatchManagementServiceImpl (org.jboss.fuse.patch.management.impl.GitPatchManagementServiceImpl)21 ObjectId (org.eclipse.jgit.lib.ObjectId)16 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Map (java.util.Map)8 Ref (org.eclipse.jgit.lib.Ref)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 IOException (java.io.IOException)4 FeaturesProcessing (org.apache.karaf.features.internal.model.processing.FeaturesProcessing)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 PatchManagement (org.jboss.fuse.patch.management.PatchManagement)4 Bundle (org.osgi.framework.Bundle)4 BundleContext (org.osgi.framework.BundleContext)4 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)4 ComponentContext (org.osgi.service.component.ComponentContext)4 FileReader (java.io.FileReader)3 LinkedList (java.util.LinkedList)3