diff options
author | Alex Shatov <alexs@att.com> | 2017-09-27 18:51:43 -0400 |
---|---|---|
committer | Alex Shatov <alexs@att.com> | 2017-09-27 18:51:43 -0400 |
commit | a2ffa33f55ae178e91df119aa19d2bede35d083f (patch) | |
tree | 67fd5579f3a8a599093f8b6cf12648766ce7b826 | |
parent | f7f1ab925d8e9e81cb28f4bc3f5ae2be8bdf5f98 (diff) |
51% unit test of the deployment-handler
* only unit testing the /policy/components at the moment
* use -f Dockerfile_UT when building the image
* unit test runs inside docker container
* after docker run finishes copy the logs and coverage folder
from docker container. Sample commands:
docker cp test_deployment_handler:/opt/app/dh test_logs/
docker logs test_deployment_handler > \
test_logs/$(date +%Y_%m%d-%H%M%S)_test_deployment_handler.log 2>&1
Change-Id: I77276550e2ffb7094e02ffa20741afac6bfea96f
Issue-Id: DCAEGEN2-62
Signed-off-by: Alex Shatov <alexs@att.com>
-rw-r--r-- | Dockerfile_UT | 34 | ||||
-rw-r--r-- | deployment-handler.js | 51 | ||||
-rw-r--r-- | package.json | 18 | ||||
-rw-r--r-- | tests/test_policy.js | 221 |
4 files changed, 295 insertions, 29 deletions
diff --git a/Dockerfile_UT b/Dockerfile_UT new file mode 100644 index 0000000..3665bf2 --- /dev/null +++ b/Dockerfile_UT @@ -0,0 +1,34 @@ +FROM node:6.10.3 +MAINTAINER maintainer +ENV INSROOT /opt/app +ENV APPUSER dh +ENV APPDIR ${INSROOT}/${APPUSER} + +WORKDIR ${APPDIR} + +RUN mkdir -p ${APPDIR}/lib \ + && mkdir -p ${APPDIR}/tests \ + && mkdir -p ${APPDIR}/etc \ + && mkdir -p ${APPDIR}/log \ + && useradd -d ${APPDIR} ${APPUSER} + +COPY *.js ${APPDIR}/ +COPY *.json ${APPDIR}/ +COPY *.yaml ${APPDIR}/ +COPY ./lib/ ${APPDIR}/lib/ +COPY ./tests/ ${APPDIR}/tests/ +COPY ./etc/log4js.json ${APPDIR}/etc/log4js.json + +RUN npm install \ + && chown -R ${APPUSER}:${APPUSER} ${APPDIR} \ + && chmod 777 ${APPDIR}/lib \ + && chmod 777 ${APPDIR}/tests \ + && chmod 777 ${APPDIR}/log \ + && chmod 777 ${APPDIR}/etc \ + && ls -la && ls -la ./tests + +USER ${APPUSER} +VOLUME ${APPDIR}/log +EXPOSE 8443 +# ENTRYPOINT ["/usr/local/bin/npm", "test"] +ENTRYPOINT ["/usr/local/bin/npm", "run", "test-cov"] diff --git a/deployment-handler.js b/deployment-handler.js index 01455d4..171f73c 100644 --- a/deployment-handler.js +++ b/deployment-handler.js @@ -34,6 +34,27 @@ const DEPLOYMENTS_PATH = "/dcae-deployments"; const POLICY_PATH = "/policy"; const SWAGGER_UI_PATH = "/swagger-ui"; +const app = express(); + +/* Set up the application */ +app.set('x-powered-by', false); +app.set('etag', false); + +/* Give each request a unique request ID */ +app.use(require('./lib/middleware').assignId); + +/* If authentication is set up, check it */ +app.use(require('./lib/auth').checkAuth); + +/* Set up API routes */ +app.use(INFO_PATH, require('./lib/info')); +app.use(DEPLOYMENTS_PATH, require('./lib/dcae-deployments')); +app.use(POLICY_PATH, require('./lib/policy')); +app.use(SWAGGER_UI_PATH, require('./lib/swagger-ui')); + +/* Set up error handling */ +app.use(require('./lib/middleware').handleErrors); + const start = function(config) { const startTime = new Date(); @@ -56,26 +77,6 @@ const start = function(config) { log.debug(null, "Configuration: " + JSON.stringify(config)); - /* Set up the application */ - const app = express(); - app.set('x-powered-by', false); - app.set('etag', false); - - /* Give each request a unique request ID */ - app.use(require('./lib/middleware').assignId); - - /* If authentication is set up, check it */ - app.use(require('./lib/auth').checkAuth); - - /* Set up API routes */ - app.use(INFO_PATH, require('./lib/info')); - app.use(DEPLOYMENTS_PATH, require('./lib/dcae-deployments')); - app.use(POLICY_PATH, require('./lib/policy')); - app.use(SWAGGER_UI_PATH, require('./lib/swagger-ui')); - - /* Set up error handling */ - app.use(require('./lib/middleware').handleErrors); - /* Start the server */ var server = null; var usingTLS = false; @@ -105,7 +106,7 @@ const start = function(config) { server.listen(config.listenPort, config.listenHost, function(err) { var addr = server.address(); - var msg = ("Dispatcher version " + config.version + " listening on " + var msg = ("Deployment-handler version " + config.version + " listening on " + addr.address + ":" + addr.port + " pid: " + process.pid + (usingTLS ? " " : " not ") + "using TLS (HTTPS)"); log.metrics(null, {startTime: startTime, complete: true}, msg); @@ -143,7 +144,9 @@ conf.configure() .then(start) .catch(function(e) { log.error(e.logCode ? e : createError( - 'Dispatcher exiting due to start-up problem: ' + e.message, 500, + 'Deployment-handler exiting due to start-up problem: ' + e.message, 500, 'system', 552)); - console.error("Dispatcher exiting due to startup problem: " + e.message); -});
\ No newline at end of file + console.error("Deployment-handler exiting due to startup problem: " + e.message); +}); + +module.exports = app;
\ No newline at end of file diff --git a/package.json b/package.json index ec38a72..b6215cf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "DCAE-Orch-Deployment-Handler", - "version": "4.2.0", - "description": "DCAE Orchestrator Deployment Handler", + "name": "onap-dcae-deployment-handler", + "version": "4.2.1", + "description": "ONAP DCAE Deployment Handler", "main": "deployment-handler.js", "dependencies": { "adm-zip": "^0.4.7", @@ -12,9 +12,17 @@ "uuid": "^3.0.1", "yamljs": "latest" }, - "devDependencies": {}, + "devDependencies": { + "chai": "^4.1.2", + "chai-http": "^3.0.0", + "istanbul": "^0.4.5", + "mocha": "^3.5.3", + "nock": "^9.0.21" + }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha --reporter spec tests/", + "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec tests/", + "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot tests/" }, "author": "author", "license": "(Apache-2.0)" diff --git a/tests/test_policy.js b/tests/test_policy.js new file mode 100644 index 0000000..3c1a5da --- /dev/null +++ b/tests/test_policy.js @@ -0,0 +1,221 @@ +/* +Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. + +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and limitations under the License. +*/ + +/** + * handling policy updates + */ + +"use strict"; + +const nock = require('nock'); +const chai = require('chai') + , chaiHttp = require('chai-http') + , expect = chai.expect; + +chai.use(chaiHttp); + +const REQ_ID = "111"; +const RUN_TS = new Date(); +const RUN_TS_HOURS = RUN_TS.getHours(); + +const CONSUL_URL = 'http://consul:8500'; +const TEST_CLOUDIFY_MANAGER = "test_cloudify_manager"; +const CLOUDIFY_URL = "http://" + TEST_CLOUDIFY_MANAGER + ":80"; + +const POLICY_ID = 'policy_id'; +const POLICY_VERSION = "policyVersion"; +const POLICY_NAME = "policyName"; +const POLICY_BODY = 'policy_body'; +const POLICY_CONFIG = 'config'; + +const MONKEYED_POLICY_ID = "DCAE_alex.Config_peach" +const MONKEYED_POLICY_ID_2 = "DCAE_alex.Config_peach_2" + +function create_policy_body(policy_id, policy_version=1) { + const prev_ver = policy_version - 1; + const timestamp = new Date(RUN_TS.getTime()); + timestamp.setHours(RUN_TS_HOURS + prev_ver); + + const this_ver = policy_version.toString(); + const config = { + "policy_updated_from_ver": prev_ver.toString(), + "policy_updated_to_ver": this_ver.toString(), + "policy_hello": "world!", + "policy_updated_ts": timestamp, + "updated_policy_id": policy_id + }; + return { + "policyConfigMessage": "Config Retrieved! ", + "policyConfigStatus": "CONFIG_RETRIEVED", + "type": "JSON", + POLICY_NAME: policy_id + "." + this_ver + ".xml", + POLICY_VERSION: this_ver, + POLICY_CONFIG: config, + "matchingConditions": { + "ECOMPName": "DCAE", + "ConfigName": "alex_config_name" + }, + "responseAttributes": {}, + "property": null + }; +} + +function create_policy(policy_id, policy_version=1) { + return { + POLICY_ID : policy_id, + POLICY_BODY : MonkeyedPolicyBody.create_policy_body(policy_id, policy_version) + }; +} + +nock(CONSUL_URL).persist().get('/v1/kv/deployment_handler?raw') + .reply(200, {"logLevel": "DEBUG", "cloudify": {"protocol": "http"}}); + +nock(CONSUL_URL).persist().get('/v1/catalog/service/cloudify_manager') + .reply(200, [{ + "ID":"deadbeef-dead-beef-dead-beefdeadbeef", + "Node":"devorcl00", + "Address": TEST_CLOUDIFY_MANAGER, + "Datacenter":"rework-central", + "TaggedAddresses":{"lan": TEST_CLOUDIFY_MANAGER,"wan": TEST_CLOUDIFY_MANAGER}, + "NodeMeta":{}, + "ServiceID":"cloudify_manager", + "ServiceName":"cloudify_manager", + "ServiceTags":["http://" + TEST_CLOUDIFY_MANAGER + "/api/v2.1"], + "ServiceAddress": TEST_CLOUDIFY_MANAGER, + "ServicePort":80, + "ServiceEnableTagOverride":false, + "CreateIndex":16, + "ModifyIndex":16 + }]); + +nock(CONSUL_URL).persist().get('/v1/catalog/service/inventory') + .reply(200, [{ + "ID": "", + "Node": "inventory_test", + "Address": "inventory", + "Datacenter": "rework-central", + "TaggedAddresses": null, + "NodeMeta": null, + "ServiceID": "inventory", + "ServiceName": "inventory", + "ServiceTags": [], + "ServiceAddress": "inventory", + "ServicePort": 8080, + "ServiceEnableTagOverride": false, + "CreateIndex": 8068, + "ModifyIndex": 8068 + }]); + +nock(CLOUDIFY_URL).persist().get(/[/]api[/]v2[.]1[/]node-instances/) + .reply(200, { + "items": [ + { + "deployment_id": "demo_dcae_policy_depl", + "id": "host_vm_163f7", + "runtime_properties": { + "application_config": { + "capacity_ts": "2017-09-07T16:54:31.696Z", + "capacity": "123", + "policy_hello": "world!", + "policy_updated_ts": "2017-09-05T18:09:54.109548Z", + "policy_updated_from_ver": "20", + "location": "neverland", + "updated_policy_id": MONKEYED_POLICY_ID_2, + "policy_updated_to_ver": "21", + "location_ts": "2017-09-07T16:54:31.696Z" + }, + "execute_operation": "policy_update", + "service_component_name": "2caa5ccf-bfc6-4a75-aca7-4af03745f478.unknown.unknown.unknown.dcae.onap.org", + "exe_task": "node_configure", + "policies": { + "DCAE_alex.Config_host_location_policy_id_value": { + "policy_required": true, + "policy_body": create_policy_body(MONKEYED_POLICY_ID, 55), + "policy_id": MONKEYED_POLICY_ID + }, + "DCAE_alex.Config_host_capacity_policy_id_value": { + "policy_required": true, + "policy_body": create_policy_body(MONKEYED_POLICY_ID_2, 21), + "policy_id": MONKEYED_POLICY_ID_2 + } + } + } + } + ], + "metadata": { + "pagination": { + "total": 1, + "offset": 0, + "size": 10000 + } + } + }); + +describe('test policy on deployment-handler', () => { + it('starting', function() { + console.log("go testing deployment-handler"); + + const conf = require('./../lib/config'); + const logging = require('./../lib/logging'); + const log = logging.getLogger(); + + console.log("started logger"); + log.debug(REQ_ID, "started logger"); + + console.log("conf.configure"); + + return conf.configure() + .then(function(config) { + logging.setLevel(config.logLevel); + + /* Set up exported configuration */ + config.apiLinks = {"test" : true}; + // exports.config = config; + process.mainModule.exports.config = config; + + console.log("got configuration:", JSON.stringify(config)); + + log.debug(REQ_ID, "Configuration: " + JSON.stringify(config)); + const main_app = require('./../deployment-handler'); + console.log("loaded main_app"); + + const req_path = "/policy/components"; + const test_txt = "GET " + req_path; + describe(test_txt, () => { + console.log(test_txt); + it('GET all the components with policy from cloudify', function() { + console.log("chai", test_txt); + return chai.request(main_app).get(req_path) + .then(function(res) { + console.log("res for", test_txt, JSON.stringify(res.body)); + log.debug(REQ_ID, "received " + JSON.stringify(res.body)); + expect(res).to.have.status(200); + expect(res).to.be.json; + }) + .catch(function(err) { + console.error("err for", test_txt, err); + throw err; + }); + }); + }); + }) + .catch(function(e) { + const error = "test of deployment-handler exiting due to startup problem: " + e.message; + console.error(error); + throw e; + }); + }); +});
\ No newline at end of file |