diff --git a/lib/consul/index.js b/lib/consul/index.js index 5985a84..0384d0c 100644 --- a/lib/consul/index.js +++ b/lib/consul/index.js @@ -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 */ diff --git a/test/git2consul_expand_no_extensions_update_test.js b/test/git2consul_expand_no_extensions_update_test.js new file mode 100644 index 0000000..745ce58 --- /dev/null +++ b/test/git2consul_expand_no_extensions_update_test.js @@ -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(); + }); + }); + }); + }); + }); + + }); + }); + }); + + }); + + + + +}); diff --git a/test/git2consul_file_delete_expand_no_extensions_test.js b/test/git2consul_file_delete_expand_no_extensions_test.js new file mode 100644 index 0000000..83b7a4f --- /dev/null +++ b/test/git2consul_file_delete_expand_no_extensions_test.js @@ -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(); + }); + }); + }); + }); + }); + }); + }); + }); + +});