summaryrefslogtreecommitdiffhomepage
path: root/test/unit/control.py
blob: 99436ca02c4292d5650c605f43b2262299d2a97d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json

from unit.http import TestHTTP
from unit.option import option


def args_handler(conf_func):
    def args_wrapper(self, *args):
        argcount = conf_func.__code__.co_argcount
        url_default = '/config'
        conf = None

        if argcount == 2:
            url = args[0] if len(args) == 1 else url_default

        elif argcount == 3:
            conf = args[0]

            if isinstance(conf, dict) or isinstance(conf, list):
                conf = json.dumps(conf)

            url = args[1] if len(args) == 2 else url_default

        url = url if url.startswith('/') else url_default + '/' + url
        arguments = (self, url) if conf is None else (self, conf, url)

        return json.loads(conf_func(*arguments))

    return args_wrapper


class TestControl(TestHTTP):
    @args_handler
    def conf(self, conf, url):
        return self.put(**self._get_args(url, conf))['body']

    @args_handler
    def conf_get(self, url):
        return self.get(**self._get_args(url))['body']

    @args_handler
    def conf_delete(self, url):
        return self.delete(**self._get_args(url))['body']

    @args_handler
    def conf_post(self, conf, url):
        return self.post(**self._get_args(url, conf))['body']

    def _get_args(self, url, conf=None):
        args = {
            'url': url,
            'sock_type': 'unix',
            'addr': option.temp_dir + '/control.unit.sock',
        }

        if conf is not None:
            args['body'] = conf

        return args