summaryrefslogtreecommitdiff
path: root/jstests/readonly/lib/read_only_test.js
blob: e3b68671966ada4b07f1aec0fb541f3734b279fa (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var StandaloneFixture, ShardedFixture, runReadOnlyTest, zip2, cycleN;

(function() {
    "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);
        }
    }

    StandaloneFixture = function() {};

    StandaloneFixture.prototype.runLoadPhase = function runLoadPhase(test) {
        this.mongod = MongoRunner.runMongod({});
        this.dbpath = this.mongod.dbpath;

        test.load(this.mongod.getDB("test")[test.name]);
        assert.commandWorked(this.mongod.getDB("local").dropDatabase());
        MongoRunner.stopMongod(this.mongod);
    };

    StandaloneFixture.prototype.runExecPhase = function runExecPhase(test) {
        try {
            makeDirectoryReadOnly(this.dbpath);

            var options = {queryableBackupMode: "", noCleanData: true, dbpath: this.dbpath};

            this.mongod = MongoRunner.runMongod(options);

            test.exec(this.mongod.getDB("test")[test.name]);

            MongoRunner.stopMongod(this.mongod);
        } finally {
            makeDirectoryWritable(this.dbpath);
        }
    };

    ShardedFixture = function() {
        this.nShards = 3;
    };

    ShardedFixture.prototype.runLoadPhase = function runLoadPhase(test) {
        this.shardingTest = new ShardingTest({nopreallocj: true, mongos: 1, shards: this.nShards});

        this.paths = this.shardingTest.getDBPaths();

        jsTest.log("sharding test collection...");

        // Use a hashed shard key so we actually hit multiple shards.
        this.shardingTest.shardColl(test.name, {_id: "hashed"});

        test.load(this.shardingTest.getDB("test")[test.name]);
    };

    ShardedFixture.prototype.runExecPhase = function runExecPhase(test) {
        jsTest.log("restarting shards...");
        try {
            for (var i = 0; i < this.nShards; ++i) {
                var opts = {queryableBackupMode: "", dbpath: this.paths[i]};

                assert.commandWorked(this.shardingTest["d" + i].getDB("local").dropDatabase());
                this.shardingTest.restartMongod(i, opts, () => {
                    makeDirectoryReadOnly(this.paths[i]);
                });
            }

            jsTest.log("restarting mongos...");

            this.shardingTest.restartMongos(0);

            test.exec(this.shardingTest.getDB("test")[test.name]);

            this.paths.forEach((path) => {
                makeDirectoryWritable(path);
            });

            this.shardingTest.stop();
        } finally {
            this.paths.forEach((path) => {
                makeDirectoryWritable(path);
            });
        }
    };

    runReadOnlyTest = function(test) {
        printjson(test);

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

        var fixtureType = TestData.fixture || "standalone";

        var fixture = null;
        if (fixtureType === "standalone") {
            fixture = new StandaloneFixture();
        } else if (fixtureType === "sharded") {
            fixture = new ShardedFixture();
        } else {
            throw new Error("fixtureType must be one of either 'standalone' or 'sharded'");
        }

        jsTest.log("starting load phase for test: " + test.name);
        fixture.runLoadPhase(test);

        jsTest.log("starting execution phase for test: " + test.name);
        fixture.runExecPhase(test);
    };

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

    zip2 = function * (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;
        }
    };
}());