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

increase galaxykit usage #2000

Merged
merged 4 commits into from
Dec 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 79 additions & 145 deletions galaxy_ng/tests/integration/api/test_namespace_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
See: https://issues.redhat.com/browse/AAH-1303

"""
from time import sleep

import pytest
from ansible.errors import AnsibleError

from ..utils import (
build_collection as galaxy_build_collection,
get_all_namespaces,
get_client,
generate_unused_namespace,
wait_for_all_tasks,
wait_for_task,
)

from galaxykit.namespaces import get_namespace, get_namespace_collections
from galaxykit.repositories import search_collection
from galaxykit.users import get_me

from ..utils.iqe_utils import is_stage_environment
from ..utils.repo_management_utils import upload_new_artifact
from ..utils.tasks import wait_for_all_tasks_gk
from ..utils.tools import generate_random_string

pytestmark = pytest.mark.qa # noqa: F821

Expand All @@ -29,39 +29,27 @@
]
)
@pytest.mark.all
def test_namespace_create_and_delete(ansible_config, api_version):
def test_namespace_create_and_delete(api_version, galaxy_client):
"""Tests whether a namespace can be created and deleted"""

# http://192.168.1.119:8002/api/automation-hub/_ui/v1/namespaces/
# http://192.168.1.119:8002/api/automation-hub/v3/namespaces/
# {name: "testnamespace1", groups: []}

config = ansible_config("partner_engineer")
api_client = get_client(config, request_token=True, require_auth=True)
api_prefix = config.get("api_prefix").rstrip("/")

new_namespace = generate_unused_namespace(api_client=api_client, api_version=api_version)
gc = galaxy_client("partner_engineer")
new_namespace = f"ns_test_{generate_random_string()}"
payload = {'name': new_namespace, 'groups': []}
resp = api_client(f'{api_prefix}/{api_version}/namespaces/', args=payload, method='POST')
resp = gc.post(f"{api_version}/namespaces/", body=payload)
assert resp['name'] == new_namespace

existing2 = get_all_namespaces(api_client=api_client, api_version=api_version)
existing2 = dict((x['name'], x) for x in existing2)
assert new_namespace in existing2

# This should throw an AnsibleError because the response body is an
# empty string and can not be parsed to JSON
try:
resp = api_client(
f'{api_prefix}/{api_version}/namespaces/{new_namespace}/',
method='DELETE'
)
except AnsibleError:
pass

existing3 = get_all_namespaces(api_client=api_client, api_version=api_version)
existing3 = dict((x['name'], x) for x in existing3)
assert new_namespace not in existing3
if api_version == "v3":
get_namespace(gc, new_namespace)
gc.delete(f"{api_version}/namespaces/{new_namespace}/", parse_json=False)
with pytest.raises(KeyError):
get_namespace(gc, new_namespace)
if api_version == "_ui/v1":
gc.get(f"{api_version}/namespaces/?name={new_namespace}")
gc.delete(f"{api_version}/namespaces/{new_namespace}/", parse_json=False)
r = get_namespace_collections(gc, new_namespace)
assert len(r["data"]) == 0


@pytest.mark.galaxyapi_smoke
Expand All @@ -75,17 +63,11 @@ def test_namespace_create_and_delete(ansible_config, api_version):
]
)
@pytest.mark.min_hub_version("4.9")
def test_namespace_create_with_user(ansible_config, user_property):
config = ansible_config("partner_engineer")
api_client = get_client(config, request_token=True, require_auth=True)
api_prefix = config.get("api_prefix").rstrip("/")

# find this client's user info...
me = api_client(f'{api_prefix}/_ui/v1/me/')
def test_namespace_create_with_user(galaxy_client, user_property):
gc = galaxy_client("partner_engineer")
me = get_me(gc)
username = me['username']

new_namespace = generate_unused_namespace(api_client=api_client)

new_namespace = f"ns_test_{generate_random_string()}"
# make a namespace with a user and without defining groups ...
object_roles = [
'galaxy.collection_namespace_owner',
Expand All @@ -100,7 +82,7 @@ def test_namespace_create_with_user(ansible_config, user_property):
}
]
}
resp = api_client(f'{api_prefix}/_ui/v1/my-namespaces/', args=payload, method='POST')
resp = gc.post("_ui/v1/my-namespaces/", body=payload)

# should have the right results ...
assert resp['name'] == new_namespace
Expand All @@ -121,23 +103,17 @@ def test_namespace_create_with_user(ansible_config, user_property):
]
)
@pytest.mark.min_hub_version("4.9")
def test_namespace_edit_with_user(ansible_config, user_property):
config = ansible_config("partner_engineer")
api_client = get_client(config, request_token=True, require_auth=True)
api_prefix = config.get("api_prefix").rstrip("/")

# find this client's user info...
me = api_client(f'{api_prefix}/_ui/v1/me/')
def test_namespace_edit_with_user(galaxy_client, user_property):
gc = galaxy_client("partner_engineer")
me = get_me(gc)
username = me['username']

new_namespace = generate_unused_namespace(api_client=api_client)

new_namespace = f"ns_test_{generate_random_string()}"
# make a namespace without users and without groups ...
payload = {
'name': new_namespace,
}
resp = api_client(f'{api_prefix}/_ui/v1/my-namespaces/', args=payload, method='POST')

resp = gc.post("_ui/v1/my-namespaces/", body=payload)
# should have the right results ...
assert resp['name'] == new_namespace
assert resp['groups'] == []
Expand All @@ -157,12 +133,7 @@ def test_namespace_edit_with_user(ansible_config, user_property):
}
]
}
resp = api_client(
f'{api_prefix}/_ui/v1/my-namespaces/{new_namespace}/',
args=payload,
method='PUT'
)

resp = gc.put(f"_ui/v1/my-namespaces/{new_namespace}/", body=payload)
# should have the right results ...
assert resp['name'] == new_namespace
assert resp['groups'] == []
Expand All @@ -174,46 +145,38 @@ def test_namespace_edit_with_user(ansible_config, user_property):
@pytest.mark.namespace
@pytest.mark.all
@pytest.mark.min_hub_version("4.9dev")
def test_namespace_edit_logo(ansible_config):

config = ansible_config("admin")
api_client = get_client(config, request_token=True, require_auth=True)
api_prefix = config.get("api_prefix").rstrip("/")

new_namespace = generate_unused_namespace(api_client=api_client)

def test_namespace_edit_logo(galaxy_client):
gc = galaxy_client("admin")
new_namespace = f"ns_test_{generate_random_string()}"
payload = {
'name': new_namespace,
}
my_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/', args=payload, method='POST')
my_namespace = gc.post("_ui/v1/my-namespaces/", body=payload)
assert my_namespace["avatar_url"] == ''

namespaces = api_client(f'{api_prefix}/_ui/v1/my-namespaces/')

namespaces = gc.get('_ui/v1/my-namespaces/')
name = my_namespace["name"]

payload = {
"name": name,
"avatar_url": "http://placekitten.com/400/400"
}
api_client(f'{api_prefix}/_ui/v1/my-namespaces/{name}/', args=payload, method='PUT')

wait_for_all_tasks(api_client)
updated_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/{name}/')
gc.put(f"_ui/v1/my-namespaces/{name}/", body=payload)
wait_for_all_tasks_gk(gc)
updated_namespace = gc.get(f'_ui/v1/my-namespaces/{name}/')
assert updated_namespace["avatar_url"] != ""

payload = {
"name": name,
"avatar_url": "http://placekitten.com/123/456"
}
resp = api_client(f'{api_prefix}/_ui/v1/my-namespaces/{name}/', args=payload, method='PUT')

wait_for_all_tasks(api_client)
updated_again_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/{name}/')
gc.put(f"_ui/v1/my-namespaces/{name}/", body=payload)
wait_for_all_tasks_gk(gc)
updated_again_namespace = gc.get(f"_ui/v1/my-namespaces/{name}/")
assert updated_namespace["avatar_url"] != updated_again_namespace["avatar_url"]

# verify no additional namespaces are created
resp = api_client(f'{api_prefix}/_ui/v1/my-namespaces/')
resp = gc.get("_ui/v1/my-namespaces/")
assert resp["meta"]["count"] == namespaces["meta"]["count"]

# verify no side effects
Expand All @@ -226,65 +189,46 @@ def test_namespace_edit_logo(ansible_config):
assert my_namespace[field] != updated_again_namespace[field]


def _test_namespace_logo_propagates_to_collections(ansible_config, upload_artifact, is_insights):
admin_config = ansible_config("admin")
api_prefix = admin_config.get("api_prefix").rstrip("/")
api_client = get_client(admin_config, request_token=True, require_auth=True)

namespace_name = generate_unused_namespace(api_client=api_client)

# create empty namespace
def _test_namespace_logo_propagates_to_collections(galaxy_client, is_insights):
gc = galaxy_client("admin")
namespace_name = f"ns_test_{generate_random_string()}"
payload = {
'name': namespace_name
}
my_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/', args=payload, method='POST')
wait_for_all_tasks(api_client)
my_namespace = gc.post("_ui/v1/my-namespaces/", body=payload)
assert my_namespace["avatar_url"] == ''
assert my_namespace["avatar_sha256"] is None
assert my_namespace["metadata_sha256"] is not None

artifact = galaxy_build_collection(namespace=namespace_name)
artifact = upload_new_artifact(
gc, namespace_name, "published", "1.0.1", tags=["application"]
)
if is_stage_environment():
sleep(90)

# upload collection to namespace
upload_task = upload_artifact(admin_config, api_client, artifact)
resp = wait_for_task(api_client, upload_task)
assert resp["state"] == "completed"
resp = search_collection(gc, namespace=namespace_name, name=artifact.name)

# verify cv index is correct
search_url = (
api_prefix
+ '/v3/plugin/ansible/search/collection-versions/'
+ f'?namespace={namespace_name}&name={artifact.name}'
)
resp = api_client.request(search_url)
assert resp['data'][0]['namespace_metadata']["avatar_url"] is None

# upload logo to namespace
payload = {
"name": namespace_name,
"avatar_url": "http://placekitten.com/123/456"
}
api_client(f'{api_prefix}/_ui/v1/my-namespaces/{namespace_name}/', args=payload, method='PUT')
wait_for_all_tasks(api_client)
gc.put(f"_ui/v1/my-namespaces/{namespace_name}/", body=payload)
if is_stage_environment():
sleep(90)
wait_for_all_tasks_gk(gc)

# namespace logo was updated correctly
my_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/{namespace_name}/')
my_namespace = gc.get(f'_ui/v1/my-namespaces/{namespace_name}/')
assert my_namespace["avatar_url"] is not None

search_url = (
api_prefix
+ '/v3/plugin/ansible/search/collection-versions/'
+ f'?namespace={namespace_name}&name={artifact.name}'
)
resp = api_client(search_url)
resp = search_collection(gc, namespace=namespace_name, name=artifact.name)
cv_namespace_metadata = resp['data'][0]['namespace_metadata']

namespace_metadata = api_client(
api_prefix
+ '/pulp/api/v3/content/ansible/namespaces/'
f'?name={namespace_name}&ordering=-pulp_created'
)['results'][0]

resp = gc.get(f"pulp/api/v3/content/ansible/namespaces/"
f"?name={namespace_name}&ordering=-pulp_created")
namespace_metadata = resp['results'][0]
# verify that collection is using latest namespace avatar
assert cv_namespace_metadata['avatar_url'] == namespace_metadata['avatar_url']

Expand All @@ -303,34 +247,24 @@ def _test_namespace_logo_propagates_to_collections(ansible_config, upload_artifa
"company": "RedHat Inc.",
"avatar_url": "http://placekitten.com/654/321"
}
api_client(
f'{api_prefix}/_ui/v1/my-namespaces/{namespace_name}/',
args=payload,
method='PUT'
)
gc.put(f"_ui/v1/my-namespaces/{namespace_name}/", body=payload)
if is_stage_environment():
sleep(90)
assert my_namespace["avatar_sha256"] is not None
assert my_namespace["metadata_sha256"] is not None
wait_for_all_tasks(api_client)
wait_for_all_tasks_gk(gc)

my_namespace = api_client(f'{api_prefix}/_ui/v1/my-namespaces/{namespace_name}/')
my_namespace = gc.get(f'_ui/v1/my-namespaces/{namespace_name}/')

# verify cv metadata are latest and correct
search_url = (
api_prefix
+ '/v3/plugin/ansible/search/collection-versions/'
+ f'?namespace={namespace_name}&name={artifact.name}'
)
resp = api_client(search_url)
resp = search_collection(gc, namespace=namespace_name, name=artifact.name)
cv_namespace_metadata = resp['data'][0]['namespace_metadata']
assert cv_namespace_metadata["description"] == "hehe hihi haha"
assert cv_namespace_metadata["company"] == "RedHat Inc."

namespace_metadata = api_client(
api_prefix
+ '/pulp/api/v3/content/ansible/namespaces/'
f'?name={namespace_name}&ordering=-pulp_created'
)['results'][0]

resp = gc.get(f"pulp/api/v3/content/ansible/namespaces/"
f"?name={namespace_name}&ordering=-pulp_created")
namespace_metadata = resp["results"][0]
# verify cv idnex is using latest matedata
assert cv_namespace_metadata['avatar_url'] == namespace_metadata['avatar_url']

Expand All @@ -342,11 +276,11 @@ def _test_namespace_logo_propagates_to_collections(ansible_config, upload_artifa
@pytest.mark.deployment_community
@pytest.mark.deployment_standalone
@pytest.mark.min_hub_version("4.9dev")
def test_namespace_logo_propagates_to_collections(ansible_config, upload_artifact):
_test_namespace_logo_propagates_to_collections(ansible_config, upload_artifact, False)
def test_namespace_logo_propagates_to_collections(galaxy_client):
_test_namespace_logo_propagates_to_collections(galaxy_client, False)


@pytest.mark.namespace
@pytest.mark.deployment_cloud
def test_insights_namespace_logo_propagates_to_collections(ansible_config, upload_artifact):
_test_namespace_logo_propagates_to_collections(ansible_config, upload_artifact, True)
def test_insights_namespace_logo_propagates_to_collections(galaxy_client):
_test_namespace_logo_propagates_to_collections(galaxy_client, True)
12 changes: 12 additions & 0 deletions galaxy_ng/tests/integration/utils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def wait_for_all_tasks(client, timeout=300):
time.sleep(1)


def wait_for_all_tasks_gk(gc, timeout=300):
ready = False
wait_until = time.time() + timeout
while not ready:
if wait_until < time.time():
raise TaskWaitingTimeout()
running_count = gc.get("pulp/api/v3/tasks/?state=running")["count"]
waiting_count = gc.get("pulp/api/v3/tasks/?state=waiting")["count"]
ready = running_count == 0 and waiting_count == 0
time.sleep(1)


def wait_for_task(api_client, resp, task_id=None, timeout=6000, raise_on_error=False):
if task_id:
url = f"v3/tasks/{task_id}/"
Expand Down
Loading