treeherder/tests/test_setup.py

64 строки
1.9 KiB
Python
Исходник Обычный вид История

2013-03-19 23:03:26 +04:00
import pytest
import responses
from celery import current_app
2013-03-26 20:24:38 +04:00
from django.core.cache import cache
from django.core.management import call_command
2013-03-19 23:03:26 +04:00
Bug 1384518 - Switch from Memcached to Redis Since: * Redis has additional features we need (eg for bug 1409679) * the Redis server, python client and Django backend are more actively maintained (so have persistent connections/pooling that actually works, which gives a big perf win) * we can use Heroku's own Redis addon rather than relying on a third-party's (to hopefully prevent a repeat of the certificate expiration downtime) This commit: * Switches pylibmc/django-pylibmc to redis-py/django-redis, and configures the new backend according to: http://niwinz.github.io/django-redis/latest/ https://github.com/andymccurdy/redis-py * Uses redis-py's native support for TLS to connect to the Heroku Redis server's stunnel port directly, avoiding the complexity of using a buildpack to create an stunnel between the dyno and server: https://devcenter.heroku.com/articles/securing-heroku-redis#connecting-directly-to-stunnel * Uses explicit `REDIS_URL` values on Travis/Vagrant rather than relying on the django-environ `default` value (for parity with how we configure `DATABASE_URL` and others). * Removes the pylibmc connection-closing workaround from `wsgi.py`. Note: Whilst the Heroku docs suggest using `django-redis-cache`, it's not as actively maintained, so we're using `django-redis` instead: https://devcenter.heroku.com/articles/heroku-redis https://github.com/niwinz/django-redis https://github.com/sebleier/django-redis-cache Before this is merged, Heroku Redis instances will need to be created for stage/production (prototype done) - likely on plan `premium-3`: https://elements.heroku.com/addons/heroku-redis We'll also need to pick an eviction policy: https://devcenter.heroku.com/articles/heroku-redis#maxmemory-policy Once deployed, the `memcachier-tls-buildpack` buildpack will no longer be required and can be removed from prototype/stage/prod, along with the `TREEHERDER_MEMCACHED` environment variable and Memcachier addon.
2018-01-17 21:09:44 +03:00
from treeherder.config.utils import get_tls_redis_url
from treeherder.etl.common import fetch_text
2013-03-19 23:03:26 +04:00
def test_block_unmocked_requests():
"""Ensure the `block_unmocked_requests` fixture prevents requests from hitting the network."""
url = 'https://example.com'
with pytest.raises(RuntimeError, message='Tests must mock all HTTP requests!'):
fetch_text(url)
with responses.RequestsMock() as rsps:
rsps.add(responses.GET, url, body='Mocked requests still work')
text = fetch_text(url)
assert text == 'Mocked requests still work'
@pytest.mark.django_db
def test_no_missing_migrations():
"""Check no model changes have been made since the last `./manage.py makemigrations`."""
call_command('makemigrations', interactive=False, dry_run=True, check_changes=True)
def test_django_cache():
"""Test the Django cache backend & associated server are properly set up."""
k, v = 'my_key', 'my_value'
cache.set(k, v, 10)
assert cache.get(k) == v
2013-05-13 21:19:19 +04:00
Bug 1384518 - Switch from Memcached to Redis Since: * Redis has additional features we need (eg for bug 1409679) * the Redis server, python client and Django backend are more actively maintained (so have persistent connections/pooling that actually works, which gives a big perf win) * we can use Heroku's own Redis addon rather than relying on a third-party's (to hopefully prevent a repeat of the certificate expiration downtime) This commit: * Switches pylibmc/django-pylibmc to redis-py/django-redis, and configures the new backend according to: http://niwinz.github.io/django-redis/latest/ https://github.com/andymccurdy/redis-py * Uses redis-py's native support for TLS to connect to the Heroku Redis server's stunnel port directly, avoiding the complexity of using a buildpack to create an stunnel between the dyno and server: https://devcenter.heroku.com/articles/securing-heroku-redis#connecting-directly-to-stunnel * Uses explicit `REDIS_URL` values on Travis/Vagrant rather than relying on the django-environ `default` value (for parity with how we configure `DATABASE_URL` and others). * Removes the pylibmc connection-closing workaround from `wsgi.py`. Note: Whilst the Heroku docs suggest using `django-redis-cache`, it's not as actively maintained, so we're using `django-redis` instead: https://devcenter.heroku.com/articles/heroku-redis https://github.com/niwinz/django-redis https://github.com/sebleier/django-redis-cache Before this is merged, Heroku Redis instances will need to be created for stage/production (prototype done) - likely on plan `premium-3`: https://elements.heroku.com/addons/heroku-redis We'll also need to pick an eviction policy: https://devcenter.heroku.com/articles/heroku-redis#maxmemory-policy Once deployed, the `memcachier-tls-buildpack` buildpack will no longer be required and can be removed from prototype/stage/prod, along with the `TREEHERDER_MEMCACHED` environment variable and Memcachier addon.
2018-01-17 21:09:44 +03:00
def test_get_tls_redis_url():
"""
Test conversion from REDIS_URL to the stunnel TLS URL described here:
https://devcenter.heroku.com/articles/securing-heroku-redis#connecting-directly-to-stunnel
"""
REDIS_URL = 'redis://h:abc8069@ec2-12-34-56-78.compute-1.amazonaws.com:8069'
TLS_REDIS_URL = 'rediss://h:abc8069@ec2-12-34-56-78.compute-1.amazonaws.com:8070'
assert get_tls_redis_url(REDIS_URL) == TLS_REDIS_URL
2013-05-13 21:19:19 +04:00
@current_app.task
def add(x, y):
return x + y
def test_celery_setup():
"Test celery executes a task properly"
result = add.delay(7, 3)
assert result.wait() == 10
@pytest.mark.django_db(transaction=True)
def test_load_initial_data():
"Test load_initial_data executes properly"
call_command('load_initial_data')