diff options
Diffstat (limited to 'tests/test_policy.js')
-rw-r--r-- | tests/test_policy.js | 589 |
1 files changed, 424 insertions, 165 deletions
diff --git a/tests/test_policy.js b/tests/test_policy.js index c0ad243..0d7550e 100644 --- a/tests/test_policy.js +++ b/tests/test_policy.js @@ -1,5 +1,5 @@ /* -Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. +Copyright(c) 2017-2018 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. @@ -20,205 +20,464 @@ See the License for the specific language governing permissions and limitations "use strict"; -const nock = require('nock'); -const chai = require('chai') +const nock = require('nock') + , chai = require('chai') , chaiHttp = require('chai-http') - , expect = chai.expect; + , expect = chai.expect + , assert = chai.assert; chai.use(chaiHttp); -const REQ_ID = "111"; +const dh = require('./mock_deployment_handler'); +const utils = require('./mock_utils'); + 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" +const BLUEPRINT_ID = "demo_dcaepolicy"; +const DEPLOYMENT_ID = "demo_dcae_policy_depl"; +const OPERATION_POLICY_UPDATE = "dcae.interfaces.policy.policy_update"; +const EXECUTE_OPERATION = "execute_operation"; + +const MONKEYED_POLICY_ID = "DCAE_alex.Config_peach"; +const MONKEYED_POLICY_ID_2 = "DCAE_alex.Config_peach_2"; +const MONKEYED_POLICY_ID_3 = "DCAE_alex.Config_peach_3"; +const MONKEYED_POLICY_ID_4 = "DCAE_alex.Config_peach_4"; +const MONKEYED_POLICY_ID_5 = "DCAE_alex.Config_peach_5"; +const MONKEYED_POLICY_ID_6 = "DCAE_alex.Config_peach_6"; +const CLAMP_POLICY_ID = "CLAMP.Config_clamp_policy"; -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 CFY_API = "/api/v2.1"; +const CFY_API_NODE_INSTANCES = CFY_API + "/node-instances"; +const CFY_API_EXECUTIONS = CFY_API + "/executions"; +const CFY_API_EXECUTION = CFY_API_EXECUTIONS + "/"; +function create_policy_body(policy_id, policy_version=1, matching_conditions=null) { 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 + + const matchingConditions = { + "ONAPName": "DCAE", + "ConfigName": "alex_config_name" }; + if (matching_conditions) { + Object.assign(matchingConditions, matching_conditions); + } 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" - }, + [POLICY_NAME]: (policy_id && (policy_id + "." + this_ver + ".xml") || null), + [POLICY_VERSION]: this_ver, + [POLICY_CONFIG]: {"policy_hello": "world!"}, + "matchingConditions": matchingConditions, "responseAttributes": {}, "property": null }; } -function create_policy(policy_id, policy_version=1) { +function create_policy(policy_id, policy_version=1, matching_conditions=null) { return { - POLICY_ID : policy_id, - POLICY_BODY : MonkeyedPolicyBody.create_policy_body(policy_id, policy_version) + [POLICY_ID] : policy_id, + [POLICY_BODY] : create_policy_body(policy_id, policy_version, matching_conditions) }; } -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 - } +const message_catch_up = { + "errored_scopes": [], + "catch_up": true, + "scope_prefixes": ["DCAE_alex.Config_", "DCAE.Config_"], + "errored_policies": {}, + "latest_policies": {} +}; + +const cloudify_node_instances = [ + { + "deployment_id": DEPLOYMENT_ID, + "id": "host_vm_163f7", + "runtime_properties": { + "application_config": { + "policy_hello": "world!", + "location": "neverland", + "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": { + [MONKEYED_POLICY_ID]: { + "policy_required": true, + "policy_persistent": true, + "policy_body": create_policy_body(MONKEYED_POLICY_ID, 55), + "policy_id": MONKEYED_POLICY_ID + }, + [MONKEYED_POLICY_ID_2]: { + "policy_persistent": false, + "policy_body": create_policy_body(MONKEYED_POLICY_ID_2, 21, {"key1": "value1"}), + "policy_id": MONKEYED_POLICY_ID_2 + }, + [MONKEYED_POLICY_ID_3]: { + "policy_persistent": false, + "policy_body": create_policy_body(MONKEYED_POLICY_ID_3, 33, {"service": "alex_service"}), + "policy_id": MONKEYED_POLICY_ID_3 + }, + [MONKEYED_POLICY_ID_5]: { + "policy_persistent": false, + "policy_body": create_policy_body(MONKEYED_POLICY_ID_5, 1), + "policy_id": MONKEYED_POLICY_ID_5 + }, + [CLAMP_POLICY_ID]: { + "policy_persistent": false, + "policy_body": create_policy_body(CLAMP_POLICY_ID, 9), + "policy_id": CLAMP_POLICY_ID + } + }, + "policy_filters": { + "db_client_policies_c83de": { + "policy_filter_id": "db_client_policies_c83de", + "policy_filter": { + "policyName": MONKEYED_POLICY_ID_2 + ".*", + "unique": false, + "onapName": "DCAE", + "configName": "alex_config_name", + "configAttributes": {"key1": "value1"} + } + }, + "db_client_policies_microservice_09f09": { + "policy_filter_id": "db_client_policies_microservice_09f09", + "policy_filter": { + "policyName": MONKEYED_POLICY_ID + ".*", + "unique": false, + "onapName": "DCAE", + "configName": "alex_config_name", + "configAttributes": {"service": "alex_service"} + } + }, + "policy_filter_by_id_02d02": { + "policy_filter_id": "policy_filter_by_id_02d02", + "policy_filter": { + "policyName": MONKEYED_POLICY_ID_6 + } + }, + "new_policies_09f09": { + "policy_filter_id": "new_policies_09f09", + "policy_filter": { + "policyName": MONKEYED_POLICY_ID_4 + ".*", + "unique": false, + "onapName": "DCAE", + "configName": "alex_config_name", + "configAttributes": {"service": "alex_service"} + } + }, + "db_client_policies_not_found_cfed6": { + "policy_filter_id": "db_client_policies_not_found_cfed6", + "policy_filter": { + "configAttributes": {"not-to-be-found": "ever"}, + "unique": false, + "onapName": "DCAE", + "policyName": "DCAE_alex.Config_not_found_ever_.*" + } + }, + "filter_without_policy_name_22abcd": { + "policy_filter_id": "filter_without_policy_name", + "policy_filter": {"onapName": "DCAE"} + }, + "db_client_policies_no_match_afed8": { + "policy_filter_id": "db_client_policies_no_match_afed8", + "policy_filter": { + "policyName": "DCAE_alex.Config_not_found_ever_.*" } } } - ], - "metadata": { - "pagination": { - "total": 1, - "offset": 0, - "size": 10000 - } } - }); + }, + { + "deployment_id": DEPLOYMENT_ID, + "id": "no_policies_on_node_1212beef", + "runtime_properties": {"application_config": {}} + }, + { + "deployment_id": DEPLOYMENT_ID, + "id": "no_policy_filters_on_node_55ham", + "runtime_properties": { + "application_config": {}, + "policies": {} + } + } +]; -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("setting main_app..."); - main_app.set_app(); - console.log("set 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.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; - }); - }); +function nock_cfy_node_instances(action_timer) { + nock(dh.CLOUDIFY_URL).get(CFY_API_NODE_INSTANCES).query(true) + .reply(200, function(uri) { + console.log(action_timer.step, "get", dh.CLOUDIFY_URL, uri); + return JSON.stringify({ + "items": cloudify_node_instances, + "metadata": {"pagination": {"total": cloudify_node_instances.length, "offset": 0, "size": 10000}} }); - }) - .catch(function(e) { - const error = "test of deployment-handler exiting due to startup problem: " + e.message; - console.error(error); - throw e; }); +} + +function test_get_policy_components(dh_server) { + const req_path = "/policy/components"; + const test_txt = "GET " + req_path; + describe(test_txt, () => { + it('GET all the components with policy from cloudify', function() { + const action_timer = new utils.ActionTimer(); + console.log(action_timer.step, test_txt); + nock_cfy_node_instances(action_timer); + + return chai.request(dh_server.app).get(req_path) + .then(function(res) { + console.log(action_timer.step, "res for", test_txt, res.text); + expect(res).to.have.status(200); + expect(res).to.be.json; + }) + .catch(function(err) { + console.error(action_timer.step, "err for", test_txt, err); + throw err; + }); + }); + }); +} + +function test_post_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = JSON.parse(JSON.stringify(message_catch_up)); + message.errored_scopes = ["CLAMP.Config_"]; + message.latest_policies = { + [MONKEYED_POLICY_ID]: create_policy(MONKEYED_POLICY_ID, 55), + [MONKEYED_POLICY_ID_2]: create_policy(MONKEYED_POLICY_ID_2, 22, {"key1": "value1"}), + [MONKEYED_POLICY_ID_4]: create_policy(MONKEYED_POLICY_ID_4, 77, {"service": "alex_service"}), + [MONKEYED_POLICY_ID_5]: create_policy(MONKEYED_POLICY_ID_5, "nan_version"), + [MONKEYED_POLICY_ID_6]: create_policy(MONKEYED_POLICY_ID_6, 66), + "junk_policy": create_policy("junk_policy", "nan_version"), + "fail_filtered": create_policy("fail_filtered", 12, {"ONAPName": "not-match"}), + "fail_filtered_2": create_policy("fail_filtered_2", 32, {"ConfigName": "not-match2"}), + "": create_policy("", 1) + }; + const test_txt = "POST " + req_path + " - catchup " + JSON.stringify(message); + describe(test_txt, () => { + it('POST policy-update - catchup', function() { + const action_timer = new utils.ActionTimer(); + console.log(action_timer.step, test_txt); + const execution_id = "policy_catch_up"; + const resp_to_exe = {"status": "none"}; + nock_cfy_node_instances(action_timer); + + nock(dh.CLOUDIFY_URL).post(CFY_API_EXECUTIONS) + .reply(201, function(uri, requestBody) { + requestBody = JSON.stringify(requestBody); + console.log(action_timer.step, "on_post", dh.CLOUDIFY_URL, uri, requestBody); + Object.assign(resp_to_exe, JSON.parse(requestBody)); + resp_to_exe.status = "pending"; + resp_to_exe.created_at = RUN_TS; + resp_to_exe.workflow_id = EXECUTE_OPERATION; + resp_to_exe.is_system_workflow = false; + resp_to_exe.blueprint_id = BLUEPRINT_ID; + resp_to_exe.error = ""; + resp_to_exe.id = execution_id; + resp_to_exe.parameters.run_by_dependency_order = false; + resp_to_exe.parameters.operation = OPERATION_POLICY_UPDATE; + resp_to_exe.parameters.type_names = []; + + console.log(action_timer.step, "reply to post", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + + return JSON.stringify(resp_to_exe); + }); + + nock(dh.CLOUDIFY_URL).get(CFY_API_EXECUTION + execution_id) + .reply(200, function(uri) { + resp_to_exe.status = "pending"; + console.log(action_timer.step, "get", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + return JSON.stringify(resp_to_exe); + }); + nock(dh.CLOUDIFY_URL).get(CFY_API_EXECUTION + execution_id) + .times(2) + .reply(200, function(uri) { + resp_to_exe.status = "started"; + console.log(action_timer.step, "get", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + return JSON.stringify(resp_to_exe); + }); + nock(dh.CLOUDIFY_URL).get(CFY_API_EXECUTION + execution_id) + .reply(200, function(uri) { + resp_to_exe.status = "terminated"; + console.log(action_timer.step, "get", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + return JSON.stringify(resp_to_exe); + }); + + for (var extra_i = 1; extra_i <= 100000; extra_i++) { + const policy_id = "extra_" + extra_i; + message.latest_policies[policy_id] = create_policy(policy_id, extra_i); + } + + return chai.request(dh_server.app).post(req_path) + .set('content-type', 'application/json') + .set('X-ECOMP-RequestID', 'test_post_policy_catch_up') + .send(message) + .then(function(res) { + console.log(action_timer.step, "res for", test_txt, res.text); + expect(res).to.have.status(200); + expect(res).to.be.json; + + return utils.sleep(25000); + }) + .then(function() { + console.log(action_timer.step, "the end of test"); + }) + .catch(function(err) { + console.error(action_timer.step, "err for", test_txt, err); + throw err; + }); + }).timeout(60000); + }); +} + +function test_fail_cfy_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = JSON.parse(JSON.stringify(message_catch_up)); + message.latest_policies = { + [MONKEYED_POLICY_ID_6]: create_policy(MONKEYED_POLICY_ID_6, 66) + }; + const test_txt = "fail POST " + req_path + " - catchup without execution_id " + JSON.stringify(message); + describe(test_txt, () => { + it('fail POST policy-update - catchup without execution_id', function() { + const action_timer = new utils.ActionTimer(); + console.log(action_timer.step, test_txt); + const execution_id = "policy_catch_up"; + const resp_to_exe = {"status": "none"}; + nock_cfy_node_instances(action_timer); + + nock(dh.CLOUDIFY_URL).post(CFY_API_EXECUTIONS) + .reply(201, function(uri, requestBody) { + requestBody = JSON.stringify(requestBody); + console.log(action_timer.step, "on_post", dh.CLOUDIFY_URL, uri, requestBody); + Object.assign(resp_to_exe, JSON.parse(requestBody)); + resp_to_exe.status = "pending"; + + console.log(action_timer.step, "reply to post", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + + return JSON.stringify(resp_to_exe); + }); + + return chai.request(dh_server.app).post(req_path) + .set('content-type', 'application/json') + .set('X-ECOMP-RequestID', 'test_post_policy_catch_up') + .send(message) + .then(function(res) { + console.log(action_timer.step, "res for", test_txt, res.text); + expect(res).to.have.status(200); + expect(res).to.be.json; + + return utils.sleep(1000); + }) + .then(function() { + console.log(action_timer.step, "the end of test"); + }) + .catch(function(err) { + console.error(action_timer.step, "err for", test_txt, err); + throw err; + }); + }).timeout(30000); + }); +} + +function test_fail_400_cfy_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = JSON.parse(JSON.stringify(message_catch_up)); + message.latest_policies = { + [MONKEYED_POLICY_ID_6]: create_policy(MONKEYED_POLICY_ID_6, 66) + }; + const test_txt = "fail 400 POST " + req_path + " - existing_running_execution_error " + JSON.stringify(message); + describe(test_txt, () => { + it('fail 400 POST policy-update - existing_running_execution_error', function() { + const action_timer = new utils.ActionTimer(); + console.log(action_timer.step, test_txt); + const execution_id = "policy_catch_up"; + const resp_to_exe = {"error_code": "existing_running_execution_error"}; + nock_cfy_node_instances(action_timer); + + nock(dh.CLOUDIFY_URL).post(CFY_API_EXECUTIONS).times(5) + .reply(400, function(uri, requestBody) { + console.log(action_timer.step, "on_post", dh.CLOUDIFY_URL, uri, JSON.stringify(requestBody)); + console.log(action_timer.step, "reply to post", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + return JSON.stringify(resp_to_exe); + }); + + return chai.request(dh_server.app).post(req_path) + .set('content-type', 'application/json') + .set('X-ECOMP-RequestID', 'test_post_policy_catch_up') + .send(message) + .then(function(res) { + console.log(action_timer.step, "res for", test_txt, res.text); + expect(res).to.have.status(200); + expect(res).to.be.json; + + return utils.sleep(25000); + }) + .then(function() { + console.log(action_timer.step, "the end of test"); + }) + .catch(function(err) { + console.error(action_timer.step, "err for", test_txt, err); + throw err; + }); + }).timeout(30000); }); -});
\ No newline at end of file +} + +function test_fail_404_cfy_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = JSON.parse(JSON.stringify(message_catch_up)); + message.latest_policies = { + [MONKEYED_POLICY_ID_6]: create_policy(MONKEYED_POLICY_ID_6, 66) + }; + const test_txt = "fail 404 POST " + req_path + " - not_found_error " + JSON.stringify(message); + describe(test_txt, () => { + it('fail 404 POST policy-update - not_found_error', function() { + const action_timer = new utils.ActionTimer(); + console.log(action_timer.step, test_txt); + const execution_id = "policy_catch_up"; + const resp_to_exe = {"error_code": "not_found_error"}; + nock_cfy_node_instances(action_timer); + + nock(dh.CLOUDIFY_URL).post(CFY_API_EXECUTIONS).times(5) + .reply(404, function(uri, requestBody) { + console.log(action_timer.step, "on_post", dh.CLOUDIFY_URL, uri, JSON.stringify(requestBody)); + console.log(action_timer.step, "reply to post", dh.CLOUDIFY_URL, uri, JSON.stringify(resp_to_exe)); + return JSON.stringify(resp_to_exe); + }); + + return chai.request(dh_server.app).post(req_path) + .set('content-type', 'application/json') + .set('X-ECOMP-RequestID', 'test_post_policy_catch_up') + .send(message) + .then(function(res) { + console.log(action_timer.step, "res for", test_txt, res.text); + expect(res).to.have.status(200); + expect(res).to.be.json; + + return utils.sleep(1000); + }) + .then(function() { + console.log(action_timer.step, "the end of test"); + }) + .catch(function(err) { + console.error(action_timer.step, "err for", test_txt, err); + throw err; + }); + }).timeout(30000); + }); +} + +dh.add_tests([ + test_get_policy_components, + test_post_policy_catch_up, + test_fail_cfy_policy_catch_up, + test_fail_400_cfy_policy_catch_up, + test_fail_404_cfy_policy_catch_up +]); |