Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(storage): add regression tests. #88

Merged
merged 2 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ To run the regression tests, first create and configure a project following the
After that, set the following environment variables:

- **GCLOUD_TESTS_PROJECT_ID**: Developers Console project's ID (e.g. bamboo-shift-455)
- **GCLOUD_TESTS_BUCKET_NAME**: The name of the bucket to use for the Cloud Storage API tests
- **GCLOUD_TESTS_KEY**: The path to the JSON key file.

Lastly, create the indexes used in the datastore regression tests using the [gcloud command-line tool](https://developers.google.com/cloud/sdk/gcloud/) and the indexes that you can find in `regression/data/index/yaml`:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"devDependencies" : {
"mocha": "*",
"istanbul": "*"
"istanbul": "*",
"tmp": "*"
},
"scripts": {
"test": "node_modules/mocha/bin/_mocha --reporter spec",
Expand Down
Binary file added regression/data/CloudPlatform_128px_Retina.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion regression/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

if (!process.env.GCLOUD_TESTS_PROJECT_ID &&
!process.env.GCLOUD_TESTS_BUCKET_NAME &&
!process.env.GCLOUD_TESTS_KEY) {
var error = ['To run the regression tests, you need to set the value of some environment variables.',
'Please check the README for instructions.'
Expand All @@ -24,5 +25,6 @@ if (!process.env.GCLOUD_TESTS_PROJECT_ID &&

module.exports = {
projectId: process.env.GCLOUD_TESTS_PROJECT_ID,
keyFilename: process.env.GCLOUD_TESTS_KEY
bucketName: process.env.GCLOUD_TESTS_BUCKET_NAME,
keyFilename: process.env.GCLOUD_TESTS_KEY,
};
188 changes: 188 additions & 0 deletions regression/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var assert = require('assert'),
async = require('async'),
crypto = require('crypto'),
fs = require('fs'),
tmp = require('tmp');

var env = require('./env.js'),
gcloud = require('../lib'),
bucket = new gcloud.storage.Bucket(env);

var pathToLogoFile = 'regression/data/CloudPlatform_128px_Retina.png',
logoFileMd5Hash;

describe('storage', function() {

describe('write, read and remove files', function() {

before(function(done) {
var md5sum = crypto.createHash('md5');
s = fs.ReadStream(pathToLogoFile);
s.on('data', function(d) {
md5sum.update(d);
});
s.on('error', done);
s.on('end', function() {
logoFileMd5Hash = md5sum.digest('base64');
done();
});
});

it('should write/remove from file', function(done) {
var fileName = 'CloudLogo';
bucket.write(fileName, { filename: pathToLogoFile }, function(err, fileObject, resp) {
if (err) { return done(err); }
assert.equal(fileObject.md5Hash, logoFileMd5Hash);
bucket.remove(fileName, done);
});
});

it('should write/remove from stream', function(done) {
var fileName = 'CloudLogo';
bucket.write(fileName, { data: fs.createReadStream(pathToLogoFile) },
function(err, fileObject) {
if (err) { return done(err); }
assert.equal(fileObject.md5Hash, logoFileMd5Hash);
bucket.remove(fileName, function(err) {
if (err) { return done(err); }
done();
});
});
});

it('should write/read/remove from a buffer', function(done) {
var fileName = 'MyBuffer',
fileContent = 'Hello World';
tmp.setGracefulCleanup();
tmp.file(function _tempFileCreated(err, path, fd) {
if (err) {return done(err)};
bucket.write(fileName, { data: fileContent }, function(err, fileObject) {
if (err) { return done(err); }
assert(fileObject);
var content = '';
bucket.createReadStream(fileName)
.pipe(fs.createWriteStream(path))
.on('error', done)
.on('complete', function(content) {
bucket.remove(fileName, function(err) {
if (err) { return done(err); }
fs.readFile(path, function(err, data) {
assert.equal(data, fileContent);
done();
});
});
});
});
});
});

it('should write and read metadata', function(done) {
var fileName = 'CloudLogo',
myMetadata = { contentType: 'image/png'};
bucket.write(fileName, { filename: pathToLogoFile, metadata: myMetadata },
function(err, fileObject) {
if (err) { return done(err); }
bucket.stat(fileName, function(err, metadata) {
if (err) { return done(err); }
assert.equal(metadata['contentType'], myMetadata['contentType']);
bucket.remove(fileName, function(err) {
if (err) { return done(err); }
done();
});
});
});
});

it('should copy an existing file', function(done) {
var fileName = 'CloudLogo',
copyName = 'CloudLogoCopy';

bucket.write(fileName, { filename: pathToLogoFile }, function(err, fileObject) {
if (err) { return done(err); }
bucket.copy(fileName, { name: copyName }, function() {
if (err) { return done(err); }
bucket.remove(copyName, function(err) {
if (err) { return done(err); }
bucket.remove(fileName, function(err) {
if (err) { return done(err); }
done();
});
});
});
});
});

});

describe('list files', function() {

var filenames = ['CloudLogo1', 'CloudLogo2', 'CloudLogo3'];

before(function(done) {
bucket.write(filenames[0], { filename: pathToLogoFile }, function(err, fileObject) {
if (err) { return done(err); }
bucket.copy(filenames[0], { name: filenames[1] }, function() {
if (err) { return done(err); }
bucket.copy(filenames[0], { name: filenames[2] }, function() {
if (err) { return done(err); }
done();
});
});
});
});

it('should list files', function(done) {
bucket.list(function(err, files, nextQuery) {
if (err) { return done(err); }
assert.equal(files.length, 3);
assert.equal(nextQuery, null);
done();
});
});

it('should paginate the list', function(done) {
bucket.list({ maxResults: 2 }, function(err, files, nextQuery) {
if (err) { return done(err); }
assert.equal(files.length, 2);
assert(nextQuery);
bucket.list(nextQuery, function(err, files, nextQuery) {
if (err) { return done(err); }
assert(files);
done();
});
});
});

after(function(done) {
async.parallel([
function(callback) {
bucket.remove(filenames[0], callback);
},
function(callback) {
bucket.remove(filenames[1], callback);
},
function(callback) {
bucket.remove(filenames[2], callback);
}
], done);
});

});

});