mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Revert "Allow spaces in network names"
This reverts commit 85b22fabbe
.
Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
parent
58744967be
commit
4d1334c9ee
2 changed files with 14 additions and 34 deletions
|
@ -17,11 +17,11 @@ import (
|
||||||
"github.com/docker/libnetwork/osl"
|
"github.com/docker/libnetwork/osl"
|
||||||
)
|
)
|
||||||
|
|
||||||
// restrictedNameRegex represents the regular expression which regulates the allowed network or endpoint names.
|
// RestrictedNameChars collects the characters allowed to represent a network or endpoint name.
|
||||||
const restrictedNameRegex = `^[\w]+[\w-. ]*[\w]+$`
|
const restrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
|
||||||
|
|
||||||
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
|
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
|
||||||
var restrictedNamePattern = regexp.MustCompile(restrictedNameRegex)
|
var restrictedNamePattern = regexp.MustCompile(`^/?` + restrictedNameChars + `+$`)
|
||||||
|
|
||||||
// Config encapsulates configurations of various Libnetwork components
|
// Config encapsulates configurations of various Libnetwork components
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -243,7 +243,7 @@ func (c *Config) ProcessOptions(options ...Option) {
|
||||||
// ValidateName validates configuration objects supported by libnetwork
|
// ValidateName validates configuration objects supported by libnetwork
|
||||||
func ValidateName(name string) error {
|
func ValidateName(name string) error {
|
||||||
if !restrictedNamePattern.MatchString(name) {
|
if !restrictedNamePattern.MatchString(name) {
|
||||||
return fmt.Errorf("%q includes invalid characters, resource name has to conform to %q", name, restrictedNameRegex)
|
return fmt.Errorf("%s includes invalid characters, only %q are allowed", name, restrictedNameChars)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,37 +46,17 @@ func TestOptionsLabels(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidName(t *testing.T) {
|
func TestValidName(t *testing.T) {
|
||||||
assertName(t, "test", true)
|
if err := ValidateName("test"); err != nil {
|
||||||
assertName(t, "test1", true)
|
t.Fatal("Name validation fails for a name that must be accepted")
|
||||||
assertName(t, "test1.2_3", true)
|
|
||||||
assertName(t, "_test", true)
|
|
||||||
assertName(t, "test_", true)
|
|
||||||
assertName(t, "looks-good", true)
|
|
||||||
assertName(t, " test", false)
|
|
||||||
assertName(t, "test ", false)
|
|
||||||
assertName(t, "test.", false)
|
|
||||||
assertName(t, ".test", false)
|
|
||||||
assertName(t, "", false)
|
|
||||||
assertName(t, " ", false)
|
|
||||||
assertName(t, "<>$$^", false)
|
|
||||||
assertName(t, "this is a good network name", true)
|
|
||||||
assertName(t, "this is also-good", true)
|
|
||||||
assertName(t, " this is a not good network name", false)
|
|
||||||
assertName(t, "this is a not either ", false)
|
|
||||||
assertName(t, "this one\nis not good", false)
|
|
||||||
assertName(t, "this one\tis not good", false)
|
|
||||||
assertName(t, "this one\ris not good", false)
|
|
||||||
assertName(t, "this one\vis not good", false)
|
|
||||||
assertName(t, "this one\fis not good", false)
|
|
||||||
}
|
|
||||||
func assertName(t *testing.T, name string, mustSucceed bool) {
|
|
||||||
msg := "Name validation succeeds for a case when it is expected to fail"
|
|
||||||
if mustSucceed {
|
|
||||||
msg = "Name validation fails for a name that must be accepted"
|
|
||||||
}
|
}
|
||||||
err := ValidateName(name)
|
if err := ValidateName(""); err == nil {
|
||||||
if (err == nil) != mustSucceed {
|
t.Fatal("Name validation succeeds for a case when it is expected to fail")
|
||||||
t.Fatalf("%s: %s", msg, name)
|
}
|
||||||
|
if err := ValidateName(" "); err == nil {
|
||||||
|
t.Fatal("Name validation succeeds for a case when it is expected to fail")
|
||||||
|
}
|
||||||
|
if err := ValidateName("<>$$^"); err == nil {
|
||||||
|
t.Fatal("Name validation succeeds for a case when it is expected to fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue