Skip to content

Commit

Permalink
Unit test fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Wolfe <Wolfe1@gmail.com>
  • Loading branch information
Wolfe1 committed May 25, 2022
1 parent 5f070c2 commit 14e4664
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pkg/scalers/selenium_grid_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strconv"
"strings"
"math"

v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -229,7 +230,7 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
if capability.BrowserName == browserName {
if strings.HasPrefix(capability.BrowserVersion, browserVersion) {
count++
} else if capability.BrowserVersion == "" && browserVersion == DefaultBrowserVersion {
} else if browserVersion == DefaultBrowserVersion {
count++
}
}
Expand Down Expand Up @@ -258,8 +259,9 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
var gridNodeCount = int64(seleniumResponse.Data.Grid.NodeCount)

if gridMaxSession > 0 && gridNodeCount > 0 {
count /= (gridMaxSession / gridNodeCount)
// Get count, convert count to next highest int64
var floatCount float64 = float64(count) / (float64(gridMaxSession) / float64(gridNodeCount))
count = int64(math.Ceil(floatCount))
}

return count, nil
}
40 changes: 39 additions & 1 deletion pkg/scalers/selenium_grid_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
}
}`),
browserName: "",
sessionBrowserName: "",
browserVersion: "latest",
},
want: 0,
Expand All @@ -99,13 +100,14 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "latest",
},
want: 2,
wantErr: false,
},
{
name: "2 active sessions with matching browsername on 2 nodes and maxSession=4 should return count as 3",
name: "2 active sessions with matching browsername on 2 nodes and maxSession=4 should return count as 3 (rounded up from 2.5)",
args: args{
b: []byte(`{
"data": {
Expand All @@ -131,11 +133,45 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "latest",
},
want: 3,
wantErr: false,
},
{
name: "2 active sessions with matching browsername on 1 node and maxSession=3 should return count as 2 (rounded up from 1.33)",
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 3,
"nodeCount": 1
},
"sessionsInfo": {
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\",\n \"browserVersion\": \"91.0\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": [
{
"id": "0f9c5a941aa4d755a54b84be1f6535b1",
"capabilities": "{\n \"acceptInsecureCerts\": false,\n \"browserName\": \"chrome\",\n \"browserVersion\": \"91.0.4472.114\",\n \"chrome\": {\n \"chromedriverVersion\": \"91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs\\u002fbranch-heads\\u002f4472@{#1462})\",\n \"userDataDir\": \"\\u002ftmp\\u002f.com.google.Chrome.DMqx9m\"\n },\n \"goog:chromeOptions\": {\n \"debuggerAddress\": \"localhost:35839\"\n },\n \"networkConnectionEnabled\": false,\n \"pageLoadStrategy\": \"normal\",\n \"platformName\": \"linux\",\n \"proxy\": {\n },\n \"se:cdp\": \"http:\\u002f\\u002flocalhost:35839\",\n \"se:cdpVersion\": \"91.0.4472.114\",\n \"se:vncEnabled\": true,\n \"se:vncLocalAddress\": \"ws:\\u002f\\u002flocalhost:7900\\u002fwebsockify\",\n \"setWindowRect\": true,\n \"strictFileInteractability\": false,\n \"timeouts\": {\n \"implicit\": 0,\n \"pageLoad\": 300000,\n \"script\": 30000\n },\n \"unhandledPromptBehavior\": \"dismiss and notify\",\n \"webauthn:extension:largeBlob\": true,\n \"webauthn:virtualAuthenticators\": true\n}",
"nodeId": "d44dcbc5-0b2c-4d5e-abf4-6f6aa5e0983c"
},
{
"id": "0f9c5a941aa4d755a54b84be1f6535b2",
"capabilities": "{\n \"acceptInsecureCerts\": false,\n \"browserName\": \"chrome\",\n \"browserVersion\": \"91.0.4472.114\",\n \"chrome\": {\n \"chromedriverVersion\": \"91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs\\u002fbranch-heads\\u002f4472@{#1462})\",\n \"userDataDir\": \"\\u002ftmp\\u002f.com.google.Chrome.DMqx9m\"\n },\n \"goog:chromeOptions\": {\n \"debuggerAddress\": \"localhost:35839\"\n },\n \"networkConnectionEnabled\": false,\n \"pageLoadStrategy\": \"normal\",\n \"platformName\": \"linux\",\n \"proxy\": {\n },\n \"se:cdp\": \"http:\\u002f\\u002flocalhost:35839\",\n \"se:cdpVersion\": \"91.0.4472.114\",\n \"se:vncEnabled\": true,\n \"se:vncLocalAddress\": \"ws:\\u002f\\u002flocalhost:7900\\u002fwebsockify\",\n \"setWindowRect\": true,\n \"strictFileInteractability\": false,\n \"timeouts\": {\n \"implicit\": 0,\n \"pageLoad\": 300000,\n \"script\": 30000\n },\n \"unhandledPromptBehavior\": \"dismiss and notify\",\n \"webauthn:extension:largeBlob\": true,\n \"webauthn:virtualAuthenticators\": true\n}",
"nodeId": "d44dcbc5-0b2c-4d5e-abf4-6f6aa5e0983d"
}
]
}
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "latest",
},
want: 2,
wantErr: false,
},
{
name: "2 active sessions with matching browsername on 2 nodes should return count as 5",
args: args{
Expand Down Expand Up @@ -163,6 +199,7 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "latest",
},
want: 5,
Expand Down Expand Up @@ -190,6 +227,7 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "91.0",
},
want: 2,
Expand Down

0 comments on commit 14e4664

Please sign in to comment.