From 2c715c3a41039b3d4343b6473f2ebdda5e8bf7a8 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Thu, 20 Dec 2018 11:13:16 +0300 Subject: [PATCH] Better processing complex browser versions specification (fixes #208, fixes #206) --- docs/selenoid-commands.adoc | 22 ++++++++++++++++++++ selenoid/docker.go | 40 ++++++++++++++++++++----------------- selenoid/docker_test.go | 19 ++++++++++++++++++ 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/docs/selenoid-commands.adoc b/docs/selenoid-commands.adoc index de9718e..e6c8b8c 100644 --- a/docs/selenoid-commands.adoc +++ b/docs/selenoid-commands.adoc @@ -161,3 +161,25 @@ An alternative to downloading `cm` manually is using Docker container: ---- docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${HOME}:/root -e OVERRIDE_HOME=${HOME} aerokube/cm:latest-release selenoid start ---- + +=== Downloading Only Some Browser Versions + +By default CM downloads browser images corresponding to 2 last versions of Firefox, Chrome and Opera. To download concrete browser versions - use `--browsers` flag as follows: + +.Download concrete versions of Firefox and Chrome +[source,bash] +---- +./cm selenoid start --browsers 'firefox:51.0;firefox:55.0;chrome:66.0' +---- + +.Download a range of Firefox versions and 3 last Opera versions +[source,bash] +---- +./cm selenoid start --browsers 'firefox:>51.0,<=55.0;opera' --last-versions 3 +---- + +.Download Android image (not downloaded by default because of image size) +[source,bash] +---- +./cm selenoid start --browsers 'android:6.0' +---- diff --git a/selenoid/docker.go b/selenoid/docker.go index 9c30930..e676a8d 100644 --- a/selenoid/docker.go +++ b/selenoid/docker.go @@ -355,30 +355,32 @@ func (c *DockerConfigurator) createConfig() SelenoidConfig { return browsers } -func parseRequestedBrowsers(logger *Logger, requestedBrowsers string) map[string]*ver.Constraints { - ret := make(map[string]*ver.Constraints) +func parseRequestedBrowsers(logger *Logger, requestedBrowsers string) map[string][]ver.Constraints { + ret := make(map[string][]ver.Constraints) if requestedBrowsers != "" { for _, section := range strings.Split(requestedBrowsers, semicolon) { pieces := strings.Split(section, colon) - if len(pieces) == 2 { - browserName := pieces[0] - versionConstraintString := pieces[1] - versionConstraint, err := ver.NewConstraint(versionConstraintString) - if err != nil { - logger.Errorf(`Invalid version constraint %s: %v - ignoring browser "%s"...`, versionConstraintString, err, browserName) - continue + if len(pieces) >= 1 { + browserName := strings.TrimSpace(pieces[0]) + if _, ok := ret[browserName]; !ok { + ret[browserName] = []ver.Constraints{} + } + if len(pieces) == 2 { + versionConstraintString := strings.TrimSpace(pieces[1]) + versionConstraint, err := ver.NewConstraint(versionConstraintString) + if err != nil { + logger.Errorf(`Invalid version constraint %s: %v - ignoring browser "%s"...`, versionConstraintString, err, browserName) + continue + } + ret[browserName] = append(ret[browserName], versionConstraint) } - ret[browserName] = &versionConstraint - } else if len(pieces) == 1 { - browserName := pieces[0] - ret[browserName] = nil } } } return ret } -func (c *DockerConfigurator) getBrowsersToIterate(requestedBrowsers map[string]*ver.Constraints) map[string]string { +func (c *DockerConfigurator) getBrowsersToIterate(requestedBrowsers map[string][]ver.Constraints) map[string]string { defaultBrowsers := map[string]string{ "firefox": "selenoid/firefox", "chrome": "selenoid/chrome", @@ -430,8 +432,8 @@ func filterOutLatest(tags []string) []string { return ret } -func (c *DockerConfigurator) filterTags(tags []string, versionConstraint *ver.Constraints) []string { - if versionConstraint != nil { +func (c *DockerConfigurator) filterTags(tags []string, versionConstraints []ver.Constraints) []string { + if len(versionConstraints) > 0 { var ret []string for _, tag := range tags { version, err := ver.NewVersion(tag) @@ -439,8 +441,10 @@ func (c *DockerConfigurator) filterTags(tags []string, versionConstraint *ver.Co c.Errorf("Skipping tag %s as it does not follow semantic versioning: %v", tag, err) continue } - if versionConstraint.Check(version) { - ret = append(ret, tag) + for _, vc := range versionConstraints { + if vc.Check(version) { + ret = append(ret, tag) + } } } return ret diff --git a/selenoid/docker_test.go b/selenoid/docker_test.go index 8685516..218db59 100644 --- a/selenoid/docker_test.go +++ b/selenoid/docker_test.go @@ -477,3 +477,22 @@ func TestValidEnviron(t *testing.T) { AssertThat(t, validateEnviron([]string{"=::=::"}), EqualTo{[]string{}}) AssertThat(t, validateEnviron([]string{"HOMEDRIVE=C:", "DOCKER_HOST=192.168.0.1", "=::=::"}), EqualTo{[]string{"HOMEDRIVE=C:", "DOCKER_HOST=192.168.0.1"}}) } + +func TestParseRequestedBrowsers(t *testing.T) { + output := parseRequestedBrowsers(&Logger{}, "firefox:>45.0,51.0;opera; android:7.1;firefox:<50.0") + AssertThat(t, len(output), EqualTo{3}) + + ff, ok := output["firefox"] + AssertThat(t, ok, Is{true}) + AssertThat(t, ff, Not{nil}) + AssertThat(t, len(ff), EqualTo{2}) + + opera, ok := output["opera"] + AssertThat(t, ok, Is{true}) + AssertThat(t, len(opera), EqualTo{0}) + + android, ok := output["android"] + AssertThat(t, ok, Is{true}) + AssertThat(t, android, Not{nil}) + AssertThat(t, len(android), EqualTo{1}) +}