Skip to content

Commit

Permalink
Merge pull request #12 from vania-pooh/master
Browse files Browse the repository at this point in the history
Supporting multiple ports per host in JSON (fixes #11)
  • Loading branch information
vania-pooh authored Dec 17, 2018
2 parents ac85afb + dcce804 commit 808a5c8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ See [test-data/input.json](test-data/input.json) for full example. In the **host
},
"region-b": {
"selenium-cloud-b-[1:40].example.com": {
"port": 4445,
"ports": "444[5:8]",
"count": 2
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

type Host struct {
Port int `json:"port"`
Ports string `json:"ports"`
Count int `json:"count"`
Username string `json:"username"`
Password string `json:"password"`
Expand Down
36 changes: 26 additions & 10 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,20 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
for hostPattern, host := range region {
hostNames := parseHostPattern(hostPattern)
for _, hostName := range hostNames {
h := ggr.Host{
Name: hostName,
Port: host.Port,
Count: host.Count,
Username: host.Username,
Password: host.Password,
ports := getPorts(host.Port, host.Ports)
for _, port := range ports {
h := ggr.Host{
Name: hostName,
Port: port,
Count: host.Count,
Username: host.Username,
Password: host.Password,
}
if host.VNC != "" {
h.VNC = preProcessVNC(hostName, port, host.VNC)
}
xmlHosts = append(xmlHosts, h)
}
if host.VNC != "" {
h.VNC = preProcessVNC(hostName, host.Port, host.VNC)
}
xmlHosts = append(xmlHosts, h)
}
}
xmlRegions = append(xmlRegions, ggr.Region{
Expand All @@ -147,6 +150,19 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
return xmlRegions
}

func getPorts(port int, ports string) []int {
if ports != "" {
var ret []int
for _, maybePort := range parseHostPattern(ports) {
if port, err := strconv.Atoi(maybePort); err == nil {
ret = append(ret, port)
}
}
return ret
}
return []int{port}
}

func preProcessVNC(hostName string, port int, vnc string) string {
const selenoid = "selenoid"
const hostPattern = "$hostName"
Expand Down
8 changes: 8 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,11 @@ func TestPreProcessVNC(t *testing.T) {
AssertThat(t, preProcessVNC("selenoid-host.example.com", 4444, "selenoid"), EqualTo{"ws://selenoid-host.example.com:4444/vnc"})
AssertThat(t, preProcessVNC("vnc-host.example.com", 5900, "vnc://$hostName:5900"), EqualTo{"vnc://vnc-host.example.com:5900"})
}

func TestGetPorts(t *testing.T) {
AssertThat(t, getPorts(4444, ""), EqualTo{[]int{4444}})
AssertThat(t, getPorts(4444, "4445"), EqualTo{[]int{4445}})
AssertThat(t, getPorts(4444, "444[5:8]"), EqualTo{[]int{4445, 4446, 4447, 4448}})
AssertThat(t, getPorts(4444, "44[5:8]4"), EqualTo{[]int{4454, 4464, 4474, 4484}})
AssertThat(t, len(getPorts(4444, "NaN")), EqualTo{0})
}

0 comments on commit 808a5c8

Please sign in to comment.