diff options
Diffstat (limited to 'tests/test_policy.js')
-rw-r--r-- | tests/test_policy.js | 474 |
1 files changed, 408 insertions, 66 deletions
diff --git a/tests/test_policy.js b/tests/test_policy.js index e32b2f6..86d6ab4 100644 --- a/tests/test_policy.js +++ b/tests/test_policy.js @@ -29,6 +29,7 @@ const nock = require('nock') chai.use(chaiHttp); const dh = require('./mock_deployment_handler'); +const utils = require('./mock_utils'); const RUN_TS = new Date(); const RUN_TS_HOURS = RUN_TS.getHours(); @@ -39,109 +40,450 @@ 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"; -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 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"; +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": { - "ONAPName": "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(dh.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 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": {} } } -); +]; + +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}} + }); + }); +} function test_get_policy_components(dh_server) { 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() { + 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("res for", test_txt, res.text); + 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("err for", test_txt, err); + console.error(action_timer.step, "err for", test_txt, err); throw err; }); }); }); } -dh.add_tests([test_get_policy_components]); +function test_post_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = { + "errored_scopes": ["CLAMP.Config_"], + "catch_up": true, + "scope_prefixes": ["DCAE_alex.Config_", "DCAE.Config_"], + "errored_policies": {}, + "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); + }); + + 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); + }); +} + +function test_fail_cfy_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = { + "errored_scopes": [], + "catch_up": true, + "scope_prefixes": ["DCAE_alex.Config_", "DCAE.Config_"], + "errored_policies": {}, + "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 = { + "errored_scopes": [], + "catch_up": true, + "scope_prefixes": ["DCAE_alex.Config_", "DCAE.Config_"], + "errored_policies": {}, + "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); + }); +} + +function test_fail_404_cfy_policy_catch_up(dh_server) { + const req_path = "/policy"; + const message = { + "errored_scopes": [], + "catch_up": true, + "scope_prefixes": ["DCAE_alex.Config_", "DCAE.Config_"], + "errored_policies": {}, + "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 +]); |