2020-11-11 14:43:37 +00:00
|
|
|
import { getResolvers } from 'consul-ui/components/consul/discovery-chain/utils';
|
2020-01-16 16:31:09 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { get } from 'consul-ui/tests/helpers/api';
|
|
|
|
|
|
|
|
const dc = 'dc-1';
|
|
|
|
const nspace = 'default';
|
2021-12-06 10:22:09 +00:00
|
|
|
const partition = 'default';
|
2020-01-16 16:31:09 +00:00
|
|
|
const request = {
|
|
|
|
url: `/v1/discovery-chain/service-name?dc=${dc}`,
|
|
|
|
};
|
2020-11-11 14:43:37 +00:00
|
|
|
module('Unit | Component | consul/discovery-chain/get-resolvers', function() {
|
2020-01-16 16:31:09 +00:00
|
|
|
test('it assigns Subsets correctly', function(assert) {
|
|
|
|
return get(request.url, {
|
|
|
|
headers: {
|
|
|
|
cookie: {
|
|
|
|
CONSUL_RESOLVER_COUNT: 1,
|
|
|
|
CONSUL_SUBSET_COUNT: 1,
|
|
|
|
CONSUL_REDIRECT_COUNT: 0,
|
|
|
|
CONSUL_FAILOVER_COUNT: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-16 16:31:09 +00:00
|
|
|
const childId = Object.keys(Chain.Targets)[1];
|
|
|
|
const target = Chain.Targets[`${childId}`];
|
|
|
|
const firstChild = actual[0].Children[0];
|
|
|
|
assert.equal(firstChild.Subset, true);
|
|
|
|
assert.equal(firstChild.ID, target.ID);
|
|
|
|
assert.equal(firstChild.Name, target.ServiceSubset);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
test('it assigns Redirects correctly', function(assert) {
|
|
|
|
return get(request.url, {
|
|
|
|
headers: {
|
|
|
|
cookie: {
|
|
|
|
CONSUL_RESOLVER_COUNT: 1,
|
|
|
|
CONSUL_REDIRECT_COUNT: 1,
|
|
|
|
CONSUL_FAILOVER_COUNT: 0,
|
|
|
|
CONSUL_SUBSET_COUNT: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-16 16:31:09 +00:00
|
|
|
const childId = Object.keys(Chain.Targets)[1];
|
|
|
|
const target = Chain.Targets[`${childId}`];
|
|
|
|
const firstChild = actual[0].Children[0];
|
2021-12-09 10:47:58 +00:00
|
|
|
assert.equal(firstChild.Redirect, 'Datacenter');
|
2020-01-16 16:31:09 +00:00
|
|
|
assert.equal(firstChild.ID, target.ID);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
test('it assigns Failovers to Subsets correctly', function(assert) {
|
|
|
|
return Promise.all(
|
|
|
|
['Datacenter', 'Namespace'].map(function(failoverType) {
|
|
|
|
return get(request.url, {
|
|
|
|
headers: {
|
|
|
|
cookie: {
|
|
|
|
CONSUL_RESOLVER_COUNT: 1,
|
|
|
|
CONSUL_REDIRECT_COUNT: 0,
|
|
|
|
CONSUL_SUBSET_COUNT: 1,
|
|
|
|
CONSUL_FAILOVER_COUNT: 1,
|
|
|
|
CONSUL_FAILOVER_TYPE: failoverType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-16 16:31:09 +00:00
|
|
|
const actualSubset = actual[0].Children[0];
|
|
|
|
assert.equal(actualSubset.Subset, true);
|
|
|
|
assert.equal(actualSubset.Failover.Type, failoverType);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
test('it assigns Failovers correctly', function(assert) {
|
|
|
|
return Promise.all(
|
2021-12-06 10:22:09 +00:00
|
|
|
['Datacenter', 'Partition', 'Namespace'].map(function(failoverType, i) {
|
2020-01-16 16:31:09 +00:00
|
|
|
return get(request.url, {
|
|
|
|
headers: {
|
|
|
|
cookie: {
|
|
|
|
CONSUL_RESOLVER_COUNT: 1,
|
|
|
|
CONSUL_REDIRECT_COUNT: 0,
|
|
|
|
CONSUL_SUBSET_COUNT: 0,
|
|
|
|
CONSUL_FAILOVER_COUNT: 1,
|
|
|
|
CONSUL_FAILOVER_TYPE: failoverType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-16 16:31:09 +00:00
|
|
|
const node = Chain.Nodes[`resolver:${Object.keys(Chain.Targets)[0]}`];
|
|
|
|
const expected = node.Resolver.Failover.Targets.map(item => item.split('.').reverse()[i]);
|
|
|
|
assert.equal(actual[0].Failover.Type, failoverType);
|
|
|
|
assert.deepEqual(actual[0].Failover.Targets, expected);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2020-01-30 16:09:05 +00:00
|
|
|
test('it finds subsets with failovers correctly', function(assert) {
|
|
|
|
return Promise.resolve({
|
|
|
|
Chain: {
|
|
|
|
ServiceName: 'service-name',
|
|
|
|
Namespace: 'default',
|
|
|
|
Datacenter: 'dc-1',
|
|
|
|
Protocol: 'http',
|
|
|
|
StartNode: '',
|
|
|
|
Nodes: {
|
2021-12-06 10:22:09 +00:00
|
|
|
'resolver:v2.dc-failover.default.default.dc-1': {
|
2020-01-30 16:09:05 +00:00
|
|
|
Type: 'resolver',
|
2021-12-06 10:22:09 +00:00
|
|
|
Name: 'v2.dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Resolver: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Target: 'v2.dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Failover: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Targets: [
|
|
|
|
'v2.dc-failover.default.default.dc-5',
|
|
|
|
'v2.dc-failover.default.default.dc-6',
|
|
|
|
],
|
2020-01-30 16:09:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: {
|
2021-12-06 10:22:09 +00:00
|
|
|
'v2.dc-failover.default.default.dc-1': {
|
|
|
|
ID: 'v2.dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Service: 'dc-failover',
|
|
|
|
Namespace: 'default',
|
2021-12-06 10:22:09 +00:00
|
|
|
Partition: 'default',
|
2020-01-30 16:09:05 +00:00
|
|
|
Datacenter: 'dc-1',
|
|
|
|
Subset: {
|
|
|
|
Filter: '',
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 10:22:09 +00:00
|
|
|
'v2.dc-failover.default.default.dc-6': {
|
|
|
|
ID: 'v2.dc-failover.default.default.dc-6',
|
2020-01-30 16:09:05 +00:00
|
|
|
Service: 'dc-failover',
|
|
|
|
Namespace: 'default',
|
2021-12-06 10:22:09 +00:00
|
|
|
Partition: 'default',
|
2020-01-30 16:09:05 +00:00
|
|
|
Datacenter: 'dc-6',
|
|
|
|
Subset: {
|
|
|
|
Filter: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-30 16:09:05 +00:00
|
|
|
const expected = {
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Name: 'dc-failover',
|
|
|
|
Children: [
|
|
|
|
{
|
|
|
|
Subset: true,
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'v2.dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Name: 'v2',
|
|
|
|
Failover: {
|
|
|
|
Type: 'Datacenter',
|
|
|
|
Targets: ['dc-5', 'dc-6'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
assert.deepEqual(actual[0], expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
test('it finds services with failovers correctly', function(assert) {
|
|
|
|
return Promise.resolve({
|
|
|
|
Chain: {
|
|
|
|
ServiceName: 'service-name',
|
|
|
|
Namespace: 'default',
|
|
|
|
Datacenter: 'dc-1',
|
|
|
|
Protocol: 'http',
|
|
|
|
StartNode: '',
|
|
|
|
Nodes: {
|
2021-12-06 10:22:09 +00:00
|
|
|
'resolver:dc-failover.default.default.dc-1': {
|
2020-01-30 16:09:05 +00:00
|
|
|
Type: 'resolver',
|
2021-12-06 10:22:09 +00:00
|
|
|
Name: 'dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Resolver: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Target: 'dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Failover: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Targets: ['dc-failover.default.default.dc-5', 'dc-failover.default.default.dc-6'],
|
2020-01-30 16:09:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: {
|
2021-12-06 10:22:09 +00:00
|
|
|
'dc-failover.default.default.dc-1': {
|
|
|
|
ID: 'dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Service: 'dc-failover',
|
|
|
|
Namespace: 'default',
|
2021-12-06 10:22:09 +00:00
|
|
|
Partition: 'default',
|
2020-01-30 16:09:05 +00:00
|
|
|
Datacenter: 'dc-1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-01-30 16:09:05 +00:00
|
|
|
const expected = {
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'dc-failover.default.default.dc-1',
|
2020-01-30 16:09:05 +00:00
|
|
|
Name: 'dc-failover',
|
|
|
|
Children: [],
|
|
|
|
Failover: {
|
|
|
|
Type: 'Datacenter',
|
|
|
|
Targets: ['dc-5', 'dc-6'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
assert.deepEqual(actual[0], expected);
|
|
|
|
});
|
|
|
|
});
|
2020-02-12 17:47:52 +00:00
|
|
|
test('it finds services with redirects with failovers correctly', function(assert) {
|
|
|
|
return Promise.resolve({
|
|
|
|
Chain: {
|
|
|
|
ServiceName: 'service-name',
|
|
|
|
Namespace: 'default',
|
2021-12-06 10:22:09 +00:00
|
|
|
Partition: 'default',
|
2020-02-12 17:47:52 +00:00
|
|
|
Datacenter: 'dc-1',
|
|
|
|
Protocol: 'http',
|
|
|
|
StartNode: '',
|
|
|
|
Nodes: {
|
2021-12-06 10:22:09 +00:00
|
|
|
'resolver:dc-failover.default.default.redirect-dc-1': {
|
2020-02-12 17:47:52 +00:00
|
|
|
Type: 'resolver',
|
2021-12-06 10:22:09 +00:00
|
|
|
Name: 'dc-failover.default.default.redirect-dc-1',
|
2020-02-12 17:47:52 +00:00
|
|
|
Resolver: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Target: 'dc-failover.default.default.redirect-dc-1',
|
2020-02-12 17:47:52 +00:00
|
|
|
Failover: {
|
2021-12-06 10:22:09 +00:00
|
|
|
Targets: [
|
|
|
|
'dc-failover.default.default.redirect-dc-5',
|
|
|
|
'dc-failover.default.default.redirect-dc-6',
|
|
|
|
],
|
2020-02-12 17:47:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: {
|
2021-12-09 10:47:58 +00:00
|
|
|
'dc-failover.default.default.redirect-dc-1': {
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'dc-failover.default.default.redirect-dc-1',
|
2020-02-12 17:47:52 +00:00
|
|
|
Service: 'dc-failover',
|
|
|
|
Namespace: 'default',
|
2021-12-06 10:22:09 +00:00
|
|
|
Partition: 'default',
|
2020-02-12 17:47:52 +00:00
|
|
|
Datacenter: 'redirect-dc-1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).then(function({ Chain }) {
|
2021-12-06 10:22:09 +00:00
|
|
|
const actual = getResolvers(dc, partition, nspace, Chain.Targets, Chain.Nodes);
|
2020-02-12 17:47:52 +00:00
|
|
|
// Both the parent and the child should have a Failover property
|
|
|
|
// as in order for a redirect to have failovers it must redirect to a
|
|
|
|
// service that already has failovers
|
|
|
|
const expected = {
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'dc-failover.default.default.dc-1',
|
2020-02-12 17:47:52 +00:00
|
|
|
Name: 'dc-failover',
|
|
|
|
Failover: {
|
|
|
|
Targets: ['redirect-dc-5', 'redirect-dc-6'],
|
|
|
|
Type: 'Datacenter',
|
|
|
|
},
|
|
|
|
Children: [
|
|
|
|
{
|
|
|
|
Failover: {
|
|
|
|
Targets: ['redirect-dc-5', 'redirect-dc-6'],
|
|
|
|
Type: 'Datacenter',
|
|
|
|
},
|
2021-12-06 10:22:09 +00:00
|
|
|
ID: 'dc-failover.default.default.redirect-dc-1',
|
2020-02-12 17:47:52 +00:00
|
|
|
Name: 'redirect-dc-1',
|
2021-12-09 10:47:58 +00:00
|
|
|
Redirect: 'Datacenter',
|
2020-02-12 17:47:52 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
assert.deepEqual(actual[0], expected);
|
|
|
|
});
|
|
|
|
});
|
2020-01-16 16:31:09 +00:00
|
|
|
});
|