Skip to content

Commit

Permalink
test(config): add tests on config
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 29, 2015
1 parent 285f052 commit f1415ee
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ etc/*.pid
test_results
.env
dump.rdb
test/fixtures/*.json
6 changes: 3 additions & 3 deletions bin/redsmin
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node

var log = require('../lib/log')('info');
var config = require('../lib/config')(log);
var Config = require('../lib/config')(log);
var config = new Config(Config.config.config_file);
var p = require('path');
var fs = require('fs');
var daemon = require("daemonize2").setup({
Expand All @@ -13,10 +14,9 @@ var daemon = require("daemonize2").setup({

log.cli();


daemon.on("starting", function () {
log.info("Starting redsmin daemon...");
log.debug("Connecting on Redis at " + config().redis);
log.debug("Connecting on Redis at " + config.redis);
}).on("started", function (pid) {
log.info("Redsmin daemon started. PID: " + pid);
}).on("stopping", function () {
Expand Down
58 changes: 35 additions & 23 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ module.exports = function (log) {
key: ''
},
redis: {
uri: '',
key: null,
uri: 'redis://127.0.0.1:6379',
auth: null
}
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = config.redsmin.reject_unauthorized;
var FILEPATH = config.config_file;
assert(_.isString(FILEPATH));

assert(_.isString(config.config_file));
/**
* Strip all Javascript type comments from the string.
* (extracted from http://lorenwest.github.com/node-config/ )
Expand Down Expand Up @@ -96,9 +93,10 @@ module.exports = function (log) {
}

function Config(jsonFilename) {
assert(_.isString(jsonFilename));
_.extend(this, config);
this.jsonFilename = fs.realpathSync(jsonFilename);

this.jsonFilename = fs.realpathSync(jsonFilename);
var fileContent = fs.readFileSync(this.jsonFilename, 'UTF-8');
var json = null;

Expand All @@ -111,9 +109,15 @@ module.exports = function (log) {
}

// valid the connection key
json.key = config.redsmin.key || json.key;
this.checkKey(json.key);
this.json = json;
this.redis.uri = json.redis || this.redis.uri;
this.redis.auth = json.auth || this.redis.auth;
this.redsmin.key = json.key || this.redsmin.key;
this.checkKey(this.redsmin.key, function (err) {
if (err) {
log.error(err);
process.exit(1);
}
});
}

/**
Expand All @@ -125,29 +129,37 @@ module.exports = function (log) {
*/
Config.prototype.write = function (key, redis, auth, cb) {
var newConfig = _.extend({}, {
key: key || this.key,
redis: redis || this.redis,
auth: auth || this.auth
key: key || this.redsmin.key,
redis: redis || this.redis.uri,
auth: auth || this.redis.auth
});

fs.writeFile(this.jsonFilename, JSON.stringify(newConfig), 'utf-8', function (err) {
if (!err) {
this.key = newConfig.key;
this.redis = newConfig.redis;
this.auth = newConfig.auth;
this.checkKey(newConfig.key, function (err) {
if (err) {
return cb(err);
}
cb(err);

fs.writeFile(this.jsonFilename, JSON.stringify(newConfig), 'utf-8', function (err) {
if (!err) {
this.redsmin.key = newConfig.key;
this.redis.uri = newConfig.redis;
this.redis.auth = newConfig.auth;
}
cb(err);
}.bind(this));
}.bind(this));

};

Config.prototype.checkKey = function (key) {
Config.prototype.checkKey = function (key, f) {
if (!KEY_REGEX.test(key)) {
log.error('Invalid connection key "' + key + '", please browse http://bit.ly/YAIeAM');
return false;
return f(Config.INVALID_KEY.replace('{key}', key));
}

return true;
return f(null, key);
};

return new Config(FILEPATH);
Config.INVALID_KEY = 'Invalid connection key "{key}", please browse http://bit.ly/YAIeAM';
Config.config = config; // expose env. vars. config
return Config;
};
69 changes: 69 additions & 0 deletions lib/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';
var configFactory = require('./config');
var t = require('chai').assert;
var fs = require('fs');
var path = require('path');

describe('config', function () {
var Config;

beforeEach(function (done) {
Config = configFactory(console);
done();
});

it('should return a configuration', function () {
var config = new Config(path.resolve(__dirname, '../test/fixtures/redsmin_default.json'));
t.strictEqual(config.redis.auth, null);
t.strictEqual(config.redis.uri, 'redis://127.0.0.1:6379');

t.strictEqual(config.redsmin.key, '5331e06500617e0b0a0000aa');
t.strictEqual(config.redsmin.port, 993);
t.strictEqual(config.redsmin.hostname, 'ssl.redsmin.com');
t.strictEqual(config.redsmin.reject_unauthorized, 1);
});

describe('write', function () {
it('should check the key', function (done) {
var config = new Config(path.resolve(__dirname, '../test/fixtures/redsmin_default.json'));
config.write('invalid-key', '', '', function (err) {
t.include(err, 'Invalid connection key');
done();
});
});

it('should rewrite the configuration file', function (done) {
var FILE = path.resolve(__dirname, '../test/fixtures/redsmin.json');
var jsonConfig = {
"key": "5331e06500617e0b090000aa",
"redis": "redis://127.0.0.1:6379",
"auth": "aa"
};
var KEY = 'aaaae06500617e0b090000aa';
var REDIS = '10.10.10.10:2939';
var AUTH = 'bb';

fs.writeFileSync(FILE, JSON.stringify(jsonConfig), 'utf-8');
var config = new Config(FILE);

config.write(KEY, REDIS, AUTH, function (err) {
t.strictEqual(err, null);

t.strictEqual(config.redis.auth, AUTH);
t.strictEqual(config.redis.uri, REDIS);
t.strictEqual(config.redsmin.key, KEY);
t.strictEqual(config.redsmin.port, 993);
t.strictEqual(config.redsmin.hostname, 'ssl.redsmin.com');
t.strictEqual(config.redsmin.reject_unauthorized, 1);

t.strictEqual(fs.readFileSync(FILE, 'utf-8'), JSON.stringify({
key: KEY,
redis: REDIS,
auth: AUTH
}));
done();
});
});
});

});
Empty file added test/fixtures/.gitkeep
Empty file.

0 comments on commit f1415ee

Please sign in to comment.