Skip to content

Commit

Permalink
GEODE-2430: Remove jar and zip files from test resources
Browse files Browse the repository at this point in the history
This closes apache#393

(cherry picked from commit e769796)
  • Loading branch information
jaredjstewart authored and jinmeiliao committed Feb 8, 2017
1 parent f637dc6 commit 831fa44
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@

import static org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
import static org.apache.geode.distributed.ConfigurationProperties.USE_CLUSTER_CONFIGURATION;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.commons.io.FileUtils;
import org.apache.geode.internal.ClassBuilder;
import org.apache.geode.management.internal.configuration.utils.ZipUtils;
import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase;
import org.apache.geode.test.dunit.rules.LocatorServerStartupRule;
import org.junit.Before;
import org.junit.Rule;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class ClusterConfigBaseTest extends JUnit4DistributedTestCase {
public static final String CLUSTER_CONFIG_ZIP_FILENAME = "cluster_config.zip";
public static final String CLUSTER_CONFIG_ZIP_PATH =
ClusterConfigBaseTest.class.getResource(CLUSTER_CONFIG_ZIP_FILENAME).getPath();
public String clusterConfigZipPath;

public static final ConfigGroup CLUSTER = new ConfigGroup("cluster").regions("regionForCluster")
.jars("cluster.jar").maxLogFileSize("5000").configFiles("cluster.properties", "cluster.xml");
Expand All @@ -56,11 +60,70 @@ public class ClusterConfigBaseTest extends JUnit4DistributedTestCase {

@Before
public void before() throws Exception {
clusterConfigZipPath = buildClusterZipFile();
locatorProps = new Properties();
serverProps = new Properties();

// the following are default values, we don't need to set them. We do it for clarity purpose
locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
serverProps.setProperty(USE_CLUSTER_CONFIGURATION, "true");
}

private String buildClusterZipFile() throws Exception {
ClassBuilder classBuilder = new ClassBuilder();
File clusterConfigDir = this.lsRule.getTempFolder().newFolder("cluster_config");

File clusterDir = new File(clusterConfigDir, "cluster");
String clusterXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<cache xmlns=\"http://geode.apache.org/schema/cache\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" copy-on-read=\"false\" is-server=\"false\" lock-lease=\"120\" lock-timeout=\"60\" search-timeout=\"300\" version=\"1.0\" xsi:schemaLocation=\"http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd\">\n"
+ "<region name=\"regionForCluster\">\n"
+ " <region-attributes data-policy=\"replicate\" scope=\"distributed-ack\"/>\n"
+ " </region>\n" + "</cache>\n";
writeFile(clusterDir, "cluster.xml", clusterXml);
writeFile(clusterDir, "cluster.properties", "log-file-size-limit=5000");
createJarFileWithClass("Cluster", "cluster.jar", clusterDir);

File group1Dir = new File(clusterConfigDir, "group1");
String group1Xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<cache xmlns=\"http://geode.apache.org/schema/cache\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" copy-on-read=\"false\" is-server=\"false\" lock-lease=\"120\" lock-timeout=\"60\" search-timeout=\"300\" version=\"1.0\" xsi:schemaLocation=\"http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd\">\n"
+ "<region name=\"regionForGroup1\">\n"
+ " <region-attributes data-policy=\"replicate\" scope=\"distributed-ack\"/>\n"
+ " </region>\n" + "</cache>\n";
writeFile(group1Dir, "group1.xml", group1Xml);
writeFile(group1Dir, "group1.properties", "log-file-size-limit=6000");
createJarFileWithClass("Group1", "group1.jar", group1Dir);


File group2Dir = new File(clusterConfigDir, "group2");
String group2Xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<cache xmlns=\"http://geode.apache.org/schema/cache\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" copy-on-read=\"false\" is-server=\"false\" lock-lease=\"120\" lock-timeout=\"60\" search-timeout=\"300\" version=\"1.0\" xsi:schemaLocation=\"http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd\">\n"
+ "<region name=\"regionForGroup2\">\n"
+ " <region-attributes data-policy=\"replicate\" scope=\"distributed-ack\"/>\n"
+ " </region>\n" + "</cache>\n";
writeFile(group2Dir, "group1.xml", group2Xml);
writeFile(group2Dir, "group2.properties", "log-file-size-limit=7000");
createJarFileWithClass("Group2", "group2.jar", group2Dir);


File clusterConfigZip = lsRule.getTempFolder().newFile("cluster_config.zip");
ZipUtils.zipDirectory(clusterConfigDir.getCanonicalPath(), clusterConfigZip.getCanonicalPath());

FileUtils.deleteDirectory(clusterConfigDir);
return clusterConfigZip.getCanonicalPath();
}

private File writeFile(File dir, String fileName, String content) throws IOException {
dir.mkdirs();
File file = new File(dir, fileName);
FileUtils.writeStringToFile(file, content);

return file;
}

protected String createJarFileWithClass(String className, String jarName, File dir)
throws IOException {
File jarFile = new File(dir, jarName);
new ClassBuilder().writeJarFromName(className, jarFile);
return jarFile.getCanonicalPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.geode.internal.ClassBuilder;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.internal.cli.result.CommandResult;
import org.apache.geode.test.dunit.rules.GfshShellConnectionRule;
Expand All @@ -30,6 +31,9 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;
import java.io.IOException;

@Category(DistributedTest.class)
public class ClusterConfigDeployJarDUnitTest extends ClusterConfigBaseTest {
private GfshShellConnectionRule gfshConnector;
Expand All @@ -39,9 +43,10 @@ public class ClusterConfigDeployJarDUnitTest extends ClusterConfigBaseTest {
@Before
public void before() throws Exception {
super.before();
clusterJar = getClass().getResource("cluster.jar").getPath();
group1Jar = getClass().getResource("group1.jar").getPath();
group2Jar = getClass().getResource("group2.jar").getPath();

clusterJar = createJarFileWithClass("Cluster", "cluster.jar", lsRule.getTempFolder().getRoot());
group1Jar = createJarFileWithClass("Group1", "group1.jar", lsRule.getTempFolder().getRoot());
group2Jar = createJarFileWithClass("Group2", "group2.jar", lsRule.getTempFolder().getRoot());
}

@After
Expand All @@ -53,7 +58,7 @@ public void after() throws Exception {

@Test
public void testDeployToNoServer() throws Exception {
String clusterJarPath = getClass().getResource("cluster.jar").getPath();
String clusterJarPath = clusterJar;
// set up the locator/servers
Locator locator = lsRule.startLocatorVM(0, locatorProps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testImportWithRunningServerWithData() throws Exception {
});

CommandResult result = gfshConnector
.executeCommand("import cluster-configuration --zip-file-name=" + CLUSTER_CONFIG_ZIP_PATH);
.executeCommand("import cluster-configuration --zip-file-name=" + clusterConfigZipPath);

assertThat(result.getStatus()).isEqualTo(Result.Status.ERROR);
assertThat(result.getContent().toString()).contains("existing data in regions: " + regionName);
Expand All @@ -105,7 +105,7 @@ public void testImportWithRunningServer() throws Exception {
// even though we have a region recreated, we can still import since there is no data
// in the region
CommandResult result = gfshConnector
.executeCommand("import cluster-configuration --zip-file-name=" + CLUSTER_CONFIG_ZIP_PATH);
.executeCommand("import cluster-configuration --zip-file-name=" + clusterConfigZipPath);

assertThat(result.getStatus()).isEqualTo(Result.Status.OK)
.describedAs(result.getContent().toString());
Expand All @@ -130,7 +130,7 @@ public void testImportWithRunningServer() throws Exception {
@Test
public void testImportClusterConfig() throws Exception {
CommandResult result = gfshConnector
.executeCommand("import cluster-configuration --zip-file-name=" + CLUSTER_CONFIG_ZIP_PATH);
.executeCommand("import cluster-configuration --zip-file-name=" + clusterConfigZipPath);
assertThat(result.getStatus()).isEqualTo(Result.Status.OK);

// Make sure that a backup of the old clusterConfig was created
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testImportWithMultipleLocators() throws Exception {
Locator locator2 = lsRule.startLocatorVM(2, locatorProps);

CommandResult result = gfshConnector
.executeCommand("import cluster-configuration --zip-file-name=" + CLUSTER_CONFIG_ZIP_PATH);
.executeCommand("import cluster-configuration --zip-file-name=" + clusterConfigZipPath);
assertThat(result.getStatus()).isEqualTo(Result.Status.OK);

CONFIG_FROM_ZIP.verify(locator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private Locator startLocatorWithLoadCCFromDir() throws Exception {
// tempFolder/locator-0/cluster_config/cluster/cluster.jar
// tempFolder/locator-0/cluster_config/group1/ {group1.xml, group1.properties, group1.jar}
// tempFolder/locator-0/cluster_config/group2/ ...
ZipUtils.unzip(CLUSTER_CONFIG_ZIP_PATH, configDir.getCanonicalPath());
ZipUtils.unzip(clusterConfigZipPath, configDir.getCanonicalPath());

Properties properties = new Properties();
properties.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import static org.apache.geode.distributed.ConfigurationProperties.SECURITY_MANAGER;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.commons.io.FileUtils;
import org.apache.geode.distributed.internal.ClusterConfigurationService;
import org.apache.geode.distributed.internal.InternalLocator;
import org.apache.geode.management.internal.cli.i18n.CliStrings;
import org.apache.geode.management.internal.configuration.utils.ZipUtils;
import org.apache.geode.security.SimpleTestSecurityManager;
import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase;
import org.apache.geode.test.dunit.rules.GfshShellConnectionRule;
Expand All @@ -37,20 +39,12 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;
import java.util.Properties;

@Category({DistributedTest.class, SecurityTest.class})
public class ClusterConfigWithSecurityDUnitTest extends JUnit4DistributedTestCase {

// the zip file is under test/resource/org/apache/geode/management/internal/configuration
// it only contains cluster.properites whose content is
// mcast-port=0
// log-file-size-limit=8000
// security-manager=org.apache.geode.example.security.ExampleSecurityManager

public static final String CLUSTER_CONFIG_ZIP_FILENAME = "cluster_config_security.zip";
public static final String CLUSTER_CONFIG_ZIP_PATH =
ClusterConfigBaseTest.class.getResource(CLUSTER_CONFIG_ZIP_FILENAME).getPath();
public String clusterConfigZipPath;

@Rule
public LocatorServerStartupRule lsRule = new LocatorServerStartupRule();
Expand All @@ -60,6 +54,8 @@ public class ClusterConfigWithSecurityDUnitTest extends JUnit4DistributedTestCas

@Before
public void before() throws Exception {
clusterConfigZipPath = buildSecureClusterConfigZip();

locatorProps = new Properties();
locatorProps.setProperty(SECURITY_MANAGER, SimpleTestSecurityManager.class.getName());
locator0 = lsRule.startLocatorVM(0, locatorProps);
Expand Down Expand Up @@ -92,7 +88,7 @@ public void testImportNotOverwriteSecurity() throws Exception {
"cluster");

connector.executeAndVerifyCommand(
"import cluster-configuration --zip-file-name=" + CLUSTER_CONFIG_ZIP_PATH);
"import cluster-configuration --zip-file-name=" + clusterConfigZipPath);

locator0.invoke(() -> {
InternalLocator locator = LocatorServerStartupRule.locatorStarter.locator;
Expand All @@ -106,4 +102,17 @@ public void testImportNotOverwriteSecurity() throws Exception {
.isEqualTo(SimpleTestSecurityManager.class.getName());
});
}

private String buildSecureClusterConfigZip() throws Exception {
File clusterDir = lsRule.getTempFolder().newFolder("cluster");
File clusterSubDir = new File(clusterDir, "cluster");

String clusterProperties = "mcast-port=0\n" + "log-file-size-limit=8000\n"
+ "security-manager=org.apache.geode.example.security.ExampleSecurityManager";
FileUtils.writeStringToFile(new File(clusterSubDir, "cluster.properties"), clusterProperties);
File clusterZip = new File(lsRule.getTempFolder().getRoot(), "cluster_config_security.zip");
ZipUtils.zipDirectory(clusterDir.getCanonicalPath(), clusterZip.getCanonicalPath());
FileUtils.deleteDirectory(clusterDir);
return clusterZip.getCanonicalPath();
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 831fa44

Please sign in to comment.