Fix case sensitivity issue; also verify we don't get collisions

This commit is contained in:
Chris Bednarski 2015-09-25 18:59:17 -07:00
parent e6253b21f6
commit f1f285fb10
4 changed files with 32 additions and 3 deletions

View file

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/hashicorp/hcl"
@ -378,10 +379,22 @@ func parseResources(result *structs.Resources, obj *hclobj.Object) error {
return err
}
// Keep track of labels we've already seen so we can ensure there
// are no collisions when we turn them into environment variables.
// lowercase:NomalCase so we can get the first for the error message
seenLabel := map[string]string{}
for _, label := range r.DynamicPorts {
if !reDynamicPorts.MatchString(label) {
return errDynamicPorts
}
first, seen := seenLabel[strings.ToLower(label)]
if seen {
return fmt.Errorf("Found a port label collision: `%s` overlaps with previous `%s`", label, first)
} else {
seenLabel[strings.ToLower(label)] = label
}
}
result.Networks = []*structs.NetworkResource{&r}

View file

@ -191,5 +191,21 @@ func TestBadPorts(t *testing.T) {
if !strings.Contains(err.Error(), errDynamicPorts.Error()) {
t.Fatalf("\nExpected error\n %s\ngot\n %v", errDynamicPorts, err)
}
}
func TestOverlappingPorts(t *testing.T) {
path, err := filepath.Abs(filepath.Join("./test-fixtures", "overlapping-ports.hcl"))
if err != nil {
t.Fatalf("Can't get absoluate path for file: %s", err)
}
_, err = ParseFile(path)
if err == nil {
t.Fatalf("Expected an error")
}
if !strings.Contains(err.Error(), "Found a port label collision") {
t.Fatalf("Expected collision error; got %v", err)
}
}

View file

@ -43,7 +43,7 @@ job "binstore-storagelocker" {
network {
mbits = "100"
reserved_ports = [1,2,3]
dynamic_ports = ["Http", "pie-waka", "admin"]
dynamic_ports = ["this_is_aport", "this#is$not-a!port"]
}
}
}

View file

@ -43,7 +43,7 @@ job "binstore-storagelocker" {
network {
mbits = "100"
reserved_ports = [1,2,3]
dynamic_ports = ["HTTP", "HTTPS", "ADMIN"]
dynamic_ports = ["http", "https", "admin"]
}
}
}