Skip to content

Commit

Permalink
* test infrastruture:
Browse files Browse the repository at this point in the history
   - moved common pytest code into test/pyhttpd
   - does basic setup for a list of host names and some htdocs
   - added modules/core and moved encoding tests from http2 there
   - all test methods have module name in in prefix now, so to test only core, run
     > pytest -k test_core



git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1894134 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
icing committed Oct 11, 2021
1 parent fa7f375 commit ded853c
Show file tree
Hide file tree
Showing 101 changed files with 538 additions and 458 deletions.
5 changes: 1 addition & 4 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,7 @@ APACHE_FAST_OUTPUT(support/Makefile)

if test -d ./test; then
APACHE_FAST_OUTPUT(test/Makefile)
fi
if test -d ./test/modules/http2; then
APACHE_FAST_OUTPUT(test/modules/http2/Makefile)
AC_CONFIG_FILES([test/modules/http2/config.ini])
AC_CONFIG_FILES([test/pyhttpd/config.ini])
fi

dnl ## Finalize the variables
Expand Down
2 changes: 2 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.ini
gen
6 changes: 6 additions & 0 deletions test/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ test: $(bin_PROGRAMS)
# dbu_OBJECTS = dbu.lo
# dbu: $(dbu_OBJECTS)
# $(LINK) $(dbu_OBJECTS) $(PROGRAM_LDADD)

clean:
rm -rf gen

distclean:
rm -f pytest/config.ini
12 changes: 12 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), '.'))

from pyhttpd.env import HttpdTestEnv

def pytest_report_header(config, startdir):
env = HttpdTestEnv()
return f"[apache httpd: {env.get_httpd_version()}, mpm: {env.mpm_type}, {env.prefix}]"


1 change: 1 addition & 0 deletions test/modules/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

39 changes: 39 additions & 0 deletions test/modules/core/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
import os

import pytest
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))

from .env import CoreTestEnv


def pytest_report_header(config, startdir):
env = CoreTestEnv(setup_dirs=False)
return f"core [apache: {env.get_httpd_version()}, mpm: {env.mpm_type}, {env.prefix}]"


@pytest.fixture(scope="module")
def env(pytestconfig) -> CoreTestEnv:
level = logging.INFO
console = logging.StreamHandler()
console.setLevel(level)
console.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
logging.getLogger('').addHandler(console)
logging.getLogger('').setLevel(level=level)
env = CoreTestEnv(pytestconfig=pytestconfig)
env.apache_access_log_clear()
env.apache_error_log_clear()
return env


@pytest.fixture(autouse=True, scope="module")
def _session_scope(env):
yield
assert env.apache_stop() == 0
errors, warnings = env.apache_errors_and_warnings()
assert (len(errors), len(warnings)) == (0, 0),\
f"apache logged {len(errors)} errors and {len(warnings)} warnings: \n"\
"{0}\n{1}\n".format("\n".join(errors), "\n".join(warnings))

18 changes: 18 additions & 0 deletions test/modules/core/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import inspect
import logging
import os

from pyhttpd.env import HttpdTestEnv, HttpdTestSetup

log = logging.getLogger(__name__)


class CoreTestEnv(HttpdTestEnv):

def __init__(self, pytestconfig=None, setup_dirs=True):
super().__init__(pytestconfig=pytestconfig,
local_dir=os.path.dirname(inspect.getfile(CoreTestEnv)))
if setup_dirs:
self._setup = HttpdTestSetup(env=self)
self._setup.make()
self.issue_certs()
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import time

import pytest

from h2_conf import HttpdConf
from pyhttpd.conf import HttpdConf


class TestEncoding:
Expand All @@ -11,17 +9,15 @@ class TestEncoding:

@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
extras = {
'base': f"""
conf = HttpdConf(env)
conf.add(f"""
<Directory "{env.gen_dir}">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
""",
}
conf = HttpdConf(env)
conf.add_vhost_test1(extras=extras)
""")
conf.add_vhost_test1()
conf.add_vhost_test2(extras={
f"test2.{env.http_tld}": "AllowEncodedSlashes on",
})
Expand All @@ -32,9 +28,10 @@ def _class_scope(self, env):
assert env.apache_restart() == 0
yield
errors, warnings = env.apache_errors_and_warnings()
nl = "\n"
assert (len(errors), len(warnings)) == (TestEncoding.EXP_AH10244_ERRS, 0),\
f"apache logged {len(errors)} errors and {len(warnings)} warnings: \n"\
"{0}\n{1}\n".format("\n".join(errors), "\n".join(warnings))
f"apache logged {len(errors)} errors and {len(warnings)} warnings: \n"\
f"{nl.join(errors)}\n{nl.join(warnings)}\n"
env.apache_error_log_clear()

# check handling of url encodings that are accepted
Expand All @@ -47,7 +44,7 @@ def _class_scope(self, env):
"/nothing/%2e/%2e%2e/006/006.css",
"/nothing/%2e/%2e%2e/006/006%2ecss",
])
def test_203_01(self, env, path):
def test_core_001_01(self, env, path):
url = env.mkurl("https", "test1", path)
r = env.curl_get(url)
assert r.response["status"] == 200
Expand All @@ -62,7 +59,7 @@ def test_203_01(self, env, path):
"/006/../006/006.css",
"/006/%2e%2e/006/006.css",
])
def test_203_03(self, env, path):
def test_core_001_03(self, env, path):
url = env.mkurl("https", "test1", path)
r = env.curl_get(url)
assert r.response["status"] == 200
Expand All @@ -83,7 +80,7 @@ def test_203_03(self, env, path):
["/nothing/%25%32%65%25%32%65/%25%32%65%25%32%65/h2_env.py", 404],
["/cgi-bin/%25%32%65%25%32%65/%25%32%65%25%32%65/h2_env.py", 404],
])
def test_203_04(self, env, path, status):
def test_core_001_04(self, env, path, status):
url = env.mkurl("https", "cgi", path)
r = env.curl_get(url)
assert r.response["status"] == status
Expand All @@ -98,8 +95,7 @@ def test_203_04(self, env, path, status):
["test2", "/x%252f.test", 200],
["test2", "/10%25abnormal.txt", 200],
])
def test_203_20(self, env, host, path, status):
def test_core_001_20(self, env, host, path, status):
url = env.mkurl("https", host, path)
r = env.curl_get(url)
assert r.response["status"] == status

20 changes: 0 additions & 20 deletions test/modules/http2/Makefile.in

This file was deleted.

File renamed without changes.
18 changes: 6 additions & 12 deletions test/modules/http2/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import os

import pytest
import sys

from h2_certs import CertificateSpec, H2TestCA
from h2_env import H2TestEnv
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))

from .env import H2TestEnv


def pytest_report_header(config, startdir):
Expand All @@ -25,7 +27,7 @@ def pytest_generate_tests(metafunc):
metafunc.parametrize('repeat', range(count))


@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def env(pytestconfig) -> H2TestEnv:
level = logging.INFO
console = logging.StreamHandler()
Expand All @@ -34,20 +36,12 @@ def env(pytestconfig) -> H2TestEnv:
logging.getLogger('').addHandler(console)
logging.getLogger('').setLevel(level=level)
env = H2TestEnv(pytestconfig=pytestconfig)
cert_specs = [
CertificateSpec(domains=env.domains, key_type='rsa4096'),
CertificateSpec(domains=env.domains_noh2, key_type='rsa2048'),
]
ca = H2TestCA.create_root(name=env.http_tld,
store_dir=os.path.join(env.server_dir, 'ca'), key_type="rsa4096")
ca.issue_certs(cert_specs)
env.set_ca(ca)
env.apache_access_log_clear()
env.apache_error_log_clear()
return env


@pytest.fixture(autouse=True, scope="session")
@pytest.fixture(autouse=True, scope="module")
def _session_scope(env):
yield
assert env.apache_stop() == 0
Expand Down
99 changes: 99 additions & 0 deletions test/modules/http2/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import inspect
import logging
import os
import subprocess

from pyhttpd.certs import CertificateSpec
from pyhttpd.conf import HttpdConf
from pyhttpd.env import HttpdTestEnv, HttpdTestSetup

log = logging.getLogger(__name__)


class H2TestSetup(HttpdTestSetup):

def __init__(self, env: 'HttpdTestEnv'):
super().__init__(env=env)

def make(self):
super().make(add_modules=["http2", "proxy_http2"])
self._add_h2test()

def _add_h2test(self):
p = subprocess.run([self.env.apxs, '-c', 'mod_h2test.c'],
capture_output=True,
cwd=os.path.join(self.env.local_dir, 'mod_h2test'))
rv = p.returncode
if rv != 0:
log.error(f"compiling md_h2test failed: {p.stderr}")
raise Exception(f"compiling md_h2test failed: {p.stderr}")

modules_conf = os.path.join(self.env.server_dir, 'conf/modules.conf')
with open(modules_conf, 'a') as fd:
# load our test module which is not installed
fd.write(f"LoadModule h2test_module \"{self.env.local_dir}/mod_h2test/.libs/mod_h2test.so\"\n")


class H2TestEnv(HttpdTestEnv):

def __init__(self, pytestconfig=None, setup_dirs=True):
super().__init__(pytestconfig=pytestconfig,
local_dir=os.path.dirname(inspect.getfile(H2TestEnv)),
add_base_conf="""
H2MinWorkers 1
H2MaxWorkers 64
""",
interesting_modules=["http2", "proxy_http2", "h2test"])
self.add_cert_specs([
CertificateSpec(domains=[
f"push.{self._http_tld}",
f"hints.{self._http_tld}",
f"ssl.{self._http_tld}",
f"pad0.{self._http_tld}",
f"pad1.{self._http_tld}",
f"pad2.{self._http_tld}",
f"pad3.{self._http_tld}",
f"pad8.{self._http_tld}",
]),
CertificateSpec(domains=[f"noh2.{self.http_tld}"], key_type='rsa2048'),
])
if setup_dirs:
self._setup = H2TestSetup(env=self)
self._setup.make()
self.issue_certs()
self.setup_data_1k_1m()


def setup_data_1k_1m(self):
s100 = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678\n"
with open(os.path.join(self.gen_dir, "data-1k"), 'w') as f:
for i in range(10):
f.write(s100)
with open(os.path.join(self.gen_dir, "data-10k"), 'w') as f:
for i in range(100):
f.write(s100)
with open(os.path.join(self.gen_dir, "data-100k"), 'w') as f:
for i in range(1000):
f.write(s100)
with open(os.path.join(self.gen_dir, "data-1m"), 'w') as f:
for i in range(10000):
f.write(s100)


class H2Conf(HttpdConf):

def __init__(self, env: HttpdTestEnv, path=None):
super().__init__(env=env, path=path)


def add_vhost_noh2(self):
self.start_vhost(self.env.https_port, "noh2", aliases=["noh2-alias"], doc_root="htdocs/noh2", with_ssl=True)
self.add(f"""
Protocols http/1.1
SSLOptions +StdEnvVars""")
self.end_vhost()
self.start_vhost(self.env.http_port, "noh2", aliases=["noh2-alias"], doc_root="htdocs/noh2", with_ssl=False)
self.add(" Protocols http/1.1")
self.add(" SSLOptions +StdEnvVars")
self.end_vhost()
return self
17 changes: 0 additions & 17 deletions test/modules/http2/test_000_infra.py

This file was deleted.

Loading

0 comments on commit ded853c

Please sign in to comment.