Exemplo n.º 1
0
def test_add_should_increment_seq(tmpdir, request_cls):
    seqfile = str(tmpdir / '.SEQ')
    with open(seqfile, 'w') as f:
        print(7, file=f)
    request = request_cls.return_value
    request.reqid = None
    with ReqManager(str(tmpdir)) as rm:
        rm.add(request)
    assert '8\n' == open(seqfile).read()
Exemplo n.º 2
0
def test_add_updates_existing_with_same_comment(tmpdir):
    with ReqManager(str(tmpdir)) as rm:
        first = rm.add_request(30, 'script', 'comment')
        rm.add_request(60, 'script2', 'comment')

        requests = rm.requests()
        assert len(requests) == 1
        assert requests[first.uuid].estimate == 60
        assert requests[first.uuid].script == 'script2'
Exemplo n.º 3
0
def test_add_should_init_seq_to_allocate_ids(tmpdir, request_cls):
    seqfile = str(tmpdir / '.SEQ')
    request = request_cls.return_value
    request.reqid = None
    with ReqManager(str(tmpdir)) as rm:
        rm.add(request)
    assert os.path.isfile(seqfile)
    assert '0\n' == open(seqfile).read()
    assert 0 == request.reqid
Exemplo n.º 4
0
def request_population(n, dir):
    """Create a ReqManager with a pregenerated population of N requests.

    The ReqManager and a list of Requests are passed to the calling code.
    """
    with ReqManager(str(dir)) as reqmanager:
        reqmanager.PREPARE_SCRIPTS = str(dir / 'scripts/prepare')
        os.makedirs(reqmanager.PREPARE_SCRIPTS)
        reqmanager.FINISH_SCRIPTS = str(dir / 'scripts/finish')
        os.makedirs(reqmanager.FINISH_SCRIPTS)
        requests = []
        for i in range(n):
            req = reqmanager.add_request(1,
                                         'exit 0',
                                         uuid=pyuuid.UUID(
                                             '{:032d}'.format(i + 2**32)))
            requests.append(req)
        yield (reqmanager, requests)
Exemplo n.º 5
0
def test_archive(tmpdir, dir_fac):
    directory = dir_fac.return_value
    directory.end_maintenance = mock.Mock()
    with ReqManager(str(tmpdir)) as rm:
        request = rm.add_request(1,
                                 script='exit 0',
                                 uuid='f02c4745-46e5-11e3-8000-000000000000')
        request.execute()
        rm.archive_requests()
        assert not os.path.exists(rm.requestsdir + '/0'), \
            'request 0 should not exist in requestsdir'
        assert os.path.exists(rm.archivedir + '/0'), \
            'request 0 should exist in archivedir'
        directory.end_maintenance.assert_called_with(
            {request.uuid: {
                'duration': 0,
                'result': 'success'
            }})
Exemplo n.º 6
0
def test_postpone(tmpdir, dir_fac):
    directory = dir_fac.return_value
    directory.postpone_maintenance = mock.Mock()
    with freezegun.freeze_time('2011-07-27 07:12:00', tz_offset=0):
        with ReqManager(str(tmpdir)) as rm:
            rm.PREPARE_SCRIPTS = str(tmpdir / 'scripts/prepare')
            os.makedirs(rm.PREPARE_SCRIPTS)
            rm.FINISH_SCRIPTS = str(tmpdir / 'scripts/finish')
            os.makedirs(rm.FINISH_SCRIPTS)
            req = rm.add_request(300, script='exit 69')
            req.starttime = now()
            req.save()
            assert req.state == Request.DUE
            rm.execute_requests()
            rm.postpone_requests()
            req = rm.load_request(req.reqid)
            assert req.state == Request.PENDING
            assert req.starttime is None
            directory.postpone_maintenance.assert_called_with(
                {req.uuid: {
                    'postpone_by': 300
                }})
Exemplo n.º 7
0
def test_add_request_should_set_path(tmpdir):
    with ReqManager(str(tmpdir)) as rm:
        request = rm.add_request(30, 'script', 'comment')
    assert request.path == tmpdir / 'requests' / '0'
Exemplo n.º 8
0
def test_should_open_lockfile(tmpdir):
    with ReqManager(str(tmpdir)) as rm:
        # invoke any method that requires locking
        rm.runnable_requests()
        assert not rm.lockfile.closed, ('lock file {0!r} is not open'.format(
            rm.lockfile))
Exemplo n.º 9
0
def test_init_should_create_directories(tmpdir):
    spooldir = str(tmpdir / 'maintenance')
    ReqManager(spooldir)
    assert os.path.isdir(spooldir)
    assert os.path.isdir(spooldir + '/requests')
    assert os.path.isdir(spooldir + '/archive')
Exemplo n.º 10
0
def test_load_should_return_request(tmpdir):
    with ReqManager(str(tmpdir)) as rm:
        req1 = rm.add_request(300, comment='do something')
        req2 = rm.load_request(req1.reqid)
        assert req1 == req2
Exemplo n.º 11
0
def test_schedule_emptylist(tmpdir, dir_fac):
    directory = dir_fac.return_value
    directory.schedule_maintenance = mock.Mock()
    with ReqManager(str(tmpdir)) as rm:
        rm.update_schedule()
    assert 0 == directory.schedule_maintenance.call_count
Exemplo n.º 12
0
def test_add_does_not_fold_when_comment_differs(tmpdir):
    with ReqManager(str(tmpdir)) as rm:
        rm.add_request(30, 'script', 'comment1')
        rm.add_request(60, 'script2', 'comment2')
        assert len(rm.requests()) == 2