__init__.py 8.65 KB
Newer Older
1
2
3

from __future__ import print_function

4
5
6
7
8
9
10
import unittest
import nose

import os
import pkg_resources

import gridtk
11
12
import subprocess, signal
import time
13
14
15

from gridtk.models import Job

16
class GridTKTest(unittest.TestCase):
17
18
19
20
21
22
23
  # This class defines tests for the gridtk

  def setUp(self):
    # Create a temporary directory that will contain all outputs
    import tempfile
    self.temp_dir = tempfile.mkdtemp(prefix='gridtk_test')
    self.log_dir = os.path.join(self.temp_dir, 'logs')
24
25
    self.database = os.path.join(self.temp_dir, 'database.sql3')
    self.scheduler_job = None
26
27
28


  def tearDown(self):
29
30
31
    # make sure that all scheduler jobs are stopped after exiting
    if self.scheduler_job:
      self.scheduler_job.send_signal(signal.SIGINT)
32
      self.scheduler_job.wait()
33
34
35
36
37
38
39
40
41
42
43
44
    # Clean up the mess that we created
    import shutil
    shutil.rmtree(self.temp_dir)

  def test01_local(self):
    # This test executes all commands of the local grid manager and asserts that everything is fine

    # first, add some commands to the database
    script_1 = pkg_resources.resource_filename('gridtk.tests', 'test_script.sh')
    script_2 = pkg_resources.resource_filename('gridtk.tests', 'test_array.sh')
    from gridtk.script import jman
    # add a simple script that will write some information to the
45
46
    jman.main(['./bin/jman', '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_1', script_1])
    jman.main(['./bin/jman', '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_2',  '--dependencies', '1', '--parametric', '1-7:2', script_2])
47
48

    # check that the database was created successfully
49
    assert os.path.exists(self.database)
50

51
    print()
52
    # test that the list command works (should also work with the "default" grid manager
53
54
    jman.main(['./bin/jman', '--database', self.database, 'list', '--job-ids', '1'])
    jman.main(['./bin/jman', '--database', self.database, 'list', '--job-ids', '2', '--print-array-jobs', '--print-dependencies'])
55
56

    # get insight into the database
57
    job_manager = gridtk.local.JobManagerLocal(database=self.database)
58
59
60
61
62
63
    session = job_manager.lock()
    jobs = list(session.query(Job))
    assert len(jobs) == 2
    assert jobs[0].id == 1
    assert jobs[1].id == 2
    assert len(jobs[1].array) == 4
64
65
    assert jobs[0].status == 'submitted'
    assert jobs[1].status == 'submitted'
66
67
68
69
70
71
72
73
74
75
76

    # check that the job dependencies are correct
    waiting = jobs[0].get_jobs_waiting_for_us()
    assert len(waiting) == 1
    assert waiting[0].id == 2
    waited = jobs[1].get_jobs_we_wait_for()
    assert len(waited) == 1
    assert waited[0].id == 1

    job_manager.unlock()

77
78
    # now, start the local execution of the job in a parallel job
    self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '5', '--parallel', '2'])
79

80
    # sleep some time to assure that the scheduler was able to start the first job
81
    time.sleep(4)
82
83
84
    # ... and kill the scheduler
    self.scheduler_job.send_signal(signal.SIGINT)
    self.scheduler_job = None
85

86
87
88
89
90
91
92
93
94
95
96
    # now, the first job needs to have status failure, and the second needs to be queued
    session = job_manager.lock()
    jobs = list(session.query(Job))
    assert len(jobs) == 2
    assert jobs[0].status == 'failure'
    assert jobs[1].status == 'queued'
    # the result files should not be there yet
    assert not os.path.exists(jobs[0].std_out_file())
    assert not os.path.exists(jobs[0].std_err_file())
    job_manager.unlock()

97

98
99
100
101
    # reset the job 1
    jman.main(['./bin/jman', '--local', '--database', self.database, 'resubmit', '--job-id', '1', '--running-jobs'])

    # now, start the local execution of the job in a parallel job
102
    self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '5', '--parallel', '2'])
103
104

    # sleep some time to assure that the scheduler was able to finish the first and start the second job
105
    time.sleep(9)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    # ... and kill the scheduler
    self.scheduler_job.send_signal(signal.SIGINT)
    self.scheduler_job = None

    # Job 1 and two array jobs of job two should be finished now, the other two still need to be queued
    session = job_manager.lock()
    jobs = list(session.query(Job))
    assert len(jobs) == 2
    assert jobs[0].status == 'failure'
    assert jobs[1].status == 'executing'
    assert jobs[1].array[0].status == 'failure'
    assert jobs[1].array[0].result == 1
    assert jobs[1].array[1].status == 'success'
    assert jobs[1].array[1].result == 0
    assert len([a for a in jobs[1].array if a.status == 'queued']) == 2
    out_file = jobs[0].std_out_file()
    err_file = jobs[0].std_err_file()
    job_manager.unlock()

    # the result files of the first job should now be there
126
127
128
129
130
    assert os.path.isfile(out_file)
    assert os.path.isfile(err_file)
    assert open(out_file).read().rstrip() == 'This is a text message to std-out'
    assert open(err_file).read().rstrip() == 'This is a text message to std-err'

131
132
133
134
135
136
    # resubmit all jobs
    jman.main(['./bin/jman', '--local', '--database', self.database, 'resubmit', '--running-jobs'])
    # check that the log files have been cleaned
    assert not os.path.exists(out_file)
    assert not os.path.exists(err_file)
    # ... but the log dir still exists
137
    assert os.path.exists(self.log_dir)
138

139
    # now, let the scheduler run all jobs
140
    self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '1', '--parallel', '2', '--die-when-finished'])
141
    # ... and kill the scheduler
142
    time.sleep(10)
143
    assert self.scheduler_job.poll() is not None
144
    self.scheduler_job = None
145

146
147
148
149
150
    # check that all output files are generated again
    assert os.path.isfile(out_file)
    assert os.path.isfile(err_file)
    assert open(out_file).read().rstrip() == 'This is a text message to std-out'
    assert open(err_file).read().rstrip() == 'This is a text message to std-err'
151
152
153

    # check that exactly four output and four error files have been created
    files = os.listdir(self.log_dir)
154
    assert len(files) == 10
155
156
157
158
    for i in range(1,8,2):
      assert 'test_2.o2.%d'%i in files
      assert 'test_2.e2.%d'%i in files

159
    # check that all array jobs are finished now
160
    session = job_manager.lock()
161
162
163
164
165
166
167
168
    jobs = list(session.query(Job))
    assert len(jobs) == 2
    assert jobs[1].status == 'failure'
    assert jobs[1].array[0].status == 'failure'
    assert jobs[1].array[0].result == 1
    for i in range(1,4):
      assert jobs[1].array[i].status == 'success'
      assert jobs[1].array[i].result == 0
169
170
    job_manager.unlock()

171
    print()
172
173
174
    # test that the list command still works
    jman.main(['./bin/jman', '--database', self.database, 'list', '--print-array-jobs'])

175
    print()
176
    # test that the report command works
177
178
    jman.main(['./bin/jman', '--database', self.database, 'report'])

179
    # clean-up
180
    jman.main(['./bin/jman', '--local', '--database', self.database, 'delete'])
181

182
    # check that the database and the log files are gone
183
    assert len(os.listdir(self.temp_dir)) == 0
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
    # add the scripts again, but this time with the --stop-on-failure option
    jman.main(['./bin/jman', '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_1', '--stop-on-failure', script_1])
    jman.main(['./bin/jman', '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_2',  '--dependencies', '1', '--parametric', '1-7:2', '--stop-on-failure', script_2])

    # and execute them, but without writing the log files
    self.scheduler_job = subprocess.Popen(['./bin/jman', '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '0.1', '--parallel', '2', '--die-when-finished', '--no-log-files'])
    # ... and kill the scheduler
    time.sleep(3)
    assert self.scheduler_job.poll() is not None
    self.scheduler_job = None

    # assert that the log files are not there
    assert not os.path.isfile(out_file)
    assert not os.path.isfile(err_file)


    # check that all array jobs are finished now
    session = job_manager.lock()
    jobs = list(session.query(Job))
    assert len(jobs) == 2
    assert jobs[0].status == 'failure'
    assert jobs[0].result == 255
    assert jobs[1].status == 'failure'
    assert jobs[1].result is None
    job_manager.unlock()

    # and clean up again
    jman.main(['./bin/jman', '--local', '--database', self.database, 'delete'])

214
215
216
217
218

  def test02_grid(self):
    # Tests the functionality of the grid toolkit in the grid
    raise nose.plugins.skip.SkipTest("This test is not yet implemented. If you find a proper ways to test the grid functionality, please go ahead and implement the test.")