test_app_and_env_vars.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_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:8000")
  18. assert response.text == response_text
  19. nginx_config = get_nginx_config(container)
  20. assert "client_max_body_size 0;" in nginx_config
  21. assert "worker_processes 1;" in nginx_config
  22. assert "listen 8080;" in nginx_config
  23. assert "worker_connections 1024;" 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. logs = get_logs(container)
  29. assert "getting INI configuration from /application/custom_app/uwsgi.ini" in logs
  30. assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs
  31. assert "ini = /application/custom_app/uwsgi.ini" in logs
  32. assert "ini = /etc/uwsgi/uwsgi.ini" in logs
  33. assert "socket = /tmp/uwsgi.sock" in logs
  34. assert "chown-socket = nginx:nginx" in logs
  35. assert "chmod-socket = 664" in logs
  36. assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs
  37. assert "need-app = true" in logs
  38. assert "die-on-term = true" in logs
  39. assert "show-config = true" in logs
  40. assert "wsgi-file = /application/custom_app/main.py" in logs
  41. assert "processes = 16" in logs
  42. assert "cheaper = 2" in logs
  43. assert "Checking for script in /app/prestart.sh" in logs
  44. assert "Running script /app/prestart.sh" in logs
  45. assert "custom prestart.sh running" in logs
  46. assert "spawned uWSGI master process" in logs
  47. assert "spawned uWSGI worker 1" in logs
  48. assert "spawned uWSGI worker 2" in logs
  49. assert "spawned uWSGI worker 3" not in logs
  50. assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs
  51. assert "success: nginx entered RUNNING state, process has stayed up for" in logs
  52. assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs
  53. def test_env_vars_1() -> None:
  54. name = os.getenv("NAME", "")
  55. dockerfile_content = generate_dockerfile_content_custom_app(name)
  56. dockerfile = "Dockerfile"
  57. response_text = get_response_text2()
  58. sleep_time = int(os.getenv("SLEEP_TIME", 3))
  59. remove_previous_container(client)
  60. tag = "uwsgi-nginx-testimage"
  61. test_path = Path(__file__)
  62. path = test_path.parent / "custom_app"
  63. dockerfile_path = path / dockerfile
  64. dockerfile_path.write_text(dockerfile_content)
  65. client.images.build(path=str(path), dockerfile=dockerfile, tag=tag)
  66. container = client.containers.run(
  67. tag,
  68. name=CONTAINER_NAME,
  69. environment={
  70. "UWSGI_INI": "/application/custom_app/uwsgi.ini",
  71. "LISTEN_PORT": "8080",
  72. },
  73. ports={"8080": "8000"},
  74. detach=True,
  75. )
  76. time.sleep(sleep_time)
  77. verify_container(container, response_text)
  78. container.stop()
  79. # Test that everything works after restarting too
  80. container.start()
  81. time.sleep(sleep_time)
  82. verify_container(container, response_text)
  83. container.stop()
  84. container.remove()