2016-10-10 18:01:36 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-10 21:22:50 +08:00
|
|
|
require('../helper');
|
|
|
|
var assert = require('assert');
|
2019-10-04 00:24:39 +08:00
|
|
|
var appServer = require('../../lib/server');
|
|
|
|
var redisUtils = require('./redis-utils');
|
2016-10-10 18:01:36 +08:00
|
|
|
var debug = require('debug')('batch-test-client');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var JobStatus = require('../../lib/batch/job-status');
|
2016-10-17 21:02:34 +08:00
|
|
|
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
|
2019-10-04 00:24:39 +08:00
|
|
|
var batchFactory = require('../../lib/batch/index');
|
2016-10-10 18:01:36 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function response (code) {
|
2016-10-10 18:01:36 +08:00
|
|
|
return {
|
|
|
|
status: code
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var RESPONSE = {
|
|
|
|
OK: response(200),
|
2016-11-24 03:46:19 +08:00
|
|
|
CREATED: response(201),
|
|
|
|
BAD_REQUEST: response(400)
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function BatchTestClient (config) {
|
2016-10-10 18:01:36 +08:00
|
|
|
this.config = config || {};
|
|
|
|
this.server = appServer();
|
|
|
|
|
2016-10-17 21:02:34 +08:00
|
|
|
this.batch = batchFactory(metadataBackend, redisUtils.getPool(), this.config.name);
|
2016-10-10 18:01:36 +08:00
|
|
|
this.batch.start();
|
|
|
|
|
|
|
|
this.pendingJobs = [];
|
|
|
|
this.ready = false;
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batch.on('ready', function () {
|
2016-10-10 18:01:36 +08:00
|
|
|
this.ready = true;
|
2019-12-24 01:19:08 +08:00
|
|
|
this.pendingJobs.forEach(function (pendingJob) {
|
2018-02-14 02:47:00 +08:00
|
|
|
this.createJob(pendingJob.job, pendingJob.override, pendingJob.callback);
|
2016-10-10 18:01:36 +08:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BatchTestClient;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.isReady = function () {
|
2016-10-10 18:01:36 +08:00
|
|
|
return this.ready;
|
|
|
|
};
|
|
|
|
|
2018-02-14 02:47:00 +08:00
|
|
|
BatchTestClient.prototype.getExpectedResponse = function (override) {
|
|
|
|
return override.response || this.config.response || RESPONSE.CREATED;
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.createJob = function (job, override, callback) {
|
2016-10-17 16:52:35 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
2016-10-10 18:01:36 +08:00
|
|
|
if (!this.isReady()) {
|
|
|
|
this.pendingJobs.push({
|
|
|
|
job: job,
|
2018-02-14 02:47:00 +08:00
|
|
|
override: override || {},
|
2016-10-10 18:01:36 +08:00
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
return debug('Waiting for Batch service to be ready');
|
|
|
|
}
|
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-10-17 16:52:35 +08:00
|
|
|
url: this.getUrl(override),
|
2016-10-10 18:01:36 +08:00
|
|
|
headers: {
|
2016-10-17 16:52:35 +08:00
|
|
|
host: this.getHost(override),
|
2018-02-16 18:52:57 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
authorization: this.getAuthorization(override)
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: JSON.stringify(job)
|
|
|
|
},
|
2018-02-14 02:47:00 +08:00
|
|
|
this.getExpectedResponse(override),
|
2016-10-10 18:01:36 +08:00
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-06-05 21:49:15 +08:00
|
|
|
|
|
|
|
if (res.statusCode < 400) {
|
2019-02-27 17:09:08 +08:00
|
|
|
return callback(null, new JobResult(JSON.parse(res.body), this, override), res);
|
2018-06-05 21:49:15 +08:00
|
|
|
} else {
|
|
|
|
return callback(null, res);
|
|
|
|
}
|
2016-10-10 18:01:36 +08:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.getJobStatus = function (jobId, override, callback) {
|
2016-10-10 18:01:36 +08:00
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-10-17 16:52:35 +08:00
|
|
|
url: this.getUrl(override, jobId),
|
2016-10-10 18:01:36 +08:00
|
|
|
headers: {
|
2018-02-16 18:52:57 +08:00
|
|
|
host: this.getHost(override),
|
|
|
|
authorization: this.getAuthorization(override)
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
2016-11-23 19:30:16 +08:00
|
|
|
method: 'GET',
|
|
|
|
timeout: override.timeout
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
|
|
|
RESPONSE.OK,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, JSON.parse(res.body));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.getWorkInProgressJobs = function (override, callback) {
|
2016-10-28 21:08:42 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2017-09-20 22:17:33 +08:00
|
|
|
url: '/api/v1/jobs-wip',
|
2016-10-28 21:08:42 +08:00
|
|
|
headers: {
|
|
|
|
host: this.getHost(override)
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
RESPONSE.OK,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, JSON.parse(res.body));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.cancelJob = function (jobId, override, callback) {
|
2016-10-10 18:01:36 +08:00
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-11-24 02:20:44 +08:00
|
|
|
url: this.getUrl(override, jobId),
|
2016-10-10 18:01:36 +08:00
|
|
|
headers: {
|
2016-10-17 16:52:35 +08:00
|
|
|
host: this.getHost(override)
|
2016-10-10 18:01:36 +08:00
|
|
|
},
|
|
|
|
method: 'DELETE'
|
|
|
|
},
|
2016-11-24 03:46:19 +08:00
|
|
|
override.statusCode,
|
2016-10-10 18:01:36 +08:00
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, JSON.parse(res.body));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.drain = function (callback) {
|
|
|
|
this.batch.stop(function () {
|
2016-10-12 22:43:18 +08:00
|
|
|
return redisUtils.clean('batch:*', callback);
|
|
|
|
});
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.getHost = function (override) {
|
2016-10-17 16:52:35 +08:00
|
|
|
return override.host || this.config.host || 'vizzuality.cartodb.com';
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2018-02-16 18:52:57 +08:00
|
|
|
BatchTestClient.prototype.getAuthorization = function (override) {
|
|
|
|
const auth = override.authorization || this.config.authorization;
|
|
|
|
|
|
|
|
if (auth) {
|
|
|
|
return `Basic ${new Buffer(auth).toString('base64')}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.getUrl = function (override, jobId) {
|
2016-10-10 18:01:36 +08:00
|
|
|
var urlParts = ['/api/v2/sql/job'];
|
|
|
|
if (jobId) {
|
|
|
|
urlParts.push(jobId);
|
|
|
|
}
|
2018-02-16 18:52:57 +08:00
|
|
|
return `${urlParts.join('/')}${override.anonymous ? '' : '?api_key=' + this.getApiKey(override)}`;
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BatchTestClient.prototype.getApiKey = function (override) {
|
2016-10-17 16:52:35 +08:00
|
|
|
return override.apiKey || this.config.apiKey || '1234';
|
|
|
|
};
|
2016-10-10 18:01:36 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
/** **************** JobResult ******************/
|
2016-10-10 18:01:36 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function JobResult (job, batchTestClient, override) {
|
2016-10-10 18:01:36 +08:00
|
|
|
this.job = job;
|
|
|
|
this.batchTestClient = batchTestClient;
|
2016-10-17 16:52:35 +08:00
|
|
|
this.override = override;
|
2016-10-10 18:01:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
JobResult.prototype.getStatus = function (requiredStatus, callback) {
|
2016-11-24 02:20:44 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = requiredStatus;
|
|
|
|
requiredStatus = undefined;
|
|
|
|
}
|
|
|
|
|
2016-10-10 18:01:36 +08:00
|
|
|
var self = this;
|
2016-11-22 21:26:21 +08:00
|
|
|
var attempts = 1;
|
2016-11-23 19:30:16 +08:00
|
|
|
self.override.timeout = 1000;
|
|
|
|
|
2016-10-10 18:01:36 +08:00
|
|
|
var interval = setInterval(function () {
|
2016-10-17 16:52:35 +08:00
|
|
|
self.batchTestClient.getJobStatus(self.job.job_id, self.override, function (err, job) {
|
2016-10-10 18:01:36 +08:00
|
|
|
if (err) {
|
|
|
|
clearInterval(interval);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-11-22 21:26:21 +08:00
|
|
|
attempts += 1;
|
|
|
|
|
2016-11-24 02:20:44 +08:00
|
|
|
if (attempts > 20) {
|
2016-11-22 21:26:21 +08:00
|
|
|
clearInterval(interval);
|
2016-11-24 20:09:33 +08:00
|
|
|
return callback(new Error('Reached maximum number of request (20) to check job status'));
|
2016-11-22 21:26:21 +08:00
|
|
|
}
|
2016-10-10 18:01:36 +08:00
|
|
|
|
2016-11-24 02:20:44 +08:00
|
|
|
if (hasRequiredStatus(job, requiredStatus)) {
|
2016-10-10 18:01:36 +08:00
|
|
|
clearInterval(interval);
|
2016-11-24 20:25:14 +08:00
|
|
|
self.job = job;
|
2016-10-10 18:01:36 +08:00
|
|
|
return callback(null, job);
|
|
|
|
} else {
|
|
|
|
debug('Job %s [status=%s] waiting to be done', self.job.job_id, job.status);
|
|
|
|
}
|
|
|
|
});
|
2016-11-24 02:20:44 +08:00
|
|
|
}, 100);
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function hasRequiredStatus (job, requiredStatus) {
|
2016-11-24 02:20:44 +08:00
|
|
|
if (requiredStatus) {
|
|
|
|
return job.status === requiredStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JobStatus.isFinal(job.status)) {
|
|
|
|
if (job.fallback_status !== undefined) {
|
|
|
|
if (JobStatus.isFinal(job.fallback_status) || job.fallback_status === JobStatus.SKIPPED) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 03:46:19 +08:00
|
|
|
JobResult.prototype.cancel = function (callback) {
|
2016-11-24 22:40:09 +08:00
|
|
|
var self = this;
|
2016-11-24 03:46:19 +08:00
|
|
|
this.override.statusCode = response(RESPONSE.OK);
|
2016-11-24 22:40:09 +08:00
|
|
|
this.batchTestClient.cancelJob(this.job.job_id, this.override, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
self.job = job;
|
|
|
|
callback(null, job);
|
|
|
|
});
|
2016-11-24 03:46:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobResult.prototype.tryCancel = function (callback) {
|
2016-11-24 22:40:09 +08:00
|
|
|
var self = this;
|
2016-11-24 03:46:19 +08:00
|
|
|
this.override.statusCode = response();
|
2016-11-24 22:40:09 +08:00
|
|
|
this.batchTestClient.cancelJob(this.job.job_id, this.override, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
self.job = job;
|
|
|
|
callback(null, job);
|
|
|
|
});
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
2016-11-24 20:25:14 +08:00
|
|
|
|
2016-11-24 22:40:09 +08:00
|
|
|
JobResult.prototype.validateExpectedResponse = function (expected) {
|
|
|
|
var actual = this.job.query;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
actual.query.forEach(function (actualQuery, index) {
|
2016-11-24 20:25:14 +08:00
|
|
|
var expectedQuery = expected.query[index];
|
|
|
|
assert.ok(expectedQuery);
|
2019-12-24 01:19:08 +08:00
|
|
|
Object.keys(expectedQuery).forEach(function (expectedKey) {
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(
|
2016-11-24 20:25:14 +08:00
|
|
|
actualQuery[expectedKey],
|
|
|
|
expectedQuery[expectedKey],
|
|
|
|
'Expected value for key "' + expectedKey + '" does not match: ' + actualQuery[expectedKey] + ' ==' +
|
|
|
|
expectedQuery[expectedKey] + ' at query index=' + index + '. Full response: ' +
|
|
|
|
JSON.stringify(actual, null, 4)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
var propsToCheckDate = ['started_at', 'ended_at'];
|
2019-12-24 01:19:08 +08:00
|
|
|
propsToCheckDate.forEach(function (propToCheckDate) {
|
2016-11-24 20:25:14 +08:00
|
|
|
if (actualQuery.hasOwnProperty(propToCheckDate)) {
|
|
|
|
assert.ok(new Date(actualQuery[propToCheckDate]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(actual.onsuccess, expected.onsuccess);
|
|
|
|
assert.strictEqual(actual.onerror, expected.onerror);
|
2016-11-24 20:25:14 +08:00
|
|
|
};
|