diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java index fe38b652b..baf03b8fd 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java @@ -21,6 +21,9 @@ import java.nio.file.FileSystems; import org.apache.sshd.client.subsystem.sftp.SftpClientFactory; +import org.eclipse.ice.commands.Command; +import org.eclipse.ice.commands.CommandConfiguration; +import org.eclipse.ice.commands.CommandFactory; import org.eclipse.ice.commands.CommandStatus; import org.eclipse.ice.commands.Connection; import org.eclipse.ice.commands.ConnectionAuthorizationHandler; @@ -89,6 +92,9 @@ public static void setUpBeforeClass() throws Exception { public static void tearDownAfterClass() throws Exception { ConnectionManager manager = ConnectionManagerFactory.getConnectionManager(); manager.removeAllConnections(); + + // Delete the output files that were created in the remote-remote test + RemoteRemoteFileTransferTest.tearDownAfterClass(); } /** @@ -287,7 +293,7 @@ public void testRemoteFileHandlerCopyCommand() throws Exception { // Get the file transfer handler IFileHandler handler = factory.getFileHandler(fileCreator.getConnection().getConfiguration()); String separator = FileSystems.getDefault().getSeparator(); - String filename = theSource.substring(theSource.lastIndexOf(separator)+1); + String filename = theSource.substring(theSource.lastIndexOf(separator) + 1); // Now try to copy the file CommandStatus status = handler.copy(theSource, theDestination); @@ -316,7 +322,7 @@ public void testSetHandleType() throws Exception { theSource = fileCreator.getSource(); theDestination = fileCreator.getDestination(); String separator = FileSystems.getDefault().getSeparator(); - String filename = theSource.substring(theSource.lastIndexOf(separator)+1); + String filename = theSource.substring(theSource.lastIndexOf(separator) + 1); FileHandler handler = factory.getFileHandler(fileCreator.getConnection().getConfiguration()); @@ -347,7 +353,7 @@ public void testRemoteFileHandlerMoveCommand() throws Exception { // Get the file transfer handler IFileHandler handler = factory.getFileHandler(fileCreator.getConnection().getConfiguration()); String separator = FileSystems.getDefault().getSeparator(); - String filename = theSource.substring(theSource.lastIndexOf(separator)+1); + String filename = theSource.substring(theSource.lastIndexOf(separator) + 1); // Now try to move the file CommandStatus status = handler.move(theSource, theDestination); assertEquals(CommandStatus.SUCCESS, status); @@ -392,11 +398,12 @@ public void testRemoteFileHandlerFactoryDestinationNonexistent() throws Exceptio fileCreator.deleteRemoteDestination(); } - + /** - * This tests the factory method returning a remote to remote file transfer - * over different hosts - * @throws IOException + * This tests the factory method returning a remote to remote file transfer over + * different hosts + * + * @throws IOException */ @Test public void testRemoteRemoteFileTransfer() throws IOException { @@ -404,20 +411,55 @@ public void testRemoteRemoteFileTransfer() throws IOException { transferTest.setupConnectionConfigs(); ConnectionConfiguration remoteHostB = transferTest.getRemoteHostBConnectionConfig(); KeyPathConnectionAuthorizationHandler remoteHostC = transferTest.getRemoteHostCAuth(); - + Connection bConn = ConnectionManagerFactory.getConnectionManager().openConnection(remoteHostB); bConn.setSftpChannel(SftpClientFactory.instance().createSftpClient(bConn.getSession())); - + transferTest.createRemoteHostBSourceFile(); theSource = transferTest.getSource(); theDestination = "/tmp/"; - + IFileHandler handler = factory.getFileHandler(remoteHostB, remoteHostC); // This checks existence, and regardless that check is handled by the unit test CommandStatus status = handler.copy(theSource, theDestination); assertTrue(status.equals(CommandStatus.SUCCESS)); transferTest.deleteHostBSource(); + + // Get the filename by splitting the path by "/" + String separator = "/"; + if (theSource.contains("\\")) + separator = "\\"; + + String[] tokens = theSource.split(separator); + String filename = tokens[tokens.length - 1]; + + // Delete file from remote host C + // Delete the file that was moved with another command + String remoteHostCKeyPath = remoteHostC.getKeyPath(); + String command = "ssh -i " + remoteHostCKeyPath + " " + remoteHostC.getUsername() + "@" + + remoteHostC.getHostname(); + command += " \"rm " + theDestination + filename + "\""; + + CommandConfiguration config = new CommandConfiguration(); + config.setExecutable(command); + config.setAppendInput(false); + config.setCommandId(99); + config.setErrFileName("lsErr.txt"); + config.setOutFileName("lsOut.txt"); + config.setNumProcs("1"); + // WD doesn't matter since we are rming with an absolute path + config.setWorkingDirectory("/tmp/doesnt/matter"); + + // Get the command + CommandFactory factory = new CommandFactory(); + Command rmCommand = factory.getCommand(config, remoteHostB); + + status = rmCommand.execute(); + // Just warn, since it isn't a huge deal if a file wasn't successfully deleted + if (!status.equals(CommandStatus.SUCCESS)) + System.out.println("Couldn't delete destination file at : " + theDestination + filename); + } /** diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileHandlerTest.java index 3ed79b17c..4076014ce 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileHandlerTest.java @@ -17,6 +17,9 @@ import java.nio.file.FileSystems; import org.apache.sshd.client.subsystem.sftp.SftpClientFactory; +import org.eclipse.ice.commands.Command; +import org.eclipse.ice.commands.CommandConfiguration; +import org.eclipse.ice.commands.CommandFactory; import org.eclipse.ice.commands.CommandStatus; import org.eclipse.ice.commands.Connection; import org.eclipse.ice.commands.ConnectionAuthorizationHandler; @@ -106,6 +109,9 @@ private static void makeRemoteHostConfigs() { @AfterClass public static void tearDownAfterClass() throws Exception { ConnectionManagerFactory.getConnectionManager().closeAllConnections(); + + // Delete the output files that were made + RemoteRemoteFileTransferTest.tearDownAfterClass(); } /** @@ -164,11 +170,15 @@ public void testCheckExistenceNonExistentDestination() throws IOException { RemoteRemoteFileHandler handler = new RemoteRemoteFileHandler(); handler.setConnectionConfiguration(remoteHostB); handler.setDestinationAuthorization(remoteHostCAuth); - - handler.checkExistence(source, "/nonexistent/destination/"); - - transferTest.deleteHostBSource(); - + try { + handler.checkExistence(source, "/nonexistent/destination/"); + } catch (IOException e) { + // Expected exception, but still want to delete the host source file + transferTest.deleteHostBSource(); + // If exception was caught, test was a success. So end it by throwing + // an exception + throw new IOException(); + } } /** @@ -187,6 +197,13 @@ public void testMove() throws IOException { transferTest.createRemoteHostBSourceFile(); source = transferTest.getSource(); destination = "/tmp/"; + // Get the filename by splitting the path by "/" + String separator = "/"; + if (source.contains("\\")) + separator = "\\"; + + String[] tokens = source.split(separator); + String filename = tokens[tokens.length - 1]; RemoteRemoteFileHandler handler = new RemoteRemoteFileHandler(); handler.setConnectionConfiguration(remoteHostB); @@ -198,6 +215,31 @@ public void testMove() throws IOException { assertTrue(status.equals(CommandStatus.SUCCESS)); transferTest.deleteHostBSource(); + // Delete file from remote host C + // Delete the file that was moved with another command + String remoteHostCKeyPath = remoteHostCAuth.getKeyPath(); + String command = "ssh -i " + remoteHostCKeyPath + " " + remoteHostCAuth.getUsername() + "@" + remoteHostCAuth.getHostname(); + command += " \"rm " + destination + filename + "\""; + + CommandConfiguration config = new CommandConfiguration(); + config.setExecutable(command); + config.setAppendInput(false); + config.setCommandId(99); + config.setErrFileName("lsErr.txt"); + config.setOutFileName("lsOut.txt"); + config.setNumProcs("1"); + // WD doesn't matter since we are rming with an absolute path + config.setWorkingDirectory("/tmp/doesnt/matter"); + + // Get the command + CommandFactory factory = new CommandFactory(); + Command rmCommand = factory.getCommand(config, remoteHostB); + + status = rmCommand.execute(); + // Just warn, since it isn't a huge deal if a file wasn't successfully deleted + if (!status.equals(CommandStatus.SUCCESS)) + System.out.println("Couldn't delete destination file at : " + destination + filename); + } } diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileTransferTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileTransferTest.java index 7ac9a362a..40b58b1aa 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileTransferTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/RemoteRemoteFileTransferTest.java @@ -76,7 +76,7 @@ public class RemoteRemoteFileTransferTest { * Remote host C key path that is needed to establish connection between host B * and host C */ - String remoteHostCKeyPath = "/path/to/keykey"; + String remoteHostCKeyPath = "/home/4jo/.ssh/dummykey"; /** * Authorization for remote host C @@ -245,9 +245,9 @@ protected void setupConnectionConfigs() { hostBConnection = new ConnectionConfiguration(); hostBConnection.setName("hostB"); ConnectionAuthorizationHandler bauth = new KeyPathConnectionAuthorizationHandler(); - bauth.setHostname("host"); - bauth.setUsername("user"); - bauth.setOption("/path/to/key"); + bauth.setHostname("denisovan"); + bauth.setUsername("4jo"); + bauth.setOption("/home/4jo/.ssh/denisovankey"); hostBConnection.setAuthorization(bauth); @@ -323,6 +323,7 @@ protected boolean checkPathExistsRemoteHostC() throws IOException { // Just warn, since it isn't a huge deal if a file wasn't successfully deleted if (!status.equals(CommandStatus.SUCCESS)) System.out.println("Couldn't delete destination file at : " + destination); + return true; }