Skip to content

Commit

Permalink
Merge pull request #407 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added ability to override environment variables in capability (fixes …
  • Loading branch information
aandryashin committed Mar 29, 2018
2 parents bb02af1 + d7de8dc commit d470d40
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
23 changes: 17 additions & 6 deletions docs/special-capabilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,36 @@ timeZone: "Europe/Moscow"
You can find most of available time zones https://en.wikipedia.org/wiki/List_of_tz_database_time_zones[here].
Without this capability launched browser containers will have the same timezone as Selenoid one.

=== Per-session Environment Variables: env

Sometimes you may want to set some environment variables for every test case (for example to test with different default locales). To achieve this pass one more capability:

.Type: array, format: <key>=<value>
----
env: ["LANG=ru_RU.UTF-8", "LANGUAGE=ru:en", "LC_ALL=ru_RU.UTF-8"]
----

Environment variables from this capability are appended to variables from configuration file.

=== Links to Application Containers: applicationContainers

Sometimes you may need to link browser container to application container running on the same host machine.
This allows you to use cool URLs like `http://my-cool-app/` in tests.
To achieve this simply pass information about one or more desired links via capability:

.Type: string, format: <container-name>[:alias] (comma-separated)
.Type: array, format: <container-name>[:alias]
----
applicationContainers: "spring-application-main:my-cool-app,spring-application-gateway"
applicationContainers: ["spring-application-main:my-cool-app", "spring-application-gateway"]
----

=== Hosts Entries: hostsEntries

Although you can configure a separate list of `/etc/hosts` entries for every browser image in <<Browsers Configuration File>>
sometimes you may need to add more entries for particular test cases. This can be easily achieved with:

.Type: string, format: <hostname>:<ip-address> (comma-separated)
.Type: array, format: <hostname>:<ip-address>
----
hostsEntries: "example.com:192.168.0.1,test.com:192.168.0.2"
hostsEntries: ["example.com:192.168.0.1", "test.com:192.168.0.2"]
----

Entries will be inserted to `/etc/hosts` before entries from browsers configuration file.
Expand All @@ -128,9 +139,9 @@ Thus entries from capabilities override entries from configuration file if some

In big clusters you may want to pass additional metadata to every browser session: environment, VCS revision, build number and so on. These labels can be then used to enrich session logs and send them to a centralized log storage. Later this metadata can be used for more efficient search through logs.

.Type: string, format: <key>:<value> (comma-separated)
.Type: array, format: <key>=<value>
----
labels: "environment:testing,build-number:14353"
labels: ["environment=testing", "build-number=14353"]
----

Labels from this capability override labels from browsers configuration file. When `name` capability is specified - it is automatically added as a label to container.
Expand Down
22 changes: 11 additions & 11 deletions service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

const (
comma = ","
colon = ":"
equality = "="
sysAdmin = "SYS_ADMIN"
overrideVideoOutputDir = "OVERRIDE_VIDEO_OUTPUT_DIR"
vncPort = "5900"
Expand Down Expand Up @@ -78,9 +78,8 @@ func (d *Docker) StartWithCancel() (*StartedService, error) {
if !d.Privileged {
hostConfig.CapAdd = strslice.StrSlice{sysAdmin}
}
if d.ApplicationContainers != "" {
links := strings.Split(d.ApplicationContainers, comma)
hostConfig.Links = links
if len(d.ApplicationContainers) > 0 {
hostConfig.Links = d.ApplicationContainers
}
if len(d.Service.Sysctl) > 0 {
hostConfig.Sysctls = d.Service.Sysctl
Expand Down Expand Up @@ -201,8 +200,8 @@ func getLogConfig(logConfig ctr.LogConfig, caps session.Caps) ctr.LogConfig {
logConfig.Config[tag] = caps.TestName
}
_, ok = logConfig.Config[labels]
if caps.Labels != "" && !ok {
logConfig.Config[labels] = caps.Labels
if len(caps.Labels) > 0 && !ok {
logConfig.Config[labels] = strings.Join(caps.Labels, comma)
}
}
return logConfig
Expand All @@ -228,6 +227,7 @@ func getEnv(service ServiceBase, caps session.Caps) []string {
fmt.Sprintf("ENABLE_VNC=%v", caps.VNC),
}
env = append(env, service.Service.Env...)
env = append(env, caps.Env...)
return env
}

Expand All @@ -247,8 +247,8 @@ func getContainerHostname(caps session.Caps) string {

func getExtraHosts(service *config.Browser, caps session.Caps) []string {
extraHosts := service.Hosts
if caps.HostsEntries != "" {
extraHosts = append(strings.Split(caps.HostsEntries, comma), extraHosts...)
if len(caps.HostsEntries) > 0 {
extraHosts = append(caps.HostsEntries, extraHosts...)
}
return extraHosts
}
Expand All @@ -261,9 +261,9 @@ func getLabels(service *config.Browser, caps session.Caps) map[string]string {
for k, v := range service.Labels {
labels[k] = v
}
if caps.Labels != "" {
for _, lbl := range strings.Split(caps.Labels, comma) {
kv := strings.SplitN(lbl, colon, 2)
if len(caps.Labels) > 0 {
for _, lbl := range caps.Labels {
kv := strings.SplitN(lbl, equality, 2)
if len(kv) == 2 {
key := kv[0]
value := kv[1]
Expand Down
3 changes: 3 additions & 0 deletions service/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"errors"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/util"
"os"
)
Expand All @@ -17,6 +18,7 @@ import (
type Driver struct {
ServiceBase
Environment
session.Caps
}

// StartWithCancel - Starter interface implementation
Expand Down Expand Up @@ -47,6 +49,7 @@ func (d *Driver) StartWithCancel() (*StartedService, error) {
cmdLine = append(cmdLine, fmt.Sprintf("--port=%s", port))
cmd := exec.Command(cmdLine[0], cmdLine[1:]...)
cmd.Env = append(cmd.Env, d.Service.Env...)
cmd.Env = append(cmd.Env, d.Caps.Env...)
if d.CaptureDriverLogs {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
2 changes: 1 addition & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (m *DefaultManager) Find(caps session.Caps, requestId uint64) (Starter, boo
LogConfig: m.Config.ContainerLogs}, true
case []interface{}:
log.Printf("[%d] [USING_DRIVER] [%s] [%s]", requestId, browserName, version)
return &Driver{ServiceBase: serviceBase, Environment: *m.Environment}, true
return &Driver{ServiceBase: serviceBase, Environment: *m.Environment, Caps: caps}, true
}
return nil, false
}
Expand Down
7 changes: 4 additions & 3 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ func createDockerStarter(t *testing.T, env *service.Environment, cfg *config.Con
Video: true,
VideoScreenSize: "1024x768",
VideoFrameRate: 25,
HostsEntries: "example.com:192.168.0.1,test.com:192.168.0.2",
Labels: "label1:some-value,label2",
ApplicationContainers: "one,two",
Env: []string{"LANG=ru_RU.UTF-8", "LANGUAGE=ru:en"},
HostsEntries: []string{"example.com:192.168.0.1", "test.com:192.168.0.2"},
Labels: []string{"label1=some-value", "label2"},
ApplicationContainers: []string{"one", "two"},
TimeZone: "Europe/Moscow",
ContainerHostname: "some-hostname",
TestName: "my-cool-test",
Expand Down
7 changes: 4 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type Caps struct {
TestName string `json:"name"`
TimeZone string `json:"timeZone"`
ContainerHostname string `json:"containerHostname"`
ApplicationContainers string `json:"applicationContainers"`
HostsEntries string `json:"hostsEntries"`
Labels string `json:"labels"`
Env []string `json:"env"`
ApplicationContainers []string `json:"applicationContainers"`
HostsEntries []string `json:"hostsEntries"`
Labels []string `json:"labels"`
ExtensionCapabilities map[string]interface{} `json:"selenoid:options"`
}

Expand Down

0 comments on commit d470d40

Please sign in to comment.