Merge pull request #3253 from titanous/update-container-name-validation
Update container name validation
This commit is contained in:
commit
8fa4c4b062
2 changed files with 53 additions and 4 deletions
|
@ -748,6 +748,54 @@ func TestRandomContainerName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContainerNameValidation(t *testing.T) {
|
||||
eng := NewTestEngine(t)
|
||||
runtime := mkRuntimeFromEngine(eng, t)
|
||||
defer nuke(runtime)
|
||||
|
||||
for _, test := range []struct {
|
||||
Name string
|
||||
Valid bool
|
||||
}{
|
||||
{"abc-123_AAA.1", true},
|
||||
{"\000asdf", false},
|
||||
} {
|
||||
config, _, _, err := docker.ParseRun([]string{unitTestImageID, "echo test"}, nil)
|
||||
if err != nil {
|
||||
if !test.Valid {
|
||||
continue
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var shortID string
|
||||
job := eng.Job("create", test.Name)
|
||||
if err := job.ImportEnv(config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
job.Stdout.AddString(&shortID)
|
||||
if err := job.Run(); err != nil {
|
||||
if !test.Valid {
|
||||
continue
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
container := runtime.Get(shortID)
|
||||
|
||||
if container.Name != "/"+test.Name {
|
||||
t.Fatalf("Expect /%s got %s", test.Name, container.Name)
|
||||
}
|
||||
|
||||
if c := runtime.Get("/" + test.Name); c == nil {
|
||||
t.Fatalf("Couldn't retrieve test container as /%s", test.Name)
|
||||
} else if c.ID != container.ID {
|
||||
t.Fatalf("Container /%s has ID %s instead of %s", test.Name, c.ID, container.ID)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestLinkChildContainer(t *testing.T) {
|
||||
eng := NewTestEngine(t)
|
||||
runtime := mkRuntimeFromEngine(eng, t)
|
||||
|
|
|
@ -31,8 +31,9 @@ import (
|
|||
const MaxImageDepth = 127
|
||||
|
||||
var (
|
||||
defaultDns = []string{"8.8.8.8", "8.8.4.4"}
|
||||
validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_-]+$`)
|
||||
defaultDns = []string{"8.8.8.8", "8.8.4.4"}
|
||||
validContainerNameChars = `[a-zA-Z0-9_.-]`
|
||||
validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`)
|
||||
)
|
||||
|
||||
type Capabilities struct {
|
||||
|
@ -425,8 +426,8 @@ func (runtime *Runtime) Create(config *Config, name string) (*Container, []strin
|
|||
name = utils.TruncateID(id)
|
||||
}
|
||||
} else {
|
||||
if !validContainerName.MatchString(name) {
|
||||
return nil, nil, fmt.Errorf("Invalid container name (%s), only [a-zA-Z0-9_-] are allowed", name)
|
||||
if !validContainerNamePattern.MatchString(name) {
|
||||
return nil, nil, fmt.Errorf("Invalid container name (%s), only %s are allowed", name, validContainerNameChars)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue