Skip to content

Commit

Permalink
GEODE-2725: export logs now honors --dir when connected via JMX, with
Browse files Browse the repository at this point in the history
respect to the manager filesystem.

* Absolute Paths are better.  Test class inheritance is cumbersome.

* Removed --dir usage from ExportLogsDUnitTests:
startAndEndDateCanIncludeLogs and
testExportWithStartAndEndDateTimeFiltering; these tests expected logs
to appear in the locator working directory and the --dir option was
not actually being used.

* Usage of LocalLocatorStarterRules reverted in
ExportLogsIntegrationTest as locator working directory access is
required.

* this closes apache#438

Corrected mismatched merging of strings against offline help golden file.
  • Loading branch information
PurelyApplied authored and jinmeiliao committed Apr 12, 2017
1 parent 2bf910a commit 122e650
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ public Result exportLogs(

Path tempDir = Files.createTempDirectory("exportedLogs");
// make sure the directory is created, so that even if there is no files unzipped to this dir,
// we can
// still zip it and send an empty zip file back to the client
// we can still zip it and send an empty zip file back to the client
Path exportedLogsDir = tempDir.resolve("exportedLogs");
FileUtils.forceMkdir(exportedLogsDir.toFile());

Expand All @@ -182,9 +181,14 @@ public Result exportLogs(
FileUtils.deleteQuietly(zipFile.toFile());
}

Path workingDir = Paths.get(System.getProperty("user.dir"));
Path exportedLogsZipFile = workingDir
.resolve("exportedLogs_" + System.currentTimeMillis() + ".zip").toAbsolutePath();
Path dirPath;
if (StringUtils.isBlank(dirName)) {
dirPath = Paths.get(System.getProperty("user.dir"));
} else {
dirPath = Paths.get(dirName);
}
Path exportedLogsZipFile =
dirPath.resolve("exportedLogs_" + System.currentTimeMillis() + ".zip").toAbsolutePath();

logger.info("Zipping into: " + exportedLogsZipFile.toString());
ZipUtils.zipDirectory(exportedLogsDir, exportedLogsZipFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public Result postExecution(GfshParseResult parseResult, Result commandResult, P
Path dirPath;
String dirName = parseResult.getParamValueStrings().get("dir");
if (StringUtils.isBlank(dirName)) {
dirPath = Paths.get(System.getProperty("user.dir"));
dirPath = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
} else {
dirPath = Paths.get(dirName);
dirPath = Paths.get(dirName).toAbsolutePath();
}
String fileName = "exportedLogs_" + System.currentTimeMillis() + ".zip";
File exportedLogFile = dirPath.resolve(fileName).toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public void startAndEndDateCanIncludeLogs() throws Exception {
commandStringBuilder.addOption("start-time", dateTimeFormatter.format(yesterday));
commandStringBuilder.addOption("end-time", dateTimeFormatter.format(tomorrow));
commandStringBuilder.addOption("log-level", "debug");
commandStringBuilder.addOption("dir", "someDir");

gfshConnector.executeAndVerifyCommand(commandStringBuilder.toString());

Expand All @@ -146,7 +145,6 @@ public void testExportWithStartAndEndDateTimeFiltering() throws Exception {
commandStringBuilder.addOption("start-time", dateTimeFormatter.format(cutoffTime.minusDays(1)));
commandStringBuilder.addOption("end-time", cutoffTimeString);
commandStringBuilder.addOption("log-level", "debug");
commandStringBuilder.addOption("dir", "someDir");

gfshConnector.executeAndVerifyCommand(commandStringBuilder.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,82 @@

package org.apache.geode.management.internal.cli.commands;

import static org.apache.geode.test.dunit.rules.GfshShellConnectionRule.*;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.commons.io.FileUtils;
import org.apache.geode.test.dunit.rules.GfshShellConnectionRule;
import org.apache.geode.test.dunit.rules.LocatorStarterBuilder;
import org.apache.geode.test.dunit.rules.LocalLocatorStarterRule;
import org.apache.geode.test.dunit.rules.LocatorStarterRule;
import org.apache.geode.test.junit.categories.IntegrationTest;
import org.junit.ClassRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.nio.file.Path;

import java.io.File;

@Category(IntegrationTest.class)
public class ExportLogsIntegrationTest {

@ClassRule
public static LocalLocatorStarterRule locator = new LocatorStarterBuilder().buildInThisVM();
@Rule
public LocatorStarterRule locator = new LocatorStarterRule().withJMXManager().startLocator();

@Rule
public GfshShellConnectionRule gfsh = new GfshShellConnectionRule();

protected void connect() throws Exception {
gfsh.connectAndVerify(locator.getLocatorPort(), PortType.locator);
@Before
public void connect() throws Exception {
gfsh.connectAndVerify(locator);
}

public File getWorkingDirectory() throws Exception {
return locator.getWorkingDir();
}

@Test
public void testInvalidMember() throws Exception {
connect();
gfsh.executeCommand("export logs --member=member1,member2");
assertThat(gfsh.getGfshOutput()).contains("No Members Found");
}

@Test
public void testNothingToExport() throws Exception {
connect();
gfsh.executeCommand("export logs --stats-only");
assertThat(gfsh.getGfshOutput()).contains("No files to be exported.");
}

@Test
public void withFiles_savedToLocatorWorkingDir() throws Exception {
String[] extensions = {"zip"};
// Expects locator to produce file in own working directory when connected via JMX
gfsh.executeCommand("export logs");
assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, false)).isNotEmpty();
}

@Test
public void withFiles_savedToLocatorSpecifiedRelativeDir() throws Exception {
String[] extensions = {"zip"};
Path workingDirPath = getWorkingDirectory().toPath();
Path subdirPath = workingDirPath.resolve("some").resolve("test").resolve("directory");
Path relativeDir = workingDirPath.relativize(subdirPath);
// Expects locator to produce file in own working directory when connected via JMX
gfsh.executeCommand("export logs --dir=" + relativeDir.toString());
assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, false)).isEmpty();
assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, true)).isNotEmpty();
assertThat(FileUtils.listFiles(subdirPath.toFile(), extensions, false)).isNotEmpty();
}

@Test
public void withFiles_savedToLocatorSpecifiedAbsoluteDir() throws Exception {
String[] extensions = {"zip"};
Path workingDirPath = getWorkingDirectory().toPath();
Path absoluteDirPath =
workingDirPath.resolve("some").resolve("test").resolve("directory").toAbsolutePath();
// Expects locator to produce file in own working directory when connected via JMX
gfsh.executeCommand("export logs --dir=" + absoluteDirPath.toString());
assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, false)).isEmpty();
assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, true)).isNotEmpty();
assertThat(FileUtils.listFiles(absoluteDirPath.toFile(), extensions, false)).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ protected void before(Description description) throws Throwable {

}

public void connect(MemberVM locator, String... options) throws Exception {
public void connect(Member locator, String... options) throws Exception {
connect(locator.getPort(), PortType.locator, options);
}

public void connectAndVerify(MemberVM locator, String... options) throws Exception {
public void connectAndVerify(Member locator, String... options) throws Exception {
connect(locator.getPort(), PortType.locator, options);
assertThat(this.connected).isTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
import org.apache.geode.test.junit.categories.IntegrationTest;
import org.junit.experimental.categories.Category;

import java.io.File;

@Category(IntegrationTest.class)
public class ExportLogsOverHttpIntegrationTest extends ExportLogsIntegrationTest {

@Override
protected void connect() throws Exception {
public void connect() throws Exception {
gfsh.connectAndVerify(locator.getHttpPort(), GfshShellConnectionRule.PortType.http);
}

public File getWorkingDirectory() throws Exception {
return new File(System.getProperty("user.dir"));
}

}

0 comments on commit 122e650

Please sign in to comment.