From 6f2ca6fe5534cb5ffd7aad8cea342100e235d3cf Mon Sep 17 00:00:00 2001 From: Alexander Andryashin Date: Tue, 5 Jul 2016 23:44:32 +0300 Subject: [PATCH] Logging browser startup failure. --- proxy.go | 29 ++++++++++-- proxy_test.go | 126 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 6 deletions(-) diff --git a/proxy.go b/proxy.go index b862b79..e63c230 100644 --- a/proxy.go +++ b/proxy.go @@ -81,11 +81,14 @@ func (h *Host) session(c caps) (map[string]interface{}, int) { if err != nil { return nil, seleniumError } + var reply map[string]interface{} + err = json.NewDecoder(resp.Body).Decode(&reply) + if err != nil { + return nil, seleniumError + } if resp.StatusCode != http.StatusOK { - return nil, browserFailed + return reply, browserFailed } - var reply map[string]interface{} - json.NewDecoder(resp.Body).Decode(&reply) return reply, browserStarted } @@ -123,6 +126,21 @@ func fmtBrowser(browser, version string) string { return browser } +func browserErrMsg(js map[string]interface{}) string { + if js == nil { + return "" + } + val, ok := js["value"].(map[string]interface{}) + if !ok { + return "" + } + msg, ok := val["message"].(string) + if !ok { + return "" + } + return msg +} + func route(w http.ResponseWriter, r *http.Request) { start := time.Now() id := serial() @@ -156,7 +174,8 @@ loop: } log.Printf("[%d] [SESSION_ATTEMPTED] [%s] [%s] [%s] [%s] [%d]\n", id, user, remote, fmtBrowser(browser, version), h.net(), count) excludes := make([]string, 0) - switch resp, status := h.session(c); status { + resp, status := h.session(c) + switch status { case browserStarted: sess := resp["sessionId"].(string) resp["sessionId"] = h.sum() + sess @@ -169,7 +188,7 @@ loop: excludes = append(excludes, h.region) hosts = config.find(browser, version, excludes...) } - log.Printf("[%d] [SESSION_FAILED] [%s] [%s] [%s] [%s]\n", id, user, remote, fmtBrowser(browser, version), h.net()) + log.Printf("[%d] [SESSION_FAILED] [%s] [%s] [%s] [%s] - %s\n", id, user, remote, fmtBrowser(browser, version), h.net(), browserErrMsg(resp)) if len(hosts) == 0 { break loop } diff --git a/proxy_test.go b/proxy_test.go index 648cc3d..18409d6 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -196,7 +196,7 @@ func TestSessionWrongHash(t *testing.T) { func TestStartSession(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(fmt.Sprintf(`{"sessionId":"123"}`))) + w.Write([]byte(`{"sessionId":"123"}`)) })) selenium := httptest.NewServer(mux) defer selenium.Close() @@ -249,10 +249,134 @@ func TestStartSessionFail(t *testing.T) { rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`))) + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) +} + +func TestStartSessionBrowserFail(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{"value": {"message" : "Browser startup failure..."}}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + config = Browsers{Browsers: []Browser{ + Browser{Name: "browser", DefaultVersion: "1.0", Versions: []Version{ + Version{Number: "1.0", Regions: []Region{ + Region{Hosts: Hosts{ + node, node, node, node, node, + }}, + }}, + }}}} + routes = linkRoutes(&config) + + rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`))) + AssertThat(t, err, Is{nil}) AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 5 attempt(s)"}}) } +func TestStartSessionBrowserFailUnknownError(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + config = Browsers{Browsers: []Browser{ + Browser{Name: "browser", DefaultVersion: "1.0", Versions: []Version{ + Version{Number: "1.0", Regions: []Region{ + Region{Hosts: Hosts{ + node, + }}, + }}, + }}}} + routes = linkRoutes(&config) + + rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`))) + + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) +} + +func TestStartSessionBrowserFailWrongValue(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{"value": 1}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + config = Browsers{Browsers: []Browser{ + Browser{Name: "browser", DefaultVersion: "1.0", Versions: []Version{ + Version{Number: "1.0", Regions: []Region{ + Region{Hosts: Hosts{ + node, + }}, + }}, + }}}} + routes = linkRoutes(&config) + + rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`))) + + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) +} + +func TestStartSessionBrowserFailWrongMsg(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/wd/hub/session", postOnly(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{"value": {"message" : true}}`)) + })) + selenium := httptest.NewServer(mux) + defer selenium.Close() + + host, port := hostportnum(selenium.URL) + node := Host{Name: host, Port: port, Count: 1} + + test.Lock() + defer test.Unlock() + + config = Browsers{Browsers: []Browser{ + Browser{Name: "browser", DefaultVersion: "1.0", Versions: []Version{ + Version{Number: "1.0", Regions: []Region{ + Region{Hosts: Hosts{ + node, + }}, + }}, + }}}} + routes = linkRoutes(&config) + + rsp, err := http.Post(gridrouter("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"browserName":"browser", "version":"1.0"}}`))) + + AssertThat(t, err, Is{nil}) + AssertThat(t, rsp, AllOf{Code{http.StatusInternalServerError}, Body{"cannot create session browser-1.0 on any hosts after 1 attempt(s)"}}) +} + func TestDeleteSession(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/wd/hub/session/", func(w http.ResponseWriter, r *http.Request) {