test_custom_nginx_app.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import time
  3. from pathlib import Path
  4. import docker
  5. import requests
  6. from docker.models.containers import Container
  7. from ..utils import (
  8. CONTAINER_NAME,
  9. generate_dockerfile_content_custom_nginx_app,
  10. get_logs,
  11. get_nginx_config,
  12. get_response_text2,
  13. remove_previous_container,
  14. )
  15. client = docker.from_env()
  16. def verify_container(container: Container, response_text: str) -> None:
  17. response = requests.get("http://127.0.0.1:8080")
  18. assert response.text == response_text
  19. nginx_config = get_nginx_config(container)
  20. assert "client_max_body_size 0;" not in nginx_config
  21. assert "worker_processes 2;" in nginx_config
  22. assert "listen 8080;" in nginx_config
  23. assert "worker_connections 2048;" in nginx_config
  24. assert "worker_rlimit_nofile;" not in nginx_config
  25. assert "daemon off;" in nginx_config
  26. assert "include uwsgi_params;" in nginx_config
  27. assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config
  28. assert "keepalive_timeout 300;" in nginx_config
  29. logs = get_logs(container)
  30. assert "getting INI configuration from /app/uwsgi.ini" in logs
  31. assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs
  32. assert "ini = /app/uwsgi.ini" in logs
  33. assert "ini = /etc/uwsgi/uwsgi.ini" in logs
  34. assert "socket = /tmp/uwsgi.sock" in logs
  35. assert "chown-socket = nginx:nginx" in logs
  36. assert "chmod-socket = 664" in logs
  37. assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs
  38. assert "need-app = true" in logs
  39. assert "die-on-term = true" in logs
  40. assert "show-config = true" in logs
  41. assert "wsgi-file = /app/main.py" in logs
  42. assert "processes = 16" in logs
  43. assert "cheaper = 2" in logs
  44. assert "Checking for script in /app/prestart.sh" in logs
  45. assert "Running script /app/prestart.sh" in logs
  46. assert (
  47. "Running inside /app/prestart.sh, you could add migrations to this file" in logs
  48. )
  49. assert "spawned uWSGI master process" in logs
  50. assert "spawned uWSGI worker 1" in logs
  51. assert "spawned uWSGI worker 2" in logs
  52. assert "spawned uWSGI worker 3" not in logs
  53. assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs
  54. assert "success: nginx entered RUNNING state, process has stayed up for" in logs
  55. assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs
  56. def test_env_vars_1() -> None:
  57. name = os.getenv("NAME", "")
  58. dockerfile_content = generate_dockerfile_content_custom_nginx_app(name)
  59. dockerfile = "Dockerfile"
  60. response_text = get_response_text2()
  61. sleep_time = int(os.getenv("SLEEP_TIME", 3))
  62. remove_previous_container(client)
  63. tag = "uwsgi-nginx-testimage"
  64. test_path = Path(__file__)
  65. path = test_path.parent / "custom_nginx_app"
  66. dockerfile_path = path / dockerfile
  67. dockerfile_path.write_text(dockerfile_content)
  68. client.images.build(path=str(path), dockerfile=dockerfile, tag=tag)
  69. container = client.containers.run(
  70. tag, name=CONTAINER_NAME, ports={"8080": "8080"}, detach=True
  71. )
  72. time.sleep(sleep_time)
  73. verify_container(container, response_text)
  74. container.stop()
  75. # Test that everything works after restarting too
  76. container.start()
  77. time.sleep(sleep_time)
  78. verify_container(container, response_text)
  79. container.stop()
  80. container.remove()