xref: /unit/test/test_python_basic.py (revision 1017:887a4bbabf1e)
1import unittest
2import unit
3
4
5class TestUnitPythonBasic(unit.TestUnitControl):
6    def setUpClass():
7        unit.TestUnit().check_modules('python')
8
9    conf_app = {
10        "app": {
11            "type": "python",
12            "processes": {"spare": 0},
13            "path": "/app",
14            "module": "wsgi",
15        }
16    }
17
18    conf_basic = {
19        "listeners": {"*:7080": {"application": "app"}},
20        "applications": conf_app,
21    }
22
23    def test_python_get_empty(self):
24        self.assertEqual(
25            self.conf_get(), {'listeners': {}, 'applications': {}}, 'empty'
26        )
27
28    def test_python_get_prefix_listeners(self):
29        self.assertEqual(self.conf_get('listeners'), {}, 'listeners prefix')
30
31    def test_python_get_prefix_applications(self):
32        self.assertEqual(
33            self.conf_get('applications'), {}, 'applications prefix'
34        )
35
36    def test_python_get_applications(self):
37        self.conf(self.conf_app, 'applications')
38
39        conf = self.conf_get()
40
41        self.assertEqual(conf['listeners'], {}, 'listeners')
42        self.assertEqual(
43            conf['applications'],
44            {
45                "app": {
46                    "type": "python",
47                    "processes": {"spare": 0},
48                    "path": "/app",
49                    "module": "wsgi",
50                }
51            },
52            'applications',
53        )
54
55    def test_python_get_applications_prefix(self):
56        self.conf(self.conf_app, 'applications')
57
58        self.assertEqual(
59            self.conf_get('applications'),
60            {
61                "app": {
62                    "type": "python",
63                    "processes": {"spare": 0},
64                    "path": "/app",
65                    "module": "wsgi",
66                }
67            },
68            'applications prefix',
69        )
70
71    def test_python_get_applications_prefix_2(self):
72        self.conf(self.conf_app, 'applications')
73
74        self.assertEqual(
75            self.conf_get('applications/app'),
76            {
77                "type": "python",
78                "processes": {"spare": 0},
79                "path": "/app",
80                "module": "wsgi",
81            },
82            'applications prefix 2',
83        )
84
85    def test_python_get_applications_prefix_3(self):
86        self.conf(self.conf_app, 'applications')
87
88        self.assertEqual(
89            self.conf_get('applications/app/type'), 'python', 'type'
90        )
91        self.assertEqual(
92            self.conf_get('applications/app/processes/spare'), 0, 'spare'
93        )
94
95    def test_python_get_listeners(self):
96        self.conf(self.conf_basic)
97
98        self.assertEqual(
99            self.conf_get()['listeners'],
100            {"*:7080": {"application": "app"}},
101            'listeners',
102        )
103
104    def test_python_get_listeners_prefix(self):
105        self.conf(self.conf_basic)
106
107        self.assertEqual(
108            self.conf_get('listeners'),
109            {"*:7080": {"application": "app"}},
110            'listeners prefix',
111        )
112
113    def test_python_get_listeners_prefix_2(self):
114        self.conf(self.conf_basic)
115
116        self.assertEqual(
117            self.conf_get('listeners/*:7080'),
118            {"application": "app"},
119            'listeners prefix 2',
120        )
121
122    def test_python_change_listener(self):
123        self.conf(self.conf_basic)
124        self.conf({"*:7081": {"application": "app"}}, 'listeners')
125
126        self.assertEqual(
127            self.conf_get('listeners'),
128            {"*:7081": {"application": "app"}},
129            'change listener',
130        )
131
132    def test_python_add_listener(self):
133        self.conf(self.conf_basic)
134        self.conf({"application": "app"}, 'listeners/*:7082')
135
136        self.assertEqual(
137            self.conf_get('listeners'),
138            {
139                "*:7080": {"application": "app"},
140                "*:7082": {"application": "app"},
141            },
142            'add listener',
143        )
144
145    def test_python_change_application(self):
146        self.conf(self.conf_basic)
147
148        self.conf('30', 'applications/app/processes/max')
149        self.assertEqual(
150            self.conf_get('applications/app/processes/max'),
151            30,
152            'change application max',
153        )
154
155        self.conf('"/www"', 'applications/app/path')
156        self.assertEqual(
157            self.conf_get('applications/app/path'),
158            '/www',
159            'change application path',
160        )
161
162    def test_python_delete(self):
163        self.conf(self.conf_basic)
164
165        self.assertIn(
166            'error',
167            self.conf_delete('applications/app'),
168            'delete app before listener',
169        )
170        self.assertIn(
171            'success', self.conf_delete('listeners/*:7080'), 'delete listener'
172        )
173        self.assertIn(
174            'success',
175            self.conf_delete('applications/app'),
176            'delete app after listener',
177        )
178        self.assertIn(
179            'error', self.conf_delete('applications/app'), 'delete app again'
180        )
181
182
183if __name__ == '__main__':
184    TestUnitPythonBasic.main()
185