def test_monitoring_metrics(assert_lines): assert_lines([ 'enable-metrics = true', 'metrics-dir = /var/run/mine/metrics', ], Section(runtime_dir='/var/run/', project_name='mine').monitoring.set_metrics_params( enable=True, store_dir='{project_runtime_dir}/metrics')) assert_lines([ 'metric-threshold = alarm=some,key=mycounter,reset=0,value=1000', ], Section().monitoring.set_metrics_threshold('mycounter', 1000, reset_to=0, alarm='some')) section = Section() assert_lines([ 'alarm = pinger cmd:ping 127.0.0.1', 'metric-threshold = alarm=pinger,key=mycounter,value=2000', ], section.monitoring.set_metrics_threshold( 'mycounter', 2000, alarm=section.alarms.alarm_types.command( 'pinger', 'ping 127.0.0.1')))
def test_spooler_basics(assert_lines): assert_lines([ 'touch-spoolers-reload = /here/a', ], Section().spooler.set_basic_params(touch_reload='/here/a')) assert_lines( [ 'spooler = /var/run/mine/one', 'spooler = /var/run/mine/two', ], Section(runtime_dir='/var/run/', project_name='mine').spooler.add(work_dir=[ '{project_runtime_dir}/one', '{project_runtime_dir}/two' ])) assert_lines([ 'spooler-external = home/two', ], Section().spooler.add('home/two', external=True)) assert_lines([ 'spooler = /base/here/alias1', 'spooler = /base/here/alias2', ], Section().spooler.set_basic_params( base_dir='/base/here').spooler.add(['alias1', 'alias2']))
def test_routing_rules(assert_lines): rule = Section.routing.route_rule assert_lines([ 'response-route-run = continue:', ], Section().routing.register_route( rule(rule.actions.do_continue(), subject=None, stage=rule.stages.RESPONSE))) assert_lines([ 'route-if-not = eq:${PATH_INFO};/bad chdir:/here', ], Section().routing.register_route( rule( rule.actions.dir_change('/here'), ~rule.subjects.custom( rule.vars.request('PATH_INFO')).eq('/bad')))) assert_lines([ 'route = ^/redir redirect-302:http://here', ], Section().routing.register_route( rule( rule.actions.redirect('http://here'), subject='^/redir', )))
def test_statics_basics(assert_lines): assert_lines([ 'static-index = index.html', ], Section().statics.set_basic_params(index_file='index.html')) assert_lines([ 'check-static-docroot = true', ], Section().statics.set_basic_params( static_dir=Section.statics.DIR_DOCUMENT_ROOT)) assert_lines([ 'static-map2 = /static=/var/www/static', 'static-safe = /var/www/static', ], Section().statics.register_static_map( '/static', '/var/www/static', retain_resource_path=True, safe_target=True)) assert_lines([ 'static-cache-paths = 200', ], Section().statics.set_paths_caching_params(timeout=200))
def test_monitoring_register_metric(assert_lines): monitoring = Section().monitoring my_abs = monitoring.metric_types.absolute('myabs', initial_value=100) my_abs2 = monitoring.metric_types.alias('myabs2', initial_value=200, alias_for=my_abs) with pytest.raises(ConfigurationError): # Alias without alias_for monitoring.metric_types.alias('myabs3', initial_value=200) assert_lines([ 'metric = name=myabs,initial_value=100,type=absolute', 'metric = name=myabs2,alias=myabs,initial_value=200,type=alias', 'metric = name=mymet,collector=sum,children=worker.1.requests;myabs,type=gauge', ], monitoring.register_metric([ my_abs, my_abs2, monitoring.metric_types.gauge( 'mymet', collector=monitoring.collectors.sum( ['worker.1.requests', my_abs]), ) ]))
def get_config(): configuration = [ Configuration([Section(), Section('conf1_2').env('A', 'B')], alias='uwsgicgf_test1'), Section().env('D', 'E'), ] return configuration
def test_alarms_basics(assert_lines): assert_lines([ 'alarm-msg-size = 10000', ], Section().alarms.set_basic_params(msg_size=10000)) assert_lines([ 'alarm-list = true', ], Section().alarms.print_alarms())
def test_cron(assert_lines): assert_lines([ 'cron2 = torrt walk', ], Section().master_process.add_cron_task('torrt walk')) assert_lines([ 'cron2 = week=1-3,hour=2,minute=-10,harakiri=10,legion=first,unique=1 some', ], Section().master_process.add_cron_task( 'some', hour=2, minute=-10, weekday='1-3', harakiri=10, unique=True, legion='first' ))
def test_section_plugins(assert_lines): assert_lines([ 'plugins-dir = /here\nplugins-dir = /there\nplugin = plug', ], Section().set_plugins_params(plugins='plug', search_dirs=['/here', '/there'], autoload=True)) section = Section() assert hash(section.python) == hash(section.python.name)
def test_alarms_on_queue(assert_lines): alarms = Section().alarms alarm1 = alarms.alarm_types.signal('mysig', 27) alarm2 = alarms.alarm_types.signal('some', 17) assert_lines([ 'alarm = mysig signal:27', 'alarm = some signal:17', 'alarm-backlog = mysig', 'alarm-backlog = some', ], alarms.alarm_on_queue_full([alarm1, alarm2]))
def test_alarms_on_segfault(assert_lines): alarms = Section().alarms alarm1 = alarms.alarm_types.signal('mysig', 27) alarm2 = alarms.alarm_types.signal('some', 17) assert_lines([ 'alarm = mysig signal:27', 'alarm = some signal:17', 'alarm-segfault = mysig', 'alarm-segfault = some', ], alarms.alarm_on_segfault([alarm1, alarm2]))
def test_main_process_hooks(assert_lines): section = Section() prc = section.main_process asap = prc.phases.ASAP prc.set_hook(asap, prc.actions.mount('/proc', 'proc', 'none')) prc.set_hook(asap, prc.actions.mount('/proc', flags=['rec', 'detach'])) prc.set_hook(asap, prc.actions.execute('cat /proc/self/mounts')) prc.set_hook(asap, prc.actions.call('uwsgi_log application has been loaded')) prc.set_hook(asap, prc.actions.call('putenv PATH=bin:$(PATH)', arg_int=True)) prc.set_hook(asap, prc.actions.call('some', honour_exit_status=True)) prc.set_hook(asap, prc.actions.dir_change('/here')) prc.set_hook(asap, prc.actions.exit()) prc.set_hook(prc.phases.APP_LOAD_PRE, prc.actions.exit(10)) prc.set_hook(asap, prc.actions.printout('bingo-bongo')) prc.set_hook(asap, prc.actions.file_write('/here/a.txt', 'sometext', append=True, newline=True)) prc.set_hook(asap, prc.actions.fifo_write('/here/b', '10', wait=True)) prc.set_hook(asap, prc.actions.unlink('/here/d')) prc.set_hook(asap, prc.actions.alarm('myal', 'bang')) prc.set_hook(asap, prc.actions.set_host_name('newname')) prc.set_hook(asap, prc.actions.file_create('/here/a.txt')) prc.set_hook(asap, prc.actions.dir_create('/here/there')) 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 = appendn:/here/a.txt sometext', 'hook-asap = spinningfifo:/here/b 10', 'hook-asap = unlink:/here/d', 'hook-asap = alarm:myal bang', 'hook-asap = hostname:newname', 'hook-asap = create:/here/a.txt', 'hook-asap = mkdir:/here/there', ], section) assert_lines([ 'hook-touch = /that do', ], Section().main_process.set_hook_touch('/that', 'do')) assert_lines([ 'after-request-hook = cfunc', ], Section().main_process.set_hook_after_request('cfunc'))
def test_master_fifo(assert_lines): assert_lines([ 'master-fifo = /here/my.fifo', ], Section().master_process.set_basic_params(fifo_file='/here/my.fifo')) assert_lines([ 'master-fifo = /there/is/mine_1.fifo', 'master-fifo = /there/is/mine_2.fifo', ], Section( runtime_dir='/there/is/', project_name='mine', ).master_process.set_basic_params( fifo_file=['{project_runtime_dir}_1.fifo', '{project_runtime_dir}_2.fifo']))
def test_configuration(capsys, assert_lines): # basic params init assert_lines([ 'workers = 33', ], Section(params_workers=dict(count=33))) assert 'testit' in Section().as_configuration(alias='testit').tofile() fpath = NamedTemporaryFile(delete=False).name assert fpath == Section().as_configuration().tofile(fpath) assert Section().as_configuration(alias='uwsgitest').tofile( gettempdir()).endswith('uwsgitest.ini') s1 = Section() s2 = 'some' with pytest.raises(ConfigurationError) as einfo: Configuration([s1, s2]).format() assert 'Section' in str(einfo.value) # is a section s2 = Section() with pytest.raises(ConfigurationError) as einfo: Configuration([s1, s2]).format() assert 'unique' in str(einfo.value) # has unique name s2.name = 'another' assert 'ini = :another' in Configuration( [s1, s2], autoinclude_sections=True).format() assert Configuration([s1, s2]).print_ini()
def test_logging_basics(assert_lines): logging = Section().logging assert_lines([ 'disable-logging = true', 'log-format = %(method) --> %(uri) %(metric.my) %(var.X_REQUEST_ID)', 'log-date = true', ], logging.set_basic_params( no_requests=True, template='%s --> %s %s %s' % ( logging.vars.REQ_METHOD, logging.vars.REQ_URI, logging.vars.metric('my'), logging.vars.request_var('X-Request-Id'), ), prefix_date=True)) assert_lines([ 'logformat-strftime = true', 'log-date = %%Y-%%M-%%D', ], Section().logging.set_basic_params( prefix_date='%Y-%M-%D', apply_strftime=True, )) assert_lines([ 'log-reopen = true', ], Section().logging.set_file_params(reopen_on_reload=True)) assert_lines([ 'logto2 = /a/b.log', ], Section().logging.log_into('/a/b.log', before_priv_drop=False)) assert_lines([ 'log-master = true', 'log-master-req-stream = true', ], Section().logging.set_master_logging_params( enable=True, sock_stream_requests_only=True)) assert_lines([ 'loggers-list = true', ], Section().logging.print_loggers()) assert_lines([ 'log-route = socket aline', ], Section().logging.add_logger_route('socket', 'aline'))
def test_logging_set_requests_filters(assert_lines): assert_lines([ 'log-slow = 100', ], Section().logging.set_requests_filters(slower=100)) assert_lines([ 'log-ioerror = true', ], Section().logging.set_requests_filters(io_errors=True)) assert_lines('ignore-write-errors = true', Section().logging.set_filters(write_errors=True), assert_in=False)
def test_statics_expiration(assert_lines): statics = Section().statics assert_lines([ 'static-expires-mtime = .*png 50', 'static-expires-mtime = .*pdf 50', 'static-expires-type = text/html=100', ], statics.add_expiration_rule( statics.expiration_criteria.FILENAME, ['.*png', '.*pdf'], 50, use_mod_time=True).statics.add_expiration_rule( statics.expiration_criteria.MIME_TYPE, 'text/html', 100))
def test_monitoring_collectors(assert_lines): collectors = Section().monitoring.collectors assert str(collectors.pointer()) == 'ptr' assert str(collectors.file('/here/a', get_slot=2)) == 'file,arg1=/here/a,arg1n=2' assert str(collectors.function('some')) == 'func,arg1=some' assert str(collectors.sum(['a', 'b'])) == 'sum,children=a;b' assert str(collectors.avg(['a', 'b'])) == 'avg,children=a;b' assert str(collectors.accumulator(['a', 'b'])) == 'accumulator,children=a;b' assert str(collectors.adder(['a', 'b'], 3)) == 'adder,arg1n=3,children=a;b' assert str(collectors.multiplier(['a', 'b'], 4)) == 'multiplier,arg1n=4,children=a;b'
def test_tuntap(assert_lines): routers = Section.routing.routers router = routers.tuntap( on='/tmp/tuntap.socket', device='emperor0', stats_server='127.0.0.1:3030', gateway='127.0.0.1:3032', ).set_basic_params( use_credentials='some' ).add_firewall_rule( direction='out', action='allow', src='192.168.0.0/24', dst='192.168.0.1', ).register_route( src='192.168.0.1/24', dst='192.168.0.2', gateway='192.168.0.4', ) assert_lines([ 'tuntap-use-credentials = some', 'tuntap-router = emperor0 /tmp/tuntap.socket 127.0.0.1:3030 127.0.0.1:3032', 'tuntap-router-firewall-out = allow 192.168.0.0/24 192.168.0.1', 'tuntap-router-route = 192.168.0.1/24 192.168.0.2 192.168.0.4', ], Section().routing.use_router(router) ) router = routers.tuntap().device_connect( device_name='uwsgi0', socket='/tmp/tuntap.socket', ). device_add_rule( direction='in', action='route', src='192.168.0.1', dst='192.168.0.2', target='10.20.30.40:5060', ) assert_lines([ 'tuntap-device = uwsgi0 /tmp/tuntap.socket', 'tuntap-device-rule = in 192.168.0.1 192.168.0.2 route 10.20.30.40:5060', ], Section().routing.use_router(router) )
def test_plugin_init(assert_lines): assert_lines([ 'plugin = python34', ], Section(params_python={ 'version': 34, 'python_home': '/here' }))
def test_locks_basics(assert_lines): assert_lines([ 'locks = 2', ], Section().locks.set_basic_params(count=2)) assert_lines([ 'ftok = a', ], Section().locks.set_ipcsem_params(ftok='a')) assert_lines([ 'flock2 = /here', ], Section().locks.lock_file('/here', after_setup=True))
def test_alarms_on_log(assert_lines): alarms = Section().alarms alarm1 = alarms.alarm_types.command('mycom', 'some') alarm2 = alarms.alarm_types.signal('mysig', 27) assert_lines([ 'alarm = mycom cmd:some', 'alarm = mysig signal:27', 'alarm-log = mycom,mysig some text', ], alarms.alarm_on_log([alarm1, alarm2], 'some text')) assert_lines([ 'not-alarm-log = mycom other', ], alarms.alarm_on_log(alarm1, 'other', skip=True))
def test_alarms_on_fd(assert_lines): alarms = Section().alarms alarm1 = alarms.alarm_types.signal('mysig', 27) alarm2 = alarms.alarm_types.signal('some', 17) assert_lines([ 'alarm = mysig signal:27', 'alarm = some signal:17', 'alarm-fd = mysig $(CGROUP_OOM_FD):8 damn it!', 'alarm-fd = some $(CGROUP_OOM_FD):8 damn it!', ], alarms.alarm_on_fd_ready([alarm1, alarm2], '$(CGROUP_OOM_FD)', 'damn it!', byte_count=8))
def test_broodlord(assert_lines): emperor, zerg = Broodlord( zerg_socket='/tmp/broodlord.sock', zerg_count=40, zerg_die_on_idle=30, vassals_home='/etc/vassals', vassal_queue_items_sos=10, section_emperor=(Section().master_process(enable=True).workers( count=1).logging(no_requests=True).python.set_wsgi_params( module='werkzeug.testapp:test_app'))).configure() emperor.networking.register_socket( Section.networking.sockets.default(':3031')) assert_lines([ 'master = true', 'workers = 1', 'disable-logging = true', 'wsgi = werkzeug.testapp:test_app', 'zerg-server = /tmp/broodlord.sock', 'emperor = /etc/vassals', 'emperor-broodlord = 40', 'vassal-sos-backlog = 10', 'socket = :3031', ], emperor) assert_lines([ 'master = true', 'workers = 1', 'disable-logging = true', 'wsgi = werkzeug.testapp:test_app', 'idle = 30', 'die-on-idle = true', ], zerg)
def test_routing_goto_label(assert_lines): rule = Section.routing.route_rule actions = rule.actions subjects = rule.subjects label = 'localhost' assert_lines( [ 'route-host = ^localhost$ goto:localhost', 'route-label = localhost', 'plugin = router_redirect', 'route-user-agent = .*curl.* redirect-302:http://uwsgi.it', 'route = (.*) continue:', ], Section().routing.register_route( rule(actions.do_goto(label), subjects.http_host('^localhost$'))).routing.register_route( [ rule(actions.redirect('http://uwsgi.it'), subjects.http_user_agent('.*curl.*')), rule(actions.do_continue(), subjects.path_info('(.*)')), ], label=label))
def test_applications_basics(assert_lines): assert_lines([ 'need-app = true', ], Section().applications.set_basic_params(exit_if_none=True)) assert_lines([ 'mount = /articles=app.py', ], Section().applications.mount('/articles', 'app.py')) assert_lines([ 'lazy-apps = true', ], Section().applications.switch_into_lazy_mode())
def test_fast(assert_lines): routers = Section.routing.routers router = routers.fast( on='127.0.0.1:3113', forward_to=['127.0.0.1:3123', '127.0.0.1:3124'], ).set_connections_params( defer=5 ).set_resubscription_params( addresses='127.0.0.1:3114' ).set_postbuffering_params( size=100 ).set_owner_params('idle') assert_lines([ 'fastrouter = 127.0.0.1:3113', 'fastrouter-to = 127.0.0.1:3123', 'fastrouter-to = 127.0.0.1:3124', 'fastrouter-defer-connect-timeout = 5', 'fastrouter-resubscribe = 127.0.0.1:3114', 'fastrouter-post-buffering = 100', 'fastrouter-uid = idle', ], Section().routing.use_router(router) )
def test_queue_basics(assert_lines): assert_lines([ 'queue = 100', 'queue-blocksize = 131072', ], Section().queue.enable(100, block_size=131072))
def test_master_attach_process(assert_lines): assert_lines([ 'attach-daemon2 = cmd=date', ], Section().master_process.attach_process('date')) assert_lines([ 'legion-attach-daemon2 = cmd=date', ], Section().master_process.attach_process('date', for_legion=True)) assert_lines([ 'attach-daemon2 = cmd=date,pidfile=/here/my.pid,control=1,touch=/here/one', ], Section().master_process.attach_process('date', control=True, pidfile='/here/my.pid', touch_reload='/here/one')) assert_lines([ 'attach-daemon2 = cmd=date,touch=/here/one;/there/two', ], Section().master_process.attach_process('date', touch_reload=['/here/one', '/there/two']))
def test_master_process_basics(assert_lines): assert_lines([ 'master = true', ], Section().master_process.set_basic_params(enable=True)) assert_lines([ 'reload-mercy = 10', ], Section().master_process.set_reload_params(mercy=10)) assert_lines([ 'idle = 10', ], Section().master_process.set_idle_params(timeout=10)) assert_lines([ 'catch-exceptions = true', ], Section().master_process.set_exception_handling_params(catch=True))