Skip to content

Commit

Permalink
rework test_load_data
Browse files Browse the repository at this point in the history
  • Loading branch information
jerabekjiri committed Oct 30, 2024
1 parent 90b3174 commit 2243c11
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 29 deletions.
172 changes: 150 additions & 22 deletions galaxy_ng/tests/integration/api/test_load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest

from galaxy_ng.tests.integration.conftest import is_hub_4_7_or_higher
from galaxy_ng.tests.integration.utils.iqe_utils import sign_collection_on_demand, is_ocp_env
from galaxy_ng.tests.integration.utils.iqe_utils import sign_collection_on_demand, is_ocp_env, \
aap_gateway
from galaxy_ng.tests.integration.utils.repo_management_utils import create_repo_and_dist, \
upload_new_artifact
from galaxykit.collections import deprecate_collection, \
Expand All @@ -15,19 +16,25 @@
from galaxykit.roles import put_update_role
from galaxykit.users import update_user
from galaxykit.utils import GalaxyClientError, wait_for_task
from galaxykit.client import BasicAuthClient


logger = logging.getLogger(__name__)


class TestLoadData:

"""
Test loading data that will be verified at a later stage
after the AAP upgrade or backup/restore
"""

@pytest.mark.min_hub_version("4.6dev")
@pytest.mark.load_data
def test_load_data(self, galaxy_client, data, ansible_config):
"""
Test loading data that will be verified at a later stage
after the AAP upgrade or backup/restore
"""
def test_load_users_and_groups(self, galaxy_client, settings, data):
if settings.get('ALLOW_LOCAL_RESOURCE_MANAGEMENT') is False:
pytest.skip("this test relies on local resource creation")

gc = galaxy_client("admin")

for group in data["groups"]:
Expand All @@ -52,24 +59,125 @@ def test_load_data(self, galaxy_client, data, ansible_config):
group = gc.get_group(user["group"])
gc.add_user_to_group(user["username"], group["id"])

@pytest.mark.skipif(not aap_gateway(),
reason="Load data test was skipped. Only works with gateway enabled.")
@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_gw_users_and_teams(self, galaxy_client, data):
gc = galaxy_client("admin")
gw_client = BasicAuthClient(gc.galaxy_root, gc.username, gc.password)

created_org = gw_client.post(
'/api/gateway/v1/organizations/',
body=json.dumps({'name': data['organization']})
)
assert created_org['name'] == data['organization']

created_teams = {}
for team in data["teams"]:
logger.debug(f"Creating team {team['name']}")
created_team = gw_client.post(
'/api/gateway/v1/teams/',
body=json.dumps({
'name': team['name'],
'organization': created_org['id']
})
)
assert created_team['name'] == team['name']
created_teams.update({team['name']: created_team['id']})

for user in data["users"]:
logger.debug(f"Creating user {user['username']}")
created_user = gw_client.post(
'/api/gateway/v1/users/',
body=json.dumps({
'username': user['username'],
'password': user['password']
})
)
assert created_user['username'] == user['username']

# if it exists, we should update it
updated_user = gw_client.patch(
f'/api/gateway/v1/users/{created_user["id"]}/',
body=json.dumps({
"id": created_user["id"],
"username": user["username"],
"email": user["email"],
"password": user["password"],
"is_superuser": user["is_superuser"],
})
)
assert updated_user['email'] == user['email']
assert updated_user['is_superuser'] == user['is_superuser']

teammember_role = gw_client.get(
'/api/gateway/v1/role_definitions/?name=Team Member'
)['results'][0]

# associate user to teams
team_assignment = gw_client.post(
'/api/gateway/v1/role_user_assignments/',
body=json.dumps({
'user': updated_user['id'],
'role_definition': teammember_role['id'],
'object_id': created_teams.get(user['team']),
})
)
assert team_assignment['role_definition'] == teammember_role['id']
assert team_assignment['user'] == updated_user['id']

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_namespaces(self, galaxy_client, data):
gc = galaxy_client("admin")

namespaceowner_role = None

if aap_gateway():
namespaceowner_role = gc.get(
'_ui/v2/role_definitions/?name=galaxy.collection_namespace_owner'
)['results'][0]

for ns in data["namespaces"]:
logger.debug(f"Creating namespace {ns['name']}")
gc.create_namespace(ns["name"], ns["group"],
object_roles=["galaxy.collection_namespace_owner"])
add_group(gc, ns["name"], ns["group"],
object_roles=["galaxy.collection_namespace_owner"])

if is_hub_4_7_or_higher(ansible_config):
for repo in data["repositories"]:
try:
logger.debug(f"Creating repository and distribution {repo['name']}")
create_repo_and_dist(gc, repo["name"])
except GalaxyClientError as e:
if "This field must be unique" in e.response.text:
logger.debug(
f"Repository {repo['name']} already exists. Not a problem.")
else:
raise e
if aap_gateway():
namespace = gc.create_namespace(ns["name"], None, None)
assert namespace['name'] == ns['name']

team = gc.get(f'_ui/v2/teams/?name={ns["team"]}')['results'][0]

assignment = gc.post(
'_ui/v2/role_team_assignments/',
body=json.dumps({
'team': team['id'],
'role_definition': namespaceowner_role['id'],
'object_id': namespace['id'],
'content_type': 'galaxy.namespace',
})
)
assert team['id'] == assignment['team']
else:
gc.create_namespace(ns["name"], ns["group"],
object_roles=["galaxy.collection_namespace_owner"])
add_group(gc, ns["name"], ns["group"],
object_roles=["galaxy.collection_namespace_owner"])

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_repositories_and_remotes(self, galaxy_client, data):
gc = galaxy_client("admin")

for repo in data["repositories"]:
try:
logger.debug(f"Creating repository and distribution {repo['name']}")
create_repo_and_dist(gc, repo["name"])
except GalaxyClientError as e:
if "This field must be unique" in e.response.text:
logger.debug(
f"Repository {repo['name']} already exists. Not a problem.")
else:
raise e

for remote in data["remotes"]:
try:
Expand All @@ -86,6 +194,11 @@ def test_load_data(self, galaxy_client, data, ansible_config):
else:
raise e

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_collections(self, galaxy_client, data, ansible_config):
gc = galaxy_client("admin")

for collection in data["collections"]:
if (collection["repository"] != "published"
and not is_hub_4_7_or_higher(ansible_config)):
Expand Down Expand Up @@ -118,6 +231,11 @@ def test_load_data(self, galaxy_client, data, ansible_config):
else:
raise e

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_roles(self, galaxy_client, data):
gc = galaxy_client("admin")

for role in data["roles"]:
name = role["name"]
description = role["description"]
Expand All @@ -137,6 +255,11 @@ def test_load_data(self, galaxy_client, data, ansible_config):
else:
raise e

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_remote_registries(self, galaxy_client, data):
gc = galaxy_client("admin")

for remote_registry in data["remote_registries"]:
try:
logger.debug(f"Creating remote registry {remote_registry['name']}")
Expand All @@ -150,6 +273,11 @@ def test_load_data(self, galaxy_client, data, ansible_config):
else:
raise e

@pytest.mark.min_hub_version("4.6")
@pytest.mark.load_data
def test_load_execution_environments(self, galaxy_client, data):
gc = galaxy_client("admin")

for ee in data["execution_environments"]:
try:
logger.debug(f"Creating execution environment {ee['name']}")
Expand Down
49 changes: 42 additions & 7 deletions galaxy_ng/tests/integration/api/test_verify_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import pytest
from galaxykit.client import BasicAuthClient

from galaxy_ng.tests.integration.conftest import is_hub_4_7_or_higher
from galaxy_ng.tests.integration.utils.iqe_utils import is_upgrade_from_aap23_hub46, \
galaxy_auto_sign_collections, is_upgrade_from_aap22_hub45, is_ocp_env
galaxy_auto_sign_collections, is_upgrade_from_aap22_hub45, is_ocp_env, aap_gateway
from galaxy_ng.tests.integration.utils.repo_management_utils import search_collection_endpoint
from galaxykit.collections import collection_info
from galaxykit.groups import get_group_id
Expand All @@ -30,24 +31,47 @@ def test_verify_data_users(self, galaxy_client, data):
Test that verifies the data previously loaded by test_load_data
"""
gc = galaxy_client("admin")
gw_client = None
if aap_gateway():
gw_client = BasicAuthClient(gc.galaxy_root, gc.username, gc.password)

for expected_user in data["users"]:
actual_user = get_user(gc, expected_user["username"])
if aap_gateway():
actual_user = gw_client.get(
f'/api/gateway/v1/users/?username={expected_user["username"]}'
)['results'][0]
else:
actual_user = get_user(gc, expected_user["username"])

assert expected_user["username"] == actual_user["username"]
assert expected_user["email"] == actual_user["email"]
assert expected_user["is_superuser"] == actual_user["is_superuser"]
assert expected_user["group"] in str(actual_user["groups"])

if aap_gateway():
user_teams = gw_client.get(
f'/api/gateway/v1/users/{actual_user["id"]}/teams/'
)['results']
assert expected_user["team"] in [team['name'] for team in user_teams]
else:
assert expected_user["group"] in str(actual_user["groups"])

@pytest.mark.min_hub_version("4.6dev")
@pytest.mark.verify_data
def test_verify_data_ns(self, galaxy_client, data):
"""
Test that verifies the data previously loaded by test_load_data
"""
org = data.get('organization')
gc = galaxy_client("admin")
for expected_ns in data["namespaces"]:
actual_ns = get_namespace(gc, expected_ns["name"])
assert expected_ns["name"] == actual_ns["name"]
assert expected_ns["group"] in str(actual_ns["groups"])

if aap_gateway():
user_groups = [g['name'] for g in actual_ns['groups']]
assert f'{org}::{expected_ns["team"]}' in user_groups
else:
assert expected_ns["group"] in str(actual_ns["groups"])

@pytest.mark.min_hub_version("4.6dev")
@pytest.mark.verify_data
Expand Down Expand Up @@ -94,13 +118,24 @@ def test_verify_data_collections(self, galaxy_client, data, ansible_config):

@pytest.mark.min_hub_version("4.6dev")
@pytest.mark.verify_data
def test_verify_data_groups(self, galaxy_client, data):
def test_verify_data_groups(self, galaxy_client, settings, data):
"""
Test that verifies the data previously loaded by test_load_data
"""
org = data.get('organization')
gc = galaxy_client("admin")
for expected_group in data["groups"]:
get_group_id(gc, expected_group["name"])
if aap_gateway():
# in gateway, group is replaced with team
data_key = 'teams'
else:
data_key = 'groups'

for expected_value in data[data_key]:
if aap_gateway():
# in gateway, groups are '<organization>::<team>
get_group_id(gc, f'{org}::{expected_value["name"]}')
else:
get_group_id(gc, expected_value["name"])

@pytest.mark.min_hub_version("4.7dev")
@pytest.mark.skipif(is_upgrade_from_aap23_hub46(), reason=SKIP_MESSAGE_23)
Expand Down
13 changes: 13 additions & 0 deletions galaxy_ng/tests/integration/load_data.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespaces:
- name: ns_test_1
group: group_1
team: team_1
- name: ns_test_2
group: group_1
team: team_1
- name: ns_test_3
group: group_2
team: team_2
- name: ns_test_4
group: group_2
team: team_2

collections:
# name is the suffix, collection_dep_a_{name}
Expand Down Expand Up @@ -40,22 +44,31 @@ groups:
- name: group_1
- name: group_2

teams:
- name: team_1
- name: team_2

organization: hub_org

users:
- username: user_1
password: P@ssword!
email: [email protected]
is_superuser: true
group: group_1
team: team_1
- username: user_2
password: P@ssword!
email: [email protected]
is_superuser: true
group: group_2
team: team_2
- username: user_3
password: P@ssword!
email: [email protected]
is_superuser: false
group: group_2
team: team_2

repositories:
- name: repo-test-1
Expand Down

0 comments on commit 2243c11

Please sign in to comment.