Use env vars and fix tests

This commit is contained in:
Álvaro
2020-12-16 16:32:32 +01:00
committed by GitHub
parent 92be27e700
commit 5c491a25cf
23 changed files with 1434 additions and 337 deletions
+5 -1
View File
@@ -512,7 +512,11 @@ var QueryTables = require('cartodb-query-tables').queryTables;
]
};
var statskey = 'user:localhost:mapviews';
var redisStatsClient = redis.createClient(global.environment.redis.port);
var redisStatsClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
var expectedToken; // will be set on first post and checked on second
var now = strftime('%Y%m%d', new Date());
step(
+15 -3
View File
@@ -164,7 +164,11 @@ describe('rate limit', function () {
global.environment.enabledFeatures.rateLimitsEnabled = true;
global.environment.enabledFeatures.rateLimitsByEndpoint.anonymous = true;
redisClient = redis.createClient(global.environment.redis.port);
redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
testClient = new TestClient(createMapConfig(), 1234);
});
@@ -226,7 +230,11 @@ describe('rate limit middleware', function () {
});
rateLimit = rateLimitMiddleware(userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS);
redisClient = redis.createClient(global.environment.redis.port);
redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
testClient = new TestClient(createMapConfig(), 1234);
const count = 1;
@@ -290,7 +298,11 @@ function rateLimitAndVectorTilesTest (usePostGIS) {
global.environment.enabledFeatures.rateLimitsEnabled = true;
global.environment.enabledFeatures.rateLimitsByEndpoint.tile = true;
redisClient = redis.createClient(global.environment.redis.port);
redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
const count = 1;
const period = 1;
const burst = 0;
+5 -1
View File
@@ -1506,7 +1506,11 @@ describe('template_api', function () {
layergroup: layergroup
};
var statskey = 'user:localhost:mapviews';
var redisStatsClient = redis.createClient(global.environment.redis.port);
var redisStatsClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
var templateId; // will be set on template post
var now = strftime('%Y%m%d', new Date());
var errors = [];
+19 -16
View File
@@ -1,21 +1,24 @@
'use strict';
const util = require('util');
const path = require('path');
const exec = util.promisify(require('child_process').exec);
if (!process.env.NODE_ENV) {
console.error('Please set "NODE_ENV" variable, e.g.: "NODE_ENV=test"');
process.exit(1);
}
let configFileName = process.env.NODE_ENV;
if (process.env.CARTO_WINDSHAFT_ENV_BASED_CONF) {
// we override the file with the one with env vars
configFileName = 'config';
}
const environment = require(`../config/environments/${process.env.NODE_ENV}.js`);
const environment = require(`../config/environments/${configFileName}.js`);
const REDIS_PORT = environment.redis.port;
const REDIS_CELL_PATH = path.resolve(
process.platform === 'darwin'
? './test/support/libredis_cell.dylib'
: './test/support/libredis_cell.so'
);
const REDIS_HOST = environment.redis.host;
const PGHOST = environment.postgres.host;
const PGPORT = environment.postgres.port;
const TEST_USER_ID = 1;
const TEST_USER = environment.postgres_auth_user.replace('<%= user_id %>', TEST_USER_ID);
@@ -25,28 +28,28 @@ const PUBLIC_USER_PASSWORD = environment.postgres.password;
const TEST_DB = `${TEST_USER}_db`;
async function startRedis () {
await exec(`redis-server --port ${REDIS_PORT} --loadmodule ${REDIS_CELL_PATH} --logfile ${__dirname}/redis-server.log --daemonize yes`);
// await exec(`redis-server --port ${REDIS_PORT} --loadmodule ${REDIS_CELL_PATH} --logfile ${__dirname}/redis-server.log --daemonize yes`);
}
async function stopRedis () {
await exec(`redis-cli -p ${REDIS_PORT} shutdown`);
// await exec(`redis-cli -p ${REDIS_PORT} shutdown`);
}
async function dropDatabase () {
await exec(`dropdb --if-exists ${TEST_DB}`, {
env: Object.assign({ PGUSER: 'postgres' }, process.env)
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
});
}
async function createDatabase () {
await exec(`createdb -T template_postgis -EUTF8 "${TEST_DB}"`, {
env: Object.assign({ PGUSER: 'postgres' }, process.env)
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
});
}
async function createDatabaseExtension () {
await exec(`psql -c "CREATE EXTENSION IF NOT EXISTS cartodb CASCADE;" ${TEST_DB}`, {
env: Object.assign({ PGUSER: 'postgres' }, process.env)
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
});
}
@@ -71,7 +74,7 @@ async function populateDatabase () {
`;
await exec(populateDatabaseCmd, {
env: Object.assign({ PGUSER: 'postgres' }, process.env)
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
});
}
@@ -82,13 +85,13 @@ async function populateRedis () {
HMSET rails:users:localhost \
id ${TEST_USER_ID} \
database_name "${TEST_DB}" \
database_host localhost \
database_host "${PGHOST}" \
map_key 1234
HMSET rails:users:cartodb250user \
id ${TEST_USER_ID} \
database_name "${TEST_DB}" \
database_host "localhost" \
database_host "${PGHOST}" \
database_password "${TEST_PASSWORD}" \
map_key 4321
@@ -141,7 +144,7 @@ async function populateRedis () {
database_password "public"
`;
await exec(`echo "${commands}" | redis-cli -p ${REDIS_PORT} -n 5`);
await exec(`echo "${commands}" | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5`);
}
async function main (args) {
@@ -16,7 +16,11 @@ describe('analysis-backend limits', function () {
var user = 'localhost';
beforeEach(function () {
redisClient = redis.createClient(global.environment.redis.port);
redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
keysToDelete = {};
var redisPool = new RedisPool(global.environment.redis);
this.metadataBackend = cartodbRedis({ pool: redisPool });
@@ -30,7 +30,11 @@ describe('TemplateMaps limits', function () {
};
}
var redisClient = redis.createClient(global.environment.redis.port);
var redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
var redisPool = new RedisPool(global.environment.redis);
afterEach(function (done) {
+18 -4
View File
@@ -10,8 +10,14 @@ var redis = require('redis');
const setICUEnvVariable = require('../../lib/utils/icu-data-env-setter');
// set environment specific variables
global.environment = require('../../config/environments/test');
global.environment.name = 'test';
let configFileName = process.env.NODE_ENV;
if (process.env.CARTO_WINDSHAFT_ENV_BASED_CONF) {
// we override the file with the one with env vars
configFileName = 'config';
}
global.environment = require(`../../config/environments/${configFileName}.js`);
process.env.NODE_ENV = 'test';
setICUEnvVariable();
@@ -82,7 +88,11 @@ var redisClient;
beforeEach(function () {
if (!redisClient) {
redisClient = redis.createClient(global.environment.redis.port);
redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
}
});
@@ -145,7 +155,11 @@ function deleteRedisKeys (keysToDelete, callback) {
}
Object.keys(keysToDelete).forEach(function (k) {
var redisClient = redis.createClient(global.environment.redis.port);
var redisClient = redis.createClient(
{
port: global.environment.redis.port,
host: global.environment.redis.host
});
redisClient.select(keysToDelete[k], function () {
redisClient.del(k, function (err, deletedKeysCount) {
assert.ifError(err);
+1 -1
View File
@@ -8,7 +8,7 @@ var serverOptions = require('../../../lib/server-options');
describe('windshaft', function () {
it('should have valid global environment', function () {
assert.strictEqual(global.environment.name, 'test');
assert.strictEqual(global.environment.environment, 'test');
});
it('can instantiate a Windshaft object (configured express instance)', function () {