Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Cleaned up tests to delete remote files
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Osborn <osbornjd@ornl.gov>
  • Loading branch information
Joe Osborn committed Feb 27, 2020
1 parent 3966487 commit 490da05
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 19 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -392,32 +398,68 @@ 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 {
RemoteRemoteFileTransferTest transferTest = new RemoteRemoteFileTransferTest();
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);

}

/**
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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);
Expand All @@ -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);

}

}
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 490da05

Please sign in to comment.