|
| 1 | +const assert = require('assert'); |
| 2 | +const MongoClient = require('mongodb').MongoClient; |
| 3 | + |
| 4 | +const testConfig = require('../../config.json'); |
| 5 | +const MultipleBucketsPipelineFactory = |
| 6 | + require('../../../extensions/oplogPopulator/pipeline/MultipleBucketsPipelineFactory'); |
| 7 | + |
| 8 | +const mongoUrl = |
| 9 | + `mongodb://${testConfig.queuePopulator.mongo.replicaSetHosts}` + |
| 10 | + '/db?replicaSet=rs0'; |
| 11 | +const client = new MongoClient(mongoUrl, {}); |
| 12 | +const db = client.db('metadata', { ignoreUndefined: true }); |
| 13 | + |
| 14 | +const THRESHOLD = 100; |
| 15 | + |
| 16 | +describe('PipelineFactory', function () { |
| 17 | + this.timeout(30000); |
| 18 | + |
| 19 | + const collectionName = 'test-pipeline-stripping'; |
| 20 | + let collection; |
| 21 | + let setStage; |
| 22 | + |
| 23 | + before(async () => { |
| 24 | + await client.connect(); |
| 25 | + collection = db.collection(collectionName); |
| 26 | + |
| 27 | + const factory = new MultipleBucketsPipelineFactory(THRESHOLD); |
| 28 | + const pipeline = JSON.parse(factory.getPipeline(['test-bucket'])); |
| 29 | + setStage = pipeline[1]; |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + await collection.deleteMany({}); |
| 34 | + }); |
| 35 | + |
| 36 | + after(async () => { |
| 37 | + if (collection) { |
| 38 | + await collection.drop().catch(() => {}); |
| 39 | + } |
| 40 | + await client.close(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('fullDocument path (insert/replace events)', () => { |
| 44 | + it('should preserve location when content-length is missing', async () => { |
| 45 | + const doc = { fullDocument: { value: { location: [{ key: 'a' }] } } }; |
| 46 | + await collection.insertOne(doc); |
| 47 | + const results = await collection.aggregate([setStage]).toArray(); |
| 48 | + assert.strictEqual(results.length, 1); |
| 49 | + assert.deepStrictEqual(results[0].fullDocument.value.location, [{ key: 'a' }]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should preserve location when content-length is below threshold', async () => { |
| 53 | + const doc = { fullDocument: { value: { 'content-length': 50, 'location': [{ key: 'a' }] } } }; |
| 54 | + await collection.insertOne(doc); |
| 55 | + const results = await collection.aggregate([setStage]).toArray(); |
| 56 | + assert.strictEqual(results.length, 1); |
| 57 | + assert.deepStrictEqual(results[0].fullDocument.value.location, [{ key: 'a' }]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should preserve location when content-length is zero', async () => { |
| 61 | + const doc = { fullDocument: { value: { 'content-length': 0, 'location': [{ key: 'a' }] } } }; |
| 62 | + await collection.insertOne(doc); |
| 63 | + const results = await collection.aggregate([setStage]).toArray(); |
| 64 | + assert.strictEqual(results.length, 1); |
| 65 | + assert.deepStrictEqual(results[0].fullDocument.value.location, [{ key: 'a' }]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should strip location when content-length equals threshold', async () => { |
| 69 | + const doc = { fullDocument: { value: { 'content-length': THRESHOLD, 'location': [{ key: 'a' }] } } }; |
| 70 | + await collection.insertOne(doc); |
| 71 | + const results = await collection.aggregate([setStage]).toArray(); |
| 72 | + assert.strictEqual(results.length, 1); |
| 73 | + assert.strictEqual(results[0].fullDocument.value.location, undefined); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should strip location when content-length is above threshold', async () => { |
| 77 | + const doc = { fullDocument: { value: { 'content-length': 200, 'location': [{ key: 'a' }] } } }; |
| 78 | + await collection.insertOne(doc); |
| 79 | + const results = await collection.aggregate([setStage]).toArray(); |
| 80 | + assert.strictEqual(results.length, 1); |
| 81 | + assert.strictEqual(results[0].fullDocument.value.location, undefined); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('updateDescription path (update events)', () => { |
| 86 | + it('should preserve location when content-length is missing', async () => { |
| 87 | + const doc = { updateDescription: { updatedFields: { value: { location: [{ key: 'a' }] } } } }; |
| 88 | + await collection.insertOne(doc); |
| 89 | + const results = await collection.aggregate([setStage]).toArray(); |
| 90 | + assert.strictEqual(results.length, 1); |
| 91 | + assert.deepStrictEqual( |
| 92 | + results[0].updateDescription.updatedFields.value.location, |
| 93 | + [{ key: 'a' }] |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should preserve location when content-length is below threshold', async () => { |
| 98 | + const doc = { |
| 99 | + updateDescription: { updatedFields: { value: { 'content-length': 50, 'location': [{ key: 'a' }] } } }, |
| 100 | + }; |
| 101 | + await collection.insertOne(doc); |
| 102 | + const results = await collection.aggregate([setStage]).toArray(); |
| 103 | + assert.strictEqual(results.length, 1); |
| 104 | + assert.deepStrictEqual( |
| 105 | + results[0].updateDescription.updatedFields.value.location, |
| 106 | + [{ key: 'a' }] |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should preserve location when content-length is zero', async () => { |
| 111 | + const doc = { |
| 112 | + updateDescription: { updatedFields: { value: { 'content-length': 0, 'location': [{ key: 'a' }] } } }, |
| 113 | + }; |
| 114 | + await collection.insertOne(doc); |
| 115 | + const results = await collection.aggregate([setStage]).toArray(); |
| 116 | + assert.strictEqual(results.length, 1); |
| 117 | + assert.deepStrictEqual( |
| 118 | + results[0].updateDescription.updatedFields.value.location, |
| 119 | + [{ key: 'a' }] |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should strip location when content-length equals threshold', async () => { |
| 124 | + const value = { 'content-length': THRESHOLD, 'location': [{ key: 'a' }] }; |
| 125 | + const doc = { |
| 126 | + updateDescription: { updatedFields: { value } }, |
| 127 | + }; |
| 128 | + await collection.insertOne(doc); |
| 129 | + const results = await collection.aggregate([setStage]).toArray(); |
| 130 | + assert.strictEqual(results.length, 1); |
| 131 | + assert.strictEqual( |
| 132 | + results[0].updateDescription.updatedFields.value.location, |
| 133 | + undefined |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should strip location when content-length is above threshold', async () => { |
| 138 | + const doc = { |
| 139 | + updateDescription: { updatedFields: { value: { 'content-length': 200, 'location': [{ key: 'a' }] } } }, |
| 140 | + }; |
| 141 | + await collection.insertOne(doc); |
| 142 | + const results = await collection.aggregate([setStage]).toArray(); |
| 143 | + assert.strictEqual(results.length, 1); |
| 144 | + assert.strictEqual( |
| 145 | + results[0].updateDescription.updatedFields.value.location, |
| 146 | + undefined |
| 147 | + ); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments