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