Skip to content

Commit

Permalink
WIP: kart test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
pazdera committed Dec 22, 2017
1 parent 8aaabe4 commit b2f281a
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 101 deletions.
36 changes: 16 additions & 20 deletions lib/archive.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const aws = require('aws-sdk'),
TarGz = require('tar.gz'),
const TarGz = require('tar.gz'),
semver = require('semver'),
config = require('./config'),
Build = require('./data').Build;

Build = require('./data').Build,
s3 = require('./s3-instance');

function _getNextBuildNumber(projectName, channel, version) {
let opts = {
Expand All @@ -16,7 +15,7 @@ function _getNextBuildNumber(projectName, channel, version) {
},
limit: 1
};

return list(projectName, channel, opts).then((res) => {
if (res.length > 0) {
return res[0].number + 1;
Expand All @@ -28,7 +27,7 @@ function _getNextBuildNumber(projectName, channel, version) {

/*
* Compress and upload a build to the apropriate place in the archive.
*
*
* @param {String} buildDir Path to a directory to be archived (top level dir won't be included).
* @param {String} projectName Name of the project being archived (e.g., kano-code).
* @param {String} channel Target channel (e.g., staging).
Expand Down Expand Up @@ -70,7 +69,6 @@ function _doStoreBuild(buildDir, projectName, channel, version, number, arch, me
buildDate: new Date()
});

let s3 = new aws.S3();
s3.upload(
{
Bucket: config.local.rootBucket.name,
Expand All @@ -92,17 +90,16 @@ function _doStoreBuild(buildDir, projectName, channel, version, number, arch, me

/*
* Remove a build from the archive by path or build object.
*
*
* @param {String, Object} build Either a direct path to build or a build object.
*
*
*/
function removeBuild(build) {
if (typeof build === 'string') {
build = {path: build};
}

return new Promise((resolve, reject) => {
let s3 = new aws.S3();
s3.deleteObject(
{
Bucket: config.local.rootBucket.name,
Expand Down Expand Up @@ -146,13 +143,13 @@ function _getComparator(opts) {
if (opts.sort) {
let keys,
order = opts.sort.order || 1;

if (!Array.isArray(opts.sort.key)) {
keys = [opts.sort.key];
} else {
keys = opts.sort.key.slice(0);
}

while (res === 0 && keys.length) {
let key = keys.shift();

Expand All @@ -175,36 +172,35 @@ function _getComparator(opts) {
}
}
}

return res;
};
}

/*
* List builds of a project in a channel.
*
*
* @param {String} project The name of the project (e.g., kano-code).
* @param {String} channel The name of the channel (e.g., staging).
*
*
* @param {Object} opts (optional) Modify the listing results.
* @param {Object} opts.filter Filtering options (key value pairs).
* Filtering based on metadata not supported at the moment.
*
*
* @param {Object} opts.sort Sorting options.
* @param {Sring, Array} opts.sort.key One or more keys to sort by in order of priority
* @param {Number} opts.sort.order 1 for ascending, -1 for descending.
*
*
* @param {Number} opts.limit Limit the list to N entries.
*
*/
function list(project, channel, opts) {
opts = Object.assign({}, opts);

return new Promise((resolve, reject) => {
let folder = `${project}/${channel}/`,
results;

let s3 = new aws.S3();

s3.listObjects(
{
Bucket: config.local.rootBucket.name,
Expand Down
7 changes: 3 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const aws = require('aws-sdk'),
fs = require('fs'),
os = require('os');
const fs = require('fs'),
os = require('os')
s3 = require('./s3-instance');

let configured = false,
localConfig = {},
Expand Down Expand Up @@ -40,7 +40,6 @@ function configGuard(func) {

function _downloadRemoteConfig(root) {
return new Promise((resolve, reject) => {
let s3 = new aws.S3();
s3.getObject(
{
Bucket: root.name,
Expand Down
26 changes: 11 additions & 15 deletions lib/deploy-methods/s3.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const aws = require('aws-sdk'),
config = require('../config'),
const config = require('../config'),
Release = require('../data').Release,
mime = require('mime-types'),
gunzip = require('gunzip-maybe'),
tarStream = require('tar-stream');

tarStream = require('tar-stream'),
s3 = require('../s3-instance');

function _doRelease(build, target, algorithm) {
return new Promise((resolve, reject) => {
let s3 = new aws.S3(),
bucketMap = {},
let bucketMap = {},
extract = tarStream.extract(),
dataStream = s3.getObject({
Bucket: config.local.rootBucket.name,
Expand All @@ -20,7 +18,6 @@ function _doRelease(build, target, algorithm) {
reject('Download failed: ' + error);
});


_listAllObjects({
Bucket: target
}).then((entries) => {
Expand All @@ -38,6 +35,7 @@ function _doRelease(build, target, algorithm) {
let entry = bucketMap[header.name];

if (entry) {
entry.processed = true;
if (entry.Size === header.size && entry.LastModified === header.mtime) {
console.log('Skipping ' + header.name);
stream.resume();
Expand Down Expand Up @@ -95,25 +93,25 @@ function _doRelease(build, target, algorithm) {

function _listAllObjects(params, out = []) {
return new Promise((resolve, reject) => {
let s3 = new aws.S3();
s3.listObjectsV2(params, (err, data) => {
if (err) {
return reject(err);
}

s3.listObjectsV2(params).promise()
.then(data => {
out.push(...data.Contents);
out.push(...data.Contents);

if (data.IsTruncated) {
let newParams = Object.assign(params, {ContinuationToken: data.NextContinuationToken});
resolve(_listAllObjects(newParams, out));
} else {
resolve(out);
}
}).catch(reject);
})
});
}

function _uploadKartFile(release, target) {
return new Promise((resolve, reject) => {
let s3 = new aws.S3();
s3.upload(
{
Bucket: target,
Expand All @@ -133,7 +131,6 @@ function _uploadKartFile(release, target) {

function _downloadKartFile(project, channel) {
return new Promise((resolve, reject) => {
let s3 = new aws.S3();
s3.getObject(
{
Bucket: config.remote.projects[project].channels[channel].deploy.bucket,
Expand All @@ -158,7 +155,6 @@ function _deleteEntries(bucketName, entries) {
let p = entries.map((entry) => {
return new Promise((resolve, reject) => {
if (!entry.Key.match(/\/$/)) {
let s3 = new aws.S3();
s3.deleteObject(
{
Bucket: bucketName,
Expand Down
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const aws = require('aws-sdk'),
semver = require('semver'),
const semver = require('semver'),
config = require('./config'),
Build = require('./data').Build,
Release = require('./data').Release,
deployMethods = require('./deploy-methods'),
archive = require('./archive');
archive = require('./archive'),
s3 = require('./s3-instance');

function getProjects() {
return Promise.resolve(config.remote.projects);
Expand Down Expand Up @@ -49,6 +49,7 @@ function getMOTD() {
return Promise.resolve(motd);
}


module.exports = {
configure: config.configure,
archive: {
Expand All @@ -59,5 +60,7 @@ module.exports = {
getProjects: config.guard(getProjects),
getMOTD: config.guard(getMOTD),
release: config.guard(release),
status: config.guard(status)
status: config.guard(status),

__mockS3API: s3.__mockS3API
};
13 changes: 13 additions & 0 deletions lib/s3-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const aws = require('aws-sdk');

/* Keep a global reference se we can
override it with a mock for testing. */
let s3 = new aws.S3();

function __mockS3API(mock) {
s3 = mock;
s3.__mockS3API = __mockS3API;
}

s3.__mockS3API = __mockS3API;
module.exports = s3;
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@
},
"homepage": "https://github.com/KanoComputing/kart#readme",
"dependencies": {
"async": "^2.6.0",
"aws-sdk": "^2.111.0",
"colors": "^1.1.2",
"git-rev-sync": "^1.9.1",
"gunzip-maybe": "^1.4.1",
"inquirer": "^3.2.3",
"mime-types": "^2.1.17",
"semver": "^5.4.1",
"tar.gz": "^1.0.7",
"tar-stream": "^1.5.5",
"tar.gz": "^1.0.7",
"update-notifier": "^2.3.0",
"yargs": "^8.0.2"
},
"devDependencies": {
"mocha": "^4.0.1",
"mock-aws-s3": "^2.6.0",
"tmp": "0.0.33"
}
}
Loading

0 comments on commit b2f281a

Please sign in to comment.