Skip to content

Commit f5eb0e4

Browse files
committed
Added test to run nginx as a non-root user
1 parent 9146702 commit f5eb0e4

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

tests/files/nginx/nginx.conf

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
#user nginx;
3+
worker_processes 1;
4+
5+
# load_module lib64/nginx/modules/ngx_http_fancyindex_module.so;
6+
# load_module lib64/nginx/modules/ngx_http_headers_more_filter_module.so;
7+
# load_module lib64/nginx/modules/ngx_http_image_filter_module.so;
8+
# load_module lib64/nginx/modules/ngx_http_perl_module.so;
9+
# load_module lib64/nginx/modules/ngx_http_xslt_filter_module.so;
10+
# load_module lib64/nginx/modules/ngx_mail_module.so;
11+
# load_module lib64/nginx/modules/ngx_rtmp_module.so;
12+
# load_module lib64/nginx/modules/ngx_stream_module.so;
13+
14+
#error_log /var/log/nginx/error.log;
15+
#error_log /var/log/nginx/error.log notice;
16+
#error_log /var/log/nginx/error.log info;
17+
18+
pid /tmp/nginx.pid;
19+
20+
21+
events {
22+
worker_connections 1024;
23+
use epoll;
24+
}
25+
26+
27+
28+
http {
29+
include mime.types;
30+
client_body_temp_path /tmp/client_temp;
31+
proxy_temp_path /tmp/proxy_temp_path;
32+
fastcgi_temp_path /tmp/fastcgi_temp;
33+
uwsgi_temp_path /tmp/uwsgi_temp;
34+
scgi_temp_path /tmp/scgi_temp;
35+
default_type application/octet-stream;
36+
37+
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
38+
# '$status $body_bytes_sent "$http_referer" '
39+
# '"$http_user_agent" "$http_x_forwarded_for"';
40+
41+
#access_log /var/log/nginx/access.log main;
42+
43+
sendfile on;
44+
#tcp_nopush on;
45+
46+
#keepalive_timeout 0;
47+
keepalive_timeout 65;
48+
49+
#gzip on;
50+
51+
include conf.d/*.conf;
52+
53+
server {
54+
listen 8080;
55+
server_name localhost;
56+
57+
#charset koi8-r;
58+
59+
#access_log /var/log/nginx/host.access.log main;
60+
61+
location / {
62+
root /srv/www/htdocs/;
63+
index index.html index.htm;
64+
}
65+
66+
#error_page 404 /404.html;
67+
68+
# redirect server error pages to the static page /50x.html
69+
#
70+
error_page 500 502 503 504 /50x.html;
71+
location = /50x.html {
72+
root /srv/www/htdocs/;
73+
}
74+
75+
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
76+
#
77+
#location ~ \.php$ {
78+
# proxy_pass http://127.0.0.1;
79+
#}
80+
81+
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
82+
#
83+
#location ~ \.php$ {
84+
# root /srv/www/htdocs/;
85+
# fastcgi_pass 127.0.0.1:9000;
86+
# fastcgi_index index.php;
87+
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
88+
# include fastcgi_params;
89+
#}
90+
91+
# deny access to .htaccess files, if Apache's document root
92+
# concurs with nginx's one
93+
#
94+
#location ~ /\.ht {
95+
# deny all;
96+
#}
97+
}
98+
99+
100+
# another virtual host using mix of IP-, name-, and port-based configuration
101+
#
102+
#server {
103+
# listen 8000;
104+
# listen somename:8080;
105+
# server_name somename alias another.alias;
106+
107+
# location / {
108+
# root /srv/www/htdocs/;
109+
# index index.html index.htm;
110+
# }
111+
#}
112+
113+
114+
# HTTPS server
115+
#
116+
#server {
117+
# listen 443 ssl;
118+
# server_name localhost;
119+
120+
# ssl_certificate cert.pem;
121+
# ssl_certificate_key cert.key;
122+
123+
# Allow TLS version 1.2 only, which is a recommended default these days
124+
# by international information security standards.
125+
# ssl_protocols TLSv1.2;
126+
127+
# ssl_session_cache shared:SSL:1m;
128+
# ssl_session_timeout 5m;
129+
130+
# ssl_ciphers HIGH:!aNULL:!MD5;
131+
# ssl_prefer_server_ciphers on;
132+
133+
# location / {
134+
# root /srv/www/htdocs/;
135+
# index index.html index.htm;
136+
# }
137+
#}
138+
139+
include vhosts.d/*.conf;
140+
141+
}
142+

tests/test_nginx.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
"""This module contains the tests for the nginx container, the image with nginx pre-installed."""
22

3+
from pathlib import Path
4+
from typing import List
5+
6+
import pytest
37
import requests
8+
from _pytest.mark import ParameterSet
9+
from pytest_container.container import BindMount
10+
from pytest_container.container import ContainerData
11+
from pytest_container.container import DerivedContainer
12+
from pytest_container.container import PortForwarding
13+
from pytest_container.container import container_and_marks_from_pytest_param
414
from tenacity import retry
515
from tenacity import stop_after_attempt
616
from tenacity import wait_exponential
@@ -25,3 +35,57 @@ def check_nginx_response():
2535
assert "Welcome to nginx" in resp.text
2636

2737
check_nginx_response()
38+
39+
40+
_NGINX_CONFIG_FILE_PATH = (
41+
Path(__file__).parent / "files" / "nginx" / "nginx.conf"
42+
)
43+
44+
45+
def _generate_non_root_test_matrix() -> List[ParameterSet]:
46+
params = []
47+
for ng_cont_param in CONTAINER_IMAGES:
48+
ng_cont = container_and_marks_from_pytest_param(ng_cont_param)[0]
49+
marks = ng_cont_param.marks
50+
params.append(
51+
pytest.param(
52+
DerivedContainer(
53+
base=ng_cont,
54+
forwarded_ports=[PortForwarding(container_port=8080)],
55+
extra_launch_args=(["--user", "nginx"]),
56+
volume_mounts=[
57+
BindMount(
58+
container_path="/etc/nginx/nginx.conf",
59+
host_path=_NGINX_CONFIG_FILE_PATH,
60+
),
61+
],
62+
),
63+
marks=marks,
64+
)
65+
)
66+
67+
return params
68+
69+
70+
@pytest.mark.parametrize(
71+
"container_per_test",
72+
_generate_non_root_test_matrix(),
73+
indirect=["container_per_test"],
74+
)
75+
def test_nginx_welcome_page_as_non_root_user(
76+
container_per_test: ContainerData,
77+
):
78+
"""test that the default welcome page is served by the container."""
79+
host_port = container_per_test.forwarded_ports[0].host_port
80+
81+
# Retry 5 times with exponential backoff delay
82+
@retry(
83+
wait=wait_exponential(multiplier=1, min=4, max=10),
84+
stop=stop_after_attempt(5),
85+
)
86+
def check_nginx_response():
87+
resp = requests.get(f"http://localhost:{host_port}/", timeout=30)
88+
resp.raise_for_status()
89+
assert "Welcome to nginx" in resp.text
90+
91+
check_nginx_response()

0 commit comments

Comments
 (0)