mirror of
https://github.com/status-im/consul.git
synced 2025-01-17 17:22:17 +00:00
f111d6b3e3
In order to test certain setups for our metrics visualizations we need to be able to setup several different `ui_config` settings during development/testing. Generally in the UI, we use the Web Inspector to set various cookie values to configure the UI how we need to see it whilst developing, so this PR: 1. Routes `ui_config` through a dev time only `CONSUL_UI_CONFIG` env variable so we can change it via cookies vars. 2. Adds `CONSUL_METRICS_PROXY_ENABLE`, `CONSUL_METRICS_PROVIDER` and `CONSUL_SERVICE_DASHBOARD_URL` so it's easy to set/unset these only values during development. 3. Adds an acceptance testing step so we can setup `ui_config` to whatever we want during testing. 4. Adds an async 'repository-like' method to the `UiConfig` Service so it feels like a repository - incase we ever need to get this via an HTTP API+blocking query. 5. Vaguely unrelated: we allow cookie values to be set via the location.hash whilst in development only e.g. `/ui/services#CONSUL_METRICS_PROXY_ENABLE=1` so we can link to different setups if we ever need to. All values added here are empty/falsey by default, so in order to see how it was previously you'll need to set the appropriate cookies values, but you can now also easily preview/test the the metrics viz in different/disabled states (with differing `ui_config`)
199 lines
5.9 KiB
JavaScript
199 lines
5.9 KiB
JavaScript
import getEnvironment from 'consul-ui/utils/get-environment';
|
|
import { module, test } from 'qunit';
|
|
const getEntriesByType = function(type) {
|
|
return [
|
|
{
|
|
initiatorType: 'script',
|
|
name: '',
|
|
nextHopProtocol: 'spdy',
|
|
},
|
|
];
|
|
};
|
|
const makeGetElementsBy = function(str) {
|
|
return function(name) {
|
|
return [
|
|
{
|
|
src: str,
|
|
content: str,
|
|
},
|
|
];
|
|
};
|
|
};
|
|
const win = {
|
|
performance: {
|
|
getEntriesByType: getEntriesByType,
|
|
},
|
|
location: {
|
|
hash: '',
|
|
},
|
|
localStorage: {
|
|
getItem: function(key) {},
|
|
},
|
|
};
|
|
const doc = {
|
|
cookie: '',
|
|
getElementsByTagName: makeGetElementsBy(''),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
module('Unit | Utility | getEnvironment', function() {
|
|
test('it returns a function', function(assert) {
|
|
const config = {};
|
|
const env = getEnvironment(config, win, doc);
|
|
assert.ok(typeof env === 'function');
|
|
});
|
|
test('it returns the correct operator value', function(assert) {
|
|
const config = {};
|
|
const env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_PROTOCOL'), 'spdy');
|
|
});
|
|
test('it returns the correct operator value when set via config', function(assert) {
|
|
const config = {
|
|
CONSUL_HTTP_PROTOCOL: 'hq',
|
|
};
|
|
const env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_PROTOCOL'), 'hq');
|
|
});
|
|
test('it returns the correct URL for the root of the UI', function(assert) {
|
|
let config = {
|
|
environment: 'production',
|
|
};
|
|
let expected = 'http://localhost/ui';
|
|
let doc = {
|
|
cookie: '',
|
|
getElementsByTagName: makeGetElementsBy(`${expected}/assets/consul-ui.js`),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_BASE_UI_URL'), expected);
|
|
expected = 'http://localhost/somewhere/else';
|
|
doc = {
|
|
cookie: '',
|
|
getElementsByTagName: makeGetElementsBy(`${expected}/assets/consul-ui.js`),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_BASE_UI_URL'), expected);
|
|
});
|
|
|
|
test('it returns the correct max connections depending on protocol', function(assert) {
|
|
let config = {
|
|
CONSUL_HTTP_PROTOCOL: 'hq',
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_MAX_CONNECTIONS'), undefined);
|
|
config = {
|
|
CONSUL_HTTP_PROTOCOL: 'http/1.1',
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_MAX_CONNECTIONS'), 5);
|
|
});
|
|
test('it returns the correct max connections if performance.getEntriesByType is not available', function(assert) {
|
|
const config = {};
|
|
let win = {};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_MAX_CONNECTIONS'), 5);
|
|
win = {
|
|
performance: {},
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.equal(env('CONSUL_HTTP_MAX_CONNECTIONS'), 5);
|
|
});
|
|
test('it returns the correct user value', function(assert) {
|
|
const config = {};
|
|
let win = {
|
|
localStorage: {
|
|
getItem: function(key) {
|
|
return '1';
|
|
},
|
|
},
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.ok(env('CONSUL_UI_DISABLE_REALTIME'));
|
|
win = {
|
|
localStorage: {
|
|
getItem: function(key) {
|
|
return '0';
|
|
},
|
|
},
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.notOk(env('CONSUL_UI_DISABLE_REALTIME'));
|
|
win = {
|
|
localStorage: {
|
|
getItem: function(key) {
|
|
return null;
|
|
},
|
|
},
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.notOk(env('CONSUL_UI_DISABLE_REALTIME'));
|
|
});
|
|
test('it returns the correct user value when set via config', function(assert) {
|
|
const config = {
|
|
CONSUL_UI_DISABLE_REALTIME: true,
|
|
};
|
|
const env = getEnvironment(config, win, doc);
|
|
assert.ok(env('CONSUL_UI_DISABLE_REALTIME'));
|
|
});
|
|
test('it returns the correct dev value (via cookies)', function(assert) {
|
|
let config = {
|
|
environment: 'test',
|
|
CONSUL_NSPACES_ENABLED: false,
|
|
};
|
|
let doc = {
|
|
cookie: 'CONSUL_NSPACES_ENABLE=1',
|
|
getElementsByTagName: makeGetElementsBy(''),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.ok(env('CONSUL_NSPACES_ENABLED'));
|
|
config = {
|
|
environment: 'test',
|
|
CONSUL_NSPACES_ENABLED: true,
|
|
};
|
|
doc = {
|
|
cookie: 'CONSUL_NSPACES_ENABLE=0',
|
|
getElementsByTagName: makeGetElementsBy(''),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.notOk(env('CONSUL_NSPACES_ENABLED'));
|
|
});
|
|
test('it returns the correct dev value when set via config', function(assert) {
|
|
let config = {
|
|
CONSUL_NSPACES_ENABLED: true,
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.ok(env('CONSUL_NSPACES_ENABLED'));
|
|
config = {
|
|
CONSUL_NSPACES_ENABLED: false,
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.notOk(env('CONSUL_NSPACES_ENABLED'));
|
|
});
|
|
test("it returns the correct dev value (ignoring cookies when the environment doesn't allow it)", function(assert) {
|
|
let config = {
|
|
environment: 'production',
|
|
CONSUL_NSPACES_ENABLED: false,
|
|
};
|
|
let doc = {
|
|
cookie: 'CONSUL_NSPACES_ENABLE=1',
|
|
getElementsByTagName: makeGetElementsBy(''),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
let env = getEnvironment(config, win, doc);
|
|
assert.notOk(env('CONSUL_NSPACES_ENABLED'));
|
|
config = {
|
|
environment: 'production',
|
|
CONSUL_NSPACES_ENABLED: true,
|
|
};
|
|
doc = {
|
|
cookie: 'CONSUL_NSPACES_ENABLE=0',
|
|
getElementsByTagName: makeGetElementsBy(''),
|
|
getElementsByName: makeGetElementsBy('{}'),
|
|
};
|
|
env = getEnvironment(config, win, doc);
|
|
assert.ok(env('CONSUL_NSPACES_ENABLED'));
|
|
});
|
|
});
|