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 #170 from vania-pooh/master
Browse files Browse the repository at this point in the history
Passing all session capabilities to /status (fixes #164)
  • Loading branch information
aandryashin authored Jul 27, 2017
2 parents 5b5a839 + de1d324 commit 5a75c5d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 36 deletions.
31 changes: 20 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (

// Session - session id and vnc flaf
type Session struct {
ID string `json:"id"`
Container string `json:"container,omitempty"`
VNC bool `json:"vnc"`
Screen string `json:"screen"`
ID string `json:"id"`
Container string `json:"container,omitempty"`
VNC bool `json:"vnc"`
Screen string `json:"screen"`
Caps session.Caps `json:"caps"`
}

// Sessions - used count and individual sessions for quota user
Expand Down Expand Up @@ -146,25 +147,33 @@ func (config *Config) State(sessions *session.Map, limit, queued, pending int) *
}
sessions.Each(func(id string, session *session.Session) {
state.Used++
_, ok := state.Browsers[session.Browser]
browserName := session.Caps.Name
version := session.Caps.Version
_, ok := state.Browsers[browserName]
if !ok {
state.Browsers[session.Browser] = make(Version)
state.Browsers[browserName] = make(Version)
}
_, ok = state.Browsers[session.Browser][session.Version]
_, ok = state.Browsers[browserName][version]
if !ok {
state.Browsers[session.Browser][session.Version] = make(Quota)
state.Browsers[browserName][version] = make(Quota)
}
v, ok := state.Browsers[session.Browser][session.Version][session.Quota]
v, ok := state.Browsers[browserName][version][session.Quota]
if !ok {
v = &Sessions{0, []Session{}}
state.Browsers[session.Browser][session.Version][session.Quota] = v
state.Browsers[browserName][version][session.Quota] = v
}
v.Count++
vnc := false
if session.VNC != "" {
vnc = true
}
v.Sessions = append(v.Sessions, Session{ID: id, Container: session.Container, VNC: vnc, Screen: session.Screen})
v.Sessions = append(v.Sessions, Session{
ID: id,
Container: session.Container,
VNC: vnc,
Screen: session.Caps.ScreenResolution,
Caps: session.Caps,
})
})
return state
}
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestConfigNonEmptyState(t *testing.T) {
conf.Load(confFile, logConfPath)

sessions := session.NewMap()
sessions.Put("0", &session.Session{Browser: "firefox", Version: "49.0", Quota: "unknown"})
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
Expand All @@ -89,7 +89,7 @@ func TestConfigEmptyVersions(t *testing.T) {
conf.Load(confFile, logConfPath)

sessions := session.NewMap()
sessions.Put("0", &session.Session{Browser: "firefox", Version: "49.0", Quota: "unknown"})
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
Expand All @@ -105,7 +105,7 @@ func TestConfigNonEmptyVersions(t *testing.T) {
conf.Load(confFile, logConfPath)

sessions := session.NewMap()
sessions.Put("0", &session.Session{Browser: "firefox", Version: "49.0", Quota: "unknown"})
sessions.Put("0", &session.Session{Caps: session.Caps{Name: "firefox", Version: "49.0"}, Quota: "unknown"})
state := conf.State(sessions, 1, 0, 0)
AssertThat(t, state.Total, EqualTo{1})
AssertThat(t, state.Queued, EqualTo{0})
Expand Down
7 changes: 2 additions & 5 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sync"
"time"

"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/docker/docker/api/types"
"golang.org/x/net/websocket"
Expand Down Expand Up @@ -120,7 +119,7 @@ func create(w http.ResponseWriter, r *http.Request) {
return
}
var browser struct {
Caps service.Caps `json:"desiredCapabilities"`
Caps session.Caps `json:"desiredCapabilities"`
}
err = json.Unmarshal(body, &browser)
if err != nil {
Expand Down Expand Up @@ -233,12 +232,10 @@ func create(w http.ResponseWriter, r *http.Request) {
}
sessions.Put(s.ID, &session.Session{
Quota: quota,
Browser: browser.Caps.Name,
Version: browser.Caps.Version,
Caps: browser.Caps,
URL: u,
Container: startedService.ID,
VNC: startedService.VNCHostPort,
Screen: browser.Caps.ScreenResolution,
Cancel: cancel,
Timeout: onTimeout(timeout, func() {
request{r}.session(s.ID).Delete()
Expand Down
3 changes: 2 additions & 1 deletion service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"time"

"github.com/aerokube/selenoid/session"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
Expand All @@ -19,7 +20,7 @@ import (
type Docker struct {
ServiceBase
Environment
Caps
session.Caps
LogConfig *container.LogConfig
Client *client.Client
}
Expand Down
13 changes: 3 additions & 10 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import (
"time"

"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/session"
"github.com/docker/docker/client"
)

// Caps - user capabilities
type Caps struct {
Name string `json:"browserName"`
Version string `json:"version"`
ScreenResolution string `json:"screenResolution"`
VNC bool `json:"enableVNC"`
}

// Environment - all settings that influence browser startup
type Environment struct {
IP string
Expand Down Expand Up @@ -49,7 +42,7 @@ type Starter interface {

// Manager - interface to choose appropriate starter
type Manager interface {
Find(caps Caps, requestId uint64) (Starter, bool)
Find(caps session.Caps, requestId uint64) (Starter, bool)
}

// DefaultManager - struct for default implementation
Expand All @@ -60,7 +53,7 @@ type DefaultManager struct {
}

// Find - default implementation Manager interface
func (m *DefaultManager) Find(caps Caps, requestId uint64) (Starter, bool) {
func (m *DefaultManager) Find(caps session.Caps, requestId uint64) (Starter, bool) {
browserName := caps.Name
version := caps.Version
log.Printf("[%d] [LOCATING_SERVICE] [%s-%s]\n", requestId, browserName, version)
Expand Down
13 changes: 10 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import (
"sync"
)

// Caps - user capabilities
type Caps struct {
Name string `json:"browserName"`
Version string `json:"version"`
ScreenResolution string `json:"screenResolution"`
VNC bool `json:"enableVNC"`
TestName string `json:"testName"`
}

// Session - holds session info
type Session struct {
Quota string
Browser string
Version string
Caps Caps
URL *url.URL
Container string
VNC string
Screen string
Cancel func()
Timeout chan struct{}
Lock sync.Mutex
Expand Down
7 changes: 4 additions & 3 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/pborman/uuid"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (m *HTTPTest) StartWithCancel() (*service.StartedService, error) {
return &ss, nil
}

func (m *HTTPTest) Find(caps service.Caps, requestId uint64) (service.Starter, bool) {
func (m *HTTPTest) Find(caps session.Caps, requestId uint64) (service.Starter, bool) {
return m, true
}

Expand All @@ -69,13 +70,13 @@ func (m *StartupError) StartWithCancel() (*service.StartedService, error) {
return nil, errors.New("Failed to start Service")
}

func (m *StartupError) Find(caps service.Caps, requestId uint64) (service.Starter, bool) {
func (m *StartupError) Find(caps session.Caps, requestId uint64) (service.Starter, bool) {
return m, true
}

type BrowserNotFound struct{}

func (m *BrowserNotFound) Find(caps service.Caps, requestId uint64) (service.Starter, bool) {
func (m *BrowserNotFound) Find(caps session.Caps, requestId uint64) (service.Starter, bool) {
return nil, false
}

Expand Down

0 comments on commit 5a75c5d

Please sign in to comment.