Skip to content

Commit d2783e1

Browse files
committedDec 3, 2018
Supporting VNC in JSON (fixes #9)
1 parent 31ad986 commit d2783e1

File tree

6 files changed

+77
-19
lines changed

6 files changed

+77
-19
lines changed
 

‎README.md

+25
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,29 @@ Cloud provider attributes `username` and `password` can be included in the input
9191
}
9292
}
9393
}
94+
```
95+
96+
To specify VNC proxying settings - use `vnc` attribute as follows:
97+
```
98+
"vnc-hosts": {
99+
"some-dc" : {
100+
"selenoid-host.example.com": {
101+
"port": 4444,
102+
"count": 1,
103+
"vnc": "selenoid"
104+
}
105+
}
106+
}
107+
```
108+
When `vnc` equals to `selenoid` then VNC will be set to `ws://host:port/vnc`, otherwise it will be set to specified value with `$hostName` placeholder is replaced by respective host name:
109+
```
110+
"vnc-hosts": {
111+
"some-dc" : {
112+
"some-host-[1:5].example.com": {
113+
"port": 4444,
114+
"count": 5,
115+
"vnc": "vnc://$hostName:5900"
116+
}
117+
}
118+
}
94119
```

‎cmd/data.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Host struct {
55
Count int `json:"count"`
66
Username string `json:"username"`
77
Password string `json:"password"`
8+
VNC string `json:"vnc"`
89
}
910

1011
type Region map[string]Host

‎cmd/generate.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,17 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
126126
for hostPattern, host := range region {
127127
hostNames := parseHostPattern(hostPattern)
128128
for _, hostName := range hostNames {
129-
xmlHosts = append(xmlHosts, ggr.Host{
129+
h := ggr.Host{
130130
Name: hostName,
131131
Port: host.Port,
132132
Count: host.Count,
133133
Username: host.Username,
134134
Password: host.Password,
135-
})
135+
}
136+
if host.VNC != "" {
137+
h.VNC = preProcessVNC(hostName, host.Port, host.VNC)
138+
}
139+
xmlHosts = append(xmlHosts, h)
136140
}
137141
}
138142
xmlRegions = append(xmlRegions, ggr.Region{
@@ -143,6 +147,15 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
143147
return xmlRegions
144148
}
145149

150+
func preProcessVNC(hostName string, port int, vnc string) string {
151+
const selenoid = "selenoid"
152+
const hostPattern = "$hostName"
153+
if vnc == selenoid {
154+
return fmt.Sprintf("ws://%s:%d/vnc", hostName, port)
155+
}
156+
return strings.Replace(vnc, hostPattern, hostName, -1)
157+
}
158+
146159
func parseInputFile(filePath string) (*Input, error) {
147160
bytes, err := ioutil.ReadFile(filePath)
148161
if err != nil {

‎cmd/generate_test.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestParseInputFile(t *testing.T) {
1010
input, err := parseInputFile("../test-data/input.json")
1111
AssertThat(t, err, Is{nil})
12-
AssertThat(t, len(input.Hosts), EqualTo{2})
12+
AssertThat(t, len(input.Hosts), EqualTo{3})
1313
AssertThat(t, len(input.Quota), EqualTo{1})
1414
}
1515

@@ -46,10 +46,10 @@ func TestConvert(t *testing.T) {
4646
AssertThat(t, secondVersion.Number, EqualTo{"42.0"})
4747
thirdVersion := versions[2]
4848
AssertThat(t, thirdVersion.Number, EqualTo{"43.0"})
49-
AssertThat(t, thirdVersion.Platform, EqualTo{"LINUX"})
49+
AssertThat(t, thirdVersion.Platform, EqualTo{"WINDOWS"})
5050
fourthVersion := versions[3]
51-
AssertThat(t, fourthVersion.Number, EqualTo{"43.0"})
52-
AssertThat(t, fourthVersion.Platform, EqualTo{"WINDOWS"})
51+
AssertThat(t, fourthVersion.Number, EqualTo{"45.0"})
52+
AssertThat(t, fourthVersion.Platform, EqualTo{"LINUX"})
5353

5454
firstRegions := firstVersion.Regions
5555
AssertThat(t, len(firstRegions), EqualTo{2})
@@ -73,13 +73,21 @@ func TestConvert(t *testing.T) {
7373
AssertThat(t, firstHost.Password == "" && secondHost.Password == "", Is{true})
7474
}
7575

76-
secondRegions := thirdVersion.Regions
77-
AssertThat(t, len(secondRegions), EqualTo{1})
78-
region := secondRegions[0]
76+
thirdRegions := thirdVersion.Regions
77+
AssertThat(t, len(thirdRegions), EqualTo{1})
78+
region := thirdRegions[0]
7979
AssertThat(t, region.Name == "provider-1", Is{true})
8080

8181
AssertThat(t, len(region.Hosts), EqualTo{5})
8282

83+
fourthRegions := fourthVersion.Regions
84+
AssertThat(t, len(fourthRegions), EqualTo{1})
85+
fourthRegion := fourthRegions[0]
86+
AssertThat(t, fourthRegion.Name == "some-dc", Is{true})
87+
AssertThat(t, len(fourthRegion.Hosts), EqualTo{1})
88+
vncHost := fourthRegion.Hosts[0]
89+
AssertThat(t, vncHost.VNC, EqualTo{"ws://selenoid-host.example.com:4444/vnc"})
90+
8391
for _, host := range region.Hosts {
8492
AssertThat(t, host.Username, EqualTo{"user1"})
8593
AssertThat(t, host.Password, EqualTo{"Password1"})
@@ -99,3 +107,8 @@ func TestParseVersionPlatform(t *testing.T) {
99107
AssertThat(t, v, EqualTo{"version"})
100108
AssertThat(t, p, EqualTo{"platform@platform"})
101109
}
110+
111+
func TestPreProcessVNC(t *testing.T) {
112+
AssertThat(t, preProcessVNC("selenoid-host.example.com", 4444, "selenoid"), EqualTo{"ws://selenoid-host.example.com:4444/vnc"})
113+
AssertThat(t, preProcessVNC("vnc-host.example.com", 5900, "vnc://$hostName:5900"), EqualTo{"vnc://vnc-host.example.com:5900"})
114+
}

‎test-data/input.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
"password": "Password1"
2424
}
2525
}
26+
},
27+
"vnc-hosts": {
28+
"some-dc": {
29+
"selenoid-host.example.com": {
30+
"port": 4444,
31+
"count": 1,
32+
"vnc": "selenoid"
33+
}
34+
}
2635
}
2736
},
2837

@@ -34,8 +43,8 @@
3443
"versions": {
3544
"33.0": "cloud",
3645
"42.0": "cloud",
37-
"43.0@LINUX": "cloud-provider",
38-
"43.0@WINDOWS": "cloud-provider"
46+
"43.0@WINDOWS": "cloud-provider",
47+
"45.0@LINUX": "vnc-hosts"
3948
}
4049
}
4150
}

‎test-data/output.xml

+5-8
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<host name="selenium-cloud-b-20.example.com" port="4444" count="2"></host>
9393
</region>
9494
</version>
95-
<version number="43.0" platform="LINUX">
95+
<version number="43.0" platform="WINDOWS">
9696
<region name="provider-1">
9797
<host name="cloud-provider-1.com" port="4444" count="1" username="user1" password="Password1"></host>
9898
<host name="cloud-provider-2.com" port="4444" count="1" username="user1" password="Password1"></host>
@@ -101,13 +101,10 @@
101101
<host name="cloud-provider-5.com" port="4444" count="1" username="user1" password="Password1"></host>
102102
</region>
103103
</version>
104-
<version number="43.0" platform="WINDOWS">
105-
<region name="provider-1">
106-
<host name="cloud-provider-1.com" port="4444" count="1" username="user1" password="Password1"></host>
107-
<host name="cloud-provider-2.com" port="4444" count="1" username="user1" password="Password1"></host>
108-
<host name="cloud-provider-3.com" port="4444" count="1" username="user1" password="Password1"></host>
109-
<host name="cloud-provider-4.com" port="4444" count="1" username="user1" password="Password1"></host>
110-
<host name="cloud-provider-5.com" port="4444" count="1" username="user1" password="Password1"></host>
104+
<version number="45.0" platform="LINUX">
105+
<region name="some-dc">
106+
<host name="selenoid-host.example.com" port="4444" count="1"
107+
vnc="ws://selenoid-host.example.com:4444/vnc"></host>
111108
</region>
112109
</version>
113110
</browser>

0 commit comments

Comments
 (0)
Please sign in to comment.