Search in sources :

Example 6 with DeployedJar

use of org.apache.geode.internal.DeployedJar in project geode by apache.

the class IncrementalBackupDUnitTest method testBackupUserDeployedJarFiles.

/**
   * Successful if a user deployed jar file is included as part of the backup.
   */
@Test
public void testBackupUserDeployedJarFiles() throws Exception {
    final String jarName = "BackupJarDeploymentDUnit";
    final String jarNameRegex = ".*" + jarName + ".*";
    final ClassBuilder classBuilder = new ClassBuilder();
    final byte[] classBytes = classBuilder.createJarFromName(jarName);
    VM vm0 = Host.getHost(0).getVM(0);
    /*
     * Deploy a "dummy" jar to the VM.
     */
    File deployedJarFile = vm0.invoke(() -> {
        DeployedJar deployedJar = ClassPathLoader.getLatest().getJarDeployer().deploy(jarName, classBytes);
        return deployedJar.getFile();
    });
    assertTrue(deployedJarFile.exists());
    /*
     * Perform backup. Make sure it is successful.
     */
    assertBackupStatus(baseline(vm0));
    /*
     * Make sure the user deployed jar is part of the backup.
     */
    Collection<File> memberDeployedJarFiles = FileUtils.listFiles(getBackupDirForMember(getBaselineDir(), getMemberId(vm0)), new RegexFileFilter(jarNameRegex), DirectoryFileFilter.DIRECTORY);
    assertFalse(memberDeployedJarFiles.isEmpty());
    // Shut down our member so we can perform a restore
    PersistentID id = getPersistentID(vm0);
    closeCache(vm0);
    /*
     * Get the VM's user directory.
     */
    final String vmDir = vm0.invoke(() -> System.getProperty("user.dir"));
    File backupDir = getBackupDirForMember(getBaselineDir(), getMemberId(vm0));
    vm0.bounce();
    /*
     * Cleanup "dummy" jar from file system.
     */
    Pattern pattern = Pattern.compile('^' + jarName + ".*#\\d++$");
    deleteMatching(new File("."), pattern);
    // Execute the restore
    performRestore(new File(id.getDirectory()), backupDir);
    /*
     * Make sure the user deployed jar is part of the restore.
     */
    Collection<File> restoredJars = FileUtils.listFiles(new File(vmDir), new RegexFileFilter(jarNameRegex), DirectoryFileFilter.DIRECTORY);
    assertFalse(restoredJars.isEmpty());
    List<String> restoredJarNames = new LinkedList<>();
    TransformUtils.transform(memberDeployedJarFiles, restoredJarNames, TransformUtils.fileNameTransformer);
    for (String name : restoredJarNames) {
        assertTrue(name.contains(jarName));
    }
    // Restart the member
    openCache(vm0);
    /*
     * Remove the "dummy" jar from the VM.
     */
    vm0.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            for (DeployedJar jarClassLoader : ClassPathLoader.getLatest().getJarDeployer().findDeployedJars()) {
                if (jarClassLoader.getJarName().startsWith(jarName)) {
                    ClassPathLoader.getLatest().getJarDeployer().undeploy(jarClassLoader.getJarName());
                }
            }
            return null;
        }
    });
    /*
     * Cleanup "dummy" jar from file system.
     */
    pattern = Pattern.compile('^' + jarName + ".*#\\d++$");
    deleteMatching(new File(vmDir), pattern);
}
Also used : DeployedJar(org.apache.geode.internal.DeployedJar) Pattern(java.util.regex.Pattern) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) ClassBuilder(org.apache.geode.internal.ClassBuilder) LinkedList(java.util.LinkedList) AdminException(org.apache.geode.admin.AdminException) IOException(java.io.IOException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) File(java.io.File) PersistentID(org.apache.geode.cache.persistence.PersistentID) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 7 with DeployedJar

use of org.apache.geode.internal.DeployedJar in project geode by apache.

the class ClusterConfig method verifyServer.

public void verifyServer(MemberVM<Server> serverVM) {
    // verify files exist in filesystem
    Set<String> expectedJarNames = this.getJarNames().stream().collect(toSet());
    String[] actualJarFiles = serverVM.getWorkingDir().list((dir, filename) -> filename.contains(".jar"));
    Set<String> actualJarNames = Stream.of(actualJarFiles).map(jar -> jar.replaceAll("\\.v\\d+\\.jar", ".jar")).collect(toSet());
    // We will end up with extra jars on disk if they are deployed and then undeployed
    assertThat(expectedJarNames).isSubsetOf(actualJarNames);
    // verify config exists in memory
    serverVM.invoke(() -> {
        Cache cache = GemFireCacheImpl.getInstance();
        // TODO: set compare to fail if there are extra regions
        for (String region : this.getRegions()) {
            assertThat(cache.getRegion(region)).isNotNull();
        }
        if (StringUtils.isNotBlank(this.getMaxLogFileSize())) {
            Properties props = cache.getDistributedSystem().getProperties();
            assertThat(props.getProperty(LOG_FILE_SIZE_LIMIT)).isEqualTo(this.getMaxLogFileSize());
        }
        for (String jar : this.getJarNames()) {
            DeployedJar deployedJar = ClassPathLoader.getLatest().getJarDeployer().findDeployedJar(jar);
            assertThat(deployedJar).isNotNull();
            assertThat(Class.forName(nameOfClassContainedInJar(jar), true, new URLClassLoader(new URL[] { deployedJar.getFileURL() }))).isNotNull();
        }
        // If we have extra jars on disk left over from undeploy, make sure they aren't used
        Set<String> undeployedJarNames = new HashSet<>(actualJarNames);
        undeployedJarNames.removeAll(expectedJarNames);
        for (String jar : undeployedJarNames) {
            System.out.println("Verifying undeployed jar: " + jar);
            DeployedJar undeployedJar = ClassPathLoader.getLatest().getJarDeployer().findDeployedJar(jar);
            assertThat(undeployedJar).isNull();
        }
    });
}
Also used : ClassPathLoader(org.apache.geode.internal.ClassPathLoader) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) DeployedJar(org.apache.geode.internal.DeployedJar) Locator(org.apache.geode.test.dunit.rules.Locator) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Cache(org.apache.geode.cache.Cache) URLClassLoader(java.net.URLClassLoader) Collectors.toSet(java.util.stream.Collectors.toSet) Properties(java.util.Properties) Set(java.util.Set) Server(org.apache.geode.test.dunit.rules.Server) Collectors(java.util.stream.Collectors) File(java.io.File) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) LocatorServerStartupRule(org.apache.geode.test.dunit.rules.LocatorServerStartupRule) Serializable(java.io.Serializable) List(java.util.List) Stream(java.util.stream.Stream) MemberVM(org.apache.geode.test.dunit.rules.MemberVM) LOG_FILE_SIZE_LIMIT(org.apache.geode.distributed.ConfigurationProperties.LOG_FILE_SIZE_LIMIT) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) Collections(java.util.Collections) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) DeployedJar(org.apache.geode.internal.DeployedJar) URLClassLoader(java.net.URLClassLoader) Properties(java.util.Properties) URL(java.net.URL) Cache(org.apache.geode.cache.Cache) HashSet(java.util.HashSet)

Aggregations

DeployedJar (org.apache.geode.internal.DeployedJar)7 JarDeployer (org.apache.geode.internal.JarDeployer)5 ArrayList (java.util.ArrayList)4 File (java.io.File)3 CacheClosedException (org.apache.geode.cache.CacheClosedException)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Properties (java.util.Properties)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 StringUtils (org.apache.commons.lang.StringUtils)2 Cache (org.apache.geode.cache.Cache)2 DistributedMember (org.apache.geode.distributed.DistributedMember)2 ClusterConfigurationService (org.apache.geode.distributed.internal.ClusterConfigurationService)2 ClassPathLoader (org.apache.geode.internal.ClassPathLoader)2 InternalCache (org.apache.geode.internal.cache.InternalCache)2 Configuration (org.apache.geode.management.internal.configuration.domain.Configuration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1