Skip to content

Commit

Permalink
Merge pull request #24 from alcounit/develop
Browse files Browse the repository at this point in the history
* Fix #22
* Fix #23
  • Loading branch information
alcounit authored Mar 10, 2021
2 parents b3c3480 + 0990701 commit 9d30289
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (app *App) HandleSession(w http.ResponseWriter, r *http.Request) {
mergo.Merge(&caps, *fmc)
caps.ValidateCapabilities()

browser, err = app.browsers.Find(caps.BrowserName, caps.BrowserVersion)
browser, err = app.browsers.Find(caps.GetBrowserName(), caps.BrowserVersion)
if err == nil {
break
}
Expand Down
81 changes: 56 additions & 25 deletions platform/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package platform

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -31,17 +32,21 @@ var (
vnc: intstr.FromString("5900"),
}

defaults = struct {
serviceType, testName, browserName, browserVersion, screenResolution, enableVNC, timeZone, session string
defaultsAnnotations = struct {
testName, browserName, browserVersion, screenResolution, enableVNC, timeZone string
}{
serviceType: "type",
testName: "testName",
browserName: "browserName",
browserVersion: "browserVersion",
screenResolution: "SCREEN_RESOLUTION",
enableVNC: "ENABLE_VNC",
timeZone: "TZ",
session: "session",
}
defaultLabels = struct {
serviceType, session string
}{
serviceType: "type",
session: "session",
}
)

Expand Down Expand Up @@ -116,13 +121,15 @@ func NewDefaultClient(namespace string) (Platform, error) {

//Create ...
func (cl *Client) Create(layout *ServiceSpec) (*Service, error) {
annontations := map[string]string{
defaultsAnnotations.browserName: layout.Template.BrowserName,
defaultsAnnotations.browserVersion: layout.Template.BrowserVersion,
defaultsAnnotations.testName: layout.RequestedCapabilities.TestName,
}

labels := map[string]string{
defaults.serviceType: "browser",
defaults.browserName: layout.Template.BrowserName,
defaults.browserVersion: layout.Template.BrowserVersion,
defaults.testName: layout.RequestedCapabilities.TestName,
defaults.session: layout.SessionID,
defaultLabels.serviceType: "browser",
defaultLabels.session: layout.SessionID,
}

envVar := func(name string) (i int, b bool) {
Expand All @@ -134,43 +141,48 @@ func (cl *Client) Create(layout *ServiceSpec) (*Service, error) {
return -1, false
}

i, b := envVar(defaults.screenResolution)
i, b := envVar(defaultsAnnotations.screenResolution)
if layout.RequestedCapabilities.ScreenResolution != "" {
if !b {
layout.Template.Spec.EnvVars = append(layout.Template.Spec.EnvVars,
apiv1.EnvVar{Name: defaults.screenResolution,
apiv1.EnvVar{Name: defaultsAnnotations.screenResolution,
Value: layout.RequestedCapabilities.ScreenResolution})
} else {
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaults.screenResolution, Value: layout.RequestedCapabilities.ScreenResolution}
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaultsAnnotations.screenResolution, Value: layout.RequestedCapabilities.ScreenResolution}
}
labels[defaults.screenResolution] = layout.RequestedCapabilities.ScreenResolution
annontations[defaultsAnnotations.screenResolution] = layout.RequestedCapabilities.ScreenResolution
} else {
if b {
labels[defaults.screenResolution] = layout.Template.Spec.EnvVars[i].Value
annontations[defaultsAnnotations.screenResolution] = layout.Template.Spec.EnvVars[i].Value
}
}

i, b = envVar(defaults.enableVNC)
i, b = envVar(defaultsAnnotations.enableVNC)
if layout.RequestedCapabilities.VNC {
vnc := fmt.Sprintf("%v", layout.RequestedCapabilities.VNC)
if !b {
layout.Template.Spec.EnvVars = append(layout.Template.Spec.EnvVars, apiv1.EnvVar{Name: defaults.enableVNC, Value: vnc})
layout.Template.Spec.EnvVars = append(layout.Template.Spec.EnvVars, apiv1.EnvVar{Name: defaultsAnnotations.enableVNC, Value: vnc})
} else {
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaults.enableVNC, Value: vnc}
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaultsAnnotations.enableVNC, Value: vnc}
}
labels[defaults.enableVNC] = vnc
annontations[defaultsAnnotations.enableVNC] = vnc
} else {
if b {
labels[defaults.enableVNC] = layout.Template.Spec.EnvVars[i].Value
annontations[defaultsAnnotations.enableVNC] = layout.Template.Spec.EnvVars[i].Value
}
}

i, b = envVar(defaultsAnnotations.timeZone)
if layout.RequestedCapabilities.TimeZone != "" {
i, b := envVar(defaults.timeZone)
if !b {
layout.Template.Spec.EnvVars = append(layout.Template.Spec.EnvVars, apiv1.EnvVar{Name: defaults.timeZone, Value: layout.RequestedCapabilities.TimeZone})
layout.Template.Spec.EnvVars = append(layout.Template.Spec.EnvVars, apiv1.EnvVar{Name: defaultsAnnotations.timeZone, Value: layout.RequestedCapabilities.TimeZone})
} else {
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaults.timeZone, Value: layout.RequestedCapabilities.TimeZone}
layout.Template.Spec.EnvVars[i] = apiv1.EnvVar{Name: defaultsAnnotations.timeZone, Value: layout.RequestedCapabilities.TimeZone}
}
annontations[defaultsAnnotations.timeZone] = layout.RequestedCapabilities.TimeZone
} else {
if b {
annontations[defaultsAnnotations.timeZone] = layout.Template.Spec.EnvVars[i].Value
}
}

Expand All @@ -182,6 +194,14 @@ func (cl *Client) Create(layout *ServiceSpec) (*Service, error) {
layout.Template.Meta.Labels[k] = v
}

if layout.Template.Meta.Annotations == nil {
layout.Template.Meta.Annotations = make(map[string]string)
}

if caps, err := json.Marshal(annontations); err == nil {
layout.Template.Meta.Annotations["capabilities"] = string(caps)
}

pod := &apiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: layout.SessionID,
Expand Down Expand Up @@ -318,7 +338,7 @@ func (cl *Client) Create(layout *ServiceSpec) (*Service, error) {
svc := &Service{
SessionID: podName,
URL: u,
Labels: layout.Template.Meta.Labels,
Labels: getRequestedCapabilities(pod.GetAnnotations()),
CancelFunc: func() {
cancel()
},
Expand Down Expand Up @@ -370,7 +390,7 @@ func (cl *Client) List() ([]*Service, error) {
Scheme: "http",
Host: net.JoinHostPort(host, cl.svcPort.StrVal),
},
Labels: pod.GetLabels(),
Labels: getRequestedCapabilities(pod.GetAnnotations()),
CancelFunc: func() {
cl.Delete(podName)
},
Expand Down Expand Up @@ -409,7 +429,7 @@ func (cl Client) Watch() <-chan Event {
Scheme: "http",
Host: net.JoinHostPort(host, cl.svcPort.StrVal),
},
Labels: pod.GetLabels(),
Labels: getRequestedCapabilities(pod.GetAnnotations()),
CancelFunc: func() {
cl.Delete(podName)
},
Expand Down Expand Up @@ -495,6 +515,17 @@ func getImagePullSecretList(secret string) []apiv1.LocalObjectReference {
return refList
}

func getRequestedCapabilities(annotations map[string]string) map[string]string {
if k, ok := annotations["capabilities"]; ok {
capabilities := make(map[string]string)
err := json.Unmarshal([]byte(k), &capabilities)
if err == nil {
return capabilities
}
}
return nil
}

func waitForService(u url.URL, t time.Duration) error {
up := make(chan struct{})
done := make(chan struct{})
Expand Down
4 changes: 2 additions & 2 deletions selenium/selenium.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type Capabilities struct {

//ValidateCapabilities ...
func (c *Capabilities) ValidateCapabilities() {
if c.W3CBrowserVersion == "" {
if c.W3CBrowserVersion != "" {
c.BrowserVersion = c.W3CBrowserVersion
}

if c.WC3PlatformName == "" {
if c.WC3PlatformName != "" {
c.Platform = c.WC3PlatformName
}
}
Expand Down

0 comments on commit 9d30289

Please sign in to comment.