File: test_StatusManager.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 (192 lines) | stat: -rw-r--r-- 7,537 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
########################################################################################
#                                                                                      #
#   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, sys, fcntl, signal
import shutil
from lxml import etree
from time import sleep

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 Mobyle.StatusManager import StatusManager
from Mobyle.Status import Status

DATADIR = os.path.dirname( __file__ )

def handler(signum, frame):
    pass

class StatusManagerTest(unittest.TestCase):
    """Tests the functionalities of Transaction"""

    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()
        shutil.rmtree(self.cfg.test_dir, ignore_errors=True)
        os.makedirs(self.cfg.test_dir)
        self.jobKey = "Q12345678901234"
        self.jobDir = os.path.join( self.cfg.test_dir , self.jobKey )
        os.makedirs( self.jobDir )
        self.filename = os.path.join( self.jobDir , StatusManager.file_name )

    def tearDown(self):
        shutil.rmtree( self.cfg.test_dir , ignore_errors= True )

    def testCreation(self):
        #create( filename , status )
        unknown = Status( code = -1 )
        StatusManager.create( self.jobDir , unknown )
        doc = etree.parse( self.filename )
        root = doc.getroot()
        self.assertEqual( root.tag , 'status' )
        children = list(root)
        self.assertEqual( len( children ) , 2 )
        self.assertEqual( children[0].tag , 'value')
        self.assertEqual( children[0].text , 'unknown' )
        self.assertEqual( children[1].tag , 'message')
        self.assertEqual( children[1].text , None )
        os.unlink( self.filename )

        building = Status( code = 0 )
        StatusManager.create( self.jobDir, building )
        doc = etree.parse( self.filename )
        root = doc.getroot()
        self.assertEqual( root.tag , 'status' )
        children = list(root)
        self.assertEqual( len( children ) , 2 )
        self.assertEqual( children[0].tag , 'value')
        self.assertEqual( children[0].text , 'building' )
        self.assertEqual( children[1].tag , 'message')
        self.assertEqual( children[1].text , None )
        os.unlink( self.filename )

        submitted = Status( code = 1, message= 'test message' )
        StatusManager.create( self.jobDir, submitted )
        doc = etree.parse( self.filename )
        root = doc.getroot()
        self.assertEqual( root.tag , 'status' )
        children = list(root)
        self.assertEqual( len( children ) , 2 )
        self.assertEqual( children[0].tag , 'value')
        self.assertEqual( children[0].text , 'submitted' )
        self.assertEqual( children[1].tag , 'message')
        self.assertEqual( children[1].text , 'test message' )
        os.unlink( self.filename )

        running = Status( string='running' )
        StatusManager.create( self.jobDir, running )
        doc = etree.parse( self.filename )
        root = doc.getroot()
        self.assertEqual( root.tag , 'status' )
        children = list(root)
        self.assertEqual( len( children ) , 2 )
        self.assertEqual( children[0].tag , 'value')
        self.assertEqual( children[0].text , 'running' )
        self.assertEqual( children[1].tag , 'message')
        self.assertEqual( children[1].text , None )
        os.unlink( self.filename )


    def testGetStatus( self ):
        running = Status( string='running' )
        StatusManager.create( self.jobDir, running )
        sm = StatusManager()
        recieved_status = sm.getStatus( self.jobDir )
        self.assertEqual( recieved_status , running )
        os.unlink( self.filename )

        killed = Status( string='killed' , message= "your job has been canceled" )
        StatusManager.create( self.jobDir, killed )
        sm = StatusManager()
        recieved_status = sm.getStatus( self.jobDir )
        self.assertEqual( recieved_status , killed )
        os.unlink( self.filename )


    def testSetstatus( self):
        StatusManager.create( self.jobDir, Status( string='submitted' ) )

        pending = Status( string= 'pending' )
        sm = StatusManager()
        sm.setStatus( self.jobDir , pending )
        recieved_status = sm.getStatus( self.jobDir )
        self.assertEqual( recieved_status , pending )

        finished = Status( string='finished' , message = 'your job finnished with an unusual status code, check youre results carefully')
        sm.setStatus( self.jobDir , finished )
        recieved_status = sm.getStatus( self.jobDir )
        self.assertEqual( recieved_status , finished )

        #an ended status cannot be changed anymore
        running = Status( string= 'running')
        sm.setStatus( self.jobDir , running )
        recieved_status = sm.getStatus( self.jobDir )
        self.assertNotEqual( recieved_status , running )
        self.assertEqual( recieved_status , finished )
        os.unlink( self.filename )

    def testConcurency(self):
        status = Status( string='submitted' )
        StatusManager.create( self.jobDir, status )

        ## sub-process start
        childPid = os.fork()
        if childPid: #father
            sleep(1)
            sm = StatusManager()
            self.assertEqual( status , sm.getStatus( self.jobDir ) )
            self.assertRaises( IOError , sm.setStatus , self.jobDir, status )
            os.kill( childPid , signal.SIGALRM )
            os.wait()

        else: #child
            signal.signal(signal.SIGALRM, handler)
            File = open( self.filename , 'r' )
            fcntl.lockf( File , fcntl.LOCK_SH | fcntl.LOCK_NB )
            signal.pause()
            fcntl.lockf( File , fcntl.LOCK_UN  )
            File.close()
            os._exit(0)
        ## sub-process end

        ## sub-process start
        childPid = os.fork()
        if childPid: #father
            sleep(1)
            sm = StatusManager()
            recieved_status = sm.getStatus( self.jobDir )
            self.assertEqual( recieved_status , Status( string= "unknown" )  )
            self.assertRaises( IOError , sm.setStatus , self.jobDir, status )
            os.kill( childPid , signal.SIGALRM )
            os.wait()

        else: #child
            signal.signal(signal.SIGALRM, handler)
            File = open( self.filename , 'r+' )
            fcntl.lockf( File , fcntl.LOCK_EX | fcntl.LOCK_NB )
            signal.pause()
            fcntl.lockf( File , fcntl.LOCK_UN  )
            File.close()
            os._exit(0)
        ## sub-process end


if __name__ == '__main__':
    unittest.main()