test_python_whitenoise.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import time
  2. from tests.package.test_python import TestPythonPackageBase
  3. class TestPythonPy3Whitenoise(TestPythonPackageBase):
  4. __test__ = True
  5. config = TestPythonPackageBase.config + \
  6. """
  7. BR2_PACKAGE_PYTHON3=y
  8. BR2_PACKAGE_PYTHON_DJANGO=y
  9. BR2_PACKAGE_PYTHON_WHITENOISE=y
  10. BR2_PACKAGE_PYTHON3_SQLITE=y
  11. """
  12. def test_run(self):
  13. self.login()
  14. timeout = 35
  15. cmd = "cd /opt && /usr/bin/django-admin startproject testsite"
  16. self.assertRunOk(cmd, timeout=timeout)
  17. # STATIC_ROOT needs to be set for 'collectstatic' to work.
  18. self.emulator.run("echo 'STATIC_ROOT = BASE_DIR / \"staticfiles\"' >> /opt/testsite/testsite/settings.py")
  19. cmd = "cd /opt/testsite && " + self.interpreter + " ./manage.py collectstatic"
  20. self.assertRunOk(cmd, timeout=timeout)
  21. # whitenoise docs say it needs to be added directly after SecurityMiddleware, so we do this here with sed.
  22. cmd = """sed -i -e /django.middleware.security.SecurityMiddleware/a\\ \\"whitenoise.middleware.WhiteNoiseMiddleware\\",\
  23. /opt/testsite/testsite/settings.py"""
  24. self.assertRunOk(cmd, timeout=timeout)
  25. # --nostatic ensures the builtin django server doesn't serve the static files,
  26. # so we can test that whitenoise serves them
  27. cmd = "cd /opt/testsite && " + self.interpreter + " ./manage.py runserver --nostatic 0.0.0.0:1234 > /dev/null 2>&1 & "
  28. self.assertRunOk(cmd, timeout=timeout)
  29. # give some time to setup the server
  30. for attempt in range(30 * self.emulator.timeout_multiplier):
  31. time.sleep(1)
  32. cmd = "wget http://127.0.0.1:1234/static/admin/css/base.css"
  33. _, exit_code = self.emulator.run(cmd)
  34. if exit_code == 0:
  35. break
  36. self.assertEqual(exit_code, 0, "Timeout while waiting for django server")