from uwsgiconf import Section


def test_main_process_basics(assert_lines):

    assert_lines([
        'touch-reload = /here/a',
    ], Section().main_process.set_basic_params(touch_reload='/here/a'))

    assert_lines([
        'daemonize = /here/app.log',
    ], Section().main_process.daemonize('/here/app.log'))

    assert_lines([
        'daemonize2 = /here/app.log',
    ], Section().main_process.daemonize('/here/app.log', after_app_loading=True))

    assert_lines([
        'pidfile = /here/file',
    ], Section().main_process.set_pid_file('/here/file'))

    assert_lines([
        'safe-pidfile2 = /here/file',
    ], Section().main_process.set_pid_file(
        '/here/file', before_priv_drop=False, safe=True))

    assert_lines([
        'auto-procname = true',
    ], Section().main_process.set_naming_params(autonaming=True))

    assert_lines([
        'uid = 1001',
    ], Section().main_process.set_owner_params(uid=1001))

    assert_lines([
        'exec-asap = date',
    ], Section().main_process.run_command_on_event('date'))

    assert_lines([
        'touch-exec = myfile.txt date',
    ], Section().main_process.run_command_on_touch('date', 'myfile.txt'))


def test_main_process_hooks(assert_lines):

    section = Section()

    prc = section.main_process
    asap = prc.phases.ASAP

    prc.set_hook(asap, prc.handlers.mount('/proc', 'proc', 'none'))
    prc.set_hook(asap, prc.handlers.mount('/proc', flags=['rec', 'detach']))
    prc.set_hook(asap, prc.handlers.execute('cat /proc/self/mounts'))
    prc.set_hook(asap, prc.handlers.call('uwsgi_log application has been loaded'))
    prc.set_hook(asap, prc.handlers.call('putenv PATH=bin:$(PATH)', arg_int=True))
    prc.set_hook(asap, prc.handlers.call('some', honour_exit_status=True))
    prc.set_hook(asap, prc.handlers.change_dir('/here'))
    prc.set_hook(asap, prc.handlers.exit())
    prc.set_hook(prc.phases.APP_LOAD_PRE, prc.handlers.exit(10))
    prc.set_hook(asap, prc.handlers.printout('bingo-bongo'))
    prc.set_hook(asap, prc.handlers.write('/here/a.txt', 'sometext'))
    prc.set_hook(asap, prc.handlers.write('/here/b', '10', fifo=True))
    prc.set_hook(asap, prc.handlers.unlink('/here/d'))

    assert_lines([
        'hook-asap = mount:proc none /proc',
        'hook-asap = umount:/proc rec,detach',
        'hook-asap = print:bingo-bongo',
        'hook-asap = exec:cat /proc/self/mounts',
        'hook-asap = call:uwsgi_log application has been loaded',
        'hook-asap = callint:putenv PATH=bin:$(PATH)',
        'hook-asap = callret:some',
        'hook-asap = cd:/here',
        'hook-asap = exit:',
        'hook-pre-app = exit:10',
        'hook-asap = write:/here/a.txt sometext',
        'hook-asap = writefifo:/here/b 10',
        'hook-asap = unlink:/here/d',
    ], section)
