Skip to content

Commit

Permalink
only upload log files with the same cid (#7461)
Browse files Browse the repository at this point in the history
* only upload log files with the same cid

* fix import

* fix comment

* fix unit test

* fix test for windows
  • Loading branch information
christian-bromann committed Sep 22, 2021
1 parent 21538c3 commit 3082268
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 29 deletions.
3 changes: 2 additions & 1 deletion examples/wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ exports.config = {
* @param {Object} config wdio configuration object
* @param {Array.<Object>} capabilities list of capabilities details
* @param {Array.<String>} specs List of spec file paths that are to be run
* @param {String} cid worker id (e.g. 0-0)
*/
beforeSession: function (config, capabilities, specs) {
beforeSession: function (config, capabilities, specs, cid) {
},
/**
* Gets executed before test execution begins. At this point you can access to all global
Expand Down
2 changes: 1 addition & 1 deletion examples/wdio/custom-service/my.custom.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = class CustomService {
console.log('execute onWorkerStart(cid, caps, specs, args, execArgv)')
}
beforeSession () {
console.log('execute beforeSession(config, capabilities, specs)')
console.log('execute beforeSession(config, capabilities, specs, cid)')
}
before () {
console.log('execute before(capabilities, specs)')
Expand Down
3 changes: 2 additions & 1 deletion packages/wdio-cli/src/templates/wdio.conf.tpl.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@
* @param {Object} config wdio configuration object
* @param {Array.<Object>} capabilities list of capabilities details
* @param {Array.<String>} specs List of spec file paths that are to be run
* @param {String} cid worker id (e.g. 0-0)
*/
// beforeSession: function (config, capabilities, specs) {
// beforeSession: function (config, capabilities, specs, cid) {
// },
/**
* Gets executed before test execution begins. At this point you can access to all global
Expand Down
30 changes: 16 additions & 14 deletions packages/wdio-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { EventEmitter } from 'events'
import logger from '@wdio/logger'
import { initialiseWorkerService, initialisePlugin, executeHooksWithArgs } from '@wdio/utils'
import { ConfigParser } from '@wdio/config'
import type { Options, Capabilities } from '@wdio/types'
import type { Options, Capabilities, Services } from '@wdio/types'
import type { Selector, Browser, MultiRemoteBrowser } from 'webdriverio'

import BaseReporter from './reporter'
import { initialiseInstance, filterLogTypes, getInstancesData } from './utils'

const log = logger('@wdio/runner')

type BeforeArgs = Parameters<Required<Services.HookFunctions>['before']>
type AfterArgs = Parameters<Required<Services.HookFunctions>['after']>
type BeforeSessionArgs = Parameters<Required<Services.HookFunctions>['beforeSession']>
type AfterSessionArgs = Parameters<Required<Services.HookFunctions>['afterSession']>

/**
* user types for globals are set in webdriverio
* putting this here to make compiler happy
Expand Down Expand Up @@ -138,7 +143,9 @@ export default class Runner extends EventEmitter {
caps as Capabilities.Capabilities,
args.ignoredWorkerServices
).map(this._configParser.addService.bind(this._configParser))
await executeHooksWithArgs('beforeSession', this._config.beforeSession, [this._config, this._caps, this._specs])

const beforeSessionParams: BeforeSessionArgs = [this._config, this._caps, this._specs, this._cid]
await executeHooksWithArgs('beforeSession', this._config.beforeSession, beforeSessionParams)

this._reporter = new BaseReporter(this._config, this._cid, { ...caps })
/**
Expand All @@ -157,17 +164,15 @@ export default class Runner extends EventEmitter {
* return if session initialisation failed
*/
if (!browser) {
await executeHooksWithArgs(
'after',
this._config.after as Function,
[1, this._caps, this._specs]
)
const afterArgs: AfterArgs = [1, this._caps, this._specs]
await executeHooksWithArgs('after', this._config.after as Function, afterArgs)
return this._shutdown(1, retries)
}

this._reporter.caps = browser.capabilities as Capabilities.RemoteCapability

await executeHooksWithArgs('before', this._config.before, [this._caps, this._specs, browser])
const beforeArgs: BeforeArgs = [this._caps, this._specs, browser]
await executeHooksWithArgs('before', this._config.before, beforeArgs)

/**
* kill session of SIGINT signal showed up while trying to
Expand Down Expand Up @@ -375,7 +380,7 @@ export default class Runner extends EventEmitter {

try {
logs = await global.browser.getLogs(logType)
} catch (e) {
} catch (e: any) {
return log.warn(`Couldn't fetch logs for ${logType}: ${e.message}`)
}

Expand Down Expand Up @@ -468,10 +473,7 @@ export default class Runner extends EventEmitter {
delete global.browser.sessionId
}

await executeHooksWithArgs(
'afterSession',
global.browser.config.afterSession as Function,
[this._config, capabilities, this._specs as string[]]
)
const afterSessionArgs: AfterSessionArgs = [this._config!, capabilities, this._specs as string[]]
await executeHooksWithArgs('afterSession', global.browser.config.afterSession!, afterSessionArgs)
}
}
2 changes: 1 addition & 1 deletion packages/wdio-runner/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe('wdio-runner', () => {
expect(runner['_shutdown']).toBeCalledWith(123, 2)
expect(executeHooksWithArgs).toBeCalledWith('beforeSession', [beforeSession], [config, {
browserName: '123'
}, ['foobar']])
}, ['foobar'], '0-0'])
expect(executeHooksWithArgs).toBeCalledWith('before', config.before, [caps, specs, stubBrowser])

// session capabilities should be passed to reporter
Expand Down
7 changes: 5 additions & 2 deletions packages/wdio-sauce-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class SauceService implements Services.ServiceInstance {
private _browser?: Browser<'async'> | MultiRemoteBrowser<'async'>
private _isUP?: boolean
private _suiteTitle?: string
private _cid = ''

constructor (
options: SauceServiceConfig,
Expand All @@ -42,7 +43,9 @@ export default class SauceService implements Services.ServiceInstance {
/**
* gather information about runner
*/
beforeSession () {
beforeSession (_: never, __: never, ___: never, cid: string) {
this._cid = cid

/**
* if no user and key is specified even though a sauce service was
* provided set user and key with values so that the session request
Expand Down Expand Up @@ -291,7 +294,7 @@ export default class SauceService implements Services.ServiceInstance {
}

const files = (await fs.promises.readdir(this._config.outputDir))
.filter((file) => file.endsWith('.log'))
.filter((file) => file.startsWith(`wdio-${this._cid}`) && file.endsWith('.log'))
log.info(`Uploading WebdriverIO logs (${files.join(', ')}) to Sauce Labs`)

return this._api.uploadJobAssets(
Expand Down
25 changes: 17 additions & 8 deletions packages/wdio-sauce-service/tests/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs'
import path from 'path'

import got from 'got'
import logger from '@wdio/logger'
import type { MultiRemoteBrowser } from 'webdriverio'
Expand All @@ -17,9 +19,13 @@ jest.createMockFromModule('fs')
fs.createReadStream = jest.fn()
fs.promises.stat = jest.fn().mockReturnValue(Promise.resolve({ size: 123 }))
fs.promises.readdir = jest.fn().mockReturnValue(Promise.resolve([
'fileA.log',
'fileB.log',
'fileC.log'
'wdio-0-0-browser.log',
'wdio-0-0-driver.log',
'wdio-0-0.log',
'wdio-1-0-browser.log',
'wdio-1-0-driver.log',
'wdio-1-0.log',
'wdio.log'
]))

jest.mock('form-data', () => jest.fn().mockReturnValue({
Expand Down Expand Up @@ -464,12 +470,15 @@ test('_uploadLogs should upload', async () => {
{},
{ outputDir: '/foo/bar' } as any
)
const api = { uploadJobAssets: jest.fn().mockResolvedValue({}) }
service['_api'] = api as any
await service.beforeSession(null as never, null as never, null as never, '1-0')
await service['_uploadLogs']('123')
expect((got as any as jest.Mock).mock.calls).toHaveLength(1)
expect((got as any as jest.Mock)).toHaveBeenCalledWith(
'https://api.us-west-1.saucelabs.com/v1/testcomposer/jobs/123/assets',
expect.any(Object)
)
expect(api.uploadJobAssets).toBeCalledTimes(1)
expect(api.uploadJobAssets.mock.calls[0][1].files).toHaveLength(3)
expect(api.uploadJobAssets.mock.calls[0][1].files).toContain(path.sep + path.join('foo', 'bar', 'wdio-1-0-browser.log'))
expect(api.uploadJobAssets.mock.calls[0][1].files).toContain(path.sep + path.join('foo', 'bar', 'wdio-1-0-driver.log'))
expect(api.uploadJobAssets.mock.calls[0][1].files).toContain(path.sep + path.join('foo', 'bar', 'wdio-1-0.log'))
})

test('_uploadLogs should not fail in case of a platform error', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/wdio-types/src/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ export interface HookFunctions {
* @param config wdio configuration object
* @param capabilities list of capabilities details
* @param specs list of spec file paths that are to be run
* @param cid worker id (e.g. 0-0)
*/
beforeSession?(
config: Omit<TestrunnerOptions, 'capabilities'>,
capabilities: RemoteCapability,
specs: string[]
specs: string[],
cid: string
): void;

/**
Expand Down

0 comments on commit 3082268

Please sign in to comment.