From e9ed49d9aaf09308355aca97fc79cbf1053ef4fd Mon Sep 17 00:00:00 2001 From: John Cowen Date: Wed, 12 Feb 2020 17:47:52 +0000 Subject: [PATCH] ui: Test coverage developer experience (#7275) * ui: Add getSplitters unit test and additional getResolvers test * Make ordering of makefile target consistent --- ui-v2/GNUmakefile | 4 +- .../utils/components/discovery-chain/index.js | 2 +- ui-v2/package.json | 2 +- .../discovery-chain/get-resolvers-test.js | 60 ++++++++++++++++++- .../discovery-chain/get-splitters-test.js | 54 +++++++++++++++++ 5 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 ui-v2/tests/unit/utils/components/discovery-chain/get-splitters-test.js diff --git a/ui-v2/GNUmakefile b/ui-v2/GNUmakefile index 6236ecf99e..1789a9cd4e 100644 --- a/ui-v2/GNUmakefile +++ b/ui-v2/GNUmakefile @@ -73,8 +73,8 @@ specify-coverage: test-coverage: deps specify-coverage yarn run test:coverage -test-view-coverage: deps specify-coverage - yarn run test:view:coverage +test-coverage-view: deps specify-coverage + yarn run test:coverage:view test-parallel: deps yarn run test:parallel diff --git a/ui-v2/app/utils/components/discovery-chain/index.js b/ui-v2/app/utils/components/discovery-chain/index.js index cf55773150..0263ced36f 100644 --- a/ui-v2/app/utils/components/discovery-chain/index.js +++ b/ui-v2/app/utils/components/discovery-chain/index.js @@ -39,7 +39,7 @@ export const getSplitters = function(nodes) { // Splitters need IDs adding so we can find them in the DOM later // splitters have a service.nspace as a name // do the reverse dance to ensure we don't mess up any - // serivice names with dots in them + // service names with dots in them const temp = item.Name.split('.'); temp.reverse(); temp.shift(); diff --git a/ui-v2/package.json b/ui-v2/package.json index 214e0f4ba0..53a2cf712f 100644 --- a/ui-v2/package.json +++ b/ui-v2/package.json @@ -30,7 +30,7 @@ "test:oss:view": "CONSUL_NSPACES_ENABLED=0 ember test --server --test-port=${EMBER_TEST_PORT:-7357}", "test:node": "tape ./node-tests/**/*.js", "test:coverage": "COVERAGE=true ember test --environment test --filter=Unit --test-port=${EMBER_TEST_PORT:-7357}", - "test:view:coverage": "COVERAGE=true ember test --server --environment test --filter=Unit --test-port=${EMBER_TEST_PORT:-7357}", + "test:coverage:view": "COVERAGE=true ember test --server --environment test --filter=Unit --test-port=${EMBER_TEST_PORT:-7357}", "steps:list": "node ./lib/commands/bin/list.js" }, "husky": { diff --git a/ui-v2/tests/unit/utils/components/discovery-chain/get-resolvers-test.js b/ui-v2/tests/unit/utils/components/discovery-chain/get-resolvers-test.js index 7463bcc123..8223d60c37 100644 --- a/ui-v2/tests/unit/utils/components/discovery-chain/get-resolvers-test.js +++ b/ui-v2/tests/unit/utils/components/discovery-chain/get-resolvers-test.js @@ -105,7 +105,7 @@ module('Unit | Utility | components/discovery-chain/get-resolvers', function() { Type: 'resolver', Name: 'v2.dc-failover.default.dc-1', Resolver: { - Target: 'v2.dc-failover.defauilt.dc-1', + Target: 'v2.dc-failover.default.dc-1', Failover: { Targets: ['v2.dc-failover.default.dc-5', 'v2.dc-failover.default.dc-6'], }, @@ -166,7 +166,7 @@ module('Unit | Utility | components/discovery-chain/get-resolvers', function() { Type: 'resolver', Name: 'dc-failover.default.dc-1', Resolver: { - Target: 'dc-failover.defauilt.dc-1', + Target: 'dc-failover.default.dc-1', Failover: { Targets: ['dc-failover.default.dc-5', 'dc-failover.default.dc-6'], }, @@ -196,4 +196,60 @@ module('Unit | Utility | components/discovery-chain/get-resolvers', function() { assert.deepEqual(actual[0], expected); }); }); + test('it finds services with redirects with failovers correctly', function(assert) { + return Promise.resolve({ + Chain: { + ServiceName: 'service-name', + Namespace: 'default', + Datacenter: 'dc-1', + Protocol: 'http', + StartNode: '', + Nodes: { + 'resolver:dc-failover.default.redirect-dc-1': { + Type: 'resolver', + Name: 'dc-failover.default.redirect-dc-1', + Resolver: { + Target: 'dc-failover.default.redirect-dc-1', + Failover: { + Targets: ['dc-failover.default.redirect-dc-5', 'dc-failover.default.redirect-dc-6'], + }, + }, + }, + }, + Targets: { + 'dc-failover.default.redirect-dc-1': { + ID: 'dc-failover.default.redirect-dc-1', + Service: 'dc-failover', + Namespace: 'default', + Datacenter: 'redirect-dc-1', + }, + }, + }, + }).then(function({ Chain }) { + const actual = getResolvers(dc, nspace, Chain.Targets, Chain.Nodes); + // 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 = { + ID: 'dc-failover.default.dc-1', + Name: 'dc-failover', + Failover: { + Targets: ['redirect-dc-5', 'redirect-dc-6'], + Type: 'Datacenter', + }, + Children: [ + { + Failover: { + Targets: ['redirect-dc-5', 'redirect-dc-6'], + Type: 'Datacenter', + }, + ID: 'dc-failover.default.redirect-dc-1', + Name: 'redirect-dc-1', + Redirect: true, + }, + ], + }; + assert.deepEqual(actual[0], expected); + }); + }); }); diff --git a/ui-v2/tests/unit/utils/components/discovery-chain/get-splitters-test.js b/ui-v2/tests/unit/utils/components/discovery-chain/get-splitters-test.js new file mode 100644 index 0000000000..ca1dc500d6 --- /dev/null +++ b/ui-v2/tests/unit/utils/components/discovery-chain/get-splitters-test.js @@ -0,0 +1,54 @@ +import { getSplitters } from 'consul-ui/utils/components/discovery-chain/index'; +import { module, test } from 'qunit'; + +module('Unit | Utility | components/discovery-chain/get-splitters', function() { + test('it collects and correctly parses splitter Names', function(assert) { + const actual = getSplitters({ + 'splitter:splitter-name.default': { + Type: 'splitter', + Name: 'splitter-name.default', + Splits: [ + { + Weight: 50, + NextNode: '', + }, + { + Weight: 50, + NextNode: '', + }, + ], + }, + 'splitter:not-splitter-name.default': { + Type: 'not-splitter', + Name: 'splitter-name.default', + Splits: [ + { + Weight: 50, + NextNode: '', + }, + { + Weight: 50, + NextNode: '', + }, + ], + }, + }); + const expected = { + Type: 'splitter', + Name: 'splitter-name', + ID: 'splitter:splitter-name.default', + Splits: [ + { + Weight: 50, + NextNode: '', + }, + { + Weight: 50, + NextNode: '', + }, + ], + }; + assert.equal(actual.length, 1); + assert.deepEqual(actual[0], expected); + }); +});