File: test_JobState.py

package info (click to toggle)
mobyle 1.5.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,288 kB
  • sloc: python: 22,709; makefile: 35; sh: 33; ansic: 10; xml: 6
file content (265 lines) | stat: -rw-r--r-- 12,470 bytes parent folder | download | duplicates (4)
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
########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #  
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.         #
#                                                                                      #
########################################################################################

import unittest2 as unittest
import os
import sys
import shutil
import time

MOBYLEHOME = os.path.abspath( os.path.join( os.path.dirname( __file__ ) , "../../../" ) )
os.environ['MOBYLEHOME'] = MOBYLEHOME

if ( MOBYLEHOME ) not in sys.path:
    sys.path.append( MOBYLEHOME )
if ( os.path.join( MOBYLEHOME , 'Src' ) ) not in sys.path:    
    sys.path.append( os.path.join( MOBYLEHOME , 'Src' ) )

import Mobyle.Test.MobyleTest
from lxml import etree
from Mobyle import JobState
from Mobyle.Status import Status
from Mobyle.Service import MobyleType
from Mobyle.Classes.DataType import DataTypeFactory

DATADIR = os.path.dirname( __file__ )

class JobStateTest(unittest.TestCase):
    """Tests the functionalities of Transaction"""
    
    fake_mobyle_path = '/tmp/test_Mobyle'
    
    def setUp(self):
        """ 
        setting up the configuration for the test, including:
        - test configuration that does not check dns or try to send confirmation emails
        - test session email
        """
        self.cfg = Mobyle.ConfigManager.Config()
        #self.cfg._root_url           = 'http://marygay.sis.pasteur.fr:82'
        #self.cfg._results_path       = os.path.join( self.__class__.fake_mobyle_path , 'results' )
        #self.cfg._results_url        = "%s%s" % ( self.cfg._root_url , '/mobyle/results')
        
        #create space for test
        if os.path.exists( self.__class__.fake_mobyle_path ):
                    shutil.rmtree( self.__class__.fake_mobyle_path )
        os.makedirs( self.__class__.fake_mobyle_path )

        #create jobs directory
        if os.path.exists( self.cfg.results_path() ):
                    shutil.rmtree( self.cfg.results_path() )
        os.makedirs( self.cfg.results_path() )
        
        
        self.jobID = [ self.cfg.results_url() + "/fake_job/A00000000000000",
                       self.cfg.results_url() + "/fake_job/A00000000000001" 
                       ]
        
        self.jobs = [ {'jobID'       :  self.jobID[0] ,
                       'name'        : 'http://marygay.sis.pasteur.fr:81/mobyle/programs/dnadist.xml' ,
                       'host'        : 'http://marygay.sis.pasteur.fr:81' ,
                       'status'      :  Status( code = 6 ),#'killed' ,
                       'date'        :  time.strptime( "11/20/08  12:00:00", "%x  %X"),#(2008, 11, 20, 12, 0, 0, 3, 325, -1),
                       'dataProduced': [] ,
                       'dataUsed'    : ['16bd6f89e732eb7cb0bbc13c65d202d3.aln'] ,
                       'sessionKey'  : 'R18991749411106',
                       'email'       : 'bneron@pasteur.fr',
                       'args'        : {'infile': 'clustal.aln', 'seqboot': 'True', 'replicates': '50', 'seqboot_seed': '3'} ,
                       'commandLine' : 'ln -s clustal.aln infile && seqboot < seqboot.params && mv outfile seqboot.outfile && rm infile &&ln -s seqboot.outfile infile && dnadist <dnadist.params && mv outfile dnadist.outfile' ,
                       'formattedData': {'infile': {'formattedFile': None, 'fileFmt': 'PHYLIPI', 'fmtprogram': 'squizz', 'file': 'clustal.aln', 'formattedFileFmt': None}} ,
                       'paramfiles'  : [('seqboot.params', 11L), ('dnadist.params', 11L)],
                      } ,
                      {'jobID'       : self.jobID[1] ,
                       'name'        : 'http://marygay.sis.pasteur.fr:81/mobyle/programs/dnadist.xml' ,
                       'host'        : 'http://marygay.sis.pasteur.fr:81' ,
                       'status'      :  Status( code = 4 ),#finished Status( code = 3 ),#running
                       'date'        :  time.strptime( "11/19/08  10:50:33", "%x  %X"),#(2008, 11, 19, 10, 50, 33, 2, 324, -1),
                       'dataProduced': ['f835ddf28cb6aa776a41d7c71ec9cb5a.out'] ,
                       'dataUsed'    : ['16bd6f89e732eb7cb0bbc13c65d202d3.aln'] ,
                       'sessionKey'  : 'R18991749411106',
                       'email'       : 'bneron@pasteur.fr',
                       'args'        : {'infile': 'clustal.aln', 'seqboot': 'True', 'replicates': '10', 'seqboot_seed': '3'} ,
                       'commandLine' : 'n -s clustal.aln infile && seqboot < seqboot.params && mv outfile seqboot.outfile && rm infile &&ln -s seqboot.outfile infile && dnadist <dnadist.params && mv outfile dnadist.outfile' ,
                       'formattedData': {'infile': {'formattedFile': None, 'fileFmt': 'PHYLIPI', 'fmtprogram': 'squizz', 'file': 'clustal.aln', 'formattedFileFmt': None}} ,
                       'paramfiles'  : [('seqboot.params', 11L), ('dnadist.params', 11L)],
                       },
                       ]
        self.jobDir = self._makeFakeJob( self.jobID[1] )
        self.jobState = JobState.JobState( self.jobDir )
        
    def testgetOutputs( self ):    
        outputs_recieved = self.jobState.getOutputs()
        outputs = [( "P1", 
                           [('dnadist.outfile', 206400L, 'None')]), 
                    ("P2", 
                           [('seqboot.outfile', 976580L, 'None')]), 
                    ("P3", 
                            [('dnadist.out', 25411L, 'None')])]
        self.assertTrue( len( outputs_recieved ) == 3 )
                 
    def testgetOutput( self ):    
        output_recieved = self.jobState.getOutput( 'seqboot_out' )
        self.assertEqual( output_recieved , [('seqboot.outfile', 976580L, None)] )
        
    def testgetInputFiles( self ):   
        inputs_recieved = self.jobState.getInputFiles()
        inputs = [("P1", [('clustal.aln', 90044L, 'PHYLIPI')])]
        
    def testgetDir( self ):    
        self.assertEqual( self.jobState.getDir() , self.jobDir )
    
    def testID( self ):    
        self.jobState.setID( self.jobID[0])
        self.jobState.commit()
        self.assertEqual( self.jobState.getID() , self.jobID[0] )
        self.jobState.setID( self.jobID[1])
        self.jobState.commit()
        
    def testSessionKey( self ):    
        self.jobState.setSessionKey( self.jobs[0][ 'sessionKey' ])
        self.jobState.commit()
        self.assertEqual( self.jobState.getSessionKey() , self.jobs[0][ 'sessionKey' ] )
                          
    def testCommandLine( self ):    
        self.jobState.setCommandLine( self.jobs[0][ 'commandLine' ])
        self.jobState.commit()
        self.assertEqual( self.jobState.getCommandLine() , self.jobs[0][ 'commandLine' ] )
    
    def testName( self ):    
        self.jobState.setName( self.jobs[0][ 'name' ])
        self.jobState.commit()
        self.assertEqual( self.jobState.getName() , self.jobs[0][ 'name' ] )
    
    def testDate( self ):    
        self.jobState.setDate( self.jobs[0][ 'date' ])
        self.jobState.commit()
        self.assertEqual( self.jobState.getDate() , time.strftime( "%x  %X" , self.jobs[0][ 'date' ] ) )
    
    def testEmail( self ):    
        self.jobState.setEmail( self.jobs[0][ 'email' ])
        self.jobState.commit()
        self.assertEqual( self.jobState.getEmail() , self.jobs[0][ 'email' ] )
    
    def testgetStdout( self ): 
        stdout_file = open( os.path.join( self.jobDir , os.path.basename( self.jobState.getName()))[:-4]+".out" )
        stdout = ''.join( stdout_file.readlines() )
        stdout_file.close()
        self.assertEqual( self.jobState.getStdout() , stdout )
        
    def testgetOutputFile( self ):    
        fileName = "dnadist.outfile"
        File = open( os.path.join( self.jobDir , fileName ) )
        content  = ''.join( File.readlines() )
        File.close()
        self.assertEqual( self.jobState.getOutputFile( fileName ) , content )
        
#    def testopen( self ):    
#        pass
#    
    def testgetStderr( self ):    
        stderr_file = open( os.path.join( self.jobDir , os.path.basename( self.jobState.getName()))[:-4]+".err" )
        stderr = ''.join( stderr_file.readlines() )
        stderr_file.close()
        self.assertEqual( self.jobState.getStderr() , stderr )
    
    def testsetHost( self ):
        sent_host =  'http://foo.bar.pasteur.fr'   
        self.jobState.setHost( sent_host )
        self.jobState.commit()
        doc = etree.parse( self.jobDir + "/index.xml" )
        recieved_host = doc.find( 'host' ).text
        self.assertEqual( recieved_host , sent_host )
    
    def testsetInputDataFile( self ): 
        dtf = DataTypeFactory()
        self.dataType1 = dtf.newDataType( 'Text' )
        dataFile = {  
                    'paramName'   : "in_param_file",
                    'File'        : ( 'foo.ori' , 'beautiful.format' , 234 ) ,
                    'fmtProgram'  : 'squizz',
                    'formattedFile': ( 'foo.reformat' , 'wonderful.format' , 432 ) ,
                    }   
        self.jobState.setInputDataFile( dataFile[ 'paramName' ] ,
                                        dataFile[ 'File' ],
                                        dataFile[ 'fmtProgram' ] ,
                                        dataFile[ 'formattedFile' ],
                                         )
        self.jobState.commit()
        tree = etree.parse( os.path.join( os.path.join( self.jobDir , 'index.xml') ))
        root = tree.getroot()
        param = root.xpath( 'data/input/parameter[ name = "'+ dataFile[ 'paramName' ] + '" ]')[0]                    
        self.assertEqual( param.find( 'name' ).text , dataFile[ 'paramName' ] )
        
#    def testrenameInputDataFile( self ):    
#        pass
#    
#    def testsetInputDataValue( self ):    
#        pass
#    
#    def testsetOutputDataFile( self ):    
#        pass
#    
#    def testdelInputData( self ):    
#        pass
#    
    def testgetArgs( self ): 
        self.assertEqual( self.jobState.getArgs() , self.jobs[1]['args'] )

    
    def testgetPrompt( self ):    
        prompt = "Perform a bootstrap before analysis"
        self.assertEqual( self.jobState.getPrompt( 'seqboot' ) , prompt )     
        
    def testgetFormattedData( self ):    
        self.assertEqual( self.jobState.getFormattedData() , self.jobs[1]['formattedData'] )
    
    def testParamfiles( self ):
        self.assertEqual( self.jobState.getParamfiles() , self.jobs[1]['paramfiles'] )
        new_paramfiles = ( 'test' , 22L )
        self.jobs[1]['paramfiles'].append( new_paramfiles )
        self.jobState.setParamfiles( [ new_paramfiles ] )
        self.jobState.commit()
        self.assertEqual( self.jobState.getParamfiles() ,  self.jobs[1]['paramfiles'] )
        
    def _makeFakeJob(self , jobID ):
        jobPath = JobState.url2path( jobID ) 
        jobdir = os.path.dirname( jobPath )
        if not os.path.exists( jobdir ):
            os.makedirs( jobdir , 0755)
            
        if os.path.exists( jobPath ):
            shutil.rmtree( jobPath )
        index = jobID[-1]
        
        shutil.copytree( os.path.join( DATADIR , 'fake_job' + index ) , jobPath )
        
        doc = etree.parse( os.path.join( jobPath , 'index.xml' ) )
        root = doc.getroot()
        
        nameNode = root.find( 'name' )
        nameNode.text = self.cfg.repository_url() + '/fake.xml'
        
        hostNode = root.find( 'host' )
        hostNode.text = self.cfg.root_url()
        
        idNode   = root.find( 'id' )
        idNode.text = jobID
        
        xmlfile = open(  os.path.join( jobPath , 'index.xml' ) , 'w' )
        xmlfile.write( etree.tostring( doc , xml_declaration=True , encoding='UTF-8', pretty_print= True ) )
        xmlfile.close()
        
        return jobPath
    
#    @classmethod
#    def tearDownClass( cls ):
#        shutil.rmtree( cls.fake_mobyle_path , ignore_errors= True )
#    
 
if __name__ == '__main__':
    unittest.main()