def test_scan(tmpdir): with request_population(3, tmpdir) as (rm, requests): requests[0].comment = 'foo' requests[0].save() requests[1].activity = FunnyActivity('good') requests[1].save() with ReqManager(str(tmpdir)) as rm: assert set(rm.requests.values()) == set(requests)
def main(): a = argparse.ArgumentParser(description=__doc__, epilog=""" If neither a script file or --exec is given, read the activity script from stdin. """) a.add_argument('-c', '--comment', metavar='TEXT', default='', help='announce upcoming maintenance with this message') a.add_argument('-d', '--spooldir', metavar='DIR', default=DEFAULT_DIR, help='request spool dir (default: %(default)s)') a.add_argument('estimate', metavar='ESTIMATE', help='estimate activity duration (suffixes: s, m, h)') g = a.add_mutually_exclusive_group() g.add_argument('-e', '--exec', metavar='SHELLCMD', default=False, help='execute shell command as maintenance activity') g.add_argument('file', metavar='FILE', default=None, nargs='?', type=argparse.FileType('r'), help='execute FILE as maintenance activity') args = a.parse_args() if args.file: act = args.file elif args.exec: act = io.StringIO(args.exec + '\n') else: act = sys.stdin rm = ReqManager(spooldir=args.spooldir) rm.add( Request(ShellScriptActivity(act), args.estimate, comment=args.comment))
def request_population(n, dir): """Creates 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: requests = [] for i in range(n): req = Request(Activity(), 60, comment=str(i)) req._reqid = shortuuid.encode(uuid.UUID(int=i)) reqmanager.add(req) requests.append(req) yield (reqmanager, requests)
def main(): a = argparse.ArgumentParser(description=__doc__) a.add_argument('-c', '--comment', metavar='TEXT', default=None, help='announce upcoming reboot with this message') a.add_argument('-p', '--poweroff', default=False, action='store_true', help='power off instead of reboot') a.add_argument('-d', '--spooldir', metavar='DIR', default=DEFAULT_DIR, help='request spool dir (default: %(default)s)') args = a.parse_args() action = 'poweroff' if args.poweroff else 'reboot' comment = args.comment if args.comment else ( 'Scheduled machine {}'.format(action)) rm = ReqManager(spooldir=args.spooldir) rm.add(Request(RebootActivity(action), 10 * 60, comment=comment))
def test_dont_perfom_warm_reboot_if_cold_reboot_pending( reqdir, defused_boom, boottime): for type_ in [[], ['--poweroff']]: sys.argv = [ 'reboot', '--spooldir={}'.format(str(reqdir)), '--comment={}'.format(type_), ] + type_ main() with ReqManager(str(reqdir)) as rm: rm.scan() # run soft reboot first reqs = sorted(rm.requests.values(), key=lambda r: r.activity.action, reverse=True) reqs[0].execute() reqs[0].save() assert defused_boom.call_count == 0 reqs[1].execute() reqs[1].save() assert defused_boom.call_count == 1
def test_scan_invalid(tmpdir): os.makedirs(str(tmpdir / 'requests' / 'emptydir')) open(str(tmpdir / 'requests' / 'foo'), 'w').close() ReqManager(str(tmpdir)).scan() # should not raise assert True
def test_lockfile(tmpdir): with ReqManager(str(tmpdir)): with open(str(tmpdir / '.lock')) as f: assert f.read().strip() == str(os.getpid()) with open(str(tmpdir / '.lock')) as f: assert f.read() == ''
def test_init_should_create_directories(tmpdir): spooldir = str(tmpdir / 'maintenance') ReqManager(spooldir) assert p.isdir(spooldir) assert p.isdir(p.join(spooldir, 'requests')) assert p.isdir(p.join(spooldir, 'archive'))
def reqmanager(tmpdir): with ReqManager(str(tmpdir)) as rm: yield rm
def test_init_should_create_directories(tmpdir): spooldir = str(tmpdir / "maintenance") ReqManager(spooldir) assert p.isdir(spooldir) assert p.isdir(p.join(spooldir, "requests")) assert p.isdir(p.join(spooldir, "archive"))
def reqmanager(tmpdir, agent_maintenance_config): with ReqManager(str(tmpdir), config_file=agent_maintenance_config) as rm: yield rm
def test_scan_invalid(tmpdir): os.makedirs(str(tmpdir / "requests" / "emptydir")) open(str(tmpdir / "requests" / "foo"), "w").close() ReqManager(str(tmpdir)).scan() # should not raise assert True
def comments(spooldir): """Returns dict of (reqid, comment).""" with ReqManager(str(spooldir)) as rm: rm.scan() return [req.comment for req in rm.requests.values()]