Skip to content

Commit

Permalink
Fixed tests, getting repo product based on naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
bklvsky committed May 3, 2024
1 parent 6b91553 commit df4110b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 66 deletions.
74 changes: 51 additions & 23 deletions alws/routers/uploads.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import re
import typing

from fastapi import APIRouter, Depends, Form, UploadFile
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload


from alws.auth import get_current_user
from alws.crud.products import get_products
from alws.dependencies import get_db
from alws.errors import PermissionDenied
from alws.models import User, Product, Repository, Build
from alws.models import Build, Product, Repository, User
from alws.perms import actions
from alws.perms.authorization import can_perform
from alws.utils.uploader import MetadataUploader
Expand All @@ -21,22 +21,48 @@
dependencies=[Depends(get_current_user)],
)

async def get_repo_object(session: AsyncSession, repository):
products = (await session.execute(
select(Product).filter(
Product.repositories.any(Repository.name == repository)

async def get_repo_product(
session: AsyncSession,
repository: str,
):
if repository.endswith("br"):
build = (
(
await session.execute(
select(Build).filter(
Build.repos.any(Repository.name.ilike(f'%{repository}'))
)
)
)
.scalars()
.first()
)
if build:
return (
(
await session.execute(
select(Product).filter(Product.team_id == build.team_id)
)
)
.scalars()
.first()
)
)).scalars().all()
if not products:
products = []
builds = (await session.execute(
select(Build)
.filter(Build.repos.any(Repository.name == repository))
.options(selectinload(Build.products))
)).scalars().all()
for build in builds:
products.append(build.products)
return products
else:
return (
(
await session.execute(
select(Product).filter(
Product.repositories.any(
Repository.name.ilike(f'%{repository}')
)
)
)
)
.scalars()
.first()
)


@router.post("/upload_repometada/")
async def upload_repometada(
Expand All @@ -48,12 +74,14 @@ async def upload_repometada(
):
msg = ""
uploader = MetadataUploader(session, repository)
repo_objects = await get_repo_object(session, repository)
for repo_obj in repo_objects:
if not can_perform(repo_obj, user, actions.ReleaseToProduct.name):
raise PermissionDenied(
"User does not have permissions to upload repository metadata to the repository."
)
repo_product = await get_repo_product(session, repository)
if not repo_product:
print(f"couldn't find a product or build repository {repository}")
return {"error": f"couldn't find a product or build repository {repository}"}
if not can_perform(repo_product, user, actions.ReleaseToProduct.name):
raise PermissionDenied(
f"User does not have permissions to upload repository metadata to the product {repo_product}."
)
if modules is None and comps is None:
return {"error": "there is nothing to upload"}
updated_metadata = await uploader.process_uploaded_files(modules, comps)
Expand Down
47 changes: 21 additions & 26 deletions alws/utils/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ async def upload_comps(
await self.pulp.create_rpm_publication(repo_href)
logging.info("Comps file upload has been finished")

async def upload_modules(
async def upload_rpm_modules(
self,
repo_href: str,
module_content: str,
dry_run: bool = False,
):
Expand All @@ -77,9 +76,7 @@ async def upload_modules(
_index = IndexWrapper.from_template(module_content)
with get_pulp_db() as pulp_session:
for module in _index.iter_modules():
defaults_snippet = _index.get_module_defaults_as_str(
module.name
)
defaults_snippet = _index.get_module_defaults_as_str(module.name)
defaults_checksum = hashlib.sha256(
defaults_snippet.encode('utf-8')
).hexdigest()
Expand Down Expand Up @@ -110,9 +107,7 @@ async def upload_modules(
pulp_defaults = None
for cond in conditions:
pulp_defaults = (
pulp_session.execute(
select(RpmModulemdDefaults).where(*cond)
)
pulp_session.execute(select(RpmModulemdDefaults).where(*cond))
.scalars()
.first()
)
Expand All @@ -121,9 +116,7 @@ async def upload_modules(
module_snippet = module.render()
if not pulp_module:
if dry_run:
logging.info(
"DRY_RUN: Module is not present in Pulp, creating"
)
logging.info("DRY_RUN: Module is not present in Pulp, creating")
continue
logging.info("Module is not present in Pulp, creating")
pulp_href = await self.pulp.create_module(
Expand All @@ -150,27 +143,20 @@ async def upload_modules(
db_modules.append(db_module)
else:
if dry_run:
logging.info(
"DRY_RUN: Updating existing module in Pulp"
)
logging.info("DRY_RUN: Updating existing module in Pulp")
continue
logging.info("Updating existing module in Pulp")
pulp_session.execute(
update(RpmModulemd)
.where(
RpmModulemd.content_ptr_id
== pulp_module.content_ptr_id
)
.where(RpmModulemd.content_ptr_id == pulp_module.content_ptr_id)
.values(
snippet=module_snippet,
description=module.description,
)
)
pulp_session.execute(
update(CoreContent)
.where(
CoreContent.pulp_id == pulp_module.content_ptr_id
)
.where(CoreContent.pulp_id == pulp_module.content_ptr_id)
.values(pulp_last_updated=datetime.datetime.now())
)
pulp_href = (
Expand Down Expand Up @@ -205,9 +191,7 @@ async def upload_modules(
)
pulp_session.execute(
update(CoreContent)
.where(
CoreContent.pulp_id == pulp_defaults.content_ptr_id
)
.where(CoreContent.pulp_id == pulp_defaults.content_ptr_id)
.values(pulp_last_updated=datetime.datetime.now())
)
href = (
Expand All @@ -217,6 +201,18 @@ async def upload_modules(
if href:
defaults_hrefs.append(href)
pulp_session.commit()
return db_modules, module_hrefs, defaults_hrefs

async def upload_modules(
self,
repo_href: str,
module_content: str,
dry_run: bool = False,
):
db_modules, module_hrefs, defaults_hrefs = await self.upload_rpm_modules(
module_content,
dry_run,
)
if db_modules and not dry_run:
self.session.add_all(db_modules)
await self.session.commit()
Expand All @@ -235,8 +231,7 @@ async def upload_modules(
await self.session.execute(
select(models.BuildTask)
.where(
models.BuildTask.build_id
== int(re_result["build_id"]),
models.BuildTask.build_id == int(re_result["build_id"]),
models.BuildTask.arch == re_result["arch"],
)
.options(selectinload(models.BuildTask.rpm_modules))
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"tests.fixtures.repositories",
"tests.fixtures.sign_keys",
"tests.fixtures.tests",
"tests.fixtures.uploader",
]


Expand Down
32 changes: 31 additions & 1 deletion tests/fixtures/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from alws.crud.products import create_product
from alws.models import Product
from alws.models import Product, Repository
from alws.schemas.product_schema import ProductCreate
from alws.schemas.repository_schema import RepositoryCreate
from tests.constants import ADMIN_USER_ID
from tests.test_utils.pulp_utils import get_repo_href


@pytest.fixture(
Expand Down Expand Up @@ -96,6 +99,33 @@ async def base_product(
yield product


@pytest.fixture
async def product_with_repo(
session: AsyncSession,
base_product: Product,
repository_for_product: Repository,
base_platform,
):
product = (
(
await session.execute(
select(Product)
.where(
Product.name == base_product.name,
)
.options(selectinload(Product.repositories))
)
)
.scalars()
.first()
)
product.repositories.append(repository_for_product)
session.add(product)
print([repo.name for repo in product.repositories])
await session.commit()
yield product


@pytest.mark.anyio
@pytest.fixture
async def user_product(
Expand Down
39 changes: 39 additions & 0 deletions tests/fixtures/repositories.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
from typing import Any, Dict

import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from alws.crud.repository import create_repository
from alws.models import Repository
from alws.schemas.repository_schema import RepositoryCreate


@pytest.fixture
def repo_for_upload_payload() -> Dict[str, Any]:
return {
'name': 'almalinux-8-appstream-x86_64',
'arch': 'x86_64',
'url': 'https://repo.almalinux.org/',
'type': 'rpm',
'debug': False,
}


@pytest.fixture
Expand All @@ -11,3 +28,25 @@ def create_test_repository_payload() -> Dict[str, Any]:
'tests_dir': 'almalinux/',
'tests_prefix': '8.',
}


@pytest.fixture
async def repository_for_product(
session: AsyncSession, repo_for_upload_payload: Dict[str, Any]
):
repo = (
(
await session.execute(
select(Repository).where(
Repository.name == repo_for_upload_payload['name'],
)
)
)
.scalars()
.first()
)
if not repo:
repo = Repository(**repo_for_upload_payload)
session.add(repo)
await session.commit()
yield repo
34 changes: 34 additions & 0 deletions tests/fixtures/uploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from alws import models
from alws.utils.modularity import IndexWrapper
from alws.utils.uploader import MetadataUploader
from tests.test_utils.pulp_utils import get_repo_href


@pytest.fixture
def upload_rpm_modules(monkeypatch):
async def func(
_,
module_content: str,
dry_run: bool = False,
):
db_modules = []
module_hrefs = []
defaults_hrefs = []
_index = IndexWrapper.from_template(module_content)

for module in _index.iter_modules():
pulp_href = get_repo_href()
db_module = models.RpmModule(
name=module.name,
stream=module.stream,
context=module.context,
arch=module.arch,
version=str(module.version),
pulp_href=pulp_href,
)
db_modules.append(db_module)
module_hrefs.append(pulp_href)
return db_modules, module_hrefs, defaults_hrefs
monkeypatch.setattr(MetadataUploader, "upload_rpm_modules", func)
Loading

0 comments on commit df4110b

Please sign in to comment.