Skip to content

Commit b2cbb16

Browse files
deckodkliban
authored andcommitted
Add Domains compatibility and refactor some tests to pytest style.
Closes #273 - Add new fixtures and translate other tests. Tests passing, but still some skip. - Remove custom code used to deal with package metadata and package content. - Translate a test that checks the download policies. - Finish translating tests to pytest style and remove pulp-smash. - Update template files. - Add a new pytest dependency. - Add a last test for package search.
1 parent 3eb530c commit b2cbb16

27 files changed

+513
-828
lines changed

.github/template_gitref

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2021.08.26-421-g204a709
1+
2021.08.26-422-g3b31aa3

.github/workflows/docs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ on:
1313
jobs:
1414
test:
1515
if: "endsWith(github.base_ref, 'main')"
16-
runs-on: "ubuntu-20.04"
16+
runs-on: "ubuntu-latest"
1717
defaults:
1818
run:
1919
working-directory: "pulp_npm"
@@ -48,7 +48,7 @@ jobs:
4848
4949
no-test:
5050
if: "!endsWith(github.base_ref, 'main')"
51-
runs-on: "ubuntu-20.04"
51+
runs-on: "ubuntu-latest"
5252
steps:
5353
- run: |
5454
echo "Skip docs testing on non-main branches."

.github/workflows/scripts/install.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ VARSYAML
7575

7676
cat >> vars/main.yaml << VARSYAML
7777
pulp_env: {}
78-
pulp_settings: null
78+
pulp_settings: {"orphan_protection_time": 0}
7979
pulp_scheme: https
8080
pulp_default_container: ghcr.io/pulp/pulp-ci-centos9:latest
8181
VARSYAML
@@ -93,7 +93,7 @@ if [ "$TEST" = "s3" ]; then
9393
sed -i -e '$a s3_test: true\
9494
minio_access_key: "'$MINIO_ACCESS_KEY'"\
9595
minio_secret_key: "'$MINIO_SECRET_KEY'"\
96-
pulp_scenario_settings: null\
96+
pulp_scenario_settings: {"domain_enabled": true}\
9797
pulp_scenario_env: {}\
9898
test_storages_compat_layer: false\
9999
' vars/main.yaml
@@ -108,7 +108,7 @@ if [ "$TEST" = "azure" ]; then
108108
- ./azurite:/etc/pulp\
109109
command: "azurite-blob --blobHost 0.0.0.0"' vars/main.yaml
110110
sed -i -e '$a azure_test: true\
111-
pulp_scenario_settings: null\
111+
pulp_scenario_settings: {"domain_enabled": true}\
112112
pulp_scenario_env: {}\
113113
' vars/main.yaml
114114
fi

CHANGES/273.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added Domains compatibility.

dev_requirements.txt

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ check-manifest
22
coverage
33
black
44
flake8
5-
65
flake8-black
7-
8-
96
flake8-tuple
107
flake8-quotes

functest_requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
git+https://github.com/pulp/pulp-smash.git#egg=pulp-smash
21
pytest<8
32
pytest-xdist
43
pytest-timeout
4+
pytest-custom_exit_code

pulp_npm/app/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ class PulpNpmPluginAppConfig(PulpPluginAppConfig):
88
label = "npm"
99
version = "0.1.0a5.dev"
1010
python_package_name = "pulp-npm"
11+
domain_compatible = True

pulp_npm/app/content/__init__.py

-9
This file was deleted.

pulp_npm/app/content/handler.py

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 4.2.19 on 2025-02-11 21:03
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
import pulpcore.app.util
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('core', '0126_remoteartifact_failed_at'),
12+
('npm', '0003_alter_npmdistribution_distribution_ptr_and_more'),
13+
]
14+
15+
operations = [
16+
migrations.AlterUniqueTogether(
17+
name='package',
18+
unique_together=set(),
19+
),
20+
migrations.AddField(
21+
model_name='package',
22+
name='_pulp_domain',
23+
field=models.ForeignKey(default=pulpcore.app.util.get_domain_pk, on_delete=django.db.models.deletion.PROTECT, to='core.domain'),
24+
),
25+
migrations.AlterUniqueTogether(
26+
name='package',
27+
unique_together={('name', 'version', '_pulp_domain')},
28+
),
29+
]

pulp_npm/app/models.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
66
"""
77

8+
import json
89
from logging import getLogger
910

11+
from aiohttp.web_response import Response
12+
from django.conf import settings
1013
from django.db import models
1114

15+
1216
from pulpcore.plugin.models import (
1317
Content,
1418
Remote,
1519
Repository,
1620
Distribution,
1721
)
1822

23+
from pulpcore.plugin.util import get_domain_pk
24+
from .utils import urlpath_sanitize
25+
1926
logger = getLogger(__name__)
2027

2128

@@ -42,6 +49,7 @@ class Meta:
4249

4350
name = models.CharField(max_length=214)
4451
version = models.CharField(max_length=16)
52+
_pulp_domain = models.ForeignKey("core.Domain", default=get_domain_pk, on_delete=models.PROTECT)
4553

4654
@property
4755
def relative_path(self):
@@ -52,7 +60,7 @@ def relative_path(self):
5260

5361
class Meta:
5462
default_related_name = "%(app_label)s_%(model_name)s"
55-
unique_together = ("name", "version")
63+
unique_together = ("name", "version", "_pulp_domain")
5664

5765

5866
class NpmRemote(Remote):
@@ -93,3 +101,55 @@ class NpmDistribution(Distribution):
93101

94102
class Meta:
95103
default_related_name = "%(app_label)s_%(model_name)s"
104+
105+
def content_handler(self, path):
106+
data = {}
107+
108+
repository_version = self.repository_version
109+
if not repository_version:
110+
repository_version = self.repository.latest_version()
111+
112+
content = repository_version.content
113+
packages = Package.objects.filter(name=path, pk__in=content)
114+
115+
if not packages:
116+
return None
117+
118+
data["name"] = path
119+
data["versions"] = {}
120+
versions = []
121+
122+
if settings.DOMAIN_ENABLED:
123+
prefix_url = "{}/".format(
124+
urlpath_sanitize(
125+
settings.CONTENT_ORIGIN,
126+
settings.CONTENT_PATH_PREFIX,
127+
self.pulp_domain.name,
128+
self.base_path,
129+
)
130+
)
131+
else:
132+
prefix_url = "{}/".format(
133+
urlpath_sanitize(
134+
settings.CONTENT_ORIGIN,
135+
settings.CONTENT_PATH_PREFIX,
136+
self.base_path,
137+
)
138+
)
139+
140+
for package in packages:
141+
tarball_url = f"{prefix_url}{package.relative_path.split('/')[-1]}"
142+
143+
version = {
144+
package.version: {
145+
"_id": f"{package.name}@{package.version}",
146+
"dist": {"tarball": tarball_url},
147+
}
148+
}
149+
versions.append(package.version)
150+
data["versions"].update(version)
151+
152+
data["dist-tags"] = {"latest": max(versions)}
153+
154+
serialized_data = json.dumps(data)
155+
return Response(body=serialized_data)

pulp_npm/app/serializers.py

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
"""
2-
Check `Plugin Writer's Guide`_ for more details.
3-
4-
.. _Plugin Writer's Guide:
5-
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
6-
"""
7-
8-
from gettext import gettext as _
9-
10-
from django.conf import settings
111
from rest_framework import serializers
122

133
from pulpcore.plugin import models as core_models
144
from pulpcore.plugin import serializers as platform
155

16-
from .utils import pulp_npm_content_path
176
from . import models
187

198

@@ -79,7 +68,7 @@ class Meta:
7968
help_text="The policy to use when downloading content. The possible values include: "
8069
"'immediate', 'on_demand', and 'streamed'. 'immediate' is the default.",
8170
choices=core_models.Remote.POLICY_CHOICES,
82-
default=core_models.Remote.IMMEDIATE,
71+
required=False,
8372
)
8473

8574
class Meta:
@@ -106,32 +95,11 @@ class Meta:
10695
model = models.NpmRepository
10796

10897

109-
class NpmBaseURLField(serializers.CharField):
110-
"""
111-
Field for the base_url field pointing to the npm content app.
112-
"""
113-
114-
def to_representation(self, value):
115-
"""
116-
Field representation.
117-
"""
118-
base_path = value
119-
origin = settings.CONTENT_ORIGIN
120-
prefix = pulp_npm_content_path()
121-
return "/".join((origin.strip("/"), prefix.strip("/"), base_path.lstrip("/")))
122-
123-
12498
class NpmDistributionSerializer(platform.DistributionSerializer):
12599
"""
126100
Serializer for NPM Distributions.
127101
"""
128102

129-
base_url = NpmBaseURLField(
130-
source="base_path",
131-
read_only=True,
132-
help_text=_("The URL for accessing the universe API as defined by this distribution."),
133-
)
134-
135103
class Meta:
136104
fields = platform.DistributionSerializer.Meta.fields
137105
model = models.NpmDistribution

pulp_npm/app/settings.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
"""
2-
Check `Plugin Writer's Guide`_ for more details.
3-
4-
.. _Plugin Writer's Guide:
5-
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
6-
"""

pulp_npm/app/urls.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
from django.conf import settings
2-
from django.urls import path
3-
4-
from pulp_npm.app.viewsets import PublishedPackageViewSet
5-
6-
7-
PACKAGE_API_ROOT = getattr(settings, "PACKAGE_API_ROOT", "pulp_npm/packages/<path:name>/")
8-
9-
urlpatterns = [path(PACKAGE_API_ROOT, PublishedPackageViewSet.as_view())]
1+
urlpatterns = []

pulp_npm/app/utils.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
from django.conf import settings
1+
def urlpath_sanitize(*args):
2+
"""
3+
Join an arbitrary number of strings into a /-separated path.
24
5+
Replaces uses of urljoin() that don't want/need urljoin's subtle semantics.
36
4-
def pulp_npm_content_path():
5-
"""Get base cotent path from configuration."""
6-
components = settings.CONTENT_PATH_PREFIX.split("/")
7-
components[1] = "pulp_npm"
8-
return "/".join(components)
7+
Returns: single string provided arguments separated by single-slashes
8+
9+
Args:
10+
Arbitrary list of arguments to be join()ed
11+
"""
12+
segments = []
13+
for a in args + ("",):
14+
stripped = a.strip("/")
15+
if stripped:
16+
segments.append(stripped)
17+
return "/".join(segments)

0 commit comments

Comments
 (0)