From 3bec28d7fa561d46e2c2850cd613ae393c876c7e Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 29 Aug 2014 11:49:45 -0400 Subject: [PATCH] tests: storage: refactor + bump offset. --- regression/storage.js | 81 +++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/regression/storage.js b/regression/storage.js index c16a1f7930a..03e41830981 100644 --- a/regression/storage.js +++ b/regression/storage.js @@ -40,13 +40,11 @@ var files = { }; function setHash(obj, file, done) { - var md5sum = crypto.createHash('md5'); + var hash = crypto.createHash('md5'); fs.createReadStream(obj[file].path) - .on('data', function(d) { - md5sum.update(d); - }) + .on('data', hash.update.bind(hash)) .on('end', function() { - obj[file].hash = md5sum.digest('base64'); + obj[file].hash = hash.digest('base64'); done(); }); } @@ -57,7 +55,7 @@ describe('storage', function() { var doneCalled = 0; Object.keys(files).forEach(function(file) { setHash(files, file, function() { - if (++doneCalled === 2) { + if (++doneCalled === Object.keys(files).length) { done(); } }); @@ -66,29 +64,27 @@ describe('storage', function() { describe('stream write', function() { it('should stream write, then remove large file (5mb)', function(done) { - var fileName = 'LargeFile'; - + var filename = 'LargeFile'; fs.createReadStream(files.big.path) - .pipe(bucket.createWriteStream(fileName)) + .pipe(bucket.createWriteStream(filename)) .on('error', done) .on('complete', function(fileObject) { assert.equal(fileObject.md5Hash, files.big.hash); - bucket.remove(fileName, done); + bucket.remove(filename, done); }); }); it('should write and read metadata', function(done) { - var fileName = 'CloudLogo'; + var filename = 'CloudLogo'; var myMetadata = { contentType: 'image/png' }; - fs.createReadStream(files.logo.path) - .pipe(bucket.createWriteStream(fileName, myMetadata)) + .pipe(bucket.createWriteStream(filename, myMetadata)) .on('error', done) .on('complete', function() { - bucket.stat(fileName, function(err, metadata) { + bucket.stat(filename, function(err, metadata) { assert.ifError(err); assert.equal(metadata.contentType, myMetadata.contentType); - bucket.remove(fileName, function(err) { + bucket.remove(filename, function(err) { assert.ifError(err); done(); }); @@ -97,19 +93,19 @@ describe('storage', function() { }); it('should write/read/remove from a buffer', function(done) { - var fileName = 'MyBuffer'; + var filename = 'MyBuffer'; var fileContent = 'Hello World'; tmp.setGracefulCleanup(); tmp.file(function _tempFileCreated(err, path) { assert.ifError(err); - bucket.write(fileName, fileContent, function(err, fileObject) { + bucket.write(filename, fileContent, function(err, fileObject) { assert.ifError(err); assert(fileObject); - bucket.createReadStream(fileName) + bucket.createReadStream(filename) .pipe(fs.createWriteStream(path)) .on('error', done) .on('finish', function() { - bucket.remove(fileName, function(err) { + bucket.remove(filename, function(err) { assert.ifError(err); fs.readFile(path, function(err, data) { assert.equal(data, fileContent); @@ -123,17 +119,16 @@ describe('storage', function() { }); it('should copy an existing file', function(done) { - var fileName = 'CloudLogo'; + var filename = 'CloudLogo'; var copyName = 'CloudLogoCopy'; - fs.createReadStream(files.logo.path) - .pipe(bucket.createWriteStream(fileName)) + .pipe(bucket.createWriteStream(filename)) .on('error', done) .on('complete', function() { - bucket.copy(fileName, { name: copyName }, function(err) { + bucket.copy(filename, { name: copyName }, function(err) { assert.ifError(err); async.parallel([ - bucket.remove.bind(bucket, fileName), + bucket.remove.bind(bucket, filename), bucket.remove.bind(bucket, copyName) ], done); }); @@ -151,10 +146,7 @@ describe('storage', function() { .on('complete', function() { bucket.copy(filenames[0], { name: filenames[1] }, function(err) { assert.ifError(err); - bucket.copy(filenames[0], { name: filenames[2] }, function(err) { - assert.ifError(err); - done(); - }); + bucket.copy(filenames[0], { name: filenames[2] }, done); }); }); }); @@ -192,11 +184,11 @@ describe('storage', function() { }); describe('sign urls', function() { - var fileName = 'LogoToSign.jpg'; + var filename = 'LogoToSign.jpg'; beforeEach(function(done) { fs.createReadStream(files.logo.path) - .pipe(bucket.createWriteStream(fileName)) + .pipe(bucket.createWriteStream(filename)) .on('error', done) .on('complete', done.bind(null, null)); }); @@ -205,12 +197,12 @@ describe('storage', function() { var signedReadUrl = bucket.getSignedUrl({ action: 'read', expires: Math.round(Date.now() / 1000) + 5, - resource: fileName + resource: filename }); var localFile = fs.readFileSync(files.logo.path); request.get(signedReadUrl, function(err, resp, body) { assert.equal(body, localFile); - bucket.remove(fileName, done); + bucket.remove(filename, done); }); }); @@ -218,11 +210,11 @@ describe('storage', function() { var signedDeleteUrl = bucket.getSignedUrl({ action: 'delete', expires: Math.round(Date.now() / 1000) + 5, - resource: fileName + resource: filename }); request.del(signedDeleteUrl, function(err, resp) { assert.equal(resp.statusCode, 204); - bucket.stat(fileName, function(err) { + bucket.stat(filename, function(err) { assert.equal(err.code, 404); done(); }); @@ -230,22 +222,21 @@ describe('storage', function() { }); it('should allow control of expiration', function(done) { - var offsetSeconds = 5; + var OFFSET_SECONDS = 5; var signedReadUrl = bucket.getSignedUrl({ action: 'read', - expires: Math.round(Date.now() / 1000) + offsetSeconds, - resource: fileName + expires: Math.round(Date.now() / 1000) + OFFSET_SECONDS, + resource: filename }); - var localFile = fs.readFileSync(files.logo.path); request.get(signedReadUrl, function(err, resp, body) { - assert.equal(body, localFile); + assert.equal(body, fs.readFileSync(files.logo.path)); + setTimeout(function() { + request.get(signedReadUrl, function(err, resp) { + assert.equal(resp.statusCode, 400); + bucket.remove(filename, done); + }); + }, (OFFSET_SECONDS + 5) * 1000); }); - setTimeout(function() { - request.get(signedReadUrl, function(err, resp) { - assert.equal(resp.statusCode, 400); - bucket.remove(fileName, done); - }); - }, (offsetSeconds + 1) * 1000); }); }); });