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

[BUGFIX] Allow space in service names #240

Merged
merged 1 commit into from
Feb 21, 2020
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
8 changes: 4 additions & 4 deletions promgen/migrations/0013_validation_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='farm',
name='name',
field=models.CharField(max_length=128, validators=[django.core.validators.RegexValidator(re.compile('^[-\\w]+\\Z'), "Enter a valid 'slug' consisting of Unicode letters, numbers, underscores, or hyphens.", 'invalid')]),
field=models.CharField(max_length=128, validators=[django.core.validators.RegexValidator('^[\\w][- \\w]+\\Z', 'Unicode letters, numbers, underscores, or hyphens or spaces')]),
),
migrations.AlterField(
model_name='project',
name='name',
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-\\w]+\\Z'), "Enter a valid 'slug' consisting of Unicode letters, numbers, underscores, or hyphens.", 'invalid')]),
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator('^[\\w][- \\w]+\\Z', 'Unicode letters, numbers, underscores, or hyphens or spaces')]),
),
migrations.AlterField(
model_name='rule',
Expand All @@ -32,11 +32,11 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='service',
name='name',
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-\\w]+\\Z'), "Enter a valid 'slug' consisting of Unicode letters, numbers, underscores, or hyphens.", 'invalid')]),
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator('^[\\w][- \\w]+\\Z', 'Unicode letters, numbers, underscores, or hyphens or spaces')]),
),
migrations.AlterField(
model_name='shard',
name='name',
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-\\w]+\\Z'), "Enter a valid 'slug' consisting of Unicode letters, numbers, underscores, or hyphens.", 'invalid')]),
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.RegexValidator('^[\\w][- \\w]+\\Z', 'Unicode letters, numbers, underscores, or hyphens or spaces')]),
),
]
28 changes: 28 additions & 0 deletions promgen/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2020 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE


from unittest import mock

from django.core.exceptions import ValidationError

from promgen import models
from promgen.tests import PromgenTest


class ModelTest(PromgenTest):
@mock.patch("django.dispatch.dispatcher.Signal.send")
def setUp(self, mock_signal):
self.user = self.add_force_login(id=999, username="Foo")

def test_names(self):
# Unicode is ok
models.Service(name=r"日本語", owner=self.user).full_clean()
# Spaces are ok
models.Service(name=r"foo bar", owner=self.user).full_clean()
# dash or under score are ok
models.Service(name=r"foo-bar_baz", owner=self.user).full_clean()
with self.assertRaises(ValidationError):
# Fail a name with \
models.Service(name=r"foo/bar", owner=self.user).full_clean()
models.Service(name=r"foo\bar", owner=self.user).full_clean()
9 changes: 7 additions & 2 deletions promgen/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dateutil import parser

from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator, validate_unicode_slug
from django.core.validators import RegexValidator

# See definition of duration field
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file
Expand All @@ -22,7 +22,12 @@
labelname = RegexValidator(
r"[a-zA-Z_][a-zA-Z0-9_]*", "Only alphanumeric characters are allowed."
)
labelvalue = validate_unicode_slug

# While Prometheus accepts label values of any unicode character, our values sometimes
# make it into URLs, so we want to make sure we do not have stray / characters
labelvalue = RegexValidator(
r"^[\w][- \w]+\Z", "Unicode letters, numbers, underscores, or hyphens or spaces"
)


def datetime(value):
Expand Down