From dbdd6c35fd87699b0d03c6b9352faa1b084b86e9 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Tue, 6 Jun 2017 20:31:21 +0300 Subject: [PATCH] Better Selenoid image matching (fixes #15) --- selenoid/docker.go | 12 ++++++++---- selenoid/docker_test.go | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/selenoid/docker.go b/selenoid/docker.go index 2dd2edd..9d0e363 100644 --- a/selenoid/docker.go +++ b/selenoid/docker.go @@ -18,10 +18,10 @@ import ( "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "github.com/heroku/docker-registry-client/registry" + "strconv" "strings" "time" . "vbom.ml/util/sortorder" - "strconv" ) const ( @@ -112,8 +112,12 @@ func (c *DockerConfigurator) getSelenoidImage() *types.ImageSummary { return nil } for _, img := range images { - if len(img.RepoTags) > 0 && strings.Contains(img.RepoTags[0], selenoidImage) { - return &img + const colon = ":" + if len(img.RepoTags) > 0 { + imageName := strings.Split(img.RepoTags[0], colon)[0] + if imageName == selenoidImage { + return &img + } } } return nil @@ -362,7 +366,7 @@ func (c *DockerConfigurator) Start() error { ExposedPorts: exposedPorts, }, &container.HostConfig{ - Binds: volumes, + Binds: volumes, PortBindings: portBindings, }, &network.NetworkingConfig{}, selenoidContainerName) diff --git a/selenoid/docker_test.go b/selenoid/docker_test.go index ab4eb13..f9e335d 100644 --- a/selenoid/docker_test.go +++ b/selenoid/docker_test.go @@ -13,6 +13,7 @@ import ( var ( mockDockerServer *httptest.Server + imageName = selenoidImage ) func init() { @@ -55,12 +56,12 @@ func mux() http.Handler { mux.HandleFunc("/v1.29/images/json", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - output := ` + output := fmt.Sprintf(` [{ "Id": "sha256:e216a057b1cb1efc11f8a268f37ef62083e70b1b38323ba252e25ac88904a7e8", "ParentId": "", - "RepoTags": [ "aerokube/selenoid" ], + "RepoTags": [ "%s:latest" ], "RepoDigests": [], "Created": 1474925151, "Size": 103579269, @@ -70,7 +71,7 @@ func mux() http.Handler { "Containers": 2 }] - ` + `, imageName) w.Write([]byte(output)) }, )) @@ -189,13 +190,13 @@ func testConfigure(t *testing.T, download bool) { withTmpDir(t, "test-docker-configure", func(t *testing.T, dir string) { lcConfig := LifecycleConfig{ - ConfigDir: dir, - RegistryUrl: mockDockerServer.URL, - Download: download, - Quiet: false, + ConfigDir: dir, + RegistryUrl: mockDockerServer.URL, + Download: download, + Quiet: false, LastVersions: 2, - Tmpfs: 512, - Browsers: "firefox,opera", + Tmpfs: 512, + Browsers: "firefox,opera", } c, err := NewDockerConfigurator(&lcConfig) AssertThat(t, err, Is{nil}) @@ -260,12 +261,27 @@ func TestStartStopContainer(t *testing.T) { func TestDownload(t *testing.T) { c, err := NewDockerConfigurator(&LifecycleConfig{ RegistryUrl: mockDockerServer.URL, - Quiet: true, - Version: Latest, + Quiet: true, + Version: Latest, }) AssertThat(t, c.IsDownloaded(), Is{true}) AssertThat(t, err, Is{nil}) ref, err := c.Download() AssertThat(t, ref, Not{nil}) AssertThat(t, err, Is{nil}) -} \ No newline at end of file +} + +func TestGetSelenoidImage(t *testing.T) { + defer func() { + imageName = selenoidImage + }() + c, err := NewDockerConfigurator(&LifecycleConfig{ + RegistryUrl: mockDockerServer.URL, + Quiet: true, + Version: Latest, + }) + AssertThat(t, err, Is{nil}) + AssertThat(t, c.getSelenoidImage() == nil, Is{false}) + imageName = "aerokube/selenoid-ui" + AssertThat(t, c.getSelenoidImage() == nil, Is{true}) +}