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

Commit

Permalink
Logging browser startup failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
aandryashin committed Jul 5, 2016
1 parent aea4ea1 commit 6f2ca6f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 6 deletions.
29 changes: 24 additions & 5 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
126 changes: 125 additions & 1 deletion proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6f2ca6f

Please sign in to comment.