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