2022-10-06 13:33:45 +02:00
# SPDX-FileCopyrightText: Red Hat, Inc.
# SPDX-License-Identifier: GPL-2.0-or-later
2015-06-12 05:02:13 +03:00
import os
import pprint
2016-01-05 13:36:10 +02:00
import tempfile
2016-11-16 01:15:43 +02:00
from contextlib import closing
from contextlib import contextmanager
2015-06-12 05:02:13 +03:00
import pytest
2020-05-10 09:40:03 +03:00
from ovirt_imageio . _internal import util
from ovirt_imageio . _internal import uhttp
2015-06-12 05:02:13 +03:00
2017-02-05 04:43:13 +02:00
@pytest.fixture ( scope = " session " )
2016-01-05 13:36:10 +02:00
def uhttpserver ( request ) :
tmp = tempfile . NamedTemporaryFile ( )
2018-11-08 22:02:27 +02:00
server = uhttp . Server ( tmp . name , uhttp . Connection )
2016-01-05 13:36:10 +02:00
util . start_thread ( server . serve_forever , kwargs = { " poll_interval " : 0.1 } )
request . addfinalizer ( server . shutdown )
request . addfinalizer ( tmp . close )
return server
def test_get ( uhttpserver ) :
2018-11-08 22:02:27 +02:00
uhttpserver . app = get
2016-01-05 13:36:10 +02:00
with make_connection ( uhttpserver ) as con :
con . request ( " GET " , " / " )
resp = con . getresponse ( )
log_response ( resp )
assert resp . status == 200
assert resp . getheader ( " content-type " ) == " text/plain "
2016-06-30 14:53:05 +03:00
assert resp . read ( ) == b " it works "
2016-01-05 13:36:10 +02:00
def test_put ( uhttpserver ) :
2018-11-08 22:02:27 +02:00
uhttpserver . app = echo
2016-01-05 13:36:10 +02:00
with make_connection ( uhttpserver ) as con :
2016-06-30 14:53:05 +03:00
con . request ( " PUT " , " / " , body = b " it works " )
2016-01-05 13:36:10 +02:00
resp = con . getresponse ( )
log_response ( resp )
assert resp . status == 200
assert resp . getheader ( " content-type " ) == " text/plain "
2016-06-30 14:53:05 +03:00
assert resp . read ( ) == b " it works "
2016-01-05 13:36:10 +02:00
def test_file ( tmpdir , uhttpserver ) :
2016-06-30 14:53:05 +03:00
data = b " x " * 1048576
2015-12-21 19:51:38 +02:00
tmp = tmpdir . join ( " data " )
tmp . write ( data )
2018-11-08 22:02:27 +02:00
uhttpserver . app = sendfile
2016-01-05 13:36:10 +02:00
with make_connection ( uhttpserver ) as con :
con . request ( " GET " , str ( tmp ) )
resp = con . getresponse ( )
log_response ( resp )
assert resp . status == 200
assert resp . getheader ( " content-type " ) == " text/plain "
content_length = int ( resp . getheader ( " content-length " ) )
assert content_length == os . path . getsize ( str ( tmp ) )
assert resp . read ( content_length ) == data
def test_connection_set_tunnel ( uhttpserver ) :
with make_connection ( uhttpserver ) as con :
2015-12-21 19:39:25 +02:00
with pytest . raises ( uhttp . UnsupportedError ) :
con . set_tunnel ( " 127.0.0.1 " )
2017-10-26 15:17:05 +03:00
@pytest.mark.skipif ( os . geteuid ( ) == 0 ,
reason = " Not compatible when running with root " )
2015-12-21 19:39:25 +02:00
def test_server_bind_error ( tmpdir ) :
# Make server_bind fail with EPERM
tmpdir . chmod ( 0o600 )
try :
sock = str ( tmpdir . join ( ' sock ' ) )
with pytest . raises ( OSError ) :
2018-11-08 22:02:27 +02:00
uhttp . Server ( sock , uhttp . Connection )
2015-12-21 19:39:25 +02:00
finally :
tmpdir . chmod ( 0o755 )
2018-11-08 22:02:27 +02:00
def get ( req , resp ) :
body = b " it works "
resp . headers [ " content-length " ] = len ( body )
resp . headers [ " content-type " ] = " text/plain "
resp . write ( body )
2015-06-12 05:02:13 +03:00
2018-11-08 22:02:27 +02:00
def echo ( req , resp ) :
body = req . read ( )
resp . headers [ " content-length " ] = len ( body )
resp . headers [ " content-type " ] = " text/plain "
resp . write ( body )
2015-06-12 05:02:13 +03:00
2018-11-08 22:02:27 +02:00
def sendfile ( req , resp ) :
path = req . path
resp . headers [ " content-length " ] = os . path . getsize ( path )
resp . headers [ " content-type " ] = " text/plain "
with open ( path , " rb " ) as f :
resp . write ( f . read ( ) )
2015-06-12 05:02:13 +03:00
def log_response ( resp ) :
pprint . pprint ( ( resp . status , resp . reason , resp . getheaders ( ) ) )
@contextmanager
2016-01-05 13:36:10 +02:00
def make_connection ( server ) :
2017-02-05 04:43:13 +02:00
con = uhttp . UnixHTTPConnection ( server . server_address , timeout = 2 )
2015-06-12 05:02:13 +03:00
with closing ( con ) :
yield con