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