All files / lib/checkers file.js

100% Statements 18/18
100% Branches 4/4
100% Functions 4/4
100% Lines 18/18

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  1x 1x 1x 1x                       8x 8x   8x 1x   7x 7x 7x     8x 8x         7x 8x 5x   3x         1x  
'use strict';
var checker = require('../checker'),
    fs = require('fs'),
    Result = require('../result'),
    validator = require('validator');
 
/**
 * Health check a file resource.
 *
 * @param {Object} setup: file check setup with fields:
 * - uri: a resource URI to check
 * - mode: a 3-digit file mode, e.g. 777, 644
 * @param {Function} cb: standard cb(err, result) callback
 */
function check(setup, cb) {
 
    fs.lstat(setup.uri.replace(/^file\:\/\//, ''), function (err, stats) {
        var result = new Result();
 
        if (err) {
            result.addError(err.message);
        } else {
            var modeString = parseInt(stats.mode.toString(8), 10).toString(),
                mode = modeString.slice(modeString.length - 3, modeString.length);
            checker.checkAttribute('mode', setup, _checkMode(mode), result);
        }
 
        result.setStatusByStats();
        cb(null, result);
    });
}
 
function _checkMode(actual) {
    return function (expected) {
        if (validator.matches(actual, new RegExp(expected))) {
            return 'Mode ' + actual + ' as expected';
        } else {
            throw new Error('Mode ' + actual + ' does not match the expected ' + expected);
        }
    };
}
 
exports.check = check;