Skip to content

Commit

Permalink
[BUGFIX] Allow space in service names #240
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm authored Feb 21, 2020
2 parents bd602ad + 7a62a5a commit 8033478
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
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

0 comments on commit 8033478

Please sign in to comment.