13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
18
from swift.common.swob import Request
21
from swift.common.swob import Request, Response
19
22
from swift.common.middleware import healthcheck
21
25
class FakeApp(object):
22
26
def __call__(self, env, start_response):
28
return Response(request=req, body='FAKE APP')(
25
def start_response(*args):
28
32
class TestHealthCheck(unittest.TestCase):
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 = []
40
shutil.rmtree(self.tempdir, ignore_errors=True)
42
def get_app(self, app, global_conf, **local_conf):
43
factory = healthcheck.filter_factory(global_conf, **local_conf)
46
def start_response(self, status, headers):
47
self.got_statuses.append(status)
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'])
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'])
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'])
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'])
43
79
if __name__ == '__main__':