Skip to content

Commit

Permalink
Tests: Use fixtures where possible; rucio#6529
Browse files Browse the repository at this point in the history
  • Loading branch information
rdimaio committed Mar 5, 2024
1 parent ca273f1 commit b8e3321
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 139 deletions.
6 changes: 2 additions & 4 deletions tests/test_abacus_collection_replica.py
Expand Up @@ -18,7 +18,6 @@

from rucio.common.exception import DataIdentifierNotFound
from rucio.common.schema import get_schema_value
from rucio.common.types import InternalAccount
from rucio.core.did import add_did, get_did
from rucio.core.replica import delete_replicas, get_cleaned_updated_collection_replicas
from rucio.daemons.abacus import collection_replica
Expand All @@ -33,16 +32,15 @@
@pytest.mark.noparallel(reason='uses daemons, fails when run in parallel')
class TestAbacusCollectionReplica():

def test_abacus_collection_replica_cleanup(self, vo, mock_scope, rse_factory, did_client):
def test_abacus_collection_replica_cleanup(self, vo, mock_scope, rse_factory, did_client, jdoe_account):
""" ABACUS (COLLECTION REPLICA): Test if the cleanup procedure works correctly. """
collection_replica.run(once=True)
db_session = session.get_session()
rse1, rse_id1 = rse_factory.make_rse()
rse2, rse_id2 = rse_factory.make_rse()

dataset = did_name_generator('dataset')
jdoe = InternalAccount('jdoe', vo)
add_did(mock_scope, dataset, DIDType.DATASET, jdoe)
add_did(mock_scope, dataset, DIDType.DATASET, jdoe_account)

models.CollectionReplica(scope=mock_scope, name=dataset, rse_id=rse_id1, did_type=DIDType.DATASET,
state=ReplicaState.AVAILABLE, bytes=1, length=1).save(session=db_session, flush=False)
Expand Down
16 changes: 7 additions & 9 deletions tests/test_account.py
Expand Up @@ -53,25 +53,23 @@ def test_update_account(self, vo):
assert email == 'test'
del_account(usr, 'root', vo=vo)

def test_list_account_identities(self, vo):
def test_list_account_identities(self, root_account):
""" ACCOUNT (CORE): Test listing of account identities """
email = 'email'
identity = uuid()
identity_type = IdentityType.USERPASS
account = InternalAccount('root', vo=vo)
add_account_identity(identity, identity_type, account, email, password='secret')
identities = list_identities(account)
add_account_identity(identity, identity_type, root_account, email, password='secret')
identities = list_identities(root_account)
assert {'type': identity_type, 'identity': identity, 'email': email} in identities

def test_add_account_attribute(self, vo):
def test_add_account_attribute(self, root_account):
""" ACCOUNT (CORE): Test adding attribute to account """
account = InternalAccount('root', vo=vo)
key = account_name_generator()
value = True
add_account_attribute(account, key, value)
assert {'key': key, 'value': True} in list_account_attributes(account)
add_account_attribute(root_account, key, value)
assert {'key': key, 'value': True} in list_account_attributes(root_account)
with pytest.raises(Duplicate):
add_account_attribute(account, key, value)
add_account_attribute(root_account, key, value)


def test_create_user_success(rest_client, auth_token):
Expand Down
50 changes: 21 additions & 29 deletions tests/test_authentication.py
Expand Up @@ -23,7 +23,6 @@
from rucio.api.authentication import get_auth_token_user_pass, get_auth_token_ssh, get_ssh_challenge_token, \
get_auth_token_saml
from rucio.common.exception import Duplicate, AccessDenied, CannotAuthenticate
from rucio.common.types import InternalAccount
from rucio.common.utils import ssh_sign
from rucio.core.identity import add_account_identity, del_account_identity
from rucio.core.authentication import strip_x509_proxy_attributes
Expand Down Expand Up @@ -118,12 +117,11 @@ def test_get_auth_token_user_pass_fail(self, vo):
result = get_auth_token_user_pass(account='root', username='ddmlab', password='not_secret', appid='test', ip='127.0.0.1', vo=vo)
assert result is None

def test_get_auth_token_ssh_success(self, vo):
def test_get_auth_token_ssh_success(self, vo, root_account):
"""AUTHENTICATION (CORE): SSH RSA public key exchange (good signature)."""

root = InternalAccount('root', vo=vo)
try:
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

Expand All @@ -135,14 +133,13 @@ def test_get_auth_token_ssh_success(self, vo):

assert result is not None

del_account_identity(PUBLIC_KEY, IdentityType.SSH, root)
del_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account)

def test_get_auth_token_ssh_fail(self, vo):
def test_get_auth_token_ssh_fail(self, vo, root_account):
"""AUTHENTICATION (CORE): SSH RSA public key exchange (wrong signature)."""

root = InternalAccount('root', vo=vo)
try:
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

Expand All @@ -152,14 +149,13 @@ def test_get_auth_token_ssh_fail(self, vo):

assert result is None

del_account_identity(PUBLIC_KEY, IdentityType.SSH, root)
del_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account)

def test_invalid_padding(self, vo):
def test_invalid_padding(self, vo, root_account):
"""AUTHENTICATION (CORE): SSH RSA public key exchange (public key with invalid padding)."""

root = InternalAccount('root', vo=vo)
try:
add_account_identity(INVALID_PADDED_PUBLIC_KEY, IdentityType.SSH, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity(INVALID_PADDED_PUBLIC_KEY, IdentityType.SSH, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

Expand All @@ -170,33 +166,31 @@ def test_invalid_padding(self, vo):
result = get_auth_token_ssh(account='root', signature=signature, appid='test', ip='127.0.0.1', vo=vo)
assert result is not None

del_account_identity(INVALID_PADDED_PUBLIC_KEY, IdentityType.SSH, root)
del_account_identity(INVALID_PADDED_PUBLIC_KEY, IdentityType.SSH, root_account)

def test_get_auth_token_saml_success(self, vo):
def test_get_auth_token_saml_success(self, vo, root_account):
"""AUTHENTICATION (CORE): SAML NameID (correct credentials)."""
root = InternalAccount('root', vo=vo)
try:
add_account_identity('ddmlab', IdentityType.SAML, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity('ddmlab', IdentityType.SAML, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

result = get_auth_token_saml(account='root', saml_nameid='ddmlab', appid='test', ip='127.0.0.1', vo=vo)
assert result is not None

del_account_identity('ddmlab', IdentityType.SAML, root)
del_account_identity('ddmlab', IdentityType.SAML, root_account)

def test_get_auth_token_saml_fail(self, vo):
def test_get_auth_token_saml_fail(self, vo, root_account):
"""AUTHENTICATION (CORE): SAML NameID (wrong credentials)."""
root = InternalAccount('root', vo=vo)
try:
add_account_identity('ddmlab', IdentityType.SAML, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity('ddmlab', IdentityType.SAML, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

with pytest.raises(AccessDenied):
get_auth_token_saml(account='root', saml_nameid='not_ddmlab', appid='test', ip='127.0.0.1', vo=vo)

del_account_identity('ddmlab', IdentityType.SAML, root)
del_account_identity('ddmlab', IdentityType.SAML, root_account)


def test_userpass_fail(vo, rest_client):
Expand All @@ -213,12 +207,11 @@ def test_userpass_success(vo, rest_client):


@pytest.mark.noparallel(reason='changes identities of the same account')
def test_ssh_success(vo, rest_client):
def test_ssh_success(vo, rest_client, root_account):
"""AUTHENTICATION (REST): SSH RSA public key exchange (correct credentials)."""

root = InternalAccount('root', vo=vo)
try:
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

Expand All @@ -234,16 +227,15 @@ def test_ssh_success(vo, rest_client):
assert response.status_code == 200
assert len(response.headers.get('X-Rucio-Auth-Token')) > 32

del_account_identity(PUBLIC_KEY, IdentityType.SSH, root)
del_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account)


@pytest.mark.noparallel(reason='changes identities of the same account')
def test_ssh_fail(vo, rest_client):
def test_ssh_fail(vo, rest_client, root_account):
"""AUTHENTICATION (REST): SSH RSA public key exchange (wrong credentials)."""

root = InternalAccount('root', vo=vo)
try:
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root, email='ph-adp-ddm-lab@cern.ch')
add_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account, email='ph-adp-ddm-lab@cern.ch')
except Duplicate:
pass # might already exist, can skip

Expand All @@ -253,7 +245,7 @@ def test_ssh_fail(vo, rest_client):
response = rest_client.get('/auth/ssh', headers=headers(hdrdict(headers_dict), vohdr(vo)))
assert response.status_code == 401

del_account_identity(PUBLIC_KEY, IdentityType.SSH, root)
del_account_identity(PUBLIC_KEY, IdentityType.SSH, root_account)


@pytest.mark.xfail(reason='The WebUI isn\'t linked to CERN SSO yet so this needs to be fixed once it is linked')
Expand Down
14 changes: 6 additions & 8 deletions tests/test_conveyor.py
Expand Up @@ -24,7 +24,6 @@
import pytest

import rucio.daemons.reaper.reaper
from rucio.common.types import InternalAccount
from rucio.common.utils import generate_uuid, adler32
from rucio.common.exception import ReplicaNotFound, RequestNotFound
from rucio.core import config as core_config
Expand Down Expand Up @@ -138,9 +137,9 @@ def do_GET(self):
self.send_code_and_message(200, {'Content-Type': 'application/json'}, file_content)

with MockServer(_SendScitagsJson) as mock_server:
core_config.set('packet-marking', 'enabled', True)
core_config.set('packet-marking', 'fetch_url', mock_server.base_url)
core_config.set('packet-marking', 'exp_name', 'atlas')
core_config.set('packet-marking', 'enabled', True)
core_config.set('packet-marking', 'fetch_url', mock_server.base_url)
core_config.set('packet-marking', 'exp_name', 'atlas')
yield mock_server


Expand Down Expand Up @@ -1356,18 +1355,17 @@ def test_multi_vo_certificates(file_config_mock, rse_factory, did_factory, scope

_, [scope1, scope2] = scope_factory(vos=[vo, second_vo])

def __init_test_for_vo(vo, scope):
def __init_test_for_vo(vo, scope, root_account):
src_rse, src_rse_id = rse_factory.make_rse(scheme='mock', protocol_impl='rucio.rse.protocols.posix.Default', vo=vo)
dst_rse, dst_rse_id = rse_factory.make_rse(scheme='mock', protocol_impl='rucio.rse.protocols.posix.Default', vo=vo)
all_rses = [src_rse_id, dst_rse_id]

for rse_id in all_rses:
rse_core.add_rse_attribute(rse_id, 'fts', TEST_FTS_HOST)
distance_core.add_distance(src_rse_id, dst_rse_id, distance=10)
account = InternalAccount('root', vo=vo)
did = did_factory.random_file_did(scope=scope)
replica_core.add_replica(rse_id=src_rse_id, scope=scope, name=did['name'], bytes_=1, account=account, adler32=None, md5=None)
rule_core.add_rule(dids=[did], account=account, copies=1, rse_expression=dst_rse, grouping='ALL', weight=None,
replica_core.add_replica(rse_id=src_rse_id, scope=scope, name=did['name'], bytes_=1, account=root_account, adler32=None, md5=None)
rule_core.add_rule(dids=[did], account=root_account, copies=1, rse_expression=dst_rse, grouping='ALL', weight=None,
lifetime=None, locked=False, subscription_id=None, ignore_account_limit=True)
return all_rses

Expand Down
2 changes: 1 addition & 1 deletion tests/test_dataset_replicas.py
Expand Up @@ -94,7 +94,7 @@ def test_list_datasets_per_rse(self, vo, mock_scope):
filters={'scope': mock_scope, 'name': pattern})]
assert replicas != []

def test_list_dataset_replicas_archive(self, vo):
def test_list_dataset_replicas_archive(self, vo, did_client):
""" REPLICA (CLIENT): List dataset replicas with archives. """

activity = get_schema_value('ACTIVITY')['enum'][0]
Expand Down
3 changes: 1 addition & 2 deletions tests/test_did.py
Expand Up @@ -24,7 +24,7 @@
from rucio.common.exception import (DataIdentifierNotFound, DataIdentifierAlreadyExists,
InvalidPath, UnsupportedOperation,
UnsupportedStatus, ScopeNotFound, FileAlreadyExists, FileConsistencyMismatch)
from rucio.common.types import InternalAccount, InternalScope
from rucio.common.types import InternalScope
from rucio.common.utils import generate_uuid
from rucio.core.did import (list_dids, add_did, delete_dids, get_did_atime, touch_dids, attach_dids, detach_dids,
get_metadata, set_metadata, get_did, get_did_access_cnt, add_did_to_followed,
Expand Down Expand Up @@ -434,7 +434,6 @@ def test_add_did(self, vo, did_client, rse_factory):
tmp_rse, rse_id = rse_factory.make_mock_rse()
rse2, rse2_id = rse_factory.make_mock_rse()
tmp_dsn = did_name_generator('dataset')
root = InternalAccount('root', vo=vo)

# PFN example: rfio://castoratlas.cern.ch/castor/cern.ch/grid/atlas/tzero/xx/xx/xx/filename
dataset_meta = {'project': 'data13_hip',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_import_export.py
Expand Up @@ -82,7 +82,7 @@ def reset_rses():


@pytest.fixture
def importer_example_data(vo):
def importer_example_data(vo, jdoe_account):
if not config_has_section('importer'):
config_add_section('importer')
config_set('importer', 'rse_sync_method', 'hard')
Expand Down Expand Up @@ -291,7 +291,7 @@ def check_accounts(self):
}
]
}, {
'account': InternalAccount('jdoe', vo=vo),
'account': jdoe_account,
'email': 'email'
}]
}
Expand Down
16 changes: 7 additions & 9 deletions tests/test_multi_vo.py
Expand Up @@ -45,7 +45,7 @@
from rucio.common.config import config_remove_option, config_set, config_has_section, config_add_section
from rucio.common.exception import AccessDenied, Duplicate, InvalidRSEExpression, UnsupportedAccountName, \
UnsupportedOperation, RucioException
from rucio.common.types import InternalAccount, InternalScope
from rucio.common.types import InternalAccount
from rucio.common.utils import generate_uuid, get_tmp_dir, parse_response, ssh_sign
from rucio.core import config as core_config
from rucio.core.account_counter import add_counter
Expand All @@ -56,7 +56,7 @@
from rucio.core.vo import map_vo
from rucio.daemons.automatix.automatix import automatix
from rucio.db.sqla import models, session as db_session
from rucio.tests.common import execute, headers, hdrdict, vohdr, auth, loginhdr, get_long_vo
from rucio.tests.common import execute, headers, hdrdict, vohdr, auth, loginhdr
from .test_authentication import PRIVATE_KEY, PUBLIC_KEY
from .test_oidc import get_mock_oidc_client, NEW_TOKEN_DICT

Expand All @@ -83,17 +83,16 @@ def test_multi_vo_flag(self, vo, second_vo):
config_set('common', 'multi_vo', 'True')

@pytest.mark.noparallel(reason='uses global RSE (MOCK) and fails when run in parallel')
def test_access_rule_vo(self, vo, second_vo):
def test_access_rule_vo(self, vo, second_vo, root_account, mock_scope):
""" MULTI VO (CORE): Test accessing rules from a different VO """
scope = InternalScope('mock', vo=vo)
scope = mock_scope
dataset = 'dataset_' + str(generate_uuid())
account = InternalAccount('root', vo=vo)
rse_str = ''.join(choice(ascii_uppercase) for x in range(10))
rse_name = 'MOCK_%s' % rse_str
rse_id = add_rse(rse_name, 'root', vo=vo)

add_replica(rse_id=rse_id, scope=scope, name=dataset, bytes_=10, account=account)
rule_id = add_rule(dids=[{'scope': scope, 'name': dataset}], account=account, copies=1, rse_expression='MOCK', grouping='NONE', weight=None, lifetime=None, locked=False, subscription_id=None)[0]
add_replica(rse_id=rse_id, scope=scope, name=dataset, bytes_=10, account=root_account)
rule_id = add_rule(dids=[{'scope': scope, 'name': dataset}], account=root_account, copies=1, rse_expression='MOCK', grouping='NONE', weight=None, lifetime=None, locked=False, subscription_id=None)[0]

with pytest.raises(AccessDenied):
delete_replication_rule(rule_id=rule_id, purge_replicas=False, issuer='root', vo=second_vo)
Expand Down Expand Up @@ -641,14 +640,13 @@ def test_rest_vomap_bad(self, rest_client):

class TestMultiVoClients:

def test_get_vo_from_config(self, vo):
def test_get_vo_from_config(self, long_vo):
""" MULTI VO (CLIENT): Get vo from config file when starting clients """
# Start clients with vo explicitly set to None
replica_client = ReplicaClient(vo=None)
client = Client(vo=None)
upload_client = UploadClient(_client=client)
# Check the vo has been got from the config file
long_vo = get_long_vo()
assert replica_client.vo == long_vo
assert upload_client.client.vo == long_vo

Expand Down
6 changes: 3 additions & 3 deletions tests/test_permission.py
Expand Up @@ -15,7 +15,7 @@

from rucio.api.permission import has_permission
from rucio.common.config import config_get
from rucio.common.types import InternalAccount, InternalScope
from rucio.common.types import InternalScope
from rucio.core.scope import add_scope
from rucio.core.account import add_account_attribute
from rucio.tests.common import scope_name_generator, skip_non_belleii
Expand All @@ -28,10 +28,10 @@ class TestPermissionCoreApi:

usr = 'jdoe'

def test_permission_add_did(self, vo):
def test_permission_add_did(self, vo, root_account):
""" PERMISSION(CORE): Check permission to add a did"""
scope = scope_name_generator()
add_scope(scope=InternalScope(scope, vo=vo), account=InternalAccount('root', vo=vo))
add_scope(scope=InternalScope(scope, vo=vo), account=root_account)
assert has_permission(issuer='panda', action='add_did', kwargs={'scope': scope}, vo=vo)
assert not has_permission(issuer='spock', action='add_did', kwargs={'scope': scope}, vo=vo)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_reaper.py
Expand Up @@ -24,7 +24,7 @@
from rucio.db.sqla.constants import OBSOLETE
from rucio.db.sqla.session import get_session
from rucio.common.exception import ReplicaNotFound, DataIdentifierNotFound
from rucio.common.types import InternalAccount, InternalScope
from rucio.common.types import InternalScope
from rucio.common.utils import generate_uuid
from rucio.core import did as did_core
from rucio.core import message as message_core
Expand Down Expand Up @@ -53,7 +53,7 @@
'delete': 1}}}


def __add_test_rse_and_replicas(vo, scope, rse_name, names, file_size, epoch_tombstone=False):
def __add_test_rse_and_replicas(vo, scope, rse_name, names, file_size, root_account, epoch_tombstone=False):
rse_id = rse_core.add_rse(rse_name, vo=vo)

rse_core.add_protocol(rse_id=rse_id, parameter=__mock_protocol)
Expand All @@ -67,7 +67,7 @@ def __add_test_rse_and_replicas(vo, scope, rse_name, names, file_size, epoch_tom
replica_core.add_replica(rse_id=rse_id, scope=scope,
name=file_name, bytes_=file_size,
tombstone=tombstone, meta={'datatype': 'SOME_DATATYPE'},
account=InternalAccount('root', vo=vo), adler32=None, md5=None)
account=root_account, adler32=None, md5=None)
return rse_name, rse_id, dids


Expand Down

0 comments on commit b8e3321

Please sign in to comment.