xref: /unit/test/test_python_basic.py (revision 413:ee77e227904d)
1import unit
2import unittest
3
4class TestUnitBasic(unit.TestUnitControl):
5
6    def setUpClass():
7        u = unit.TestUnit()
8        module_missed = u.check_modules('python')
9        if module_missed:
10            raise unittest.SkipTest('Unit has no ' + module_missed + ' module')
11
12    def test_python_get(self):
13        resp = self.get()
14        self.assertEqual(resp, {'listeners': {}, 'applications': {}}, 'empty')
15        self.assertEqual(self.get('/listeners'), {}, 'empty listeners prefix')
16        self.assertEqual(self.get('/applications'), {},
17            'empty applications prefix')
18
19        self.put('/applications', """
20            {
21                "app": {
22                    "type": "python",
23                    "workers": 1,
24                    "path": "/app",
25                    "module": "wsgi"
26                }
27            }
28            """)
29
30        resp = self.get()
31
32        self.assertEqual(resp['listeners'], {}, 'python empty listeners')
33        self.assertEqual(resp['applications'],
34            {
35                "app": {
36                    "type": "python",
37                    "workers": 1,
38                    "path": "/app",
39                    "module": "wsgi"
40                }
41             },
42             'python applications')
43
44        self.assertEqual(self.get('/applications'),
45            {
46                "app": {
47                    "type": "python",
48                    "workers": 1,
49                    "path": "/app",
50                    "module":"wsgi"
51                }
52            },
53            'python applications prefix')
54
55        self.assertEqual(self.get('/applications/app'),
56            {
57                "type": "python",
58                "workers": 1,
59                "path": "/app",
60                "module": "wsgi"
61            },
62            'python applications prefix 2')
63
64        self.assertEqual(self.get('/applications/app/type'), 'python',
65            'python applications type')
66        self.assertEqual(self.get('/applications/app/workers'), 1,
67            'python applications workers')
68
69        self.put('/listeners', '{"*:7080":{"application":"app"}}')
70
71        self.assertEqual(self.get()['listeners'],
72            {"*:7080":{"application":"app"}}, 'python listeners')
73        self.assertEqual(self.get('/listeners'),
74            {"*:7080":{"application":"app"}}, 'python listeners prefix')
75        self.assertEqual(self.get('/listeners/*:7080'),
76            {"application":"app"}, 'python listeners prefix 2')
77        self.assertEqual(self.get('/listeners/*:7080/application'), 'app',
78            'python listeners application')
79
80    def test_python_put(self):
81        self.put('/', """
82            {
83                "listeners": {
84                    "*:7080": {
85                        "application": "app"
86                    }
87                },
88                "applications": {
89                    "app": {
90                        "type": "python",
91                        "workers": 1,
92                        "path": "/app",
93                        "module": "wsgi"
94                    }
95                }
96            }
97            """)
98
99        resp = self.get()
100
101        self.assertEqual(resp['listeners'], {"*:7080":{"application":"app"}},
102            'put listeners')
103
104        self.assertEqual(resp['applications'],
105            {
106                "app": {
107                    "type": "python",
108                    "workers": 1,
109                    "path": "/app",
110                    "module": "wsgi"
111                }
112            },
113            'put applications')
114
115        self.put('/listeners', '{"*:7081":{"application":"app"}}')
116        self.assertEqual(self.get('/listeners'),
117            {"*:7081": {"application":"app"}}, 'put listeners prefix')
118
119        self.put('/listeners/*:7082', '{"application":"app"}')
120
121        self.assertEqual(self.get('/listeners'),
122            {
123                "*:7081": {
124                    "application": "app"
125                },
126                "*:7082": {
127                    "application": "app"
128                }
129            },
130            'put listeners prefix 3')
131
132        self.put('/applications/app/workers', '30')
133        self.assertEqual(self.get('/applications/app/workers'), 30,
134            'put applications workers')
135
136        self.put('/applications/app/path', '"/www"')
137        self.assertEqual(self.get('/applications/app/path'), '/www',
138            'put applications path')
139
140    def test_python_delete(self):
141        self.put('/', """
142            {
143                "listeners": {
144                    "*:7080": {
145                        "application": "app"
146                    }
147                },
148                "applications": {
149                    "app": {
150                        "type": "python",
151                        "workers": 1,
152                        "path": "/app",
153                        "module": "wsgi"
154                    }
155                }
156            }
157            """)
158
159        self.assertIn('error', self.delete('/applications/app'),
160            'delete app before listener')
161        self.assertIn('success', self.delete('/listeners/*:7080'),
162            'delete listener')
163        self.assertIn('success', self.delete('/applications/app'),
164            'delete app after listener')
165        self.assertIn('error', self.delete('/applications/app'),
166            'delete app again')
167
168if __name__ == '__main__':
169    unittest.main()
170