Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added --env and --browser-env flags (fixes #45)
  • Loading branch information
aandryashin authored Jul 4, 2017
2 parents 5e03d1f + 6576e39 commit 6101d50
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 37 deletions.
26 changes: 19 additions & 7 deletions cmd/selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
vnc bool
force bool
args string
env string
browserEnv string
)

func init() {
Expand Down Expand Up @@ -102,10 +104,17 @@ func initFlags() {
selenoidUpdateCmd,
} {
c.Flags().StringVarP(&version, "version", "v", selenoid.Latest, "desired version; default is latest release")
c.Flags().StringVarP(&registry, "registry", "r", registryUrl, "Docker registry to use")
}
for _, c := range []*cobra.Command{
selenoidConfigureCmd,
selenoidStartCmd,
selenoidUpdateCmd,
} {
c.Flags().StringVarP(&browsers, "browsers", "b", "", "comma separated list of browser names to process")
c.Flags().StringVarP(&browserEnv, "browser-env", "w", "", "override container or driver environment variables (e.g. \"KEY1=value1 KEY2=value2\")")
c.Flags().StringVarP(&browsersJSONUrl, "browsers-json", "j", defaultBrowsersJsonURL, "browsers JSON data URL (in most cases never need to be set manually)")
c.Flags().BoolVarP(&skipDownload, "no-download", "n", false, "only output config file without downloading images or drivers")
c.Flags().StringVarP(&registry, "registry", "r", registryUrl, "Docker registry to use")
c.Flags().IntVarP(&lastVersions, "last-versions", "l", 2, "process only last N versions (Docker only)")
c.Flags().IntVarP(&tmpfs, "tmpfs", "t", 0, "add tmpfs volume sized in megabytes (Docker only)")
c.Flags().BoolVarP(&vnc, "vnc", "s", false, "download containers with VNC support (Docker only)")
Expand All @@ -126,17 +135,20 @@ func initFlags() {
selenoidUpdateUICmd,
} {
c.Flags().StringVarP(&args, "args", "g", "", "additional service arguments (e.g. \"-limit 5\")")
c.Flags().StringVarP(&env, "env", "e", "", "override service environment variables (e.g. \"KEY1=value1 KEY2=value2\")")
}
}

func createLifecycle() (*selenoid.Lifecycle, error) {
config := selenoid.LifecycleConfig{
Quiet: quiet,
Force: force,
ConfigDir: configDir,
Browsers: browsers,
Download: !skipDownload,
Args: args,
Quiet: quiet,
Force: force,
ConfigDir: configDir,
Browsers: browsers,
BrowserEnv: browserEnv,
Download: !skipDownload,
Args: args,
Env: env,

LastVersions: lastVersions,
RegistryUrl: registry,
Expand Down
8 changes: 8 additions & 0 deletions selenoid/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ type RequestedBrowsersAware struct {
type ArgsAware struct {
Args string
}

type EnvAware struct {
Env string
}

type BrowserEnvAware struct {
BrowserEnv string
}
19 changes: 16 additions & 3 deletions selenoid/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type DockerConfigurator struct {
DownloadAware
RequestedBrowsersAware
ArgsAware
EnvAware
BrowserEnvAware
LastVersions int
Pull bool
RegistryUrl string
Expand All @@ -66,6 +68,8 @@ func NewDockerConfigurator(config *LifecycleConfig) (*DockerConfigurator, error)
DownloadAware: DownloadAware{DownloadNeeded: config.Download},
RequestedBrowsersAware: RequestedBrowsersAware{Browsers: config.Browsers},
ArgsAware: ArgsAware{Args: config.Args},
EnvAware: EnvAware{Env: config.Env},
BrowserEnvAware: BrowserEnvAware{BrowserEnv: config.BrowserEnv},
RegistryUrl: config.RegistryUrl,
LastVersions: config.LastVersions,
Tmpfs: config.Tmpfs,
Expand Down Expand Up @@ -318,6 +322,10 @@ func (c *DockerConfigurator) createVersions(browserName string, image string, ta
tmpfs["/tmp"] = fmt.Sprintf("size=%dm", c.Tmpfs)
browser.Tmpfs = tmpfs
}
browserEnv := strings.Fields(c.BrowserEnv)
if len(browserEnv) > 0 {
browser.Env = browserEnv
}
versions.Versions[version] = browser
}
return versions
Expand Down Expand Up @@ -453,7 +461,8 @@ func (c *DockerConfigurator) Start() error {
volumes = append(volumes, fmt.Sprintf("%s:%s", dockerSocket, dockerSocket))
}

return c.startContainer(selenoidContainerName, image, selenoidContainerPort, volumes, []string{}, strings.Fields(c.Args))
overrideEnv := strings.Fields(c.Env)
return c.startContainer(selenoidContainerName, image, selenoidContainerPort, volumes, []string{}, strings.Fields(c.Args), overrideEnv)
}

func (c *DockerConfigurator) StartUI() error {
Expand All @@ -470,12 +479,16 @@ func (c *DockerConfigurator) StartUI() error {
cmd = overrideCmd
}

return c.startContainer(selenoidUIContainerName, image, selenoidUIContainerPort, []string{}, links, cmd)
overrideEnv := strings.Fields(c.Env)
return c.startContainer(selenoidUIContainerName, image, selenoidUIContainerPort, []string{}, links, cmd, overrideEnv)
}

func (c *DockerConfigurator) startContainer(name string, image *types.ImageSummary, forwardedPort int, volumes []string, links []string, cmd []string) error {
func (c *DockerConfigurator) startContainer(name string, image *types.ImageSummary, forwardedPort int, volumes []string, links []string, cmd []string, envOverride []string) error {
env := os.Environ()
env = append(env, fmt.Sprintf("TZ=%s", time.Local))
if len(envOverride) > 0 {
env = envOverride
}
portString := strconv.Itoa(forwardedPort)
port, err := nat.NewPort("tcp", portString)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions selenoid/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func testConfigure(t *testing.T, download bool) {
Browsers: "firefox,opera",
Args: "-limit 42",
VNC: true,
Env: testEnv,
BrowserEnv: testEnv,
}
c, err := NewDockerConfigurator(&lcConfig)
AssertThat(t, err, Is{nil})
Expand All @@ -268,28 +270,31 @@ func testConfigure(t *testing.T, download bool) {
Port: "4444",
Path: "/wd/hub",
Tmpfs: tmpfsMap,
Env: []string{testEnv},
}
correctFFBrowsers["45.0"] = &config.Browser{
Image: "selenoid/vnc:firefox_45.0",
Port: "4444",
Path: "/wd/hub",
Tmpfs: tmpfsMap,
Env: []string{testEnv},
}
AssertThat(t, firefoxVersions, EqualTo{config.Versions{
Default: "46.0",
Versions: correctFFBrowsers,
}})

operaVersions, hasPhantomjsKey := cfg["opera"]
AssertThat(t, hasPhantomjsKey, Is{true})
operaVersions, hasOperaKey := cfg["opera"]
AssertThat(t, hasOperaKey, Is{true})
AssertThat(t, operaVersions, Is{Not{nil}})
AssertThat(t, operaVersions.Default, EqualTo{"44.0"})

correctPhantomjsBrowsers := make(map[string]*config.Browser)
correctPhantomjsBrowsers["2.1.1"] = &config.Browser{
correctOperaBrowsers := make(map[string]*config.Browser)
correctOperaBrowsers["2.1.1"] = &config.Browser{
Image: "selenoid/opera:44.0",
Port: "4444",
Tmpfs: tmpfsMap,
Env: []string{testEnv},
}
})
}
Expand Down
30 changes: 21 additions & 9 deletions selenoid/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type DriversConfigurator struct {
VersionAware
DownloadAware
ArgsAware
EnvAware
BrowserEnvAware
RequestedBrowsersAware
Browsers string
BrowsersJsonUrl string
Expand All @@ -78,6 +80,8 @@ func NewDriversConfigurator(config *LifecycleConfig) *DriversConfigurator {
ConfigDirAware: ConfigDirAware{ConfigDir: config.ConfigDir},
VersionAware: VersionAware{Version: config.Version},
ArgsAware: ArgsAware{Args: config.Args},
EnvAware: EnvAware{Env: config.Env},
BrowserEnvAware: BrowserEnvAware{BrowserEnv: config.BrowserEnv},
DownloadAware: DownloadAware{DownloadNeeded: config.Download},
RequestedBrowsersAware: RequestedBrowsersAware{Browsers: config.Browsers},
BrowsersJsonUrl: config.BrowsersJsonUrl,
Expand Down Expand Up @@ -272,25 +276,30 @@ func (d *DriversConfigurator) Configure() (*SelenoidConfig, error) {
return nil, fmt.Errorf("failed to create output directory: %v\n", err)
}
downloadedDrivers := d.downloadDrivers(browsers, d.ConfigDir)
cfg := generateConfig(downloadedDrivers)
cfg := d.generateConfig(downloadedDrivers)
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return &cfg, fmt.Errorf("failed to marshal json: %v\n", err)
}
return &cfg, ioutil.WriteFile(getSelenoidConfigPath(d.ConfigDir), data, 0644)
}

func generateConfig(downloadedDrivers []downloadedDriver) SelenoidConfig {
func (d *DriversConfigurator) generateConfig(downloadedDrivers []downloadedDriver) SelenoidConfig {
browsers := make(SelenoidConfig)
for _, dd := range downloadedDrivers {
cmd := strings.Fields(dd.Command)
browser := &config.Browser{
Image: cmd,
Path: "/",
}
browserEnv := strings.Fields(d.BrowserEnv)
if len(browserEnv) > 0 {
browser.Env = browserEnv
}
versions := config.Versions{
Default: Latest,
Versions: map[string]*config.Browser{
Latest: {
Image: cmd,
Path: "/",
},
Latest: browser,
},
}
browsers[dd.BrowserName] = versions
Expand Down Expand Up @@ -545,12 +554,14 @@ func (d *DriversConfigurator) Start() error {
if len(overrideArgs) > 0 {
args = overrideArgs
}
return runCommand(d.getSelenoidBinaryPath(), args)
env := strings.Fields(d.Env)
return runCommand(d.getSelenoidBinaryPath(), args, env)
}

func (d *DriversConfigurator) StartUI() error {
args := strings.Fields(d.Args)
return runCommand(d.getSelenoidUIBinaryPath(), args)
env := strings.Fields(d.Env)
return runCommand(d.getSelenoidUIBinaryPath(), args, env)
}

var killFunc func(os.Process) error = func(p os.Process) error {
Expand Down Expand Up @@ -605,11 +616,12 @@ func findProcesses(regex string) []os.Process {

var execCommand = exec.Command

func runCommand(command string, args []string) error {
func runCommand(command string, args []string, env []string) error {
cmd := execCommand(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env
return cmd.Start()
}

Expand Down
7 changes: 6 additions & 1 deletion selenoid/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
previousReleaseTag = "1.2.0"
latestReleaseTag = "1.2.1"
version = "version"
testEnv = "MYKEY=myvalue"
)

var (
Expand Down Expand Up @@ -148,6 +149,9 @@ func TestConfigureDrivers(t *testing.T) {
BrowsersJsonUrl: browsersJsonUrl,
Download: true,
Quiet: false,
Args: "-limit 42",
Env: testEnv,
BrowserEnv: testEnv,
}
configurator := NewDriversConfigurator(&lcConfig)
AssertThat(t, configurator.IsConfigured(), Is{false})
Expand All @@ -167,6 +171,7 @@ func TestConfigureDrivers(t *testing.T) {
Latest: {
Image: []string{unpackedFirstFile},
Path: "/",
Env: []string{testEnv},
},
},
},
Expand All @@ -176,6 +181,7 @@ func TestConfigureDrivers(t *testing.T) {
Latest: {
Image: []string{unpackedSecondFile},
Path: "/",
Env: []string{testEnv},
},
},
},
Expand Down Expand Up @@ -390,7 +396,6 @@ func TestStartStopProcess(t *testing.T) {
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Version: Latest,
Args: "-limit 42",
}
configurator := NewDriversConfigurator(&lcConfig)
AssertThat(t, configurator.IsRunning(), Is{true}) //This is probably true because test binary has name selenoid.test; no fake process is launched
Expand Down
16 changes: 9 additions & 7 deletions selenoid/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
)

type LifecycleConfig struct {
Quiet bool
Force bool
ConfigDir string
Browsers string
Download bool
Args string
Version string
Quiet bool
Force bool
ConfigDir string
Browsers string
BrowserEnv string
Download bool
Args string
Env string
Version string

// Docker specific
LastVersions int
Expand Down
12 changes: 6 additions & 6 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
"revisionTime": "2016-07-29T13:19:23Z"
},
{
"checksumSHA1": "RvHpyzTcdkjUkKBn0kmZ9irr724=",
"checksumSHA1": "RJ93VwpT4yuW2nQOfdi/ocfhoKc=",
"path": "github.com/aerokube/selenoid/config",
"revision": "5b39d2bab8e58ae2d813b38f0a4305096b8ca92e",
"revisionTime": "2017-05-13T11:25:18Z"
"revision": "368bfbbfbc0ea07ec9f81f8ea5b1aabe38516cbd",
"revisionTime": "2017-07-03T10:52:49Z"
},
{
"checksumSHA1": "Cq8PTDf6LLWGYm2Dwu/D3Nh0JqM=",
"checksumSHA1": "wwFIjzJhaV3fFP02UwrHAXFMNiE=",
"path": "github.com/aerokube/selenoid/session",
"revision": "5b39d2bab8e58ae2d813b38f0a4305096b8ca92e",
"revisionTime": "2017-05-13T11:25:18Z"
"revision": "368bfbbfbc0ea07ec9f81f8ea5b1aabe38516cbd",
"revisionTime": "2017-07-03T10:52:49Z"
},
{
"checksumSHA1": "Nge8TFcb8UiM2yAHwIoV8qdFKy4=",
Expand Down

0 comments on commit 6101d50

Please sign in to comment.