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
|
|
|
|
|
|
|
function response(code) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function BatchTestClient(config) {
|
|
|
|
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;
|
|
|
|
this.batch.on('ready', function() {
|
|
|
|
this.ready = true;
|
|
|
|
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;
|
|
|
|
|
|
|
|
BatchTestClient.prototype.isReady = function() {
|
|
|
|
return this.ready;
|
|
|
|
};
|
|
|
|
|
2018-02-14 02:47:00 +08:00
|
|
|
BatchTestClient.prototype.getExpectedResponse = function (override) {
|
|
|
|
return override.response || this.config.response || RESPONSE.CREATED;
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.createJob = function(job, override, callback) {
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +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));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-28 21:08:42 +08:00
|
|
|
BatchTestClient.prototype.getWorkInProgressJobs = function(override, callback) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +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));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
BatchTestClient.prototype.drain = function(callback) {
|
2016-10-12 22:43:18 +08:00
|
|
|
this.batch.stop(function() {
|
|
|
|
return redisUtils.clean('batch:*', callback);
|
|
|
|
});
|
2016-10-10 18:01:36 +08:00
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getHost = function(override) {
|
|
|
|
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')}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +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
|
|
|
};
|
|
|
|
|
2016-10-17 16:52:35 +08:00
|
|
|
BatchTestClient.prototype.getApiKey = function(override) {
|
|
|
|
return override.apiKey || this.config.apiKey || '1234';
|
|
|
|
};
|
2016-10-10 18:01:36 +08:00
|
|
|
|
|
|
|
/****************** JobResult ******************/
|
|
|
|
|
|
|
|
|
2016-10-17 16:52:35 +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
|
|
|
}
|
|
|
|
|
2016-11-24 02:20:44 +08:00
|
|
|
JobResult.prototype.getStatus = function(requiredStatus, callback) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-11-24 02:20:44 +08:00
|
|
|
function hasRequiredStatus(job, requiredStatus) {
|
|
|
|
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;
|
|
|
|
|
2016-11-24 20:25:14 +08:00
|
|
|
actual.query.forEach(function(actualQuery, index) {
|
|
|
|
var expectedQuery = expected.query[index];
|
|
|
|
assert.ok(expectedQuery);
|
|
|
|
Object.keys(expectedQuery).forEach(function(expectedKey) {
|
|
|
|
assert.equal(
|
|
|
|
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'];
|
|
|
|
propsToCheckDate.forEach(function(propToCheckDate) {
|
|
|
|
if (actualQuery.hasOwnProperty(propToCheckDate)) {
|
|
|
|
assert.ok(new Date(actualQuery[propToCheckDate]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(actual.onsuccess, expected.onsuccess);
|
|
|
|
assert.equal(actual.onerror, expected.onerror);
|
|
|
|
};
|