~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to test/unit/common/middleware/test_healthcheck.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-28 09:40:30 UTC
  • mfrom: (1.2.16)
  • Revision ID: package-import@ubuntu.com-20130128094030-aetz57x2qz9ye2d4
Tags: 1.7.6-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
 
 
16
import os
 
17
import shutil
 
18
import tempfile
16
19
import unittest
17
20
 
18
 
from swift.common.swob import Request
 
21
from swift.common.swob import Request, Response
19
22
from swift.common.middleware import healthcheck
20
23
 
 
24
 
21
25
class FakeApp(object):
22
26
    def __call__(self, env, start_response):
23
 
        return "FAKE APP"
 
27
        req = Request(env)
 
28
        return Response(request=req, body='FAKE APP')(
 
29
            env, start_response)
24
30
 
25
 
def start_response(*args):
26
 
    pass
27
31
 
28
32
class TestHealthCheck(unittest.TestCase):
29
33
 
30
34
    def setUp(self):
31
 
        self.app = healthcheck.HealthCheckMiddleware(FakeApp())
 
35
        self.tempdir = tempfile.mkdtemp()
 
36
        self.disable_path = os.path.join(self.tempdir, 'dont-taze-me-bro')
 
37
        self.got_statuses = []
 
38
 
 
39
    def tearDown(self):
 
40
        shutil.rmtree(self.tempdir, ignore_errors=True)
 
41
 
 
42
    def get_app(self, app, global_conf, **local_conf):
 
43
        factory = healthcheck.filter_factory(global_conf, **local_conf)
 
44
        return factory(app)
 
45
 
 
46
    def start_response(self, status, headers):
 
47
        self.got_statuses.append(status)
32
48
 
33
49
    def test_healthcheck(self):
34
50
        req = Request.blank('/healthcheck', environ={'REQUEST_METHOD': 'GET'})
35
 
        resp = self.app(req.environ, start_response)
 
51
        app = self.get_app(FakeApp(), {})
 
52
        resp = app(req.environ, self.start_response)
 
53
        self.assertEquals(['200 OK'], self.got_statuses)
36
54
        self.assertEquals(resp, ['OK'])
37
55
 
38
56
    def test_healtcheck_pass(self):
39
57
        req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
40
 
        resp = self.app(req.environ, start_response)
41
 
        self.assertEquals(resp, 'FAKE APP')
 
58
        app = self.get_app(FakeApp(), {})
 
59
        resp = app(req.environ, self.start_response)
 
60
        self.assertEquals(['200 OK'], self.got_statuses)
 
61
        self.assertEquals(resp, ['FAKE APP'])
 
62
 
 
63
    def test_healthcheck_pass_not_disabled(self):
 
64
        req = Request.blank('/healthcheck', environ={'REQUEST_METHOD': 'GET'})
 
65
        app = self.get_app(FakeApp(), {}, disable_path=self.disable_path)
 
66
        resp = app(req.environ, self.start_response)
 
67
        self.assertEquals(['200 OK'], self.got_statuses)
 
68
        self.assertEquals(resp, ['OK'])
 
69
 
 
70
    def test_healthcheck_pass_disabled(self):
 
71
        open(self.disable_path, 'w')
 
72
        req = Request.blank('/healthcheck', environ={'REQUEST_METHOD': 'GET'})
 
73
        app = self.get_app(FakeApp(), {}, disable_path=self.disable_path)
 
74
        resp = app(req.environ, self.start_response)
 
75
        self.assertEquals(['503 Service Unavailable'], self.got_statuses)
 
76
        self.assertEquals(resp, ['DISABLED BY FILE'])
 
77
 
42
78
 
43
79
if __name__ == '__main__':
44
80
    unittest.main()