xref: /unit/test/unit/control.py (revision 2073:bc6ad31ce286)
11019Szelenkov@nginx.comimport json
21477Szelenkov@nginx.com
31019Szelenkov@nginx.comfrom unit.http import TestHTTP
4*1730Szelenkov@nginx.comfrom unit.option import option
51019Szelenkov@nginx.com
61019Szelenkov@nginx.com
71051Szelenkov@nginx.comdef args_handler(conf_func):
81051Szelenkov@nginx.com    def args_wrapper(self, *args):
91051Szelenkov@nginx.com        argcount = conf_func.__code__.co_argcount
101051Szelenkov@nginx.com        url_default = '/config'
111051Szelenkov@nginx.com        conf = None
121051Szelenkov@nginx.com
131051Szelenkov@nginx.com        if argcount == 2:
141051Szelenkov@nginx.com            url = args[0] if len(args) == 1 else url_default
151051Szelenkov@nginx.com
161051Szelenkov@nginx.com        elif argcount == 3:
171051Szelenkov@nginx.com            conf = args[0]
181051Szelenkov@nginx.com
191051Szelenkov@nginx.com            if isinstance(conf, dict) or isinstance(conf, list):
201051Szelenkov@nginx.com                conf = json.dumps(conf)
211051Szelenkov@nginx.com
221051Szelenkov@nginx.com            url = args[1] if len(args) == 2 else url_default
231051Szelenkov@nginx.com
241051Szelenkov@nginx.com        url = url if url.startswith('/') else url_default + '/' + url
251051Szelenkov@nginx.com        arguments = (self, url) if conf is None else (self, conf, url)
261051Szelenkov@nginx.com
271051Szelenkov@nginx.com        return json.loads(conf_func(*arguments))
281051Szelenkov@nginx.com
291051Szelenkov@nginx.com    return args_wrapper
301051Szelenkov@nginx.com
311051Szelenkov@nginx.com
321019Szelenkov@nginx.comclass TestControl(TestHTTP):
331051Szelenkov@nginx.com    @args_handler
341051Szelenkov@nginx.com    def conf(self, conf, url):
351051Szelenkov@nginx.com        return self.put(**self._get_args(url, conf))['body']
361019Szelenkov@nginx.com
371051Szelenkov@nginx.com    @args_handler
381051Szelenkov@nginx.com    def conf_get(self, url):
391051Szelenkov@nginx.com        return self.get(**self._get_args(url))['body']
401019Szelenkov@nginx.com
411051Szelenkov@nginx.com    @args_handler
421051Szelenkov@nginx.com    def conf_delete(self, url):
431051Szelenkov@nginx.com        return self.delete(**self._get_args(url))['body']
441019Szelenkov@nginx.com
451052Szelenkov@nginx.com    @args_handler
461052Szelenkov@nginx.com    def conf_post(self, conf, url):
471052Szelenkov@nginx.com        return self.post(**self._get_args(url, conf))['body']
481052Szelenkov@nginx.com
491051Szelenkov@nginx.com    def _get_args(self, url, conf=None):
501051Szelenkov@nginx.com        args = {
511051Szelenkov@nginx.com            'url': url,
521051Szelenkov@nginx.com            'sock_type': 'unix',
531654Szelenkov@nginx.com            'addr': option.temp_dir + '/control.unit.sock',
541051Szelenkov@nginx.com        }
551019Szelenkov@nginx.com
561051Szelenkov@nginx.com        if conf is not None:
571051Szelenkov@nginx.com            args['body'] = conf
581019Szelenkov@nginx.com
591051Szelenkov@nginx.com        return args
60