All files / wdio-sumologic-reporter/src index.js

100% Statements 35/35
100% Branches 11/11
100% Functions 13/13
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163              1x   1x 1x             10x               10x 10x   10x 9x       10x 10x   10x 10x 10x 10x       2x       5x               1x               1x               1x               1x               1x               1x               1x               1x                           6x 3x     3x         3x 3x   3x         4x     4x             3x         3x 3x        
import request from 'request'
import dateFormat from 'dateformat'
import stringify from 'json-stringify-safe'
 
import WDIOReporter from '@wdio/reporter'
import logger from '@wdio/logger'
 
const log = logger('@wdio/sumologic-reporter')
 
const MAX_LINES = 100
const DATE_FORMAT = 'yyyy-mm-dd HH:mm:ss,l o'
 
/**
 * Initialize a new sumologic test reporter.
 */
export default class SumoLogicReporter extends WDIOReporter {
    constructor (options) {
        options = Object.assign({
            // don't create a log file
            stdout: true,
            // define sync interval how often logs get pushed to Sumologic
            syncInterval: 100,
            // endpoint of collector source
            sourceAddress: process.env.SUMO_SOURCE_ADDRESS
        }, options)
        super(options)
        this.options = options
 
        if (typeof this.options.sourceAddress !== 'string') {
            log.error('Sumo Logic requires "sourceAddress" paramater')
        }
 
        // Cache of entries we are yet to sync
        this.unsynced = []
        this.isSynchronising = false
 
        this.errorCount = 0
        this.specs = {}
        this.results = {}
        this.interval = setInterval(::this.sync, this.options.syncInterval)
    }
 
    get isSynchronised () {
        return this.unsynced.length === 0
    }
 
    onRunnerStart (runner) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'runner:start',
            data: runner
        }))
    }
 
    onSuiteStart (suite) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'suite:start',
            data: suite
        }))
    }
 
    onTestStart (test) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'test:start',
            data: test
        }))
    }
 
    onTestSkip (test) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'test:skip',
            data: test
        }))
    }
 
    onTestPass (test) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'test:pass',
            data: test
        }))
    }
 
    onTestFail (test) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'test:fail',
            data: test
        }))
    }
 
    onTestEnd (test) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'test:end',
            data: test
        }))
    }
 
    onSuiteEnd (suite) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'suite:end',
            data: suite
        }))
    }
 
    onRunnerEnd (runner) {
        this.unsynced.push(stringify({
            time: dateFormat(new Date(), DATE_FORMAT),
            event: 'runner:end',
            data: runner
        }))
    }
 
    sync () {
        /**
         * don't synchronise logs if
         *  - we've already send out a request and are waiting for the successful response
         *  - we have nothing to synchronise
         *  - there is an invalid source address
         */
        if (this.isSynchronising || this.unsynced.length === 0 || typeof this.options.sourceAddress !== 'string') {
            return
        }
 
        const logLines = this.unsynced.slice(0, MAX_LINES).join('\n')
 
        /**
         * set `isSynchronising` to true so we don't sync when a request is being made
         */
        this.isSynchronising = true
        log.debug('start synchronization')
 
        request({
            method: 'POST',
            uri: this.options.sourceAddress,
            body: logLines
        }, (err, resp) => {
            const failed = Boolean(err) || resp.statusCode < 200 || resp.statusCode >= 400
 
            /* istanbul ignore if  */
            if (failed) {
                return log.error('failed send data to Sumo Logic:\n', err.stack ? err.stack : err)
            }
 
            /**
             * remove transfered logs from log bucket
             */
            this.unsynced.splice(0, MAX_LINES)
 
            /**
             * reset sync flag so we can sync again
             */
            log.debug(`synchronised collector data, server status: ${resp.statusCode}`)
            this.isSynchronising = false
        })
    }
}