2022-10-06 13:33:45 +02:00
# SPDX-FileCopyrightText: Red Hat, Inc.
# SPDX-License-Identifier: GPL-2.0-or-later
2018-06-07 18:32:45 +03:00
2020-08-27 22:32:49 +02:00
import http . client as http_client
2018-06-07 18:32:45 +03:00
import io
import json
2020-03-25 13:34:19 +01:00
import os
import stat
2018-06-07 18:32:45 +03:00
import pytest
2020-05-10 09:40:03 +03:00
from ovirt_imageio . _internal import config
from ovirt_imageio . _internal import server
2018-06-07 18:32:45 +03:00
from . import http
2020-02-14 14:48:47 +01:00
from . import testutil
2018-06-07 18:32:45 +03:00
@pytest.fixture ( scope = " module " )
2020-02-27 01:17:38 +02:00
def srv ( ) :
cfg = config . load ( [ " test/conf/daemon.conf " ] )
s = server . Server ( cfg )
s . start ( )
2018-06-07 18:32:45 +03:00
try :
2020-02-27 01:17:38 +02:00
yield s
2018-06-07 18:32:45 +03:00
finally :
2020-02-27 01:17:38 +02:00
s . stop ( )
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_method_not_allowed ( srv ) :
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
res = c . request ( " FOO " , " /images/ " )
assert res . status == http_client . METHOD_NOT_ALLOWED
2018-06-07 18:32:45 +03:00
@pytest.mark.parametrize ( " method " , [ " GET " , " PUT " , " PATCH " ] )
2020-02-27 01:17:38 +02:00
def test_no_resource ( srv , method ) :
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
res = c . request ( method , " /no/resource " )
assert res . status == http_client . NOT_FOUND
2018-06-07 18:32:45 +03:00
@pytest.mark.parametrize ( " method " , [ " GET " , " PUT " , " PATCH " ] )
2020-02-27 01:17:38 +02:00
def test_no_ticket_id ( srv , method ) :
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
res = c . request ( method , " /images/ " )
assert res . status == http_client . BAD_REQUEST
2018-06-07 18:32:45 +03:00
@pytest.mark.parametrize ( " method,body " , [
2018-05-21 14:18:29 +03:00
( " GET " , None ) ,
2018-06-07 18:32:45 +03:00
( " PUT " , " body " ) ,
2018-05-21 14:18:29 +03:00
( " PATCH " , json . dumps ( { " op " : " flush " } ) . encode ( " ascii " ) ) ,
2018-06-07 18:32:45 +03:00
] )
2020-02-27 01:17:38 +02:00
def test_no_ticket ( srv , method , body ) :
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
res = c . request ( method , " /images/no-ticket " , body = body )
assert res . status == http_client . FORBIDDEN
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_put_forbidden ( srv ) :
2020-02-14 14:48:47 +01:00
ticket = testutil . create_ticket (
2018-07-08 15:12:28 +03:00
url = " file:///no/such/image " , ops = [ " read " ] )
2020-02-27 01:17:38 +02:00
srv . auth . add ( ticket )
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . put ( " /images/ " + ticket [ " uuid " ] , " content " )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . FORBIDDEN
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_put ( srv , tmpdir ) :
2018-12-03 11:53:50 +02:00
data = b " -------|after "
2020-02-14 14:48:47 +01:00
image = testutil . create_tempfile ( tmpdir , " image " , data )
ticket = testutil . create_ticket ( url = " file:// " + str ( image ) )
2020-02-27 01:17:38 +02:00
srv . auth . add ( ticket )
2018-06-07 18:32:45 +03:00
uri = " /images/ " + ticket [ " uuid " ]
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . put ( uri , " content " )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . OK
assert res . getheader ( " content-length " ) == " 0 "
with io . open ( str ( image ) ) as f :
assert f . read ( len ( data ) ) == " content|after "
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_get_forbidden ( srv ) :
2020-02-14 14:48:47 +01:00
ticket = testutil . create_ticket (
2018-07-08 15:12:28 +03:00
url = " file:///no/such/image " , ops = [ ] )
2020-02-27 01:17:38 +02:00
srv . auth . add ( ticket )
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . get ( " /images/ " + ticket [ " uuid " ] , { " header " : " content " } )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . FORBIDDEN
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_get ( srv , tmpdir ) :
2019-10-07 15:25:01 +03:00
data = b " a " * 512 + b " b " * 512
2020-02-14 14:48:47 +01:00
image = testutil . create_tempfile ( tmpdir , " image " , data )
ticket = testutil . create_ticket (
2018-06-07 18:32:45 +03:00
url = " file:// " + str ( image ) , size = 1024 )
2020-02-27 01:17:38 +02:00
srv . auth . add ( ticket )
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . get ( " /images/ " + ticket [ " uuid " ] )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . OK
assert res . read ( ) == data
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_images_zero ( srv , tmpdir ) :
2019-10-07 15:25:01 +03:00
data = b " x " * 512
2020-02-14 14:48:47 +01:00
image = testutil . create_tempfile ( tmpdir , " image " , data )
ticket = testutil . create_ticket ( url = " file:// " + str ( image ) )
2020-02-27 01:17:38 +02:00
srv . auth . add ( ticket )
2018-06-07 18:32:45 +03:00
msg = { " op " : " zero " , " size " : 20 , " offset " : 10 , " future " : True }
size = msg [ " size " ]
offset = msg . get ( " offset " , 0 )
body = json . dumps ( msg ) . encode ( " ascii " )
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . patch ( " /images/ " + ticket [ " uuid " ] , body )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . OK
assert res . getheader ( " content-length " ) == " 0 "
with io . open ( str ( image ) , " rb " ) as f :
assert f . read ( offset ) == data [ : offset ]
assert f . read ( size ) == b " \0 " * size
assert f . read ( ) == data [ offset + size : ]
2018-06-07 18:32:45 +03:00
2020-02-27 01:17:38 +02:00
def test_options ( srv ) :
2020-04-01 21:18:31 +02:00
with http . LocalClient ( srv . config ) as c :
2020-04-02 12:38:39 +02:00
res = c . options ( " /images/* " )
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
allows = { " OPTIONS " , " GET " , " PUT " , " PATCH " }
2020-07-11 21:26:17 +03:00
features = { " checksum " , " extents " , " flush " , " zero " }
tests: Don't use global config in http helper
We used to import config in http helper, and use
config.images.{host,port} for creating a connection. This will not work
when we want to test proxy and daemon in the same test, and proxy is
implemented as a daemon with proxy ticket:
proxy -> daemon -> storage
In this setup we need to start 2 daemons, each with different
configuration, so we cannot use a global config in the tests.
The first step to fix this is to stop using the global config in the
http module. Instead, add http.Client() class, accepting a config object
and use the config to create the connection. The tests still use a
global config to create the client.
For consistency, create also a http.UnixClient() class, so tests using
config.{images,tickets}.socket can use the same code as tests using
config.images.{host,port}. This is important for extents_tests running
all tests with both http and local services.
Change-Id: I42643d9b43a3f157548551bca674a49b289dd75a
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2020-02-23 20:01:53 +02:00
assert res . status == http_client . OK
assert set ( res . getheader ( " allow " ) . split ( ' , ' ) ) == allows
options = json . loads ( res . read ( ) )
assert set ( options [ " features " ] ) == features
2020-02-27 01:17:38 +02:00
assert options [ " unix_socket " ] == srv . local_service . address
2020-03-25 13:34:19 +01:00
def test_control_socket_mode ( srv ) :
socket = srv . config . control . socket
actual_mode = stat . S_IMODE ( os . stat ( socket ) . st_mode )
assert oct ( actual_mode ) == oct ( 0o660 )