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

register service names with duplicate name #2208

Merged
merged 1 commit into from
Jan 19, 2017
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
7 changes: 5 additions & 2 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2499,10 +2499,13 @@ func validateServices(t *Task) error {
outer := fmt.Errorf("service[%d] %+q validation failed: %s", i, service.Name, err)
mErr.Errors = append(mErr.Errors, outer)
}
if _, ok := knownServices[service.Name]; ok {

// Ensure that services with the same name are not being registered for
// the same port
if _, ok := knownServices[service.Name+service.PortLabel]; ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("service %q is duplicate", service.Name))
}
knownServices[service.Name] = struct{}{}
knownServices[service.Name+service.PortLabel] = struct{}{}

if service.PortLabel != "" {
servicePorts[service.PortLabel] = append(servicePorts[service.PortLabel], service.Name)
Expand Down
44 changes: 39 additions & 5 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,21 @@ func TestTask_Validate_Services(t *testing.T) {
}

s2 := &Service{
Name: "service-name",
Name: "service-name",
PortLabel: "bar",
}

s3 := &Service{
Name: "service-A",
PortLabel: "a",
}
s4 := &Service{
Name: "service-A",
PortLabel: "b",
}

ephemeralDisk := DefaultEphemeralDisk()
ephemeralDisk.SizeMB = 200
task := &Task{
Name: "web",
Driver: "docker",
Expand All @@ -523,15 +534,34 @@ func TestTask_Validate_Services(t *testing.T) {
},
Services: []*Service{s1, s2},
}
ephemeralDisk.SizeMB = 200

task1 := &Task{
Name: "web",
Driver: "docker",
Resources: DefaultResources(),
Services: []*Service{s3, s4},
LogConfig: DefaultLogConfig(),
}
task1.Resources.Networks = []*NetworkResource{
&NetworkResource{
MBits: 10,
DynamicPorts: []Port{
Port{
Label: "a",
Value: 1000,
},
Port{
Label: "b",
Value: 2000,
},
},
},
}

err := task.Validate(ephemeralDisk)
if err == nil {
t.Fatal("expected an error")
}
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)
Expand All @@ -548,6 +578,10 @@ func TestTask_Validate_Services(t *testing.T) {
if !strings.Contains(err.Error(), "cannot be less than") {
t.Fatalf("err: %v", err)
}

if err = task1.Validate(ephemeralDisk); err != nil {
t.Fatalf("err : %v", err)
}
}

func TestTask_Validate_Service_Check(t *testing.T) {
Expand Down