Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Use fixtures where possible #6531

Merged
merged 1 commit into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions tests/test_conveyor.py
Expand Up @@ -138,9 +138,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 @@ -1349,7 +1349,7 @@ def on_receive(job_params):
('conveyor', 'usercert', 'DEFAULT_DUMMY_CERT'),
('vo_certs', 'new', 'NEW_VO_DUMMY_CERT'),
]}], indirect=True)
def test_multi_vo_certificates(file_config_mock, rse_factory, did_factory, scope_factory, vo, second_vo):
def test_multi_vo_certificates(file_config_mock, rse_factory, did_factory, scope_factory, vo, second_vo, root_account):
"""
Test that submitter and poller call fts with correct certificates in multi-vo env
"""
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
8 changes: 3 additions & 5 deletions tests/test_replica_sorting.py
Expand Up @@ -23,7 +23,6 @@
import geoip2.database
import pytest

from rucio.common.types import InternalAccount, InternalScope
from rucio.common.utils import parse_replicas_from_string
from rucio.common.config import config_get
from rucio.core import rse_expression_parser, replica_sorter
Expand Down Expand Up @@ -101,17 +100,16 @@ def _get_lat_long_mock(se, gi):


@pytest.fixture
def protocols_setup(vo):
def protocols_setup(vo, root_account, mock_scope):
rse_info = copy.deepcopy(base_rse_info)

files = [{'scope': InternalScope('mock', vo=vo), 'name': 'element_0', 'bytes': 1234, 'adler32': 'deadbeef'}]
root = InternalAccount('root', vo=vo)
files = [{'scope': mock_scope, 'name': 'element_0', 'bytes': 1234, 'adler32': 'deadbeef'}]

for idx in range(len(rse_info)):
rse_info[idx]['name'] = '%s_%s' % (rse_info[idx]['site'], rse_name_generator())
rse_info[idx]['id'] = add_rse(rse_info[idx]['name'], vo=vo)
add_rse_attribute(rse_id=rse_info[idx]['id'], key='site', value=base_rse_info[idx]['site'])
add_replicas(rse_id=rse_info[idx]['id'], files=files, account=root)
add_replicas(rse_id=rse_info[idx]['id'], files=files, account=root_account)

# invalidate cache for parse_expression('site=…')
rse_expression_parser.REGION.invalidate()
Expand Down