97c5fd8b533c1f3a0fdb204b108cec1286a54c4f
[jabaws.git] / testsrc / compbio / runner / disorder / GlobPlotTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.runner.disorder;\r
20 \r
21 import static org.testng.Assert.assertEquals;\r
22 import static org.testng.Assert.assertFalse;\r
23 import static org.testng.Assert.assertNotNull;\r
24 import static org.testng.Assert.assertNull;\r
25 import static org.testng.Assert.assertTrue;\r
26 import static org.testng.Assert.fail;\r
27 \r
28 import java.io.File;\r
29 import java.io.FileInputStream;\r
30 import java.io.FileNotFoundException;\r
31 import java.io.IOException;\r
32 import java.text.ParseException;\r
33 import java.util.Map;\r
34 import java.util.Set;\r
35 \r
36 import javax.xml.bind.ValidationException;\r
37 \r
38 import org.ggf.drmaa.DrmaaException;\r
39 import org.ggf.drmaa.JobInfo;\r
40 import org.testng.annotations.BeforeMethod;\r
41 import org.testng.annotations.Test;\r
42 \r
43 import compbio.data.sequence.Score;\r
44 import compbio.engine.AsyncExecutor;\r
45 import compbio.engine.Configurator;\r
46 import compbio.engine.SyncExecutor;\r
47 import compbio.engine.client.ConfExecutable;\r
48 import compbio.engine.client.ConfiguredExecutable;\r
49 import compbio.engine.client.Executable;\r
50 import compbio.engine.client.RunConfiguration;\r
51 import compbio.engine.cluster.drmaa.ClusterUtil;\r
52 import compbio.engine.cluster.drmaa.JobRunner;\r
53 import compbio.engine.cluster.drmaa.StatisticManager;\r
54 import compbio.engine.local.LocalRunner;\r
55 import compbio.metadata.AllTestSuit;\r
56 import compbio.metadata.JobExecutionException;\r
57 import compbio.metadata.JobStatus;\r
58 import compbio.metadata.JobSubmissionException;\r
59 import compbio.metadata.LimitsManager;\r
60 import compbio.metadata.PresetManager;\r
61 import compbio.metadata.ResultNotAvailableException;\r
62 import compbio.metadata.RunnerConfig;\r
63 import compbio.util.SysPrefs;\r
64 \r
65 public class GlobPlotTester {\r
66 \r
67         public static String test_outfile = "TO1381.globprot.out";\r
68 \r
69         private GlobPlot globprot;\r
70 \r
71         @BeforeMethod(alwaysRun = true)\r
72         void init() {\r
73                 globprot = new GlobPlot();\r
74                 globprot.setInput(AllTestSuit.test_input).setOutput(test_outfile);\r
75         }\r
76 \r
77         @Test(groups = {AllTestSuit.test_group_cluster,\r
78                         AllTestSuit.test_group_runner})\r
79         public void testRunOnCluster() {\r
80                 assertFalse(SysPrefs.isWindows,\r
81                                 "Cluster execution can only be in unix environment");\r
82                 try {\r
83                         ConfiguredExecutable<GlobPlot> confGlobPlot = Configurator\r
84                                         .configureExecutable(globprot,\r
85                                                         Executable.ExecProvider.Cluster);\r
86                         JobRunner runner = JobRunner.getInstance(confGlobPlot);\r
87 \r
88                         assertNotNull(runner, "Runner is NULL");\r
89                         runner.executeJob();\r
90                         // assertNotNull("JobId is null", jobId1);\r
91                         JobStatus status = runner.getJobStatus();\r
92                         assertTrue(status == JobStatus.PENDING\r
93                                         || status == JobStatus.RUNNING,\r
94                                         "Status of the process is wrong!");\r
95                         JobInfo info = runner.getJobInfo();\r
96                         assertNotNull(info, "JobInfo is null");\r
97                         StatisticManager sm = new StatisticManager(info);\r
98                         assertNotNull(sm, "Statictic manager is null");\r
99                         try {\r
100 \r
101                                 String exits = sm.getExitStatus();\r
102                                 assertNotNull("Exit status is null", exits);\r
103                                 // cut 4 trailing zeros from the number\r
104                                 int exitsInt = ClusterUtil.CLUSTER_STAT_IN_SEC.parse(exits)\r
105                                                 .intValue();\r
106                                 assertEquals(0, exitsInt);\r
107                                 System.out.println(sm.getAllStats());\r
108 \r
109                         } catch (ParseException e) {\r
110                                 e.printStackTrace();\r
111                                 fail("Parse Exception: " + e.getMessage());\r
112                         }\r
113                         // assertFalse(runner.cleanup());\r
114                         assertTrue(sm.hasExited());\r
115                         assertFalse(sm.wasAborted());\r
116                         assertFalse(sm.hasDump());\r
117                         assertFalse(sm.hasSignaled());\r
118 \r
119                 } catch (JobSubmissionException e) {\r
120                         e.printStackTrace();\r
121                         fail("DrmaaException caught:" + e.getMessage());\r
122                 } catch (JobExecutionException e) {\r
123                         e.printStackTrace();\r
124                         fail("DrmaaException caught:" + e.getMessage());\r
125                 } catch (DrmaaException e) {\r
126                         e.printStackTrace();\r
127                         fail("DrmaaException caught:" + e.getMessage());\r
128                 }\r
129         }\r
130 \r
131         /**\r
132          * This tests fails from time to time depending on the cluster load or some\r
133          * other factors. Any client code has to adjust for this issue\r
134          */\r
135         @Test(groups = {AllTestSuit.test_group_cluster,\r
136                         AllTestSuit.test_group_runner})\r
137         public void testRunOnClusterAsync() {\r
138                 assertFalse(SysPrefs.isWindows,\r
139                                 "Cluster execution can only be in unix environment");\r
140                 try {\r
141                         ConfiguredExecutable<GlobPlot> confGlobPlot = Configurator\r
142                                         .configureExecutable(globprot,\r
143                                                         Executable.ExecProvider.Cluster);\r
144                         AsyncExecutor aengine = Configurator.getAsyncEngine(confGlobPlot);\r
145                         String jobId = aengine.submitJob(confGlobPlot);\r
146                         assertNotNull(jobId, "Runner is NULL");\r
147                         // let drmaa to start\r
148                         Thread.sleep(500);\r
149                         JobStatus status = aengine.getJobStatus(jobId);\r
150                         while (status != JobStatus.FINISHED) {\r
151                                 System.out.println("Job Status: " + status);\r
152                                 Thread.sleep(1000);\r
153                                 status = aengine.getJobStatus(jobId);\r
154                                 ConfiguredExecutable<GlobPlot> result = (ConfiguredExecutable<GlobPlot>) aengine\r
155                                                 .getResults(jobId);\r
156                                 assertNotNull(result);\r
157                                 System.out.println("RES:" + result);\r
158                                 // Some times the job could be removed from the cluster\r
159                                 // accounting\r
160                                 // before it has been reported to finish. Make sure\r
161                                 // to stop waiting in such case\r
162                                 if (status == JobStatus.UNDEFINED) {\r
163                                         break;\r
164                                 }\r
165                         }\r
166                 } catch (JobSubmissionException e) {\r
167                         e.printStackTrace();\r
168                         fail("DrmaaException caught:" + e.getMessage());\r
169                 } catch (InterruptedException e) {\r
170                         e.printStackTrace();\r
171                         fail(e.getMessage());\r
172                 } catch (ResultNotAvailableException e) {\r
173                         e.printStackTrace();\r
174                         fail(e.getMessage());\r
175                 }\r
176         }\r
177 \r
178         @Test(groups = {AllTestSuit.test_group_runner})\r
179         public void testRunLocally() {\r
180                 try {\r
181                         ConfiguredExecutable<GlobPlot> confGlobPlot = Configurator\r
182                                         .configureExecutable(globprot,\r
183                                                         Executable.ExecProvider.Local);\r
184 \r
185                         // For local execution use relative\r
186                         LocalRunner lr = new LocalRunner(confGlobPlot);\r
187                         lr.executeJob();\r
188                         ConfiguredExecutable<?> al1 = lr.waitForResult();\r
189                         assertNotNull(al1.getResults());\r
190                         Map<String, Set<Score>> al2 = confGlobPlot.getResults();\r
191                         assertNotNull(al2);\r
192                         assertEquals(al2.size(), 3);\r
193                         assertEquals(al1.getResults(), al2);\r
194                 } catch (JobSubmissionException e) {\r
195                         e.printStackTrace();\r
196                         fail(e.getLocalizedMessage());\r
197                 } catch (ResultNotAvailableException e) {\r
198                         e.printStackTrace();\r
199                         fail(e.getLocalizedMessage());\r
200                 } catch (JobExecutionException e) {\r
201                         e.printStackTrace();\r
202                         fail(e.getLocalizedMessage());\r
203                 }\r
204         }\r
205 \r
206         @Test(groups = {AllTestSuit.test_group_runner})\r
207         public void readStatistics() {\r
208                 // No execution statistics is available!\r
209         }\r
210 \r
211         @Test(groups = {AllTestSuit.test_group_runner})\r
212         public void testPersistance() {\r
213                 try {\r
214                         GlobPlot disembl = new GlobPlot();\r
215                         disembl.setError("errrr.txt").setInput(AllTestSuit.test_input)\r
216                                         .setOutput("outtt.txt");\r
217                         assertEquals(disembl.getInput(), AllTestSuit.test_input);\r
218                         assertEquals(disembl.getError(), "errrr.txt");\r
219                         assertEquals(disembl.getOutput(), "outtt.txt");\r
220                         ConfiguredExecutable<GlobPlot> cGlobPlot = Configurator\r
221                                         .configureExecutable(disembl, Executable.ExecProvider.Local);\r
222 \r
223                         SyncExecutor sexec = Configurator.getSyncEngine(cGlobPlot);\r
224                         sexec.executeJob();\r
225                         ConfiguredExecutable<?> al = sexec.waitForResult();\r
226                         assertNotNull(al.getResults());\r
227                         // Save run configuration\r
228                         assertTrue(cGlobPlot.saveRunConfiguration());\r
229 \r
230                         // See if loaded configuration is the same as saved\r
231                         RunConfiguration loadedRun = RunConfiguration\r
232                                         .load(new FileInputStream(new File(cGlobPlot\r
233                                                         .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
234                         assertEquals(\r
235                                         ((ConfExecutable<GlobPlot>) cGlobPlot)\r
236                                                         .getRunConfiguration(),\r
237                                         loadedRun);\r
238                         // Load run configuration as ConfExecutable\r
239                         ConfiguredExecutable<GlobPlot> resurrectedCGlobPlot = (ConfiguredExecutable<GlobPlot>) cGlobPlot\r
240                                         .loadRunConfiguration(new FileInputStream(new File(\r
241                                                         cGlobPlot.getWorkDirectory(),\r
242                                                         RunConfiguration.rconfigFile)));\r
243                         assertNotNull(resurrectedCGlobPlot);\r
244                         assertEquals(resurrectedCGlobPlot.getExecutable().getInput(),\r
245                                         AllTestSuit.test_input);\r
246                         assertEquals(resurrectedCGlobPlot.getExecutable().getError(),\r
247                                         "errrr.txt");\r
248                         assertEquals(resurrectedCGlobPlot.getExecutable().getOutput(),\r
249                                         "outtt.txt");\r
250                         // See in details whether executables are the same\r
251                         assertEquals(resurrectedCGlobPlot.getExecutable(), disembl);\r
252 \r
253                         ConfiguredExecutable<GlobPlot> resGlobPlot = Configurator\r
254                                         .configureExecutable(resurrectedCGlobPlot.getExecutable(),\r
255                                                         Executable.ExecProvider.Local);\r
256 \r
257                         sexec = Configurator.getSyncEngine(resGlobPlot,\r
258                                         Executable.ExecProvider.Local);\r
259                         sexec.executeJob();\r
260                         al = sexec.waitForResult();\r
261                         assertNotNull(al);\r
262 \r
263                 } catch (JobSubmissionException e) {\r
264                         e.printStackTrace();\r
265                         fail(e.getMessage());\r
266                 } catch (JobExecutionException e) {\r
267                         e.printStackTrace();\r
268                         fail(e.getMessage());\r
269                 } catch (FileNotFoundException e) {\r
270                         e.printStackTrace();\r
271                         fail(e.getMessage());\r
272                 } catch (IOException e) {\r
273                         e.printStackTrace();\r
274                         fail(e.getMessage());\r
275                 } catch (ResultNotAvailableException e) {\r
276                         e.printStackTrace();\r
277                         fail(e.getMessage());\r
278                 }\r
279         }\r
280 \r
281         @Test(groups = {AllTestSuit.test_group_runner})\r
282         public void testConfigurationLoading() {\r
283                 try {\r
284                         RunnerConfig<GlobPlot> globplotConfig = ConfExecutable\r
285                                         .getRunnerOptions(GlobPlot.class);\r
286                         // There is no GlobPlot parameters \r
287                         assertNull(globplotConfig);\r
288                         \r
289                         PresetManager<GlobPlot> disemblPresets = ConfExecutable\r
290                                         .getRunnerPresets(GlobPlot.class);\r
291                         assertNull(disemblPresets); // there is no presets\r
292 \r
293                         LimitsManager<GlobPlot> disemblLimits = ConfExecutable\r
294                                         .getRunnerLimits(GlobPlot.class);\r
295                         assertNotNull(disemblLimits);\r
296                         assertTrue(disemblLimits.getLimits().size() > 0);\r
297                         disemblLimits.validate(disemblPresets);\r
298 \r
299                 } catch (FileNotFoundException e) {\r
300                         e.printStackTrace();\r
301                         fail(e.getLocalizedMessage());\r
302                 } catch (IOException e) {\r
303                         e.printStackTrace();\r
304                         fail(e.getLocalizedMessage());\r
305                 } catch (ValidationException e) {\r
306                         e.printStackTrace();\r
307                         fail(e.getLocalizedMessage());\r
308                 }\r
309         }\r
310 \r
311 }\r