This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
CommandFactoryTest.java
727 lines (620 loc) · 25.4 KB
/
CommandFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*******************************************************************************
* Copyright (c) 2019- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.tests.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import org.apache.sshd.client.subsystem.sftp.SftpClient;
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;
import org.eclipse.ice.commands.ConnectionAuthorizationHandlerFactory;
import org.eclipse.ice.commands.ConnectionConfiguration;
import org.eclipse.ice.commands.ConnectionManager;
import org.eclipse.ice.commands.ConnectionManagerFactory;
import org.eclipse.ice.commands.LocalFileHandler;
import org.eclipse.ice.commands.RemoteCommand;
import org.eclipse.ice.commands.RemoteFileHandler;
import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* This class tests {@link org.eclipse.ice.commands.CommandFactory}.
*
* @author Jay Jay Billings, Joe Osborn
*
*/
public class CommandFactoryTest {
/**
* The hostname for which the job should run on. Default to local host name for
* now
*/
String hostname = getLocalHostname();
/**
* Create a command factory to use for getting the commands.
*/
CommandFactory factory = new CommandFactory();
/**
* A string containing the working directory for the executable to run in
*/
String workingDirectory;
/**
* Get the present working directory to run things in
*/
String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/";
/**
* A connection configuration with which to test
*/
ConnectionConfiguration connectionConfig = new ConnectionConfiguration();
/**
* Default constructor
*/
public CommandFactoryTest() {
}
/**
* Close the connections after we are finished with them in an individual test
*
* @throws Exception
*/
@After
public void tearDown() throws Exception {
ConnectionManager manager = ConnectionManagerFactory.getConnectionManager();
manager.removeAllConnections();
}
/**
* Set no strict host key checking just for tests
*
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* Run after the tests have finished processing. This function just removes the
* dummy text files that are created with log/error information from running
* various commands tests.
*
* @throws IOException
* @throws InterruptedException
*/
@AfterClass
public static void tearDownAfterClass() throws IOException, InterruptedException {
// Make and execute a simple command to remove the text files created
// in these tests.
// Make a string of all the output file names in this test
// Didn't want to do *.txt, in the event that it inadvertently deletes other
// files on people's computers
String rm = "someLocalErrFile.txt someLocalOutFile.txt someLocalErrFileDir.txt someLocalOutFileDir.txt";
rm += " someRemoteErrFile.txt someRemoteOutFile.txt someMultLocalErrFile.txt someMultLocalOutFile.txt";
rm += " someLsOutFile.txt someLsErrFile.txt someMultRemoteOutFile.txt someMultRemoteErrFile.txt";
rm += " somePythOutFile.txt somePythErrFile.txt someLsRemoteErrFile.txt someLsRemoteOutFile.txt";
rm += " src/test/java/org/eclipse/ice/tests/someInputFile.txt src/test/java/org/eclipse/ice/tests/someOtherInputFile.txt";
rm += " pythOutFile.txt pythErrFile.txt hopRemoteOutFile.txt hopRemoteErrFile.txt";
ArrayList<String> command = new ArrayList<String>();
// Build a command
if (System.getProperty("os.name").toLowerCase().contains("win")) {
command.add("powershell.exe");
} else {
command.add("/bin/bash");
command.add("-c");
}
command.add("rm " + rm);
// Execute the command with the process builder api
ProcessBuilder builder = new ProcessBuilder(command);
// Files exist in the top most directory of the package
String topDir = System.getProperty("user.dir");
File file = new File(topDir);
builder.directory(file);
// Process it
Process job = builder.start();
job.waitFor(); // wait for it to finish
// Remove lal the connections that were opened in testing
ConnectionManager manager = ConnectionManagerFactory.getConnectionManager();
manager.removeAllConnections();
}
/**
* This function tests a multi-hop remote command, where the command logs into a
* remote host and then executes on a different remote host.
*/
@Test
public void testMultiHopRemoteCommand() {
System.out.println("\n\n\nTesting a multi-hop remote command");
// Set the CommandConfiguration class
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.setExecutable("./test_code_execution.sh");
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.addInputFile("someOtherFile", "someOtherInputFile.txt");
commandConfig.setAppendInput(true);
commandConfig.setCommandId(99);
commandConfig.setErrFileName("hopRemoteErrFile.txt");
commandConfig.setOutFileName("hopRemoteOutFile.txt");
// This is the directory to run the job on the destination system, i.e.
// system C
commandConfig.setRemoteWorkingDirectory("/tmp/remoteCommandTestDirectory");
// This is the directory on the jump host which contains the job information
// and files, e.g. the script, input files, etc.
commandConfig.setWorkingDirectory("/home/4jo/remoteCommandDirectory");
// Set the connection configuration to a dummy remote connection
// This is the connection where the job will be executed
// Get a factory which determines the type of authorization
ConnectionAuthorizationHandlerFactory authFactory = new ConnectionAuthorizationHandlerFactory();
// Request a ConnectionAuthorization of type text file which contains the
// credentials
String keyPath = System.getProperty("user.home") + "/.ssh/denisovankey";
ConnectionAuthorizationHandler auth = authFactory.getConnectionAuthorizationHandler("keypath",
keyPath);
auth.setHostname("denisovan");
auth.setUsername("4jo");
// Set it
ConnectionConfiguration firstConn = new ConnectionConfiguration();
firstConn.setAuthorization(auth);
// Note the password can be input at the console by not setting the
// the password explicitly in the connection configuration
firstConn.setName("hopConnection");
firstConn.deleteWorkingDirectory(false);
ConnectionConfiguration secondConn = new ConnectionConfiguration();
String credFile = "/tmp/ice-remote-creds.txt";
if(System.getProperty("os.name").toLowerCase().contains("win"))
credFile = "C:\\Users\\Administrator\\ice-remote-creds.txt";
ConnectionAuthorizationHandler intermAuth = authFactory.getConnectionAuthorizationHandler("text",credFile);
secondConn.setAuthorization(intermAuth);
secondConn.setName("executeConnection");
secondConn.deleteWorkingDirectory(false);
// Get the command
Command remoteCommand = null;
try {
remoteCommand = factory.getCommand(commandConfig, firstConn, secondConn);
} catch (IOException e) {
e.printStackTrace();
}
// Run it
CommandStatus status = remoteCommand.execute();
// assert that it was successful
assertEquals(CommandStatus.SUCCESS, status);
}
/**
* This function tests a more boring command, e.g. just executing "ls" at the
* command prompt This shows that the API can be used to execute basic command
* line prompts.
*/
@Test
public void testBoringCommandLocally() {
System.out.println("Test boring command locally");
// Make the command configuration
CommandConfiguration cmdCfg = setupDefaultCommandConfig();
cmdCfg.setExecutable("ls -lrt");
if (System.getProperty("os.name").toLowerCase().contains("win")) {
// Add powershell interpeter if os is windows
cmdCfg.setInterpreter("powershell.exe");
// just use ls because powershell automatically adds the -lrt
// and doesn't know what -lrt is anyway
cmdCfg.setExecutable("ls");
}
cmdCfg.setNumProcs("1");
cmdCfg.setInstallDirectory("");
cmdCfg.setWorkingDirectory(pwd);
cmdCfg.setAppendInput(false);
cmdCfg.setCommandId(1);
cmdCfg.setErrFileName("someLsErrFile.txt");
cmdCfg.setOutFileName("someLsOutFile.txt");
// Make the connection configuration
ConnectionConfiguration ctCfg = new ConnectionConfiguration();
ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setHostname(hostname);
ctCfg.setAuthorization(handler);
// Get and run the command
Command cmd = null;
try {
cmd = factory.getCommand(cmdCfg, ctCfg);
} catch (IOException e) {
e.printStackTrace();
}
CommandStatus status = cmd.execute();
// Check that it properly finished
assertEquals(CommandStatus.SUCCESS, status);
System.out.println("Finished boring command locally");
}
/**
* This function tests a more boring command, e.g. just executing "ls" at the
* command prompt This shows that the API can be used to execute basic command
* line prompts.
*/
@Test
public void testBoringCommandRemotely() {
System.out.println("Test remotely ls");
// Setup the command configuration
CommandConfiguration cmdCfg = setupDefaultCommandConfig();
cmdCfg.setExecutable("ls -lrt");
cmdCfg.setAppendInput(false);
cmdCfg.setRemoteWorkingDirectory("/tmp/");
cmdCfg.setCommandId(1);
cmdCfg.setErrFileName("someLsRemoteErrFile.txt");
cmdCfg.setOutFileName("someLsRemoteOutFile.txt");
// Setup the connection configuration
ConnectionConfiguration ctCfg = setupDummyConnectionConfiguration();
ctCfg.deleteWorkingDirectory(false);
// Get and run the command
Command cmd = null;
try {
cmd = factory.getCommand(cmdCfg, ctCfg);
} catch (IOException e) {
e.printStackTrace();
}
CommandStatus status = cmd.execute();
// Assert that it finished correctly
assertEquals(CommandStatus.SUCCESS, status);
}
/**
* This function tests with real files to test an actual job processing. The job
* executes a script with some hello world commands in it.
*/
@Test
public void testFunctionalLocalCommand() {
System.out.println("Test functional local command");
// Set some things specific to the local command
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.setExecutable("./test_code_execution.sh");
// If it is windows, configure the test to run on windows
if (System.getProperty("os.name").toLowerCase().contains("win")) {
commandConfig.setExecutable(".\\test_code_execution.ps1");
commandConfig.setInterpreter("powershell.exe");
}
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.setAppendInput(true);
commandConfig.setCommandId(1);
commandConfig.setErrFileName("someLocalErrFile.txt");
commandConfig.setOutFileName("someLocalOutFile.txt");
// Make a default boring connection authorization
ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setHostname(hostname);
connectionConfig.setAuthorization(handler);
// Get the command
Command localCommand = null;
try {
localCommand = factory.getCommand(commandConfig, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}
// Run it
CommandStatus status = localCommand.execute();
assertEquals(CommandStatus.SUCCESS, status);
System.out.println("Finished functional local command");
}
/**
* Test method for {@link org.eclipse.ice.commands.CommandFactory#getCommand()}
* and for the whole {@link org.eclipse.ice.commands.LocalCommand#execute()}
* execution chain with an incomplete Command dictionary. This function is
* intended to catch the "exception" catching of the API, where the exception is
* in quotes due to the return of a bad CommandStatus rather than e.g. a true
* java Exception.
*/
@Test
public void testNonFunctionalLocalCommand() {
System.out.println("\nTesting some commands where not enough command information was provided.");
// Create a command configuration that doesn't have all the necessary
// information
// Set the CommandConfiguration class
CommandConfiguration badCommandConfig = new CommandConfiguration();
ConnectionConfiguration badConnectConfig = new ConnectionConfiguration();
ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setHostname(hostname);
badConnectConfig.setAuthorization(handler);
// Get the command
Command localCommand = null;
try {
localCommand = factory.getCommand(badCommandConfig, badConnectConfig);
} catch (IOException e) {
e.printStackTrace();
}
// Run it and expect that it fails
CommandStatus status = localCommand.execute();
assertEquals(CommandStatus.INFOERROR, status);
}
/**
* Test method for {@link org.eclipse.ice.commands.CommandFactory#getCommand()}
* and for the whole {@link org.eclipse.ice.commands.LocalCommand#execute()}
* execution chain with an uncompleted Command dictionary. This function is
* intended to test some of the exception catching, thus it is expected to
* "fail."
*/
@Test
public void testIncorrectWorkingDirectory() {
/**
* Run another non functional command, with a non existing working directory
*/
System.out.println("\nTesting a command where a nonexistent working directory was provided.");
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.setExecutable("./test_code_execution.sh");
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.setAppendInput(true);
// Set the commandConfig class
commandConfig.setCommandId(3);
commandConfig.setErrFileName("someLocalErrFileDir.txt");
commandConfig.setOutFileName("someLocalOutFileDir.txt");
commandConfig.setWorkingDirectory("/tmp/some_nonexistent_directory");
ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setHostname(hostname);
connectionConfig.setAuthorization(handler);
// Get the command
Command localCommand2 = null;
try {
localCommand2 = factory.getCommand(commandConfig, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}
// Run it and expect that it fails
CommandStatus status2 = localCommand2.execute();
assertEquals(CommandStatus.FAILED, status2);
}
/**
* This function tests a functional remote command with the full command factory
* implementation
*/
@Test
public void testFunctionalRemoteCommand() {
System.out.println("\n\n\nTesting a functional remote command");
// Set the CommandConfiguration class
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.setExecutable("./test_code_execution.sh");
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.setAppendInput(true);
commandConfig.setCommandId(4);
commandConfig.setErrFileName("someRemoteErrFile.txt");
commandConfig.setOutFileName("someRemoteOutFile.txt");
commandConfig.setRemoteWorkingDirectory("/tmp/remoteCommandTestDirectory");
// Get the connection configuration for remote testing
ConnectionConfiguration config = setupDummyConnectionConfiguration();
// Get the command
Command remoteCommand = null;
try {
remoteCommand = factory.getCommand(commandConfig, config);
} catch (IOException e) {
e.printStackTrace();
}
// Run it
CommandStatus status = remoteCommand.execute();
// assert that it was successful
assertEquals(CommandStatus.SUCCESS, status);
System.out.println("Finished remote functional command");
}
/**
* This tests a command which requires multiple input files to run remotely
*/
@Test
public void testMultipleInputFilesRemotely() {
System.out.println("Test multiple input files remotely");
// Set the CommandConfiguration class
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.setExecutable("./test_code_execution.sh");
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.setAppendInput(true);
commandConfig.setCommandId(5);
commandConfig.setErrFileName("someMultRemoteErrFile.txt");
commandConfig.setOutFileName("someMultRemoteOutFile.txt");
commandConfig.setRemoteWorkingDirectory("/tmp/remoteCommandTestDirectoryMult");
// Add another input file to the list of input files already started
commandConfig.addInputFile("someOtherFile", "someOtherInputFile.txt");
// Get the connection configuration
ConnectionConfiguration config = setupDummyConnectionConfiguration();
// Get the command
Command remoteCommand = null;
try {
remoteCommand = factory.getCommand(commandConfig, config);
} catch (IOException e) {
e.printStackTrace();
}
// Run it
CommandStatus status = remoteCommand.execute();
// assert that it was successful
assertEquals(CommandStatus.SUCCESS, status);
System.out.println("Finished multiple input files remotely");
}
/**
* This tests a command which requires multiple input files to run locally
*/
@Test
public void testMultipleInputFilesLocally() {
System.out.println("test multiple input files locally");
// Set some things specific to the local command
CommandConfiguration commandConfig = setupDefaultCommandConfig();
commandConfig.addInputFile("someInputFile", "someInputFile.txt");
commandConfig.setCommandId(6);
commandConfig.setErrFileName("someMultLocalErrFile.txt");
commandConfig.setOutFileName("someMultLocalOutFile.txt");
// Add another input file
commandConfig.addInputFile("someOtherFile", "someOtherInputFile.txt");
// Also tests case where append input is set to false and the arguments
// are passed as variables as follows
commandConfig.setExecutable("./test_code_execution.sh ${someInputFile} ${someOtherFile}");
if (System.getProperty("os.name").toLowerCase().contains("win")) {
commandConfig.setInterpreter("powershell.exe");
commandConfig.setExecutable(".\\test_code_execution.ps1");
}
commandConfig.setAppendInput(false);
ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setHostname(hostname);
connectionConfig.setAuthorization(handler);
// Get the command
Command localCommand = null;
try {
localCommand = factory.getCommand(commandConfig, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}
// Run it
CommandStatus status = localCommand.execute();
assertEquals(CommandStatus.SUCCESS, status);
System.out.println("Finished testing multiple input files locally");
}
/**
* This function tests the processing of a hello world python script, rather
* than a bash script
*/
@Test
public void testPythonScript() {
System.out.println("Testing python script");
// Get the present working directory
String pwd = System.getProperty("user.dir");
// Create the path relative to the current directory where the test script lives
String scriptDir = pwd + "/src/test/java/org/eclipse/ice/tests/commands/";
// Create a command configuration corresponding to a python script
CommandConfiguration configuration = setupDefaultCommandConfig();
configuration.setExecutable("test_python_script.py");
configuration.setInterpreter("python");
configuration.setCommandId(9);
configuration.setErrFileName("somePythErrFile.txt");
configuration.setOutFileName("somePythOutFile.txt");
configuration.setAppendInput(true);
configuration.addInputFile("inputfile", "someInputFile.txt");
configuration.addInputFile("inputfile2", "someOtherInputFile.txt");
configuration.setWorkingDirectory(scriptDir);
configuration.setRemoteWorkingDirectory("/tmp/pythonTest");
// Get the dummy connection configuration
ConnectionConfiguration connectionConfig = setupDummyConnectionConfiguration();
// Get the command and run it
Command command = null;
try {
command = factory.getCommand(configuration, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}
CommandStatus status = command.execute();
assertEquals(CommandStatus.SUCCESS, status);
}
/**
* This function tests the execution of a command where the executable lives on
* the remote host and the input files live on the local host
*/
@Test
public void testRemoteExecutableLocalInputFiles() {
System.out.println("Testing command where files live on different hosts.");
// Get the dummy connection configuration
ConnectionConfiguration connectionConfig = setupDummyConnectionConfiguration();
// Get the present working directory
String pwd = System.getProperty("user.dir");
// Create the path relative to the current directory where the test script lives
String inputFileDir = pwd + "/src/test/java/org/eclipse/ice/tests/";
// Copy the input files to this directory for this test. They are removed at the
// end of the class. Useful for putting the input files in a different directory
// in the repo from where the execution script lives.
LocalFileHandler handler = new LocalFileHandler();
handler.copy(inputFileDir + "commands/someInputFile.txt", inputFileDir);
handler.copy(inputFileDir + "commands/someOtherInputFile.txt", inputFileDir);
RemoteFileHandler remoteHandler = new RemoteFileHandler();
remoteHandler.setConnectionConfiguration(connectionConfig);
remoteHandler.copy(inputFileDir + "commands/test_python_script.py", "/tmp/test_python_script.py");
// Create a command configuration corresponding to a python script
CommandConfiguration configuration = setupDefaultCommandConfig();
// This path exists on the dummy host server
configuration.setExecutable("/tmp/test_python_script.py");
configuration.setInterpreter("python");
configuration.setCommandId(9);
configuration.setErrFileName("pythErrFile.txt");
configuration.setOutFileName("pythOutFile.txt");
configuration.setAppendInput(true);
configuration.setWorkingDirectory(inputFileDir);
configuration.addInputFile("inputfile", "someInputFile.txt");
configuration.addInputFile("inputfile2", "someOtherInputFile.txt");
configuration.setRemoteWorkingDirectory("/tmp/pythonTest");
// Get the command and run it
Command command = null;
try {
command = factory.getCommand(configuration, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}
CommandStatus status = command.execute();
assertEquals(CommandStatus.SUCCESS, status);
// Delete the moved python script on the remote server once finished
// Get the connection and channel to delete
Connection connection = ((RemoteCommand) command).getConnection();
// Delete the script
try {
SftpClient client = SftpClientFactory.instance().createSftpClient(connection.getSession());
// open the channel
connection.setSftpChannel(client);
SftpClient channel = connection.getSftpChannel();
// connect and delete the script
channel.remove("/tmp/test_python_script.py");
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("finished python script test");
}
/**
* This function sets up and returns a default case of a command configuration
* for use throughout the tests.
*
* @return
*/
private CommandConfiguration setupDefaultCommandConfig() {
CommandConfiguration config = new CommandConfiguration();
config.setNumProcs("1");
config.setInstallDirectory("");
config.setWorkingDirectory(pwd);
config.setOS(System.getProperty("os.name"));
return config;
}
/**
* A helper function to return the dummy connection configuration for remote
* testing
*
* @return - dummy connection configuration
*/
private ConnectionConfiguration setupDummyConnectionConfiguration() {
ConnectionConfiguration cfg = new ConnectionConfiguration();
// Make the connection configuration
// Get a factory which determines the type of authorization
ConnectionAuthorizationHandlerFactory authFactory = new ConnectionAuthorizationHandlerFactory();
// Request a ConnectionAuthorization of type text file which contains the
// credentials
String credFile = "/tmp/ice-remote-creds.txt";
if (System.getProperty("os.name").toLowerCase().contains("win"))
credFile = "C:\\Users\\Administrator\\ice-remote-creds.txt";
ConnectionAuthorizationHandler auth = authFactory.getConnectionAuthorizationHandler("text", credFile);
// Set it
cfg.setAuthorization(auth);
// Note the password can be input at the console by not setting the
// the password explicitly in the connection configuration
cfg.setName("dummyConnection");
cfg.deleteWorkingDirectory(true);
return cfg;
}
/**
* This function just returns the local hostname of your local computer. It is
* useful for testing a variety of local commands.
*
* @return - String - local hostname
*/
protected static String getLocalHostname() {
// Get the hostname for your local computer
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
String hostname = addr.getHostName();
return hostname;
}
}