Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…uchdb-bootstrap into neighbourhoodie-fix/two-dot-x
  • Loading branch information
jo committed Feb 27, 2021
2 parents 2ff8c62 + 4a9b9a0 commit 808fc8f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
59 changes: 50 additions & 9 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,61 @@ exports.createDatabases = function (callback) {
async.each(exports.dbnames, exports.couch.db.create, callback)
}

let version
exports.getVersion = function (callback) {
if (version) { return callback(null, version) }
exports.couch.request({
path: ''
}, (error, info) => {
if (error) { return callback(error) }
version = info.version
return callback(null, info.version)
})
}

let configPaths
exports.getConfigPath = function (callback) {
if (configPaths) { return callback(null, configPaths) } else {
exports.getVersion((error, version) => {
if (error) { return callback(error) } else {
if (version > '2') {
exports.couch.request({
path: '_membership'
}, (error, membership) => {
if (error) { return callback(error) } else {
configPaths = membership.all_nodes.map((node) => {
return `_node/${node}/_config/`
})
return callback(null, configPaths)
}
})
} else {
configPaths = ['_config/']
return callback(null, configPaths)
}
}
})
}
}

// There is an issue with section deletion in CouchDB.
// You cannot delete an entire section:
// $ curl -XDELETE http://localhost:5984/_config/couchdb-bootstrap
// {"error":"method_not_allowed","reason":"Only GET,PUT,DELETE allowed"}
exports.clearConfig = function (callback) {
exports.couch.request({
path: '_config/' + exports.configSection
}, function (error, config) {
if (error) return callback(error)
async.map(Object.keys(config), function (key, next) {
exports.getConfigPath((error, configPath) => {
if (error) { return callback(error) } else {
exports.couch.request({
method: 'DELETE',
path: '_config/' + exports.configSection + '/' + encodeURIComponent(key)
}, next)
}, callback)
path: configPath + exports.configSection
}, function (error, config) {
if (error) return callback(error)
async.map(Object.keys(config), function (key, next) {
exports.couch.request({
method: 'DELETE',
path: configPath + exports.configSection + '/' + encodeURIComponent(key)
}, next)
}, callback)
})
}
})
}
18 changes: 13 additions & 5 deletions test/test-couchdb-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const async = require('async')
var test = require('tap').test
var helper = require('./helper')
var bootstrap = require('..')
Expand All @@ -14,12 +15,19 @@ function check (t, response, done) {
}
}, 'correct response')

helper.couch.request({
path: '_config/couchdb-bootstrap/foo'
}, function (error, config) {
helper.getConfigPath((error, configPaths) => {
s.error(error)
s.equal(config, 'bar')
s.end()
const tasks = configPaths.map((configPath) => {
return helper.couch.request.bind(null, {
path: `${configPath}/couchdb-bootstrap/foo`
})
})
async.series(tasks, function (error, configs) {
const config = configs[0][0]
s.error(error)
s.equal(config, 'bar')
s.end()
})
})
})

Expand Down

0 comments on commit 808fc8f

Please sign in to comment.