Search in sources :

Example 11 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class DumpCommandIT method shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock.

@Test
@DisabledOnOs(OS.WINDOWS)
@DisabledForRoot
void shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock() throws Exception {
    DatabaseLayout databaseLayout = DatabaseLayout.ofFlat(databaseDirectory);
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
        Path file = databaseLayout.databaseLockFile();
        try (Closeable ignored = withPermissions(file, emptySet())) {
            CommandFailedException commandFailed = assertThrows(CommandFailedException.class, () -> execute("foo"));
            assertEquals("You do not have permission to dump the database.", commandFailed.getMessage());
        }
    }
}
Also used : Path(java.nio.file.Path) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) Closeable(java.io.Closeable) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) CommandFailedException(org.neo4j.cli.CommandFailedException) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test) DisabledForRoot(org.neo4j.test.extension.DisabledForRoot)

Example 12 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class DiagnosticsReportCommandIT method shouldBeAbleToAttachToPidAndRunThreadDump.

@Test
void shouldBeAbleToAttachToPidAndRunThreadDump() throws IOException {
    long pid = getPID();
    assertThat(pid).isNotEqualTo(0);
    // Write config file
    Files.createFile(testDirectory.file("neo4j.conf"));
    // write neo4j.pid file
    Path run = testDirectory.directory("run");
    Files.write(run.resolve("neo4j.pid"), String.valueOf(pid).getBytes());
    // Run command, should detect running instance
    try {
        String[] args = { "threads", "--to=" + testDirectory.absolutePath() + "/reports" };
        Path homeDir = testDirectory.homePath();
        var ctx = new ExecutionContext(homeDir, homeDir, System.out, System.err, testDirectory.getFileSystem());
        DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand(ctx);
        CommandLine.populateCommand(diagnosticsReportCommand, args);
        diagnosticsReportCommand.execute();
    } catch (CommandFailedException e) {
        if (e.getMessage().equals("Unknown classifier: threads")) {
            // If we get attach API is not available for example in some IBM jdk installs, ignore this test
            return;
        }
        throw e;
    }
    // Verify that we took a thread dump
    Path reports = testDirectory.directory("reports");
    Path[] files = FileUtils.listPaths(reports);
    assertThat(files).isNotNull();
    assertThat(files.length).isEqualTo(1);
    Path report = files[0];
    final URI uri = URI.create("jar:file:" + report.toUri().getRawPath());
    try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
        String threadDump = Files.readString(fs.getPath("threaddump.txt"));
        assertThat(threadDump).contains(DiagnosticsReportCommandIT.class.getCanonicalName());
    }
}
Also used : Path(java.nio.file.Path) ExecutionContext(org.neo4j.cli.ExecutionContext) FileSystem(java.nio.file.FileSystem) URI(java.net.URI) CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 13 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class DiagnosticsReportCommandTest method exitIfConfigFileIsMissing.

@Test
void exitIfConfigFileIsMissing() throws IOException {
    Files.delete(configFile);
    String[] args = { "--list" };
    DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand(ctx);
    CommandLine.populateCommand(diagnosticsReportCommand, args);
    CommandFailedException commandFailed = assertThrows(CommandFailedException.class, diagnosticsReportCommand::execute);
    assertThat(commandFailed.getMessage()).contains("Unable to find config file, tried: ");
}
Also used : CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 14 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class DiagnosticsReportCommandTest method printUnrecognizedClassifiers.

@Test
void printUnrecognizedClassifiers() throws Exception {
    String[] args = { "logs", "tx", "invalid" };
    DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand(ctx);
    CommandLine.populateCommand(diagnosticsReportCommand, args);
    CommandFailedException incorrectUsage = assertThrows(CommandFailedException.class, diagnosticsReportCommand::execute);
    assertEquals("Unknown classifier: invalid", incorrectUsage.getMessage());
}
Also used : CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 15 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class DumpCommand method dump.

private void dump(DatabaseLayout databaseLayout, Path archive) {
    Path databasePath = databaseLayout.databaseDirectory();
    try {
        var format = selectCompressionFormat(ctx.err());
        var lockFile = databaseLayout.databaseLockFile().getFileName().toString();
        var quarantineMarkerFile = databaseLayout.quarantineMarkerFile().getFileName().toString();
        dumper.dump(databasePath, databaseLayout.getTransactionLogsDirectory(), archive, format, path -> oneOf(path, lockFile, quarantineMarkerFile));
    } catch (FileAlreadyExistsException e) {
        throw new CommandFailedException("Archive already exists: " + e.getMessage(), e);
    } catch (NoSuchFileException e) {
        if (Paths.get(e.getMessage()).toAbsolutePath().equals(databasePath)) {
            throw new CommandFailedException("Database does not exist: " + databaseLayout.getDatabaseName(), e);
        }
        wrapIOException(e);
    } catch (IOException e) {
        wrapIOException(e);
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) CommandFailedException(org.neo4j.cli.CommandFailedException)

Aggregations

CommandFailedException (org.neo4j.cli.CommandFailedException)34 Test (org.junit.jupiter.api.Test)20 IOException (java.io.IOException)10 Path (java.nio.file.Path)10 Config (org.neo4j.configuration.Config)9 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)9 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)8 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)6 Closeable (java.io.Closeable)5 ExecutionContext (org.neo4j.cli.ExecutionContext)5 FileLockException (org.neo4j.kernel.internal.locker.FileLockException)5 NoSuchFileException (java.nio.file.NoSuchFileException)3 CheckConsistencyCommand (org.neo4j.consistency.CheckConsistencyCommand)3 ConsistencyCheckService (org.neo4j.consistency.ConsistencyCheckService)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 ConsistencyFlags (org.neo4j.consistency.checking.full.ConsistencyFlags)2 ProgressMonitorFactory (org.neo4j.internal.helpers.progress.ProgressMonitorFactory)2 DatabaseLocker (org.neo4j.kernel.internal.locker.DatabaseLocker)2 Locker (org.neo4j.kernel.internal.locker.Locker)2