Browse Source

Allow ports for tests to be specified via env

SERVEFILE_DEFAULT_PORT and SERVEFILE_SECONDARY_PORT can be used to
specify ports used in the servefile tests. This can be useful if the
default port 8080 and the secondary port 8081 (for the -p test) are
already in use. To allow automatic choosing of a free port 0 can be
specified to tell the test code to automatically select a free port.
Sebastian Lohff 7 months ago
parent
commit
b5a9c52ed1
2 changed files with 28 additions and 5 deletions
  1. 3
    0
      ChangeLog
  2. 25
    5
      tests/test_servefile.py

+ 3
- 0
ChangeLog View File

@@ -7,6 +7,9 @@ Unreleased
7 7
 
8 8
 	* fixed bug where exception was shown on transmission abort with python3
9 9
 	* fixed wrong/outdated pyopenssl package names
10
+	* ports for running servefile tests can now be specified via the
11
+	  environment variables SERVEFILE_DEFAULT_PORT and
12
+	  SERVEFILE_SECONDARY_PORT, use 0 for automatic selection
10 13
 
11 14
 
12 15
 2020-10-30 v0.5.1

+ 25
- 5
tests/test_servefile.py View File

@@ -20,6 +20,21 @@ else:
20 20
     connrefused_exc = socket.error
21 21
 
22 22
 
23
+def _get_port_from_env(var_name, default):
24
+    port = int(os.environ.get(var_name, default))
25
+    if port == 0:
26
+        # do a one-time port selection for a free port, use it for all tests
27
+        s = socket.socket()
28
+        s.bind(('', 0))
29
+        port = s.getsockname()[1]
30
+        s.close()
31
+    return port
32
+
33
+
34
+SERVEFILE_DEFAULT_PORT = _get_port_from_env('SERVEFILE_DEFAULT_PORT', 8080)
35
+SERVEFILE_SECONDARY_PORT = _get_port_from_env('SERVEFILE_SECONDARY_PORT', 8081)
36
+
37
+
23 38
 @pytest.fixture
24 39
 def run_servefile():
25 40
     instances = []
@@ -34,6 +49,10 @@ def run_servefile():
34 49
             # call servefile as python module
35 50
             servefile_path = ['-m', 'servefile']
36 51
 
52
+        # use non-default default port, if one is given via env (and none via args)
53
+        if '-p' not in args and SERVEFILE_DEFAULT_PORT != 8080:
54
+            args.extend(['-p', str(SERVEFILE_DEFAULT_PORT)])
55
+
37 56
         print("running {} with args {}".format(", ".join(servefile_path), args))
38 57
         p = subprocess.Popen([sys.executable] + servefile_path + args, **kwargs)
39 58
         time.sleep(kwargs.get('timeout', 0.3))
@@ -69,7 +88,8 @@ def datadir(tmp_path):
69 88
     return _datadir
70 89
 
71 90
 
72
-def make_request(path='/', host='localhost', port=8080, method='get', protocol='http', **kwargs):
91
+def make_request(path='/', host='localhost', port=SERVEFILE_DEFAULT_PORT, method='get', protocol='http', timeout=5,
92
+                 **kwargs):
73 93
     url = '{}://{}:{}{}'.format(protocol, host, port, path)
74 94
     print('Calling {} on {} with {}'.format(method, url, kwargs))
75 95
     r = getattr(requests, method)(url, **kwargs)
@@ -145,9 +165,9 @@ def test_redirect_and_download(run_servefile, datadir):
145 165
 def test_specify_port(run_servefile, datadir):
146 166
     data = "NOOT NOOT"
147 167
     p = datadir({'testfile': data}) / 'testfile'
148
-    run_servefile([str(p), '-p', '8081'])
168
+    run_servefile([str(p), '-p', str(SERVEFILE_SECONDARY_PORT)])
149 169
 
150
-    check_download(data, fname='testfile', port=8081)
170
+    check_download(data, fname='testfile', port=SERVEFILE_SECONDARY_PORT)
151 171
 
152 172
 
153 173
 def test_ipv4_only(run_servefile, datadir):
@@ -159,7 +179,7 @@ def test_ipv4_only(run_servefile, datadir):
159 179
 
160 180
     sock = socket.socket(socket.AF_INET6)
161 181
     with pytest.raises(connrefused_exc):
162
-        sock.connect(("::1", 8080))
182
+        sock.connect(("::1", SERVEFILE_DEFAULT_PORT))
163 183
 
164 184
 
165 185
 def test_big_download(run_servefile, datadir):
@@ -368,7 +388,7 @@ def test_abort_download(run_servefile, datadir):
368 388
     # provoke a connection abort
369 389
     # hopefully the buffers will not fill up with all of the 10mb
370 390
     sock = socket.socket(socket.AF_INET)
371
-    sock.connect(("localhost", 8080))
391
+    sock.connect(("localhost", SERVEFILE_DEFAULT_PORT))
372 392
     sock.send(b"GET /testfile HTTP/1.0\n\n")
373 393
     resp = sock.recv(100)
374 394
     assert resp != b''

Loading…
Cancel
Save