__init__.py 12.9 KB
Newer Older
1 2 3

from __future__ import print_function

4 5 6 7 8 9
import unittest

import os
import pkg_resources

import gridtk
10 11
import subprocess, signal
import time
12

André Anjos's avatar
André Anjos committed
13 14
from ..models import Job

15

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

André Anjos's avatar
André Anjos committed
27 28 29 30 31
    from ..manager import which
    bindir = os.path.join(os.path.realpath(os.curdir), 'bin')
    self.jman = which('jman', path=os.pathsep.join((bindir,
      os.environ['PATH'])))

32 33

  def tearDown(self):
34 35
    # make sure that all scheduler jobs are stopped after exiting
    if self.scheduler_job:
36
      self.scheduler_job.kill()
37 38 39 40 41 42 43
    # 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

44 45 46 47 48
    # first test, if the '/bin/bash' exists
    bash = '/bin/bash'
    if not os.path.exists(bash):
      raise SkipException("Could not find '%s' which is required to run the test scripts"%bash)

49 50
    try:

51 52
      import nose

53
      # first, add some commands to the database
André Anjos's avatar
André Anjos committed
54 55
      script_1 = pkg_resources.resource_filename(__name__, 'test_script.sh')
      script_2 = pkg_resources.resource_filename(__name__, 'test_array.sh')
56
      rdir = pkg_resources.resource_filename('gridtk', 'tests')
57 58
      from gridtk.script import jman
      # add a simple script that will write some information to the
André Anjos's avatar
André Anjos committed
59 60 61
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_1', bash, script_1])
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_2',  '--dependencies', '1', '--parametric', '1-7:2', bash, script_2])
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_3',  '--dependencies', '1', '2', '--exec-dir', rdir, bash, "test_array.sh"])
62 63

      # check that the database was created successfully
64
      self.assertTrue(os.path.exists(self.database))
65 66 67

      print()
      # test that the list command works (should also work with the "default" grid manager
André Anjos's avatar
André Anjos committed
68 69
      jman.main([self.jman, '--database', self.database, 'list', '--job-ids', '1'])
      jman.main([self.jman, '--database', self.database, 'list', '--job-ids', '2', '--print-array-jobs', '--print-dependencies', '--print-times'])
70 71 72 73 74

      # get insight into the database
      job_manager = gridtk.local.JobManagerLocal(database=self.database)
      session = job_manager.lock()
      jobs = list(session.query(Job))
75
      self.assertEqual(len(jobs), 3)
76 77
      self.assertEqual(jobs[0].id, 1)
      self.assertEqual(jobs[1].id, 2)
78
      self.assertEqual(jobs[2].id, 3)
79 80 81
      self.assertEqual(len(jobs[1].array), 4)
      self.assertEqual(jobs[0].status, 'submitted')
      self.assertEqual(jobs[1].status, 'submitted')
82
      self.assertEqual(jobs[2].status, 'submitted')
83 84 85 86 87 88
      self.assertTrue(all(j.submit_time is not None for j in jobs))
      self.assertTrue(all(j.start_time is None for j in jobs))
      self.assertTrue(all(j.finish_time is None for j in jobs))
      self.assertTrue(all(j.submit_time is not None for j in jobs[1].array))
      self.assertTrue(all(j.start_time is None for j in jobs[1].array))
      self.assertTrue(all(j.finish_time is None for j in jobs[1].array))
89 90 91

      # check that the job dependencies are correct
      waiting = jobs[0].get_jobs_waiting_for_us()
92
      self.assertEqual(len(waiting), 2)
93
      self.assertEqual(waiting[0].id, 2)
94 95 96
      self.assertEqual(waiting[1].id, 3)
      waited = jobs[2].get_jobs_we_wait_for()
      self.assertEqual(len(waited), 2)
97
      self.assertEqual(waited[0].id, 1)
98
      self.assertEqual(waited[1].id, 2)
99 100 101 102

      job_manager.unlock()

      # now, start the local execution of the job in a parallel job
André Anjos's avatar
André Anjos committed
103
      self.scheduler_job = subprocess.Popen([self.jman, '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '5', '--parallel', '2'])
104 105 106 107 108 109

      # sleep some time to assure that the scheduler was able to start the first job
      time.sleep(4)
      # ... and kill the scheduler
      self.scheduler_job.kill()
      self.scheduler_job = None
110

111 112 113
      # 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))
114
      self.assertEqual(len(jobs), 3)
115 116 117 118
      if jobs[0].status in ('submitted', 'queued', 'executing'):
        # on slow machines, we don0t want the tests to fail, so we just skip
        job_manager.unlock()
        raise nose.plugins.skip.SkipTest("This machine seems to be quite slow in processing parallel jobs.")
119 120
      self.assertEqual(jobs[0].status, 'failure')
      self.assertEqual(jobs[1].status, 'queued')
121
      self.assertEqual(jobs[2].status, 'waiting')
122 123 124 125 126 127 128
      self.assertTrue(jobs[0].start_time is not None)
      self.assertTrue(jobs[0].finish_time is not None)
      self.assertTrue(jobs[1].start_time is None)
      self.assertTrue(jobs[1].finish_time is None)
      self.assertTrue(jobs[2].start_time is None)
      self.assertTrue(jobs[2].finish_time is None)

129 130 131
      # the result files should already be there
      self.assertTrue(os.path.exists(jobs[0].std_out_file()))
      self.assertTrue(os.path.exists(jobs[0].std_err_file()))
132
      job_manager.unlock()
133 134


135
      # reset the job 1
André Anjos's avatar
André Anjos committed
136
      jman.main([self.jman, '--local', '--database', self.database, 'resubmit', '--job-id', '1', '--running-jobs', '--overwrite-command', script_1])
137

138
      # now, start the local execution of the job in a parallel job
André Anjos's avatar
André Anjos committed
139
      self.scheduler_job = subprocess.Popen([self.jman, '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '5', '--parallel', '2'])
140

141 142 143 144 145 146 147 148 149
      # sleep some time to assure that the scheduler was able to finish the first and start the second job
      time.sleep(9)
      # ... and kill the scheduler
      self.scheduler_job.kill()
      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))
150
      self.assertEqual(len(jobs), 3)
151 152 153 154
      if jobs[0].status in ('queued', 'executing') or jobs[1].status == 'queued':
        # on slow machines, we don0t want the tests to fail, so we just skip
        job_manager.unlock()
        raise nose.plugins.skip.SkipTest("This machine seems to be quite slow in processing parallel jobs.")
155 156
      self.assertEqual(jobs[0].status, 'failure')
      self.assertEqual(jobs[1].status, 'executing')
157 158 159 160
      if jobs[1].array[0].status == 'executing' or jobs[1].array[1].status == 'executing':
        # on slow machines, we don0t want the tests to fail, so we just skip
        job_manager.unlock()
        raise nose.plugins.skip.SkipTest("This machine seems to be quite slow in processing parallel jobs.")
161 162 163 164 165
      self.assertEqual(jobs[1].array[0].status, 'failure')
      self.assertEqual(jobs[1].array[0].result, 1)
      self.assertEqual(jobs[1].array[1].status, 'success')
      self.assertEqual(jobs[1].array[1].result, 0)
      self.assertEqual(len([a for a in jobs[1].array if a.status == 'queued']), 2)
166 167 168 169 170
      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
171 172 173
      self.assertTrue(os.path.isfile(out_file))
      self.assertTrue(os.path.isfile(err_file))
      self.assertEqual(open(out_file).read().rstrip(), 'This is a text message to std-out')
174
      self.assertTrue('This is a text message to std-err' in open(err_file).read().split('\n'))
175 176

      # resubmit all jobs
André Anjos's avatar
André Anjos committed
177
      jman.main([self.jman, '--local', '--database', self.database, 'resubmit', '--running-jobs'])
178
      # check that the log files have been cleaned
179 180
      self.assertFalse(os.path.exists(out_file))
      self.assertFalse(os.path.exists(err_file))
181
      # ... but the log dir still exists
182
      self.assertTrue(os.path.exists(self.log_dir))
183

184
      # now, let the scheduler run all jobs, but this time in verbose mode
André Anjos's avatar
André Anjos committed
185
      self.scheduler_job = subprocess.Popen([self.jman, '--local', '-vv', '--database', self.database, 'run-scheduler', '--sleep-time', '1', '--parallel', '2', '--die-when-finished'])
186 187 188 189 190
      # and wait for the job to finish (the timeout argument to Popen only exists from python 3.3 onwards)
      self.scheduler_job.wait()
      self.scheduler_job = None

      # check that all output files are generated again
191 192 193
      self.assertTrue(os.path.isfile(out_file))
      self.assertTrue(os.path.isfile(err_file))
      self.assertEqual(open(out_file).read().rstrip(), 'This is a text message to std-out')
194
      self.assertTrue('This is a text message to std-err' in open(err_file).read().split('\n'))
195 196 197

      # check that exactly four output and four error files have been created
      files = os.listdir(self.log_dir)
198
      self.assertEqual(len(files), 12)
199
      for i in range(1,8,2):
200 201
        self.assertTrue('test_2.o2.%d'%i in files)
        self.assertTrue('test_2.e2.%d'%i in files)
202 203 204 205

      # check that all array jobs are finished now
      session = job_manager.lock()
      jobs = list(session.query(Job))
206
      self.assertEqual(len(jobs), 3)
207 208 209
      self.assertEqual(jobs[1].status, 'failure')
      self.assertEqual(jobs[1].array[0].status, 'failure')
      self.assertEqual(jobs[1].array[0].result, 1)
210
      for i in range(1,4):
211 212
        self.assertEqual(jobs[1].array[i].status, 'success')
        self.assertEqual(jobs[1].array[i].result, 0)
213 214
      self.assertEqual(jobs[2].status, 'success')
      self.assertEqual(jobs[2].result, 0)
215 216 217 218 219 220 221 222

      self.assertTrue(all(j.submit_time is not None for j in jobs))
      self.assertTrue(all(j.start_time is not None for j in jobs))
      self.assertTrue(all(j.finish_time is not None for j in jobs))
      self.assertTrue(all(j.submit_time is not None for j in jobs[1].array))
      self.assertTrue(all(j.start_time is not None for j in jobs[1].array))
      self.assertTrue(all(j.finish_time is not None for j in jobs[1].array))

223 224 225 226
      job_manager.unlock()

      print()
      # test that the list command still works
André Anjos's avatar
André Anjos committed
227
      jman.main([self.jman, '--database', self.database, 'list', '--print-array-jobs'])
228 229 230

      print()
      # test that the report command works
André Anjos's avatar
André Anjos committed
231
      jman.main([self.jman, '--database', self.database, 'report'])
232 233

      # clean-up
André Anjos's avatar
André Anjos committed
234
      jman.main([self.jman, '--local', '--database', self.database, 'delete', '--job-ids', '1-3'])
235 236

      # check that the database and the log files are gone
237
      self.assertEqual(len(os.listdir(self.temp_dir)), 0)
238 239

      # add the scripts again, but this time with the --stop-on-failure option
André Anjos's avatar
André Anjos committed
240 241 242
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_1', '--stop-on-failure', bash, script_1])
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_2',  '--dependencies', '1', '--parametric', '1-7:2', '--stop-on-failure', bash, script_2])
      jman.main([self.jman, '--local', '--database', self.database, 'submit', '--log-dir', self.log_dir, '--name', 'test_3',  '--dependencies', '1', '2', '--exec-dir', rdir, '--stop-on-failure', bash, "test_array.sh"])
243 244

      # and execute them, but without writing the log files
André Anjos's avatar
André Anjos committed
245
      self.scheduler_job = subprocess.Popen([self.jman, '--local', '--database', self.database, 'run-scheduler', '--sleep-time', '0.1', '--parallel', '2', '--die-when-finished', '--no-log-files'])
246 247 248 249 250
      # and wait for the job to finish (the timeout argument to Popen only exists from python 3.3 onwards)
      self.scheduler_job.wait()
      self.scheduler_job = None

      # assert that the log files are not there
251 252
      self.assertFalse(os.path.isfile(out_file))
      self.assertFalse(os.path.isfile(err_file))
253 254 255 256 257


      # check that all array jobs are finished now
      session = job_manager.lock()
      jobs = list(session.query(Job))
258
      self.assertEqual(len(jobs), 3)
259 260 261 262
      self.assertEqual(jobs[0].status, 'failure')
      self.assertEqual(jobs[0].result, 255)
      self.assertEqual(jobs[1].status, 'failure')
      self.assertTrue(jobs[1].result is None)
263 264
      self.assertEqual(jobs[2].status, 'failure')
      self.assertTrue(jobs[2].result is None)
265 266 267
      job_manager.unlock()

      # and clean up again
André Anjos's avatar
André Anjos committed
268
      jman.main([self.jman, '--local', '--database', self.database, 'delete'])
269
      self.assertEqual(len(os.listdir(self.temp_dir)), 0)
270 271 272 273

    except KeyboardInterrupt:
      # make sure that the keyboard interrupt is captured and the mess is cleaned up (i.e. by calling tearDown)
      pass
274

275

276
  def notest02_grid(self):
277
    # Tests the functionality of the grid toolkit in the grid
278
    import nose
279
    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.")