summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/directoryperdb.js
blob: 5e1111f61cd7679dee436946fb52630aa86fe8e7 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * Tests that a mongod started with --directoryperdb will write data for database x into a directory
 * named x inside the dbpath.
 *
 * This test does not make sense for in-memory storage engines, since they will not produce any data
 * files.
 * @tags: [requires_persistence]
 */

(function() {
'use strict';

const baseDir = "jstests_directoryperdb";
const dbpath = MongoRunner.dataPath + baseDir + "/";
const dbname = "foo";

const isDirectoryPerDBSupported =
    jsTest.options().storageEngine == "wiredTiger" || !jsTest.options().storageEngine;

const m = MongoRunner.runMongod({dbpath: dbpath, directoryperdb: ''});

if (!isDirectoryPerDBSupported) {
    assert.isnull(m, 'storage engine without directoryperdb support should fail to start up');
    return;
} else {
    assert(m, 'storage engine with directoryperdb support failed to start up');
}

const getDir = function(dbName, dbDirPath) {
    return listFiles(dbDirPath).filter(function(path) {
        return path.name.endsWith(dbName);
    });
};

const checkDirExists = function(dbName, dbDirPath) {
    const files = getDir(dbName, dbDirPath);
    assert.eq(1,
              files.length,
              "dbpath did not contain '" + dbName +
                  "' directory when it should have: " + tojson(listFiles(dbDirPath)));
    assert.gt(listFiles(files[0].name).length, 0);
};

const checkDirRemoved = function(dbName, dbDirPath) {
    checkLog.containsJson(db.getMongo(), 4888200, {db: dbName});
    assert.soon(
        function() {
            const files = getDir(dbName, dbDirPath);
            if (files.length == 0) {
                return true;
            } else {
                return false;
            }
        },
        "dbpath contained '" + dbName +
            "' directory when it should have been removed:" + tojson(listFiles(dbDirPath)),
        10 * 1000);  // The periodic task to run data table cleanup runs once a second.
};

const db = m.getDB(dbname);
assert.commandWorked(db.bar.insert({x: 1}));
checkDirExists(dbname, dbpath);

// Test that dropping the last collection in the database causes the database directory to be
// removed.
assert(db.bar.drop());
checkDirRemoved(dbname, dbpath);

// Test that dropping the entire database causes the database directory to be removed.
assert.commandWorked(db.bar.insert({x: 1}));
checkDirExists(dbname, dbpath);
assert.commandWorked(db.dropDatabase());
checkDirRemoved(dbname, dbpath);

MongoRunner.stopMongod(m);

// Subsequent attempt to start server using same dbpath without directoryperdb should fail.
assert.isnull(MongoRunner.runMongod({dbpath: dbpath, restart: true}));
}());