Skip to content

Commit 563cfec

Browse files
Merge pull request #183 from browserstack/SDK-6948-v8-bsstag-host-recognition
fix(util): recognize *.bsstag.com hosts as BrowserStack (v8, SDK-6948)
2 parents f11c392 + 0f1d173 commit 563cfec

9 files changed

Lines changed: 356 additions & 23 deletions

File tree

‎.changeset/pr-183.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@wdio/browserstack-service": patch
3+
---
4+
5+
- SDK-6948: `*.bsstag.com` hubs are now recognised as BrowserStack on named internal staging envs (`BROWSERSTACK_STAGING_ENV` / `BROWSERSTACK_ENV`), so with the CLI flow (default) session marking and TRA linking work there. The classic (non-CLI) flow on a named staging env is not supported; its session update is skipped at debug.
6+
- SDK-6948: `update_cli` failures fall back to `BROWSERSTACK_BINARY_URL`. A failed fallback download uses the cached binary.
7+
- SDK-6948: `_printSessionURL` and `_update` log a failed session lookup or update (HTTP error) at debug instead of throwing.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@wdio/browserstack-service": patch
3+
---
4+
5+
fix(cli): fall back to `BROWSERSTACK_BINARY_URL` when `update_cli` fails (SDK-6948)
6+
7+
`update_cli` runs during bootstrap — before the binary is spawned and before GRR localizes the API hosts — so it always targets production `api.browserstack.com`. On an internal staging run (`BROWSERSTACK_STAGING_ENV`) the staging credentials are rejected there (`401`), which left the CLI binary path empty and crashed the launcher with `spawn('') … ERR_INVALID_ARG_VALUE: 'file' cannot be empty`. When `update_cli` fails and `BROWSERSTACK_BINARY_URL` is set, download the binary from that URL instead so the run can proceed; the same applies when a 2xx reply carries no download URL and no binary is cached. If that download fails, the cached binary is used when there is one, and both errors are logged. Opt-in only — with the variable unset the original error propagates exactly as before. Ports main/v9 #224 to the v8 line.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@wdio/browserstack-service": patch
3+
---
4+
5+
fix(util): recognize `*.bsstag.com` hosts as BrowserStack on internal staging envs (SDK-6948)
6+
7+
`getCloudProvider` and `isBrowserstackInfra` only matched `browserstack.com`, so a session pointed at an internal staging env (`*.bsstag.com`) was classified as non-BrowserStack: session marking and Automate <-> TRA session linking did not work there. `*.bsstag.com` hosts are now treated as BrowserStack when `BROWSERSTACK_STAGING_ENV` is a bare env name (e.g. `k8s`, trimmed; blank means unset) or `BROWSERSTACK_ENV` is `staging`/`stag`/`preprod`/`pre-prod`. Hosts are matched case-insensitively. Production behaviour is unchanged. With the CLI flow (the default), session marking and Automate <-> TRA session linking now work on those envs, because the binary supplies the staging session REST host. The classic (non-CLI) flow on a named staging env is not supported: it has no staging session REST host, so the session name/status update is skipped at debug instead of failing the hook. `_printSessionURL` and `_update` log a failed session lookup or update (HTTP error reply) at debug instead of throwing. Ports main/v9 #275 to the v8 line.

‎packages/browserstack-service/src/cli/cliUtils.ts‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,63 @@ export class CLIUtils {
187187
const nullDevice = platform() === 'win32' ? 'NUL' : '/dev/null'
188188
queryParams.cli_version = await this.runShellCommand(`${existingCliPath} version 2>${nullDevice}`)
189189
}
190-
const response = await this.requestToUpdateCLI(queryParams, config)
191-
if (nestedKeyValue(response, ['updated_cli_version'])) {
192-
logger.debug(`Need to update binary, current binary version: ${queryParams.cli_version}`)
193-
194-
const browserStackBinaryUrl = process.env.BROWSERSTACK_BINARY_URL || null
195-
if (!isNullOrEmpty(browserStackBinaryUrl)) {
196-
logger.debug(`Using BROWSERSTACK_BINARY_URL: ${browserStackBinaryUrl}`)
197-
response.url = browserStackBinaryUrl
190+
const browserStackBinaryUrl = process.env.BROWSERSTACK_BINARY_URL || null
191+
192+
const fallbackToBinaryUrl = async (reason: unknown) => {
193+
logger.warn(`update_cli request failed (${reason}); falling back to BROWSERSTACK_BINARY_URL`)
194+
try {
195+
return await this.downloadLatestBinary(browserStackBinaryUrl as string, cliDir)
196+
} catch (downloadErr) {
197+
// A cached binary is still better than failing the run; keep the download error visible.
198+
if (isNullOrEmpty(existingCliPath)) {
199+
logger.error(`BROWSERSTACK_BINARY_URL download failed (${util.format(downloadErr)}) after update_cli failed (${reason})`)
200+
throw downloadErr
201+
}
202+
logger.warn(`BROWSERSTACK_BINARY_URL download failed (${util.format(downloadErr)}); using cached binary ${existingCliPath}`)
203+
return existingCliPath
198204
}
205+
}
199206

200-
const finalBinaryPath = await this.downloadLatestBinary(nestedKeyValue(response, ['url']), cliDir)
207+
// Single close for the span on every path, including a rejected download.
208+
try {
209+
let response
210+
try {
211+
response = await this.requestToUpdateCLI(queryParams, config)
212+
} catch (err) {
213+
// The version-check call (update_cli) failed — e.g. an internal staging run where
214+
// update_cli is served from a host the provided creds are not valid on (staging creds
215+
// against the prod api host -> 401). If an explicit binary URL was supplied, use it so
216+
// the run is not blocked on this call. Opt-in only: with no BROWSERSTACK_BINARY_URL the
217+
// error propagates exactly as before, so production behaviour is unchanged.
218+
if (!isNullOrEmpty(browserStackBinaryUrl)) {
219+
logger.debug(`update_cli error: ${util.format(err)}`)
220+
const status = (err as { response?: { statusCode?: number } })?.response?.statusCode ?? (err as Error)?.message
221+
return await fallbackToBinaryUrl(status)
222+
}
223+
throw err
224+
}
225+
226+
// A 2xx reply with neither `updated_cli_version` nor `url` means there is nothing to update:
227+
// keep a cached binary, and only fetch from the explicit binary URL when there is none.
228+
if (!isNullOrEmpty(browserStackBinaryUrl) && isNullOrEmpty(existingCliPath) &&
229+
!nestedKeyValue(response, ['updated_cli_version']) && !nestedKeyValue(response, ['url'])) {
230+
return await fallbackToBinaryUrl(JSON.stringify(response))
231+
}
232+
233+
if (nestedKeyValue(response, ['updated_cli_version'])) {
234+
logger.debug(`Need to update binary, current binary version: ${queryParams.cli_version}`)
235+
236+
if (!isNullOrEmpty(browserStackBinaryUrl)) {
237+
logger.debug(`Using BROWSERSTACK_BINARY_URL: ${browserStackBinaryUrl}`)
238+
response.url = browserStackBinaryUrl
239+
}
240+
241+
return await this.downloadLatestBinary(nestedKeyValue(response, ['url']), cliDir)
242+
}
243+
return existingCliPath
244+
} finally {
201245
PerformanceTester.end(PerformanceEvents.SDK_CLI_CHECK_UPDATE)
202-
return finalBinaryPath
203246
}
204-
PerformanceTester.end(PerformanceEvents.SDK_CLI_CHECK_UPDATE)
205-
return existingCliPath
206247
}
207248

208249
static getCliDir() {

‎packages/browserstack-service/src/service.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,18 +1011,27 @@ export default class BrowserstackService implements Services.ServiceInstance {
10111011
}
10121012
const sessionUrl = `${this._sessionBaseUrl}/${sessionId}.json`
10131013
BStackLogger.debug(`Updating Browserstack session at ${sessionUrl} with request body: `, requestBody)
1014+
// SDK-6948: an HTTP error reply (e.g. 401/404 from a named staging env's session REST host)
1015+
// must not fail the calling hook. got rejects on non-2xx; main/v9 uses fetch, which resolves,
1016+
// so only HTTP-status errors are swallowed here and network errors still propagate.
1017+
const onHttpError = (err: { response?: { statusCode?: number } }) => {
1018+
if (!err?.response) {
1019+
throw err
1020+
}
1021+
BStackLogger.debug(`Could not update Browserstack session (${err.response.statusCode}) at ${sessionUrl}`)
1022+
}
10141023
if (this._turboScale) {
10151024
return got.patch(sessionUrl, {
10161025
json: requestBody,
10171026
username: this._config.user,
10181027
password: this._config.key
1019-
})
1028+
}).catch(onHttpError)
10201029
}
10211030
return got.put(sessionUrl, {
10221031
json: requestBody,
10231032
username: this._config.user,
10241033
password: this._config.key
1025-
})
1034+
}).catch(onHttpError)
10261035
}
10271036

10281037
@PerformanceTester.Measure(PERFORMANCE_SDK_EVENTS.AUTOMATE_EVENTS.PRINT_BUILDLINK)
@@ -1041,12 +1050,25 @@ export default class BrowserstackService implements Services.ServiceInstance {
10411050
responseType: 'json'
10421051
}
10431052

1044-
if (this._turboScale) {
1045-
const response = await got<TurboScaleSessionResponse>(sessionUrl, reqOpts)
1046-
browserUrl = response.body.url
1047-
} else {
1048-
const response = await got<SessionResponse>(sessionUrl, reqOpts)
1049-
browserUrl = response.body.automation_session.browser_url
1053+
try {
1054+
if (this._turboScale) {
1055+
const response = await got<TurboScaleSessionResponse>(sessionUrl, reqOpts)
1056+
browserUrl = response.body.url
1057+
} else {
1058+
const response = await got<SessionResponse>(sessionUrl, reqOpts)
1059+
const session = response.body?.automation_session
1060+
if (!session) {
1061+
BStackLogger.debug(`Could not fetch Browserstack session URL (${response.statusCode}) at ${sessionUrl}`)
1062+
return
1063+
}
1064+
browserUrl = session.browser_url
1065+
}
1066+
} catch (err) {
1067+
// SDK-6948: on a named staging env the session REST host may not serve this session
1068+
// (e.g. classic non-CLI flow). Printing the link is informational, so don't fail the hook.
1069+
const status = (err as { response?: { statusCode?: number } })?.response?.statusCode ?? (err as Error)?.message
1070+
BStackLogger.debug(`Could not fetch Browserstack session URL (${status}) at ${sessionUrl}`)
1071+
return
10501072
}
10511073

10521074
if (!this._browser) {

‎packages/browserstack-service/src/util.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,16 +1046,38 @@ export function getUniqueIdentifierForCucumber(world: ITestCaseHookParameter): s
10461046
return world.pickle.uri + '_' + world.pickle.astNodeIds.join(',')
10471047
}
10481048

1049+
/**
1050+
* SDK-6948: a named internal staging env (`BROWSERSTACK_STAGING_ENV` set to a bare token such as
1051+
* `k8s`) serves its hub/cdp from `*.bsstag.com` (e.g. `hub-k8s.bsstag.com`). Treat those hosts as
1052+
* BrowserStack so session marking and Automate <-> TRA linking work there. A dotted value (full
1053+
* host) or an unset/blank variable leaves the production-only host checks unchanged. The env value is
1054+
* trimmed and the hostname compared case-insensitively (shared rule across SDKs).
1055+
* `BROWSERSTACK_ENV` (trimmed, lowercased) of staging/stag/preprod/pre-prod also selects the bsstag
1056+
* hosts, matching node-agent constants and the binary's getEnvironmentUrl.
1057+
*/
1058+
const BSTAG_BROWSERSTACK_ENVS = ['staging', 'stag', 'preprod', 'pre-prod']
1059+
1060+
export function isStagingEnvBsstagHost(hostname: string): boolean {
1061+
const stagingEnv = process.env.BROWSERSTACK_STAGING_ENV?.trim()
1062+
const namedStagingEnv = Boolean(stagingEnv) && !stagingEnv!.includes('.')
1063+
const browserstackEnv = process.env.BROWSERSTACK_ENV?.trim().toLowerCase() || ''
1064+
if (!namedStagingEnv && !BSTAG_BROWSERSTACK_ENVS.includes(browserstackEnv)) {
1065+
return false
1066+
}
1067+
const host = hostname.toLowerCase()
1068+
return host === 'bsstag.com' || host.endsWith('.bsstag.com')
1069+
}
1070+
10491071
export function getCloudProvider(browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser): string {
10501072
if (browser && 'instances' in browser) {
10511073
// Loop through all instances
10521074
for (const instanceName of browser.instances) {
10531075
const instance = (browser as any)[instanceName] as WebdriverIO.Browser
1054-
if (instance.options && instance.options.hostname && instance.options.hostname.includes('browserstack')) {
1076+
if (instance.options && instance.options.hostname && (instance.options.hostname.includes('browserstack') || isStagingEnvBsstagHost(instance.options.hostname))) {
10551077
return 'browserstack'
10561078
}
10571079
}
1058-
} else if (browser.options && browser.options.hostname && browser.options.hostname.includes('browserstack')) { // Single browser instance
1080+
} else if (browser.options && browser.options.hostname && (browser.options.hostname.includes('browserstack') || isStagingEnvBsstagHost(browser.options.hostname))) { // Single browser instance
10591081
return 'browserstack'
10601082
}
10611083
return 'unknown_grid'
@@ -1185,7 +1207,7 @@ export function isBrowserstackInfra(config: BrowserstackConfig & Options.Testrun
11851207
// a utility function to check if the hostname is browserstack
11861208

11871209
const isBrowserstack = (str: string ): boolean => {
1188-
return str.includes('browserstack.com')
1210+
return str.includes('browserstack.com') || isStagingEnvBsstagHost(str)
11891211
}
11901212

11911213
if ((config.hostname) && !isBrowserstack(config.hostname)) {

‎packages/browserstack-service/tests/cli/cliUtils.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,104 @@ describe('CLIUtils', () => {
441441
mockConfig
442442
)
443443
})
444+
445+
it('falls back to BROWSERSTACK_BINARY_URL when update_cli fails (SDK-6948)', async () => {
446+
const saved = process.env.BROWSERSTACK_BINARY_URL
447+
process.env.BROWSERSTACK_BINARY_URL = 'https://example.com/staging-binary.zip'
448+
try {
449+
const fallbackPath = '/mock/cli/dir/binary-fallback'
450+
vi.spyOn(CLIUtils, 'runShellCommand').mockResolvedValue('1.0.0')
451+
// update_cli fails (e.g. staging creds against the prod api host -> 401)
452+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockRejectedValue({ response: { statusCode: 401 } })
453+
vi.spyOn(CLIUtils, 'downloadLatestBinary').mockResolvedValue(fallbackPath)
454+
455+
const result = await CLIUtils.checkAndUpdateCli(mockExistingPath, mockCliDir, mockConfig)
456+
457+
expect(result).toBe(fallbackPath)
458+
expect(CLIUtils.downloadLatestBinary).toHaveBeenCalledWith('https://example.com/staging-binary.zip', mockCliDir)
459+
} finally {
460+
if (saved === undefined) { delete process.env.BROWSERSTACK_BINARY_URL } else { process.env.BROWSERSTACK_BINARY_URL = saved }
461+
}
462+
})
463+
464+
it('uses the cached binary when the BROWSERSTACK_BINARY_URL download fails (SDK-6948)', async () => {
465+
const saved = process.env.BROWSERSTACK_BINARY_URL
466+
process.env.BROWSERSTACK_BINARY_URL = 'https://example.com/staging-binary.zip'
467+
try {
468+
vi.spyOn(CLIUtils, 'runShellCommand').mockResolvedValue('1.0.0')
469+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockRejectedValue({ response: { statusCode: 401 } })
470+
vi.spyOn(CLIUtils, 'downloadLatestBinary').mockRejectedValue(new Error('download failed'))
471+
472+
const result = await CLIUtils.checkAndUpdateCli(mockExistingPath, mockCliDir, mockConfig)
473+
474+
expect(result).toBe(mockExistingPath)
475+
} finally {
476+
if (saved === undefined) { delete process.env.BROWSERSTACK_BINARY_URL } else { process.env.BROWSERSTACK_BINARY_URL = saved }
477+
}
478+
})
479+
480+
it('re-throws the download error when the BROWSERSTACK_BINARY_URL download fails and no binary is cached (SDK-6948)', async () => {
481+
const saved = process.env.BROWSERSTACK_BINARY_URL
482+
process.env.BROWSERSTACK_BINARY_URL = 'https://example.com/staging-binary.zip'
483+
try {
484+
const downloadErr = new Error('download failed')
485+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockRejectedValue({ response: { statusCode: 401 } })
486+
vi.spyOn(CLIUtils, 'downloadLatestBinary').mockRejectedValue(downloadErr)
487+
488+
await expect(CLIUtils.checkAndUpdateCli('', mockCliDir, mockConfig)).rejects.toBe(downloadErr)
489+
} finally {
490+
if (saved === undefined) { delete process.env.BROWSERSTACK_BINARY_URL } else { process.env.BROWSERSTACK_BINARY_URL = saved }
491+
}
492+
})
493+
494+
it('falls back to BROWSERSTACK_BINARY_URL when a 2xx reply has neither url nor updated_cli_version and no binary is cached (SDK-6948)', async () => {
495+
const saved = process.env.BROWSERSTACK_BINARY_URL
496+
process.env.BROWSERSTACK_BINARY_URL = 'https://example.com/staging-binary.zip'
497+
try {
498+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockResolvedValue({ message: 'Unauthorized' })
499+
vi.spyOn(CLIUtils, 'downloadLatestBinary').mockResolvedValue('/mock/cli/dir/binary-fallback')
500+
501+
const result = await CLIUtils.checkAndUpdateCli('', mockCliDir, mockConfig)
502+
503+
expect(result).toBe('/mock/cli/dir/binary-fallback')
504+
expect(CLIUtils.downloadLatestBinary).toHaveBeenCalledWith('https://example.com/staging-binary.zip', mockCliDir)
505+
} finally {
506+
if (saved === undefined) { delete process.env.BROWSERSTACK_BINARY_URL } else { process.env.BROWSERSTACK_BINARY_URL = saved }
507+
}
508+
})
509+
510+
it('keeps the cached binary when a 2xx reply has neither url nor updated_cli_version (SDK-6948)', async () => {
511+
const saved = process.env.BROWSERSTACK_BINARY_URL
512+
process.env.BROWSERSTACK_BINARY_URL = 'https://example.com/staging-binary.zip'
513+
try {
514+
vi.spyOn(CLIUtils, 'runShellCommand').mockResolvedValue('1.0.0')
515+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockResolvedValue({ success: true })
516+
const downloadSpy = vi.spyOn(CLIUtils, 'downloadLatestBinary').mockResolvedValue('/should/not/be/used')
517+
518+
const result = await CLIUtils.checkAndUpdateCli(mockExistingPath, mockCliDir, mockConfig)
519+
520+
expect(result).toBe(mockExistingPath)
521+
expect(downloadSpy).not.toHaveBeenCalled()
522+
} finally {
523+
if (saved === undefined) { delete process.env.BROWSERSTACK_BINARY_URL } else { process.env.BROWSERSTACK_BINARY_URL = saved }
524+
}
525+
})
526+
527+
it('re-throws when update_cli fails and no BROWSERSTACK_BINARY_URL is set (unchanged behaviour)', async () => {
528+
const saved = process.env.BROWSERSTACK_BINARY_URL
529+
delete process.env.BROWSERSTACK_BINARY_URL
530+
try {
531+
const err = new Error('Unauthorized')
532+
vi.spyOn(CLIUtils, 'runShellCommand').mockResolvedValue('1.0.0')
533+
vi.spyOn(CLIUtils, 'requestToUpdateCLI').mockRejectedValue(err)
534+
const downloadSpy = vi.spyOn(CLIUtils, 'downloadLatestBinary').mockResolvedValue('/should/not/be/used')
535+
536+
await expect(CLIUtils.checkAndUpdateCli(mockExistingPath, mockCliDir, mockConfig)).rejects.toBe(err)
537+
expect(downloadSpy).not.toHaveBeenCalled()
538+
} finally {
539+
if (saved !== undefined) { process.env.BROWSERSTACK_BINARY_URL = saved }
540+
}
541+
})
444542
})
445543

446544
describe('setupCliPath', () => {

0 commit comments

Comments
 (0)