Skip to content

Commit

Permalink
fix: Selenium Grid scaler logic on capabilities match
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
  • Loading branch information
VietND96 committed Jan 18, 2025
1 parent 9c907cb commit cee377a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pkg/scalers/selenium_grid_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,35 +251,34 @@ func countMatchingSessions(sessions Sessions, browserName string, browserVersion
// This function checks if the request capabilities match the scaler metadata
func checkRequestCapabilitiesMatch(request Capability, browserName string, browserVersion string, _ string, platformName string) bool {
// Check if browserName matches
browserNameMatch := request.BrowserName == "" && browserName == "" ||
browserNameMatch := (request.BrowserName == "" && browserName == "") ||
strings.EqualFold(browserName, request.BrowserName)

// Check if browserVersion matches
browserVersionMatch := (request.BrowserVersion == "" && browserVersion == "") ||
(request.BrowserVersion == "stable" && browserVersion == "") ||
(strings.HasPrefix(browserVersion, request.BrowserVersion) && request.BrowserVersion != "" && browserVersion != "")
(request.BrowserVersion != "" && strings.HasPrefix(browserVersion, request.BrowserVersion))

// Check if platformName matches
platformNameMatch := request.PlatformName == "" && platformName == "" ||
strings.EqualFold(platformName, request.PlatformName)
platformNameMatch := (request.PlatformName == "" || strings.EqualFold("any", request.PlatformName) || strings.EqualFold(platformName, request.PlatformName)) &&
(platformName == "" || platformName == "any" || strings.EqualFold(platformName, request.PlatformName))

return browserNameMatch && browserVersionMatch && platformNameMatch
}

// This function checks if Node stereotypes or ongoing sessions match the scaler metadata
func checkStereotypeCapabilitiesMatch(capability Capability, browserName string, browserVersion string, sessionBrowserName string, platformName string) bool {
// Check if browserName matches
browserNameMatch := capability.BrowserName == "" && browserName == "" ||
browserNameMatch := (capability.BrowserName == "" && browserName == "") ||
strings.EqualFold(browserName, capability.BrowserName) ||
strings.EqualFold(sessionBrowserName, capability.BrowserName)

// Check if browserVersion matches
browserVersionMatch := capability.BrowserVersion == "" && browserVersion == "" ||
(strings.HasPrefix(browserVersion, capability.BrowserVersion) && capability.BrowserVersion != "" && browserVersion != "")
browserVersionMatch := (capability.BrowserVersion == "" && browserVersion == "") ||
(capability.BrowserVersion != "" && strings.HasPrefix(browserVersion, capability.BrowserVersion))

// Check if platformName matches
platformNameMatch := capability.PlatformName == "" && platformName == "" ||
strings.EqualFold(platformName, capability.PlatformName)
platformNameMatch := (capability.PlatformName == "" || strings.EqualFold("any", capability.PlatformName) || strings.EqualFold(platformName, capability.PlatformName)) &&
(platformName == "" || platformName == "any" || strings.EqualFold(platformName, capability.PlatformName))

return browserNameMatch && browserVersionMatch && platformNameMatch
}
Expand Down
68 changes: 68 additions & 0 deletions pkg/scalers/selenium_grid_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,74 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
wantOnGoingSessions: 2,
wantErr: false,
},
{
name: "1 queue request without platformName and scaler metadata without platfromName should return 1 new node and 1 ongoing session",
args: args{
b: []byte(`{
"data": {
"grid": {
"sessionCount": 2,
"maxSession": 2,
"totalSlots": 2
},
"nodesInfo": {
"nodes": [
{
"id": "node-1",
"status": "UP",
"sessionCount": 1,
"maxSession": 1,
"slotCount": 1,
"stereotypes": "[{\"slots\": 1, \"stereotype\": {\"browserName\": \"chrome\", \"platformName\": \"any\"}}]",
"sessions": [
{
"id": "session-1",
"capabilities": "{\"browserName\": \"chrome\", \"platformName\": \"any\"}",
"slot": {
"id": "9ce1edba-72fb-465e-b311-ee473d8d7b64",
"stereotype": "{\"browserName\": \"chrome\", \"platformName\": \"any\"}"
}
}
]
},
{
"id": "node-2",
"status": "UP",
"sessionCount": 1,
"maxSession": 1,
"slotCount": 1,
"stereotypes": "[{\"slots\": 1, \"stereotype\": {\"browserName\": \"chrome\", \"platformName\": \"linux\"}}]",
"sessions": [
{
"id": "session-2",
"capabilities": "{\"browserName\": \"chrome\", \"browserVersion\": \"91.0\", \"platformName\": \"linux\"}",
"slot": {
"id": "9ce1edba-72fb-465e-b311-ee473d8d7b64",
"stereotype": "{\"browserName\": \"chrome\", \"platformName\": \"linux\"}"
}
}
]
}
]
},
"sessionsInfo": {
"sessionQueueRequests": [
"{\"browserName\": \"chrome\", \"platformName\": \"linux\"}",
"{\"browserName\": \"chrome\"}",
"{\"browserName\": \"chrome\", \"platformName\": \"any\"}"
]
}
}
}`),
browserName: "chrome",
sessionBrowserName: "chrome",
browserVersion: "",
platformName: "",
},
wantNewRequestNodes: 2,
wantOnGoingSessions: 1,
wantErr: false,
},
{
name: "1 active session with matching browsername and version should return count as 2",
args: args{
Expand Down

0 comments on commit cee377a

Please sign in to comment.