~ubuntu-branches/ubuntu/utopic/mongodb/utopic

« back to all changes in this revision

Viewing changes to jstests/multiVersion/libs/verify_collection_data.js

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-07-03 09:23:46 UTC
  • mfrom: (1.3.10) (44.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20140703092346-c5bvt46wnzougyly
Tags: 1:2.6.3-0ubuntu1
* New upstream stable release:
  - Dropped patches, included upstream:
    + 0003-All-platforms-but-Windows-find-hash-in-std-tr1.patch
    + 0008-Use-system-libstemmer.patch
    + 0011-Use-a-signed-char-to-store-BSONType-enumerations.patch
    + 0001-SERVER-12064-Atomic-operations-for-gcc-non-Intel-arc.patch
    + 0002-SERVER-12065-Support-ARM-and-AArch64-builds.patch
  - d/p/*: Refreshed/rebased remaining patches.
  - Use system provided libyaml-cpp:
    + d/control: Add libyaml-cpp-dev to BD's.
    + d/rules: Enable --with-system-yaml option.
    + d/p/fix-yaml-detection.patch: Fix detection of libyaml-cpp library.
  - d/mongodb-server.mongodb.upstart: Sync changes from upstream.
  - d/control,mongodb-dev.*: Drop mongodb-dev package; it has no reverse
    dependencies and upstream no longer install header files.
  - d/NEWS: Point users to upstream upgrade documentation for upgrades
    from 2.4 to 2.6.
* Merge from Debian unstable.
* d/control: BD on libv8-3.14-dev to ensure that transitioning to new v8
  versions is a explicit action due to changes in behaviour in >= 3.25
  (LP: #1295723).
* d/mongodb-server.prerm: Dropped debug echo call from maintainer script
  (LP: #1294455).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file contains test helpers to manage and validate collection state.  This is useful for
 
2
// round trip testing of entire collections.
 
3
//
 
4
// There are three stages represented in this file:
 
5
// 1.  Data generation - CollectionDataGenerator class.  This contains helpers to generate test data
 
6
//                       for a collection.
 
7
// 2.  Data persistence - createCollectionWithData function.  This class takes a
 
8
//                        CollectionDataGenerator and inserts the generated data into the given
 
9
//                        collection.
 
10
// 3.  Data validation - CollectionDataValidator class.  This class contains functions for saving
 
11
//                       the state of a collection and comparing a collection's state to the
 
12
//                       previously saved state.
 
13
//
 
14
// Common use case:
 
15
// 1.  Create a CollectionDataGenerator
 
16
// 2.  Save collection data using the createCollectionWithData function
 
17
// 3.  Record collection state in an instance of the CollectionDataValidator class
 
18
// 4.  Do round trip or other testing
 
19
// 5.  Validate that collection has not changed using the CollectionDataValidator class
 
20
 
 
21
load( './jstests/multiVersion/libs/data_generators.js' )
 
22
 
 
23
// Function to actually add the data generated by the given dataGenerator to a collection
 
24
createCollectionWithData = function (db, collectionName, dataGenerator) {
 
25
 
 
26
    // Drop collection if exists
 
27
    // TODO: add ability to control this
 
28
    db.getCollection(collectionName).drop();
 
29
 
 
30
    print("db.createCollection(\"" + collectionName + "\", " +
 
31
          JSON.stringify(dataGenerator.collectionMetadata.get()) + ");");
 
32
    assert.eq(db.createCollection(collectionName, dataGenerator.collectionMetadata.get()).ok, 1);
 
33
 
 
34
    var collection = db.getCollection(collectionName);
 
35
 
 
36
    var numIndexes = 0;
 
37
    while (dataGenerator.indexes.hasNext()) {
 
38
        var nextIndex = dataGenerator.indexes.next();
 
39
        print("collection.ensureIndex(" + JSON.stringify(nextIndex.spec) + ", " +
 
40
              JSON.stringify(nextIndex.options) + ");");
 
41
        var ensureIndexResult = collection.ensureIndex(nextIndex.spec, nextIndex.options);
 
42
        // XXX: Is this the real way to check for errors?
 
43
        assert(ensureIndexResult === undefined, JSON.stringify(ensureIndexResult));
 
44
        numIndexes++;
 
45
    }
 
46
 
 
47
    // Make sure we actually added all the indexes we thing we added.  +1 for the _id index.
 
48
    assert.eq(db.system.indexes.find({"ns" : db.toString() + "." + collection.getName()}).count(),
 
49
              numIndexes + 1);
 
50
 
 
51
    var numInserted = 0;
 
52
    while (dataGenerator.data.hasNext()) {
 
53
        var nextDoc = dataGenerator.data.next();
 
54
        // Use _id as our ordering field just so we don't have to deal with sorting.  This only
 
55
        // matters here since we can use indexes
 
56
        nextDoc._id = numInserted;
 
57
        print("collection.insert(" + JSON.stringify(nextDoc) + ");");
 
58
        var insertResult = collection.insert(nextDoc);
 
59
        assert(db.getLastError() == null);
 
60
        numInserted++;
 
61
    }
 
62
 
 
63
    assert.eq(collection.find().count(), numInserted, "counts not equal after inserts");
 
64
 
 
65
    return db.getCollection(collectionName);
 
66
}
 
67
 
 
68
// Class to save the state of a collection and later compare the current state of a collection to
 
69
// the saved state
 
70
function CollectionDataValidator() {
 
71
 
 
72
    var initialized = false;
 
73
    var collectionStats = {};
 
74
    var indexData = [];
 
75
    var collectionData = [];
 
76
 
 
77
    // Saves the current state of the collection passed in
 
78
    this.recordCollectionData = function (collection) {
 
79
 
 
80
        // Save the indexes for this collection for later comparison
 
81
        indexData = collection.getDB().system.indexes.find({"ns" : collection.getFullName()}).sort({"name":1}).toArray();
 
82
 
 
83
        // Save the data for this collection for later comparison
 
84
        collectionData = collection.find().sort({"_id":1}).toArray();
 
85
 
 
86
        // Save the metadata for this collection for later comparison.
 
87
        // NOTE: We do this last since the data and indexes affect this output
 
88
        collectionStats = collection.stats();
 
89
 
 
90
        // XXX: in 2.4 avgObjSize was a double, but in 2.6 it is an int
 
91
        collectionStats['avgObjSize'] = Math.floor(collectionStats['avgObjSize']);
 
92
 
 
93
        initialized = true;
 
94
 
 
95
        return collection;
 
96
    }
 
97
 
 
98
    this.validateCollectionData = function (collection) {
 
99
 
 
100
        if (!initialized) {
 
101
            throw "validateCollectionWithAllData called, but data is not initialized";
 
102
        }
 
103
 
 
104
        // Get the metadata for this collection
 
105
        var newCollectionStats = collection.stats();
 
106
 
 
107
        // XXX: in 2.4 avgObjSize was a double, but in 2.6 it is an int
 
108
        newCollectionStats['avgObjSize'] = Math.floor(newCollectionStats['avgObjSize']);
 
109
 
 
110
        assert.docEq(collectionStats, newCollectionStats, "collection metadata not equal");
 
111
 
 
112
        // Get the indexes for this collection
 
113
        var newIndexData = collection.getDB().system.indexes.find({"ns" : collection.getFullName()}).sort({"name":1}).toArray();
 
114
        for (var i = 0; i < newIndexData.length; i++) {
 
115
            assert.docEq(indexData[i], newIndexData[i], "indexes not equal");
 
116
        }
 
117
 
 
118
        // Save the data for this collection for later comparison
 
119
        var newCollectionData = collection.find().sort({"_id":1}).toArray();
 
120
        for (var i = 0; i < newCollectionData.length; i++) {
 
121
            assert.docEq(collectionData[i], newCollectionData[i], "data not equal");
 
122
        }
 
123
        return true;
 
124
    }
 
125
}
 
126
 
 
127
// Tests of the functions and classes in this file
 
128
function collectionDataValidatorTests() {
 
129
 
 
130
    // TODO: These tests are hackish and depend on implementation details, but they are good enough
 
131
    // for now to convince us that the CollectionDataValidator is actually checking something
 
132
    var myValidator;
 
133
    var myGenerator;
 
134
    var collection;
 
135
 
 
136
    myGenerator = new CollectionDataGenerator({ "capped" : true });
 
137
    collection = createCollectionWithData(db, "test", myGenerator);
 
138
    myValidator = new CollectionDataValidator();
 
139
    myValidator.recordCollectionData(collection);
 
140
    db.test.dropIndex(db.system.indexes.findOne({"key.a": { "$exists" : true } }).key);
 
141
    assert.throws(myValidator.validateCollectionData, [collection], "Validation function should have thrown since we modified the collection");
 
142
 
 
143
 
 
144
    myGenerator = new CollectionDataGenerator({ "capped" : true });
 
145
    collection = createCollectionWithData(db, "test", myGenerator);
 
146
    myValidator = new CollectionDataValidator();
 
147
    myValidator.recordCollectionData(collection);
 
148
    db.test.update({_id:0}, {dummy:1});
 
149
    assert.throws(myValidator.validateCollectionData, [collection], "Validation function should have thrown since we modified the collection");
 
150
 
 
151
 
 
152
    myGenerator = new CollectionDataGenerator({ "capped" : true });
 
153
    collection = createCollectionWithData(db, "test", myGenerator);
 
154
    myValidator = new CollectionDataValidator();
 
155
    myValidator.recordCollectionData(collection);
 
156
    assert(myValidator.validateCollectionData(collection), "Validation function failed");
 
157
 
 
158
    myGenerator = new CollectionDataGenerator({ "capped" : false });
 
159
    collection = createCollectionWithData(db, "test", myGenerator);
 
160
    myValidator = new CollectionDataValidator();
 
161
    myValidator.recordCollectionData(collection);
 
162
    db.test.dropIndex(db.system.indexes.findOne({"key.a": { "$exists" : true } }).key);
 
163
    assert.throws(myValidator.validateCollectionData, [collection], "Validation function should have thrown since we modified the collection");
 
164
 
 
165
 
 
166
    myGenerator = new CollectionDataGenerator({ "capped" : false });
 
167
    collection = createCollectionWithData(db, "test", myGenerator);
 
168
    myValidator = new CollectionDataValidator();
 
169
    myValidator.recordCollectionData(collection);
 
170
    db.test.update({_id:0}, {dummy:1});
 
171
    assert.throws(myValidator.validateCollectionData, [collection], "Validation function should have thrown since we modified the collection");
 
172
 
 
173
 
 
174
    myGenerator = new CollectionDataGenerator({ "capped" : false });
 
175
    collection = createCollectionWithData(db, "test", myGenerator);
 
176
    myValidator = new CollectionDataValidator();
 
177
    myValidator.recordCollectionData(collection);
 
178
    assert(myValidator.validateCollectionData(collection), "Validation function failed");
 
179
 
 
180
    print("collection data validator tests passed!");
 
181
}