Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding '/' to consul kv del path to avoid greedy pattern matching and… #182

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/consul/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ var file_deleted = function(branch, file, cb) {

logger.trace('Deleting key %s', key_name);

if (branch.expand_keys) {
key_name += "/";
logger.trace('appending key_name: %s', key_name);
}

// Delete this key. Or, if mode is branch.expand_keys, delete all files underneath this key.
consul.kv.del({'key': key_name, token: token, recurse: branch.expand_keys}, function(err) {
/* istanbul ignore if */
Expand Down
106 changes: 106 additions & 0 deletions test/git2consul_expand_no_extensions_update_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var should = require('should');
var _ = require('underscore');

var mkdirp = require('mkdirp');

var logger = require('../lib/logging.js');

// We want this above any git2consul module to make sure logging gets configured
require('./git2consul_bootstrap_test.js');

var repo = require('../lib/git/repo.js');
var git_utils = require('./utils/git_utils.js');
var consul_utils = require('./utils/consul_utils.js');

var git_commands = require('../lib/git/commands.js');

describe('Expand keys', function() {

// The current copy of the git master branch. This is initialized before each test in the suite.
var branch;
beforeEach(function(done) {

// Each of these tests needs a working repo instance, so create it here and expose it to the suite
// namespace. These are all tests of expand_keys mode, so set that here.
git_utils.initRepo(_.extend(git_utils.createRepoConfig(), {'expand_keys': true,"ignore_file_extension" : true}), function(err, repo) {
if (err) return done(err);

// The default repo created by initRepo has a single branch, master.
branch = repo.branches['master'];

done();
});
});

/*YAML*/

it ('should handle additions of new YAML files when expand_keys and ignore_file_extensions set', function(done) {
var sample_key = 'simple.yaml';
var sample_value = "---\n\nfirst_level:\n second_level: is_all_we_need\n";

// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add a file.", function(err) {
if (err) return done(err);

branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/simple/first_level/second_level', 'is_all_we_need', function(err, value) {
if (err) return done(err);
done();
});
});
});
});



it ('should handle changing YAML files and not removing another key with suffix when expand_keys and ignore_file_extensions set', function(done) {
var sample_key = 'changeme.yaml';
var sample_value = "---\n\nfirst_level:\n is_all_we_need\n";

var sample_key2 = 'changeme_had-dev.yaml';
var sample_value2 = "---\n\nfirst_level:\n plikdwa\n";


// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add a file.", function(err) {
if (err) return done(err);
git_utils.addFileToGitRepo(sample_key2, sample_value2, "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/changeme/first_level', 'is_all_we_need', function(err, value) {
if (err) return done(err);

// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, "---\n\nfirst_level:\n is super different\n", "Change a file.", function(err) {
if (err) return done(err);

branch.handleRefChange(0, function(err) {
if (err) return done(err);

// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/changeme/first_level', 'is super different', function(err, value) {
if (err) return done(err);

consul_utils.validateValue('test_repo/master/changeme_had-dev/first_level', 'plikdwa', function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});

});
});
});

});




});
65 changes: 65 additions & 0 deletions test/git2consul_file_delete_expand_no_extensions_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var should = require('should');
var _ = require('underscore');

var mkdirp = require('mkdirp');

// We want this above any git2consul module to make sure logging gets configured
require('./git2consul_bootstrap_test.js');

var repo = require('../lib/git/repo.js');
var git_utils = require('./utils/git_utils.js');
var consul_utils = require('./utils/consul_utils.js');

var git_commands = require('../lib/git/commands.js');

describe('File operations', function() {

// The current copy of the git master branch. This is initialized before each test in the suite.
var branch;
beforeEach(function(done) {

// Each of these tests needs a working repo instance, so create it here and expose it to the suite
// namespace.
git_utils.initRepo(_.extend(git_utils.createRepoConfig(), {'expand_keys': true,"ignore_file_extension" : true}), function(err, repo) {
if (err) return done(err);

// The default repo created by initRepo has a single branch, master.
branch = repo.branches['master'];

done();
});
});

it ('should handle deletions of existing files - expand ignore file extensions', function(done) {
var sample_key = 'simple.yaml';
var sample_value = "---\n\nfirst_level:\n second_level: is_all_we_need\n";

// Add the file, call branch.handleRef to sync the commit, then delete the file and sync again.
// Finally, validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Create file to delete.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// Validate that the file was added to consul before we delete it
consul_utils.validateValue('test_repo/master/simple/first_level/second_level', 'is_all_we_need', function(err, value) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
git_utils.deleteFileFromGitRepo(sample_key, "Delete file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the branch should have removed our sample_key from consul.
consul_utils.validateValue('test_repo/master/simple/first_level/second_level', undefined, function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});
});
});
});

});