2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-04 00:32:39 +08:00
|
|
|
require('../helper');
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2018-05-04 00:46:16 +08:00
|
|
|
const querystring = require('querystring');
|
2018-05-04 00:32:39 +08:00
|
|
|
const assert = require('../support/assert');
|
2018-05-22 17:54:10 +08:00
|
|
|
const os = require('os');
|
2019-12-26 23:57:24 +08:00
|
|
|
const path = require('path');
|
2018-06-04 21:46:39 +08:00
|
|
|
const { Client } = require('pg');
|
2018-06-15 00:10:41 +08:00
|
|
|
const request = require('request');
|
2018-05-04 00:32:39 +08:00
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const StatsClient = require('../../lib/stats/client');
|
2018-05-22 17:54:10 +08:00
|
|
|
if (global.settings.statsd) {
|
|
|
|
// Perform keyword substitution in statsd
|
|
|
|
if (global.settings.statsd.prefix) {
|
|
|
|
const hostToken = os.hostname().split('.').reverse().join('.');
|
|
|
|
global.settings.statsd.prefix = global.settings.statsd.prefix.replace(/:host/, hostToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const statsClient = StatsClient.getInstance(global.settings.statsd);
|
2019-10-04 00:24:39 +08:00
|
|
|
const server = require('../../lib/server')(statsClient);
|
2018-05-22 17:54:10 +08:00
|
|
|
|
2020-12-17 18:11:49 +08:00
|
|
|
const TEST_USER_ID = 1;
|
|
|
|
const TEST_USER = 'postgres';
|
|
|
|
const TEST_DB = global.settings.db_base_name.replace('<%= user_id %>', TEST_USER_ID);
|
|
|
|
|
2018-11-30 01:42:04 +08:00
|
|
|
// Give it enough time to connect and issue the query
|
|
|
|
// but not too much so as to disconnect in the middle of the query.
|
|
|
|
const CLIENT_DISCONNECT_TIMEOUT = 100;
|
|
|
|
const assertCanReuseCanceledConnection = function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'SELECT count(*) FROM copy_endpoints_test'
|
2018-11-30 01:42:04 +08:00
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-11-30 01:42:04 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
assert.ok(res.statusCode === 200);
|
|
|
|
const result = JSON.parse(res.body);
|
|
|
|
assert.strictEqual(result.rows[0].count, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('copy-endpoints', function () {
|
|
|
|
before(function () {
|
2018-06-20 17:08:41 +08:00
|
|
|
this.client = new Client({
|
2020-12-17 18:11:49 +08:00
|
|
|
user: TEST_USER,
|
|
|
|
host: global.settings.db_host,
|
|
|
|
database: TEST_DB,
|
|
|
|
port: global.settings.db_batch_port
|
2018-05-04 00:46:16 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
this.client.connect();
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after(function () {
|
2018-06-20 17:08:41 +08:00
|
|
|
this.client.end();
|
2018-06-20 20:43:50 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
this.client.query('TRUNCATE copy_endpoints_test', err => {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('general', function () {
|
|
|
|
it('should work with copyfrom endpoint', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-14 16:44:57 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(!!response.time, true);
|
2019-09-13 17:28:37 +08:00
|
|
|
assert.strictEqual(response.total_rows, 2016);
|
2018-06-14 16:44:57 +08:00
|
|
|
done();
|
|
|
|
});
|
2018-05-22 21:56:04 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyfrom endpoint and unexisting table', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-14 16:44:57 +08:00
|
|
|
q: "COPY unexisting_table (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-06-20 17:08:41 +08:00
|
|
|
JSON.parse(res.body),
|
2018-06-14 16:44:57 +08:00
|
|
|
{
|
2019-12-26 23:57:24 +08:00
|
|
|
error: ['relation "unexisting_table" does not exist']
|
2018-06-14 16:44:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
2018-05-11 19:33:54 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyfrom endpoint and without csv', function (done) {
|
2018-05-25 01:08:35 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-05-25 01:08:35 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-05-25 01:08:35 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-05-25 01:08:35 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-06-20 17:08:41 +08:00
|
|
|
JSON.parse(res.body),
|
2018-06-14 16:44:57 +08:00
|
|
|
{
|
2019-12-24 01:19:08 +08:00
|
|
|
error: ['No rows copied']
|
2018-06-14 16:44:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyfrom endpoint and without q', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom',
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-06-20 17:08:41 +08:00
|
|
|
JSON.parse(res.body),
|
2018-06-14 16:44:57 +08:00
|
|
|
{
|
2019-12-24 01:19:08 +08:00
|
|
|
error: ['SQL is missing']
|
2018-06-14 16:44:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
2018-05-25 01:08:35 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should work with copyto endpoint', function (done) {
|
2018-05-25 01:08:35 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-20 17:12:19 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-05-25 01:08:35 +08:00
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-20 17:12:19 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err) {
|
2018-05-25 01:08:35 +08:00
|
|
|
assert.ifError(err);
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2018-06-20 17:12:19 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
2018-06-20 17:12:19 +08:00
|
|
|
q: 'COPY copy_endpoints_test TO STDOUT',
|
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-20 17:12:19 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-20 17:12:19 +08:00
|
|
|
assert.ifError(err);
|
2019-09-13 17:28:37 +08:00
|
|
|
const regex = /11\tPaul\t10\n12\tPeter\t10\n13\tMatthew\t10\n14\t\\N\t10\n15\tJames\t10\n16\t*/g;
|
|
|
|
assert.ok(res.body.match(regex));
|
2018-06-20 17:12:19 +08:00
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['content-disposition'], 'attachment; filename=%2Ftmp%2Foutput.dmp');
|
|
|
|
assert.strictEqual(res.headers['content-type'], 'application/octet-stream');
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2018-06-20 17:12:19 +08:00
|
|
|
done();
|
|
|
|
});
|
2018-06-14 16:44:57 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should work with copyto endpoint and POST method', function (done) {
|
2019-05-31 16:55:00 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2019-05-31 16:55:00 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-05-31 16:55:00 +08:00
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err) {
|
2019-05-31 16:55:00 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql/copyto',
|
|
|
|
data: querystring.stringify({
|
|
|
|
q: 'COPY copy_endpoints_test TO STDOUT',
|
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com',
|
2019-05-31 17:54:20 +08:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
2019-05-31 16:55:00 +08:00
|
|
|
},
|
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2019-05-31 16:55:00 +08:00
|
|
|
assert.ifError(err);
|
2019-09-13 17:28:37 +08:00
|
|
|
const regex = /11\tPaul\t10\n12\tPeter\t10\n13\tMatthew\t10\n14\t\\N\t10\n15\tJames\t10\n16\t*/g;
|
|
|
|
assert.ok(res.body.match(regex));
|
2019-05-31 16:55:00 +08:00
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['content-disposition'], 'attachment; filename=%2Ftmp%2Foutput.dmp');
|
|
|
|
assert.strictEqual(res.headers['content-type'], 'application/octet-stream');
|
2019-05-31 16:55:00 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyto endpoint and without sql', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
2018-06-14 16:44:57 +08:00
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-06-20 17:08:41 +08:00
|
|
|
JSON.parse(res.body),
|
2018-06-14 16:44:57 +08:00
|
|
|
{
|
2019-12-24 01:19:08 +08:00
|
|
|
error: ['SQL is missing']
|
2018-06-14 16:44:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should work with copyfrom and gzip', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-20 17:08:41 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-06-14 16:44:57 +08:00
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv.gz')),
|
2018-06-14 16:44:57 +08:00
|
|
|
headers: {
|
2018-06-20 17:08:41 +08:00
|
|
|
host: 'vizzuality.cartodb.com',
|
2018-06-14 16:44:57 +08:00
|
|
|
'content-encoding': 'gzip'
|
2018-05-25 01:08:35 +08:00
|
|
|
},
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(!!response.time, true);
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.strictEqual(response.total_rows, 6);
|
|
|
|
done();
|
2018-05-25 01:08:35 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should return an error when gzip headers are not correct', function (done) {
|
2018-08-10 18:15:31 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-08-10 18:15:31 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2018-08-10 18:15:31 +08:00
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com',
|
|
|
|
'content-encoding': 'gzip'
|
|
|
|
},
|
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-08-10 18:15:31 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-08-10 18:15:31 +08:00
|
|
|
JSON.parse(res.body),
|
|
|
|
{
|
2019-12-24 01:19:08 +08:00
|
|
|
error: ['Error while gunzipping: incorrect header check']
|
2018-08-10 18:15:31 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-25 01:08:35 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('timeout', function () {
|
|
|
|
before('set a 1 ms timeout', function () {
|
2018-07-20 21:33:12 +08:00
|
|
|
this.previous_timeout = global.settings.copy_timeout;
|
2019-09-13 17:28:37 +08:00
|
|
|
global.settings.copy_timeout = 1;
|
2018-07-20 21:33:12 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after('restore previous timeout', function () {
|
2018-07-20 21:33:12 +08:00
|
|
|
global.settings.copy_timeout = this.previous_timeout;
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyfrom and timeout', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-07-20 21:38:20 +08:00
|
|
|
q: `COPY copy_endpoints_test (id, name)
|
|
|
|
FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)`
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-20 21:38:20 +08:00
|
|
|
method: 'POST'
|
2018-06-14 16:44:57 +08:00
|
|
|
},
|
2018-07-20 21:38:20 +08:00
|
|
|
{
|
|
|
|
status: 429,
|
|
|
|
headers: { 'Content-Type': 'application/json; charset=utf-8' }
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(JSON.parse(res.body), {
|
2018-07-20 21:38:20 +08:00
|
|
|
error: [
|
|
|
|
'You are over platform\'s limits: SQL query timeout error.' +
|
2019-12-24 01:19:08 +08:00
|
|
|
' Refactor your query before running again or contact CARTO support for more details.'
|
2018-07-20 21:38:20 +08:00
|
|
|
],
|
|
|
|
context: 'limit',
|
|
|
|
detail: 'datasource'
|
2018-06-01 17:26:28 +08:00
|
|
|
});
|
2018-07-20 21:38:20 +08:00
|
|
|
done();
|
2018-06-01 17:26:28 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should fail with copyto and timeout', function (done) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
2018-07-20 21:38:20 +08:00
|
|
|
q: 'COPY populated_places_simple_reduced TO STDOUT',
|
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
2018-07-20 21:38:20 +08:00
|
|
|
const error = {
|
|
|
|
error: ['You are over platform\'s limits: SQL query timeout error.' +
|
2019-12-24 01:19:08 +08:00
|
|
|
' Refactor your query before running again or contact CARTO support for more details.'],
|
|
|
|
context: 'limit',
|
|
|
|
detail: 'datasource'
|
2018-07-20 21:38:20 +08:00
|
|
|
};
|
|
|
|
const expectedError = res.body.substring(res.body.length - JSON.stringify(error).length);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(JSON.parse(expectedError), error);
|
2018-07-20 21:38:20 +08:00
|
|
|
done();
|
2018-06-01 17:26:28 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('db connections', function () {
|
|
|
|
before(function () {
|
2018-06-14 16:44:57 +08:00
|
|
|
this.db_pool_size = global.settings.db_pool_size;
|
|
|
|
global.settings.db_pool_size = 1;
|
2018-06-12 01:17:23 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after(function () {
|
2018-06-14 16:44:57 +08:00
|
|
|
global.settings.db_pool_size = this.db_pool_size;
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('copyfrom', function (done) {
|
|
|
|
function doCopyFrom () {
|
2018-06-14 16:44:57 +08:00
|
|
|
return new Promise(resolve => {
|
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-20 17:08:41 +08:00
|
|
|
q: `COPY copy_endpoints_test (id, name)
|
2018-06-14 16:44:57 +08:00
|
|
|
FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)`
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
|
|
|
assert.ok(response.time);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
Promise.all([doCopyFrom(), doCopyFrom(), doCopyFrom()]).then(function () {
|
2018-06-14 16:44:57 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('copyto', function (done) {
|
|
|
|
function doCopyTo () {
|
2018-06-14 16:44:57 +08:00
|
|
|
return new Promise(resolve => {
|
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
|
|
|
q: 'COPY (SELECT * FROM generate_series(1, 10000)) TO STDOUT',
|
2018-06-14 16:44:57 +08:00
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-14 16:44:57 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-14 16:44:57 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
assert.ok(res.body);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2018-06-20 17:12:19 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-06-20 17:12:19 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-06-20 17:12:19 +08:00
|
|
|
method: 'POST'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err) {
|
2018-06-20 17:12:19 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
Promise.all([doCopyTo(), doCopyTo(), doCopyTo()]).then(function () {
|
2018-06-20 17:12:19 +08:00
|
|
|
done();
|
|
|
|
});
|
2018-06-14 16:44:57 +08:00
|
|
|
});
|
2018-06-12 01:17:23 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('client disconnection', function () {
|
|
|
|
before(function () {
|
2018-06-14 16:44:57 +08:00
|
|
|
this.db_pool_size = global.settings.db_pool_size;
|
|
|
|
global.settings.db_pool_size = 1;
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after(function () {
|
2018-06-14 16:44:57 +08:00
|
|
|
global.settings.db_pool_size = this.db_pool_size;
|
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2018-06-21 01:02:34 +08:00
|
|
|
const assertCanReuseConnection = function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'SELECT 1'
|
2018-06-21 01:02:34 +08:00
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-06-21 01:02:34 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
assert.ok(res.statusCode === 200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY TO returns the connection to the pool if the client disconnects', function (done) {
|
2018-06-15 00:10:41 +08:00
|
|
|
const listener = server.listen(0, '127.0.0.1');
|
|
|
|
|
|
|
|
listener.on('error', done);
|
|
|
|
listener.on('listening', function onServerListening () {
|
|
|
|
const { address, port } = listener.address();
|
|
|
|
const query = querystring.stringify({
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'COPY (SELECT * FROM generate_series(1, 1000)) TO STDOUT'
|
2018-06-15 00:10:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${address}:${port}/api/v1/sql/copyto?${query}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
req.once('data', () => req.abort());
|
2018-06-15 00:25:38 +08:00
|
|
|
req.on('response', response => {
|
|
|
|
response.on('end', () => {
|
2018-06-21 01:02:34 +08:00
|
|
|
assertCanReuseConnection(done);
|
2018-06-15 00:25:38 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-14 16:44:57 +08:00
|
|
|
});
|
2018-06-12 00:38:18 +08:00
|
|
|
});
|
2018-06-20 17:08:41 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY FROM returns the connection to the pool if the client disconnects', function (done) {
|
2018-06-15 00:10:41 +08:00
|
|
|
const listener = server.listen(0, '127.0.0.1');
|
|
|
|
|
|
|
|
listener.on('error', done);
|
|
|
|
listener.on('listening', function onServerListening () {
|
|
|
|
const { address, port } = listener.address();
|
|
|
|
const query = querystring.stringify({
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER \',\', HEADER true)'
|
2018-06-15 00:10:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${address}:${port}/api/v1/sql/copyfrom?${query}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'POST',
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv'))
|
2018-06-15 00:10:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
req.abort();
|
2018-06-20 20:43:50 +08:00
|
|
|
assertCanReuseCanceledConnection(done);
|
2018-06-15 00:10:41 +08:00
|
|
|
}, CLIENT_DISCONNECT_TIMEOUT);
|
2018-06-14 16:44:57 +08:00
|
|
|
});
|
|
|
|
});
|
2018-06-08 16:53:06 +08:00
|
|
|
});
|
2018-07-20 21:57:36 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('COPY timeouts: they can take longer than statement_timeout', function () {
|
|
|
|
before('set a very small statement_timeout for regular queries', function (done) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.response(server, {
|
2018-07-23 21:36:13 +08:00
|
|
|
url: '/api/v1/sql?q=set statement_timeout = 10',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-20 21:57:36 +08:00
|
|
|
method: 'GET'
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after('restore normal statement_timeout for regular queries', function (done) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=set statement_timeout = 2000',
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-20 21:57:36 +08:00
|
|
|
method: 'GET'
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY FROM can take longer than regular statement_timeout', function (done) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-07-20 21:57:36 +08:00
|
|
|
q: `COPY copy_endpoints_test (id, name)
|
|
|
|
FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)`
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-20 21:57:36 +08:00
|
|
|
method: 'POST'
|
|
|
|
}, {
|
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type': 'application/json; charset=utf-8' }
|
2019-12-24 01:19:08 +08:00
|
|
|
}, function (err, res) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
2019-09-13 17:28:37 +08:00
|
|
|
assert.strictEqual(response.total_rows, 2016);
|
2018-07-20 21:57:36 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY TO can take longer than regular statement_timeout', function (done) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
2018-07-20 21:57:36 +08:00
|
|
|
q: 'COPY copy_endpoints_test TO STDOUT',
|
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-20 21:57:36 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-07-20 21:57:36 +08:00
|
|
|
assert.ifError(err);
|
2018-07-20 22:17:44 +08:00
|
|
|
assert.ok(res.statusCode === 200);
|
2018-07-20 21:57:36 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-07-23 22:14:10 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('dbQuotaMiddleware', function () {
|
|
|
|
before('Set the remaining quota to 1 byte', function (done) {
|
2018-07-23 22:14:10 +08:00
|
|
|
// See the test/support/sql/quota_mock.sql
|
|
|
|
this.client.query(`CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT)
|
|
|
|
RETURNS bigint AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
2018-07-23 22:20:15 +08:00
|
|
|
RETURN 250 * 1024 * 1024 - 1;
|
2018-07-23 22:14:10 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql' VOLATILE;
|
|
|
|
`, err => done(err));
|
2018-11-30 01:42:04 +08:00
|
|
|
|
|
|
|
this.db_pool_size = global.settings.db_pool_size;
|
|
|
|
global.settings.db_pool_size = 1;
|
2018-07-23 22:14:10 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after('Restore the old quota', function (done) {
|
2018-07-23 22:14:10 +08:00
|
|
|
// See the test/support/sql/quota_mock.sql
|
|
|
|
this.client.query(`CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT)
|
|
|
|
RETURNS bigint AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
|
|
|
RETURN 200 * 1024 * 1024;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql' VOLATILE;
|
|
|
|
`, err => done(err));
|
2018-11-30 01:42:04 +08:00
|
|
|
|
|
|
|
global.settings.db_pool_size = this.db_pool_size;
|
2018-07-23 22:14:10 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY FROM fails with an error if DB quota is exhausted', function (done) {
|
2018-07-23 22:14:10 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-07-23 22:14:10 +08:00
|
|
|
q: `COPY copy_endpoints_test (id, name)
|
|
|
|
FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)`
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-23 22:14:10 +08:00
|
|
|
method: 'POST'
|
|
|
|
}, {
|
|
|
|
status: 400,
|
|
|
|
headers: { 'Content-Type': 'application/json; charset=utf-8' }
|
2019-12-24 01:19:08 +08:00
|
|
|
}, function (err, res) {
|
2019-12-26 23:57:24 +08:00
|
|
|
assert.ifError(err);
|
2018-07-23 22:14:10 +08:00
|
|
|
const response = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(response, { error: ['DB Quota exceeded'] });
|
2018-11-30 01:42:04 +08:00
|
|
|
|
|
|
|
setTimeout(() => assertCanReuseCanceledConnection(done), CLIENT_DISCONNECT_TIMEOUT);
|
2018-07-23 22:14:10 +08:00
|
|
|
});
|
|
|
|
});
|
2018-07-23 22:20:15 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('COPY TO is not affected by remaining DB quota', function (done) {
|
2018-07-23 22:20:15 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyto?' + querystring.stringify({
|
2018-07-23 22:20:15 +08:00
|
|
|
q: 'COPY copy_endpoints_test TO STDOUT',
|
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-23 22:20:15 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, {}, function (err, res) {
|
2018-07-23 22:20:15 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
assert.ok(res.statusCode === 200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-07-23 22:14:10 +08:00
|
|
|
});
|
2018-07-23 22:58:05 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('COPY FROM max POST size', function () {
|
|
|
|
before('Set a ridiculously small POST size limit', function () {
|
2018-07-23 22:58:05 +08:00
|
|
|
this.previous_max_post_size = global.settings.copy_from_max_post_size;
|
|
|
|
this.previous_max_post_size_pretty = global.settings.copy_from_max_post_size_pretty;
|
|
|
|
global.settings.copy_from_max_post_size = 10;
|
|
|
|
global.settings.copy_from_max_post_size_pretty = '10 bytes';
|
2018-11-30 01:42:04 +08:00
|
|
|
this.db_pool_size = global.settings.db_pool_size;
|
|
|
|
global.settings.db_pool_size = 1;
|
2018-07-23 22:58:05 +08:00
|
|
|
});
|
2019-12-24 01:19:08 +08:00
|
|
|
after('Restore the max POST size limit values', function () {
|
2018-07-23 22:58:05 +08:00
|
|
|
global.settings.copy_from_max_post_size = this.previous_max_post_size;
|
|
|
|
global.settings.copy_from_max_post_size_pretty = this.previous_max_post_size_pretty;
|
2018-11-30 01:42:04 +08:00
|
|
|
global.settings.db_pool_size = this.db_pool_size;
|
2018-07-23 22:58:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('honors the max POST size limit', function (done) {
|
2018-07-23 22:58:05 +08:00
|
|
|
assert.response(server, {
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
|
2018-07-23 22:58:05 +08:00
|
|
|
q: `COPY copy_endpoints_test (id, name)
|
|
|
|
FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)`
|
|
|
|
}),
|
2019-12-26 23:57:24 +08:00
|
|
|
data: fs.createReadStream(path.join(__dirname, '/../support/csv/copy_test_table.csv')),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2018-07-23 22:58:05 +08:00
|
|
|
method: 'POST'
|
|
|
|
}, {
|
|
|
|
status: 400,
|
|
|
|
headers: { 'Content-Type': 'application/json; charset=utf-8' }
|
2019-12-24 01:19:08 +08:00
|
|
|
}, function (err, res) {
|
2019-12-26 23:57:24 +08:00
|
|
|
assert.ifError(err);
|
2018-07-23 22:58:05 +08:00
|
|
|
const response = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(response, { error: ['COPY FROM maximum POST size of 10 bytes exceeded'] });
|
2018-11-30 01:42:04 +08:00
|
|
|
|
|
|
|
setTimeout(() => assertCanReuseCanceledConnection(done), CLIENT_DISCONNECT_TIMEOUT);
|
2018-07-23 22:58:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-08 16:53:06 +08:00
|
|
|
});
|