summaryrefslogtreecommitdiff
path: root/jstests/readonly/lib/read_only_test.js
blob: 55af207a3993b8d307502f1f71198bf302cb8ca3 (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
80
81
'use_strict';

function makeDirectoryReadOnly(dir) {
    if (_isWindows()) {
        run("attrib", "+r", dir, "/s");
    } else {
        run("chmod", "-R", "a-w", dir);
    }
}

function makeDirectoryWritable(dir) {
    if (_isWindows()) {
        run("attrib", "-r", dir, "/s");
    } else {
        run("chmod", "-R", "a+w", dir);
    }
}

function runReadOnlyTest(test) {
    printjson(test);

    assert.eq(typeof(test.exec), 'function');
    assert.eq(typeof(test.load), 'function');
    assert.eq(typeof(test.name), 'string');

    var options = {
        storageEngine: TestData.storageEngine,
        nopreallocj: ''
    };

    var writableMongod = MongoRunner.runMongod(options);
    var dbpath = writableMongod.dbpath;

    jsTest.log('starting load phase for test: ' + test.name);
    test.load(writableMongod.getDB('test')[test.name]);

    MongoRunner.stopMongod(writableMongod);

    makeDirectoryReadOnly(dbpath);

    try {
        var readOnlyOptions =
            Object.extend(options, {readOnly: '', dbpath: dbpath, noCleanData: true});

        var readOnlyMongod = MongoRunner.runMongod(readOnlyOptions);

        jsTest.log('starting execution phase for test: ' + test.name);
        test.exec(readOnlyMongod.getDB('test')[test.name]);

        // We need to make the directory writable so that MongoRunner can clean the dbpath.
        makeDirectoryWritable(dbpath);
        MongoRunner.stopMongod(readOnlyMongod);
    } finally {
        // One last time, just in case.
        makeDirectoryWritable(dbpath);
    }
}

function * cycleN(arr, N) {
    for (var i = 0; i < N; ++i) {
        yield arr[i % arr.length];
    }
}

function * zip2(iter1, iter2) {
    var n1 = iter1.next();
    var n2 = iter2.next();
    while (!n1.done || !n2.done) {
        var res = [];
        if (!n1.done) {
            res.push(n1.value);
            n1 = iter1.next();
        }
        if (!n2.done) {
            res.push(n2.value);
            n2 = iter2.next();
        }

        yield res;
    }
}