Ensuring service names are unique

This commit is contained in:
Diptanu Choudhury 2016-05-02 13:40:49 -07:00
parent 048ab966b0
commit 506ea1a206
2 changed files with 17 additions and 3 deletions

View File

@ -1772,13 +1772,19 @@ func (t *Task) Validate() error {
func validateServices(t *Task) error {
var mErr multierror.Error
// Ensure that services don't ask for non-existent ports.
// Ensure that services don't ask for non-existent ports and their names are
// unique.
servicePorts := make(map[string][]string)
knownServices := make(map[string]struct{})
for i, service := range t.Services {
if err := service.Validate(); err != nil {
outer := fmt.Errorf("service %d validation failed: %s", i, err)
mErr.Errors = append(mErr.Errors, outer)
}
if _, ok := knownServices[service.Name]; ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("service %q is duplicate", service.Name))
}
knownServices[service.Name] = struct{}{}
if service.PortLabel != "" {
servicePorts[service.PortLabel] = append(servicePorts[service.PortLabel], service.Name)

View File

@ -276,7 +276,7 @@ func TestTask_Validate(t *testing.T) {
}
func TestTask_Validate_Services(t *testing.T) {
s := &Service{
s1 := &Service{
Name: "service-name",
PortLabel: "bar",
Checks: []*ServiceCheck{
@ -287,6 +287,10 @@ func TestTask_Validate_Services(t *testing.T) {
},
}
s2 := &Service{
Name: "service-name",
}
task := &Task{
Name: "web",
Driver: "docker",
@ -296,7 +300,7 @@ func TestTask_Validate_Services(t *testing.T) {
MemoryMB: 100,
IOPS: 10,
},
Services: []*Service{s},
Services: []*Service{s1, s2},
}
err := task.Validate()
if err == nil {
@ -305,6 +309,10 @@ func TestTask_Validate_Services(t *testing.T) {
if !strings.Contains(err.Error(), "referenced by services service-name does not exist") {
t.Fatalf("err: %s", err)
}
if !strings.Contains(err.Error(), "service \"service-name\" is duplicate") {
t.Fatalf("err: %v", err)
}
}
func TestTask_Validate_LogConfig(t *testing.T) {