Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hub-client / dockerhub / appstatus / tests / test_views.py
Size: Mime:
# Copyright 2015 Docker, Inc. All rights reserved.

import json

from django.test import TestCase
from django.test.client import RequestFactory

from ..views import AppStatusViews


class ViewTests(TestCase):
    """Test the view functions directly without invoking the Django app
    machinery, by using ``HttpRequest`` created by ``RequestFactory``.
    """

    def setUp(self):
        self.factory = RequestFactory()
        self.asv = AppStatusViews()


    def test_ip_restriction(self):
        restricted_endpoints = {
            '/_echo': self.asv.echo,
            '/_health': self.asv.health,
            '/_stacks': self.asv.stacks,
            '/_heap': self.asv.heap,
        }
        for path, fn in restricted_endpoints.items():
            req = self.factory.get(path, HTTP_X_FORWARDED_FOR='1.2.3.4')
            resp = fn(req)
            self.assertEqual(404, resp.status_code, '{} is not protected!'.format(path))


    def test_default_health(self):
        req = self.factory.get('/_health')
        resp = self.asv.health(req)
        self.assertEqual(200, resp.status_code)

    def test_good_health(self):
        status = dict(database='ok', cache='ok: all good', queue='ok')
        asv = AppStatusViews(health_check=lambda: status)

        req = self.factory.get('/_health')
        resp = asv.health(req)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(status, json.loads(resp.content))

    def test_bad_health(self):
        status = dict(database='ok', cache='unreachable', queue='ok')
        asv = AppStatusViews(health_check=lambda: status)

        req = self.factory.get('/_health')
        resp = asv.health(req)
        self.assertEqual(500, resp.status_code)
        self.assertEqual(status, json.loads(resp.content))

    def test_exception_with_health(self):
        def exception_health_fn():
            raise Exception('Health check itself is borked')

        asv = AppStatusViews(health_check=exception_health_fn)

        req = self.factory.get('/_health')
        resp = asv.health(req)
        self.assertEqual(500, resp.status_code)

    def test_echo(self):
        req = self.factory.get('/_echo',
                               {'foo': 'bar'},
                               MY_CUSTOM_HEADER='Go Docker',
                               HTTP_COOKIE='key=value; cookie=monster')
        resp = self.asv.echo(req)
        self.assertEqual(200, resp.status_code)

        resp_j = json.loads(resp.content)
        self.assertEqual(dict(key='value', cookie='monster'), resp_j['cookies'])
        self.assertEqual('Go Docker', resp_j['meta']['MY_CUSTOM_HEADER'])

    def test_stacks(self):
        req = self.factory.get('/_stacks')
        resp = self.asv.stacks(req)
        self.assertEqual(200, resp.status_code)

        resp_j = json.loads(resp.content)
        self.assertTrue('pid' in resp_j)
        self.assertTrue('threads' in resp_j)

    def test_heap(self):
        # This test is a bit slow because we actually run heap analysis
        # on the current process. Well, but we want to exercise the heap
        # analysis somehow. Shikata ga nai.
        req = self.factory.get('/_heap')
        resp = self.asv.heap(req)
        self.assertEqual(200, resp.status_code)

        resp_j = json.loads(resp.content)
        self.assertTrue('pid' in resp_j)
        self.assertTrue('summary' in resp_j)

    def test_version(self):
        req = self.factory.get('/_version')
        with self.settings(VERSION_NUMBER='0.0.1', SERVICE_NAME='test-service'):
            resp = self.asv.version(req)
        self.assertEqual(200, resp.status_code)

        resp_j = json.loads(resp.content)
        self.assertEqual(resp_j['version'], '0.0.1')
        self.assertEqual(resp_j['service_name'], 'test-service')