Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1 | # ============LICENSE_START======================================================= |
Lusheng Ji | cc30700 | 2018-02-12 11:04:54 -0500 | [diff] [blame] | 2 | # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 3 | # ================================================================================ |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============LICENSE_END========================================================= |
| 16 | # |
| 17 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 18 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 19 | """tests of decorators around the cloudify operations to handle policy actions""" |
| 20 | |
| 21 | import copy |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 22 | import json |
| 23 | import logging |
| 24 | from datetime import datetime, timedelta |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 25 | from functools import wraps |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 26 | |
| 27 | import pytest |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 28 | from cloudify import ctx |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 29 | from cloudify.exceptions import NonRecoverableError |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 30 | from cloudify.state import current_ctx |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 31 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 32 | from onap_dcae_dcaepolicy_lib import dcae_policy |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 33 | from onap_dcae_dcaepolicy_lib.dcae_policy import Policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 34 | from tests.log_ctx import CtxLogger |
| 35 | from tests.mock_cloudify_ctx import (TARGET_NODE_ID, TARGET_NODE_NAME, |
| 36 | MockCloudifyContextFull) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 37 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 38 | POLICY_ID = 'policy_id' |
| 39 | POLICY_VERSION = "policyVersion" |
| 40 | POLICY_NAME = "policyName" |
| 41 | POLICY_BODY = 'policy_body' |
| 42 | POLICY_CONFIG = 'config' |
Alex Shatov | b73f81e | 2018-03-23 14:34:34 -0400 | [diff] [blame^] | 43 | CONFIG_NAME = "ConfigName" |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 44 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 45 | MONKEYED_POLICY_ID = 'monkeyed.Config_peach' |
| 46 | MONKEYED_POLICY_ID_2 = 'monkeyed.Config_peach_2' |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 47 | MONKEYED_POLICY_ID_M = 'monkeyed.Config_multi' |
| 48 | MONKEYED_POLICY_ID_M_2 = 'monkeyed.Config_multi_2' |
| 49 | MONKEYED_POLICY_ID_M_3 = 'monkeyed.Config_multi_3' |
| 50 | MONKEYED_POLICY_ID_B = 'monkeyed.Config_both' |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 51 | APPLICATION_CONFIG = "application_config" |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 52 | LOG_FILE = 'logs/test_onap_dcae_dcaepolicy_lib.log' |
| 53 | LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 54 | |
| 55 | RUN_TS = datetime.utcnow() |
| 56 | |
| 57 | class MonkeyedLogHandler(object): |
| 58 | """keep the shared logger handler here""" |
| 59 | _log_handler = None |
| 60 | |
| 61 | @staticmethod |
| 62 | def add_handler_to(logger): |
| 63 | """adds the local handler to the logger""" |
| 64 | if not MonkeyedLogHandler._log_handler: |
| 65 | MonkeyedLogHandler._log_handler = logging.FileHandler(LOG_FILE) |
| 66 | MonkeyedLogHandler._log_handler.setLevel(logging.DEBUG) |
| 67 | formatter = logging.Formatter( |
| 68 | fmt='%(asctime)s.%(msecs)03d %(levelname)+8s ' + \ |
| 69 | '%(threadName)s %(name)s.%(funcName)s: %(message)s', \ |
| 70 | datefmt='%Y%m%d_%H%M%S') |
| 71 | MonkeyedLogHandler._log_handler.setFormatter(formatter) |
| 72 | logger.addHandler(MonkeyedLogHandler._log_handler) |
| 73 | |
| 74 | class MonkeyedPolicyBody(object): |
| 75 | """policy body that policy-engine returns""" |
| 76 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 77 | def create_policy_body(policy_id, policy_version=1, priority=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 78 | """returns a fake policy-body""" |
| 79 | prev_ver = policy_version - 1 |
| 80 | timestamp = RUN_TS + timedelta(hours=prev_ver) |
| 81 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 82 | this_ver = str(policy_version) |
| 83 | config = { |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 84 | "policy_updated_from_ver": str(prev_ver), |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 85 | "policy_updated_to_ver": this_ver, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 86 | "policy_hello": LOREM_IPSUM[prev_ver % len(LOREM_IPSUM)], |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 87 | "policy_updated_ts": timestamp.isoformat()[:-3] + 'Z', |
| 88 | "updated_policy_id": policy_id |
| 89 | } |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 90 | config.update(copy.deepcopy(kwargs) or {}) |
| 91 | |
| 92 | matching_conditions = { |
| 93 | "ONAPName": "DCAE", |
Alex Shatov | b73f81e | 2018-03-23 14:34:34 -0400 | [diff] [blame^] | 94 | CONFIG_NAME: "alex_config_name" |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 95 | } |
| 96 | if priority is not None: |
| 97 | matching_conditions["priority"] = priority |
| 98 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 99 | return { |
| 100 | "policyConfigMessage": "Config Retrieved! ", |
| 101 | "policyConfigStatus": "CONFIG_RETRIEVED", |
| 102 | "type": "JSON", |
| 103 | POLICY_NAME: "{0}.{1}.xml".format(policy_id, this_ver), |
| 104 | POLICY_VERSION: this_ver, |
| 105 | POLICY_CONFIG: config, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 106 | "matchingConditions": matching_conditions, |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 107 | "responseAttributes": {}, |
| 108 | "property": None |
| 109 | } |
| 110 | |
| 111 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 112 | def create_policy_bare(policy_id, policy_version=1, priority=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 113 | """returns the whole policy object for policy_id and policy_version""" |
| 114 | return { |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 115 | POLICY_ID: policy_id, |
| 116 | POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 117 | policy_id, policy_version, priority, **kwargs) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 121 | def create_policy(policy_id, policy_version=1, policy_persistent=True, priority=None, **kwargs): |
| 122 | """returns the whole policy object for policy_id and policy_version""" |
| 123 | policy = MonkeyedPolicyBody.create_policy_bare( |
| 124 | policy_id, policy_version, priority, **kwargs) |
| 125 | policy[dcae_policy.POLICY_PERSISTENT] = bool(policy_persistent) |
| 126 | return policy |
| 127 | |
| 128 | @staticmethod |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 129 | def is_the_same_dict(policy_body_1, policy_body_2): |
| 130 | """check whether both policy_body objects are the same""" |
| 131 | if not isinstance(policy_body_1, dict) or not isinstance(policy_body_2, dict): |
| 132 | return False |
| 133 | for key in policy_body_1.keys(): |
| 134 | if key not in policy_body_2: |
| 135 | return False |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 136 | |
| 137 | val_1 = policy_body_1[key] |
| 138 | val_2 = policy_body_2[key] |
| 139 | if isinstance(val_1, dict) \ |
| 140 | and not MonkeyedPolicyBody.is_the_same_dict(val_1, val_2): |
| 141 | return False |
| 142 | if (val_1 is None and val_2 is not None) \ |
| 143 | or (val_1 is not None and val_2 is None) \ |
| 144 | or (val_1 != val_2): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 145 | return False |
| 146 | return True |
| 147 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 148 | class MonkeyedNode(object): |
| 149 | """node in cloudify""" |
| 150 | BLUEPRINT_ID = 'test_dcae_policy_bp_id' |
| 151 | DEPLOYMENT_ID = 'test_dcae_policy_dpl_id' |
| 152 | EXECUTION_ID = 'test_dcae_policy_exe_id' |
| 153 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 154 | def __init__(self, node_id, node_name, node_type, properties=None, |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 155 | relationships=None, runtime_properties=None): |
| 156 | self.node_id = node_id |
| 157 | self.node_name = node_name |
| 158 | self.ctx = MockCloudifyContextFull( |
| 159 | node_id=self.node_id, |
| 160 | node_name=self.node_name, |
| 161 | node_type=node_type, |
| 162 | blueprint_id=MonkeyedNode.BLUEPRINT_ID, |
| 163 | deployment_id=MonkeyedNode.DEPLOYMENT_ID, |
| 164 | execution_id=MonkeyedNode.EXECUTION_ID, |
| 165 | properties=properties, |
| 166 | relationships=relationships, |
| 167 | runtime_properties=runtime_properties |
| 168 | ) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 169 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 170 | def get_app_config(): |
| 171 | """just get the config""" |
| 172 | config = copy.deepcopy(dict(ctx.instance.runtime_properties.get(APPLICATION_CONFIG, {}))) |
| 173 | if not config: |
| 174 | config = copy.deepcopy(dict(ctx.node.properties.get(APPLICATION_CONFIG, {}))) |
| 175 | return config |
| 176 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 177 | def operation_node_configure(**kwargs): |
| 178 | """do the node-configure operation""" |
| 179 | ctx.logger.info("operation_node_configure kwargs: {0}".format(kwargs)) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 180 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 181 | app_config = get_app_config() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 182 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 183 | ctx.logger.info("property app_config: {0}".format(json.dumps(app_config))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 184 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 185 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task') |
| 186 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 187 | def node_configure(**kwargs): |
| 188 | """decorate with @Policies.gather_policies_to_node on policy consumer node to |
| 189 | bring all policies to runtime_properties["policies"] |
| 190 | """ |
| 191 | operation_node_configure(**kwargs) |
| 192 | |
| 193 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task') |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 194 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 195 | def node_configure_wrong_order_path(**kwargs): |
| 196 | """wrong data in param policy_apply_order_path""" |
| 197 | operation_node_configure(**kwargs) |
| 198 | |
| 199 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task') |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 200 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 201 | def node_configure_empty_order_path(**kwargs): |
| 202 | """wrong data in param policy_apply_order_path""" |
| 203 | operation_node_configure(**kwargs) |
| 204 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 205 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation') |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 206 | @Policies.update_policies_on_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 207 | def policy_update(updated_policies, removed_policies=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 208 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
| 209 | |
| 210 | :updated_policies: contains the list of changed policy-configs when configs_only=True (default). |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 211 | """ |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 212 | # This is how to merge the policies into default app_config object |
| 213 | # (runtime_properties[APPLICATION_CONFIG] = application_config) |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 214 | app_config = get_app_config() |
| 215 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 216 | |
| 217 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 218 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 219 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation') |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 220 | @Policies.update_policies_on_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 221 | def policy_update_not_only_config(updated_policies, removed_policies=None, **kwargs): |
| 222 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 223 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 224 | :updated_policies: contains the list of changed policy-configs when configs_only=True (default). |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 225 | """ |
| 226 | # This is how to merge the policies into default app_config object |
| 227 | # (runtime_properties[APPLICATION_CONFIG] = application_config) |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 228 | app_config = get_app_config() |
| 229 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 230 | |
| 231 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 232 | |
| 233 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation') |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 234 | @Policies.update_policies_on_node() |
| 235 | def policy_update_many_calcs(updated_policies, removed_policies=None, policies=None, **kwargs): |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 236 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
| 237 | |
| 238 | :updated_policies: contains the list of changed policy-configs when configs_only=True (default). |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 239 | """ |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 240 | app_config = get_app_config() |
| 241 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 242 | |
| 243 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 244 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 245 | class CurrentCtx(object): |
| 246 | """cloudify context""" |
| 247 | _node_ms = None |
| 248 | |
| 249 | @staticmethod |
| 250 | def set_current_ctx(include_bad=True, include_good=True): |
| 251 | """set up the nodes for cloudify""" |
| 252 | Policies._init() |
| 253 | |
| 254 | def add_target_to_relationships(relationships, node): |
| 255 | """adds the node as the target of relationships""" |
| 256 | relationships.append({TARGET_NODE_ID: node.node_id, TARGET_NODE_NAME: node.node_name}) |
| 257 | |
| 258 | relationships = [] |
| 259 | if include_good: |
| 260 | node_policy = MonkeyedNode( |
| 261 | 'dcae_policy_node_id', |
| 262 | 'dcae_policy_node_name', |
| 263 | dcae_policy.DCAE_POLICY_TYPE, |
| 264 | {POLICY_ID: MONKEYED_POLICY_ID}, |
| 265 | None, |
| 266 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 267 | MONKEYED_POLICY_ID, priority="1")} |
| 268 | ) |
| 269 | add_target_to_relationships(relationships, node_policy) |
| 270 | |
| 271 | if include_bad: |
| 272 | bad_policy_2 = MonkeyedNode( |
| 273 | 'bad_policy_2_node_id', |
| 274 | 'bad_policy_2_node_name', |
| 275 | dcae_policy.DCAE_POLICY_TYPE, |
| 276 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 277 | None, |
| 278 | None |
| 279 | ) |
| 280 | add_target_to_relationships(relationships, bad_policy_2) |
| 281 | |
| 282 | if include_good: |
| 283 | node_policy_2 = MonkeyedNode( |
| 284 | 'dcae_policy_node_id_2', |
| 285 | 'dcae_policy_node_name_2', |
| 286 | dcae_policy.DCAE_POLICY_TYPE, |
| 287 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 288 | None, |
| 289 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 290 | MONKEYED_POLICY_ID_2, 4, priority="2")} |
| 291 | ) |
| 292 | add_target_to_relationships(relationships, node_policy_2) |
| 293 | |
| 294 | if include_bad: |
| 295 | bad_policy_3 = MonkeyedNode( |
| 296 | 'bad_policy_3_node_id', |
| 297 | 'bad_policy_3_node_name', |
| 298 | dcae_policy.DCAE_POLICY_TYPE, |
| 299 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 300 | None, |
| 301 | None |
| 302 | ) |
| 303 | add_target_to_relationships(relationships, bad_policy_3) |
| 304 | |
| 305 | bad_policy_4 = MonkeyedNode( |
| 306 | 'bad_policy_4_node_id', |
| 307 | 'bad_policy_4_node_name', |
| 308 | dcae_policy.DCAE_POLICY_TYPE, |
| 309 | None, |
| 310 | None, |
| 311 | None |
| 312 | ) |
| 313 | add_target_to_relationships(relationships, bad_policy_4) |
| 314 | |
| 315 | weird_policy_5 = MonkeyedNode( |
| 316 | 'weird_policy_5_node_id', |
| 317 | 'weird_policy_5_node_name', |
| 318 | dcae_policy.DCAE_POLICY_TYPE, |
| 319 | None, |
| 320 | None, |
| 321 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 322 | MONKEYED_POLICY_ID, 3, priority="2")} |
| 323 | ) |
| 324 | add_target_to_relationships(relationships, weird_policy_5) |
| 325 | |
| 326 | if include_good: |
| 327 | node_policies = MonkeyedNode( |
| 328 | 'dcae_policies_node_id', |
| 329 | 'dcae_policies_node_name', |
| 330 | dcae_policy.DCAE_POLICIES_TYPE, |
Alex Shatov | b73f81e | 2018-03-23 14:34:34 -0400 | [diff] [blame^] | 331 | {dcae_policy.POLICY_FILTER: { |
| 332 | POLICY_NAME: MONKEYED_POLICY_ID_M + ".*", |
| 333 | dcae_policy.CONFIG_ATTRIBUTES: json.dumps({ |
| 334 | CONFIG_NAME: "alex_config_name" |
| 335 | }) |
| 336 | }}, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 337 | None, |
| 338 | {dcae_policy.POLICIES_FILTERED: { |
| 339 | MONKEYED_POLICY_ID_M: |
| 340 | MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M, 3)}} |
| 341 | ) |
| 342 | add_target_to_relationships(relationships, node_policies) |
| 343 | |
| 344 | node_policies_2 = MonkeyedNode( |
| 345 | 'dcae_policies_2_node_id', |
| 346 | 'dcae_policies_2_node_name', |
| 347 | dcae_policy.DCAE_POLICIES_TYPE, |
| 348 | {dcae_policy.POLICY_FILTER: {POLICY_NAME: MONKEYED_POLICY_ID_M + ".*"}}, |
| 349 | None, |
| 350 | {dcae_policy.POLICIES_FILTERED: { |
| 351 | MONKEYED_POLICY_ID_M: |
| 352 | MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M, 2)}} |
| 353 | ) |
| 354 | add_target_to_relationships(relationships, node_policies_2) |
| 355 | |
| 356 | policies_empty = MonkeyedNode( |
| 357 | 'dcae_policies_empty_node_id', |
| 358 | 'dcae_policies_empty_node_name', |
| 359 | dcae_policy.DCAE_POLICIES_TYPE, |
| 360 | {dcae_policy.POLICY_FILTER: {"empty": None}}, |
| 361 | None, |
| 362 | None |
| 363 | ) |
| 364 | add_target_to_relationships(relationships, policies_empty) |
| 365 | |
| 366 | policies_empty_2 = MonkeyedNode( |
| 367 | 'dcae_policies_empty_2_node_id', |
| 368 | 'dcae_policies_empty_2_node_name', |
| 369 | dcae_policy.DCAE_POLICIES_TYPE, |
| 370 | None, |
| 371 | None, |
| 372 | None |
| 373 | ) |
| 374 | add_target_to_relationships(relationships, policies_empty_2) |
| 375 | |
| 376 | non_policies = MonkeyedNode( |
| 377 | 'non_policies_node_id', |
| 378 | 'non_policies_node_name', |
| 379 | "non.policy.type" |
| 380 | ) |
| 381 | add_target_to_relationships(relationships, non_policies) |
| 382 | |
| 383 | if include_good: |
| 384 | node_policies_b = MonkeyedNode( |
| 385 | 'dcae_policies_b_node_id', |
| 386 | 'dcae_policies_b_node_name', |
| 387 | dcae_policy.DCAE_POLICIES_TYPE, |
| 388 | {dcae_policy.POLICY_FILTER: {POLICY_NAME: MONKEYED_POLICY_ID_M + ".*"}}, |
| 389 | None, |
| 390 | {dcae_policy.POLICIES_FILTERED: { |
| 391 | MONKEYED_POLICY_ID_B: |
| 392 | MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_B, 1)}} |
| 393 | ) |
| 394 | add_target_to_relationships(relationships, node_policies_b) |
| 395 | |
| 396 | node_policies_b_2 = MonkeyedNode( |
| 397 | 'dcae_policies_b_2_node_id', |
| 398 | 'dcae_policies_b_2_node_name', |
| 399 | dcae_policy.DCAE_POLICIES_TYPE, |
| 400 | {dcae_policy.POLICY_FILTER: {POLICY_NAME: MONKEYED_POLICY_ID_M + ".*"}}, |
| 401 | None, |
| 402 | {dcae_policy.POLICIES_FILTERED: { |
| 403 | MONKEYED_POLICY_ID_B: |
| 404 | MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_B, 2)}} |
| 405 | ) |
| 406 | add_target_to_relationships(relationships, node_policies_b_2) |
| 407 | |
| 408 | if include_good: |
| 409 | node_policy_b = MonkeyedNode( |
| 410 | 'dcae_policy_b_node_id', |
| 411 | 'dcae_policy_b_node_name', |
| 412 | dcae_policy.DCAE_POLICY_TYPE, |
| 413 | {POLICY_ID: MONKEYED_POLICY_ID_B}, |
| 414 | None, |
| 415 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 416 | MONKEYED_POLICY_ID_B, 4, priority="1.5")} |
| 417 | ) |
| 418 | add_target_to_relationships(relationships, node_policy_b) |
| 419 | |
| 420 | node_policies_b_5 = MonkeyedNode( |
| 421 | 'dcae_policies_b_5_node_id', |
| 422 | 'dcae_policies_b_5_node_name', |
| 423 | dcae_policy.DCAE_POLICIES_TYPE, |
| 424 | {dcae_policy.POLICY_FILTER: {POLICY_NAME: MONKEYED_POLICY_ID_M + ".*"}}, |
| 425 | None, |
| 426 | {dcae_policy.POLICIES_FILTERED: { |
| 427 | MONKEYED_POLICY_ID_B: |
| 428 | MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_B, 5)}} |
| 429 | ) |
| 430 | add_target_to_relationships(relationships, node_policies_b_5) |
| 431 | |
| 432 | |
| 433 | CurrentCtx._node_ms = MonkeyedNode( |
| 434 | 'test_ms_id', 'test_ms_name', "ms.nodes.type", |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 435 | {APPLICATION_CONFIG: MonkeyedPolicyBody.create_policy_body( |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 436 | "no_policy", db_port="123", weather="snow")[POLICY_CONFIG] |
| 437 | }, |
| 438 | relationships |
| 439 | ) |
| 440 | current_ctx.set(CurrentCtx._node_ms.ctx) |
| 441 | MonkeyedLogHandler.add_handler_to(CurrentCtx._node_ms.ctx.logger) |
| 442 | |
| 443 | @staticmethod |
| 444 | def reset(): |
| 445 | """reset context""" |
| 446 | current_ctx.set(CurrentCtx._node_ms.ctx) |
| 447 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 448 | def cfy_ctx(include_bad=True, include_good=True): |
| 449 | """test and safely clean up""" |
| 450 | def cfy_ctx_decorator(func): |
| 451 | """test and safely clean up""" |
| 452 | if not func: |
| 453 | return |
| 454 | |
| 455 | @wraps(func) |
| 456 | def ctx_wrapper(*args, **kwargs): |
| 457 | """test""" |
| 458 | try: |
| 459 | CurrentCtx.set_current_ctx(include_bad, include_good) |
| 460 | |
| 461 | func(*args, **kwargs) |
| 462 | |
| 463 | finally: |
| 464 | ctx.logger.info("MockCloudifyContextFull.clear") |
| 465 | MockCloudifyContextFull.clear() |
| 466 | current_ctx.clear() |
| 467 | |
| 468 | return ctx_wrapper |
| 469 | return cfy_ctx_decorator |
| 470 | |
| 471 | @cfy_ctx(include_bad=True) |
| 472 | def test_gather_policies_to_node(): |
| 473 | """test gather_policies_to_node""" |
Alex Shatov | b73f81e | 2018-03-23 14:34:34 -0400 | [diff] [blame^] | 474 | node_configure() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 475 | |
| 476 | runtime_properties = ctx.instance.runtime_properties |
| 477 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 478 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 479 | assert dcae_policy.POLICIES in runtime_properties |
| 480 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 481 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 482 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 483 | @cfy_ctx(include_bad=True) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 484 | def test_policies_to_node(): |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 485 | """test gather_policies_to_node""" |
| 486 | node_configure() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 487 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 488 | runtime_properties = ctx.instance.runtime_properties |
| 489 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 490 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 491 | assert dcae_policy.POLICIES in runtime_properties |
| 492 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 493 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 494 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 495 | assert MONKEYED_POLICY_ID in policies |
| 496 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 497 | policy = policies[MONKEYED_POLICY_ID] |
| 498 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 499 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 500 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 501 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 502 | |
| 503 | assert MONKEYED_POLICY_ID_B in policies |
| 504 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 505 | policy = policies[MONKEYED_POLICY_ID_B] |
| 506 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_b))) |
| 507 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 508 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 509 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 510 | |
| 511 | assert MONKEYED_POLICY_ID_2 in policies |
| 512 | expected_2 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 4, priority="2") |
| 513 | policy = policies[MONKEYED_POLICY_ID_2] |
| 514 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(expected_2))) |
| 515 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 516 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_2) |
| 517 | assert MonkeyedPolicyBody.is_the_same_dict(expected_2, policy) |
| 518 | |
| 519 | assert MONKEYED_POLICY_ID_M in policies |
| 520 | expected_m = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M, 2, False) |
| 521 | policy = policies[MONKEYED_POLICY_ID_M] |
| 522 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(expected_m))) |
| 523 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(policy))) |
| 524 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_m) |
| 525 | assert MonkeyedPolicyBody.is_the_same_dict(expected_m, policy) |
| 526 | |
| 527 | @cfy_ctx(include_bad=True) |
| 528 | def test_update_policies(): |
| 529 | """test policy_update""" |
| 530 | node_configure() |
| 531 | |
| 532 | runtime_properties = ctx.instance.runtime_properties |
| 533 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 534 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 535 | assert dcae_policy.POLICIES in runtime_properties |
| 536 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 537 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 538 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 539 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 540 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 541 | False, priority="1") |
| 542 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 543 | |
| 544 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 545 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 546 | |
| 547 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 548 | |
| 549 | policy_update(updated_policies=[updated_policy], |
| 550 | added_policies={ |
| 551 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 552 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 553 | }, |
| 554 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 555 | |
| 556 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 557 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 558 | |
| 559 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 560 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 561 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 562 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 563 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 564 | |
| 565 | assert MONKEYED_POLICY_ID_2 in policies |
| 566 | policy = policies[MONKEYED_POLICY_ID_2] |
| 567 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 568 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 569 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 570 | |
| 571 | assert MONKEYED_POLICY_ID in policies |
| 572 | policy = policies[MONKEYED_POLICY_ID] |
| 573 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 574 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 575 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 576 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 577 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 578 | |
| 579 | assert MONKEYED_POLICY_ID_B in policies |
| 580 | policy = policies[MONKEYED_POLICY_ID_B] |
| 581 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 582 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 583 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 584 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 585 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 586 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 587 | @cfy_ctx(include_bad=True) |
| 588 | def test_update_not_only_config(): |
| 589 | """test policy_update""" |
| 590 | node_configure() |
| 591 | |
| 592 | runtime_properties = ctx.instance.runtime_properties |
| 593 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 594 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 595 | assert dcae_policy.POLICIES in runtime_properties |
| 596 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 597 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 598 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 599 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 600 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 601 | False, priority="1") |
| 602 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 603 | |
| 604 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 605 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 606 | |
| 607 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 608 | |
| 609 | policy_update_not_only_config(updated_policies=[updated_policy], |
| 610 | added_policies={ |
| 611 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 612 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 613 | }, |
| 614 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 615 | |
| 616 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 617 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 618 | |
| 619 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 620 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 621 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 622 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 623 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 624 | |
| 625 | assert MONKEYED_POLICY_ID_2 in policies |
| 626 | policy = policies[MONKEYED_POLICY_ID_2] |
| 627 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 628 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 629 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 630 | |
| 631 | assert MONKEYED_POLICY_ID in policies |
| 632 | policy = policies[MONKEYED_POLICY_ID] |
| 633 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 634 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 635 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 636 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 637 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 638 | |
| 639 | assert MONKEYED_POLICY_ID_B in policies |
| 640 | policy = policies[MONKEYED_POLICY_ID_B] |
| 641 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 642 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 643 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 644 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 645 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 646 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 647 | @cfy_ctx(include_bad=True) |
| 648 | def test_update_policies_not(): |
| 649 | """test policy_update - ignore all policies with junk params""" |
| 650 | node_configure() |
| 651 | |
| 652 | runtime_properties = ctx.instance.runtime_properties |
| 653 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 654 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 655 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 656 | assert APPLICATION_CONFIG in runtime_properties |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 657 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 658 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 659 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 660 | ctx.logger.info("app_config: {0}".format(json.dumps(app_config))) |
| 661 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 662 | expected_policies = copy.deepcopy(policies) |
| 663 | expected_app_config = copy.deepcopy(app_config) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 664 | |
| 665 | existing_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID, priority="1") |
| 666 | damaged_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_2) |
| 667 | del damaged_policy[POLICY_BODY][POLICY_CONFIG] |
| 668 | updated_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_3, 3) |
| 669 | added_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_2, 4) |
| 670 | junk_policy_filter_id = "<<<junk_removed_policy_id>>>" |
| 671 | unexpected_removed_policy_id = "<<<junk_removed_policy_id>>>" |
| 672 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 673 | |
| 674 | assert MONKEYED_POLICY_ID in policies |
| 675 | assert MONKEYED_POLICY_ID_2 in policies |
| 676 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 677 | assert MONKEYED_POLICY_ID_M_3 not in policies |
| 678 | assert unexpected_removed_policy_id not in policies |
| 679 | assert junk_policy_filter_id not in runtime_properties.get(dcae_policy.POLICY_FILTERS, {}) |
| 680 | |
| 681 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 682 | |
| 683 | policy_update(updated_policies=[existing_policy, damaged_policy, updated_policy], |
| 684 | added_policies={ |
| 685 | junk_policy_filter_id: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 686 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 687 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 688 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_2: damaged_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 689 | }, |
| 690 | removed_policies=[unexpected_removed_policy_id]) |
| 691 | |
| 692 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 693 | |
| 694 | ctx.logger.info("policies not changed: {0}".format(json.dumps(policies))) |
| 695 | assert MonkeyedPolicyBody.is_the_same_dict(policies, expected_policies) |
| 696 | assert MonkeyedPolicyBody.is_the_same_dict(expected_policies, policies) |
| 697 | |
| 698 | ctx.logger.info("app_config not changed: {0}".format(json.dumps(app_config))) |
| 699 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_app_config) |
| 700 | assert MonkeyedPolicyBody.is_the_same_dict(expected_app_config, app_config) |
| 701 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 702 | @cfy_ctx(include_bad=True) |
| 703 | def test_update_many_calcs(): |
| 704 | """test policy_update""" |
| 705 | node_configure() |
| 706 | |
| 707 | runtime_properties = ctx.instance.runtime_properties |
| 708 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 709 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 710 | assert dcae_policy.POLICIES in runtime_properties |
| 711 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 712 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 713 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 714 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 715 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 716 | False, priority="1") |
| 717 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 718 | |
| 719 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 720 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 721 | |
| 722 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 723 | |
| 724 | policy_update_many_calcs(updated_policies=[updated_policy], |
| 725 | added_policies={ |
| 726 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 727 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 728 | }, |
| 729 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 730 | |
| 731 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 732 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 733 | |
| 734 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 735 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 736 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 737 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 738 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 739 | |
| 740 | assert MONKEYED_POLICY_ID_2 in policies |
| 741 | policy = policies[MONKEYED_POLICY_ID_2] |
| 742 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 743 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 744 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 745 | |
| 746 | assert MONKEYED_POLICY_ID in policies |
| 747 | policy = policies[MONKEYED_POLICY_ID] |
| 748 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 749 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 750 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 751 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 752 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 753 | |
| 754 | assert MONKEYED_POLICY_ID_B in policies |
| 755 | policy = policies[MONKEYED_POLICY_ID_B] |
| 756 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 757 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 758 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 759 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 760 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 761 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 762 | @cfy_ctx(include_bad=True) |
| 763 | def test_remove_all_policies(): |
| 764 | """test policy_update - remove all policies""" |
| 765 | node_configure() |
| 766 | |
| 767 | runtime_properties = ctx.instance.runtime_properties |
| 768 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 769 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 770 | assert dcae_policy.POLICIES in runtime_properties |
| 771 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 772 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 773 | |
| 774 | remove_policy_ids = policies.keys() |
| 775 | |
| 776 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 777 | |
| 778 | ctx.logger.info("removed: {0}".format(remove_policy_ids)) |
| 779 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 780 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 781 | assert Policies.get_policy_bodies() == [] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 782 | |
| 783 | assert APPLICATION_CONFIG in runtime_properties |
| 784 | assert APPLICATION_CONFIG in ctx.node.properties |
| 785 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 786 | expected_config = dict(ctx.node.properties[APPLICATION_CONFIG]) |
| 787 | ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config))) |
| 788 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config) |
| 789 | assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config) |
| 790 | |
| 791 | @cfy_ctx(include_bad=True) |
| 792 | def test_remove_all_policies_twice(): |
| 793 | """test policy_update - remove all policies twice""" |
| 794 | node_configure() |
| 795 | |
| 796 | runtime_properties = ctx.instance.runtime_properties |
| 797 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 798 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 799 | assert dcae_policy.POLICIES in runtime_properties |
| 800 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 801 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 802 | |
| 803 | remove_policy_ids = policies.keys() |
| 804 | |
| 805 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 806 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 807 | |
| 808 | ctx.logger.info("removed: {0}".format(remove_policy_ids)) |
| 809 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 810 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 811 | assert Policies.get_policy_bodies() == [] |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 812 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 813 | assert APPLICATION_CONFIG in runtime_properties |
| 814 | assert APPLICATION_CONFIG in ctx.node.properties |
| 815 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 816 | expected_config = dict(ctx.node.properties[APPLICATION_CONFIG]) |
| 817 | ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config))) |
| 818 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config) |
| 819 | assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 820 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 821 | @cfy_ctx(include_bad=True) |
| 822 | def test_remove_then_update(): |
| 823 | """test policy_update""" |
| 824 | node_configure() |
| 825 | |
| 826 | runtime_properties = ctx.instance.runtime_properties |
| 827 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 828 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 829 | assert dcae_policy.POLICIES in runtime_properties |
| 830 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 831 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 832 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 833 | remove_policy_ids = policies.keys() |
| 834 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 835 | |
| 836 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 837 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 838 | False, priority="1") |
| 839 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 840 | |
| 841 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 842 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 843 | |
| 844 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 845 | |
| 846 | policy_update(updated_policies=[updated_policy], |
| 847 | added_policies={ |
| 848 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 849 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 850 | }, |
| 851 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 852 | |
| 853 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 854 | assert MONKEYED_POLICY_ID_M not in policies |
| 855 | |
| 856 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 857 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 858 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 859 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 860 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 861 | |
| 862 | assert MONKEYED_POLICY_ID_2 in policies |
| 863 | policy = policies[MONKEYED_POLICY_ID_2] |
| 864 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 865 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 866 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 867 | |
| 868 | assert MONKEYED_POLICY_ID in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 869 | assert MONKEYED_POLICY_ID_B in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 870 | |
| 871 | @cfy_ctx(include_bad=True) |
| 872 | def test_remove_update_many_calcs(): |
| 873 | """test policy_update""" |
| 874 | node_configure() |
| 875 | |
| 876 | runtime_properties = ctx.instance.runtime_properties |
| 877 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 878 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 879 | assert dcae_policy.POLICIES in runtime_properties |
| 880 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 881 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 882 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 883 | remove_policy_ids = policies.keys() |
| 884 | policy_update_many_calcs(updated_policies=None, |
| 885 | added_policies=None, |
| 886 | removed_policies=remove_policy_ids) |
| 887 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 888 | assert dcae_policy.POLICIES in runtime_properties |
| 889 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 890 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 891 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 892 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 893 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 894 | False, priority="1") |
| 895 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 896 | |
| 897 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 898 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 899 | |
| 900 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 901 | |
| 902 | policy_update_many_calcs(updated_policies=[updated_policy], |
| 903 | added_policies={ |
| 904 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 905 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 906 | }, |
| 907 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 908 | |
| 909 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 910 | assert MONKEYED_POLICY_ID_M not in policies |
| 911 | |
| 912 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 913 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 914 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 915 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 916 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 917 | |
| 918 | assert MONKEYED_POLICY_ID_2 in policies |
| 919 | policy = policies[MONKEYED_POLICY_ID_2] |
| 920 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 921 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 922 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 923 | |
| 924 | assert MONKEYED_POLICY_ID in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 925 | assert MONKEYED_POLICY_ID_B in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 926 | |
| 927 | @cfy_ctx(include_bad=True) |
| 928 | def test_bad_update_many_calcs(): |
| 929 | """test policy_update""" |
| 930 | node_configure() |
| 931 | |
| 932 | runtime_properties = ctx.instance.runtime_properties |
| 933 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 934 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 935 | assert dcae_policy.POLICIES in runtime_properties |
| 936 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 937 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 938 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 939 | damaged_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 940 | damaged_policy[POLICY_BODY][POLICY_CONFIG] = ["damaged config"] |
| 941 | |
| 942 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 943 | False, priority="1") |
| 944 | added_policy[POLICY_BODY][POLICY_CONFIG] = {"unexpected":"foo", "none": None} |
| 945 | |
| 946 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(damaged_policy))) |
| 947 | |
| 948 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 949 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 950 | |
| 951 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 952 | |
| 953 | policy_update_many_calcs(updated_policies=[damaged_policy], |
| 954 | added_policies={ |
| 955 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 956 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 957 | }, |
| 958 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 959 | |
| 960 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 961 | assert MONKEYED_POLICY_ID_M not in policies |
| 962 | |
| 963 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 964 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 965 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 966 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 967 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 968 | |
| 969 | assert MONKEYED_POLICY_ID_2 in policies |
| 970 | policy = policies[MONKEYED_POLICY_ID_2] |
| 971 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 972 | assert MonkeyedPolicyBody.is_the_same_dict(policy, damaged_policy) |
| 973 | assert MonkeyedPolicyBody.is_the_same_dict(damaged_policy, policy) |
| 974 | |
| 975 | assert MONKEYED_POLICY_ID in policies |
| 976 | policy = policies[MONKEYED_POLICY_ID] |
| 977 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 978 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 979 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 980 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 981 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 982 | |
| 983 | assert MONKEYED_POLICY_ID_B in policies |
| 984 | policy = policies[MONKEYED_POLICY_ID_B] |
| 985 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 986 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 987 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 988 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 989 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 990 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 991 | @cfy_ctx(include_bad=True, include_good=False) |
| 992 | def test_bad_policies(): |
| 993 | """test bad policy nodes""" |
| 994 | node_configure() |
| 995 | |
| 996 | runtime_properties = ctx.instance.runtime_properties |
| 997 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 998 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 999 | assert dcae_policy.POLICIES in runtime_properties |
| 1000 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1001 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 1002 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1003 | @cfy_ctx(include_bad=True, include_good=False) |
| 1004 | def test_wrong_ctx_node_configure(): |
| 1005 | """test wrong ctx""" |
| 1006 | current_ctx.set(ctx.instance.relationships[0]) |
| 1007 | ctx_type = ctx.type |
| 1008 | |
| 1009 | with pytest.raises(NonRecoverableError) as excinfo: |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1010 | node_configure() |
| 1011 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1012 | CurrentCtx.reset() |
| 1013 | ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value))) |
| 1014 | assert ctx_type == 'cloudify.relationships.depends_on' |
| 1015 | assert str(excinfo.value) == "can only invoke gather_policies_to_node on node" |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1016 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1017 | @cfy_ctx(include_bad=True, include_good=False) |
| 1018 | def test_wrong_ctx_policy_update(): |
| 1019 | """test wrong ctx""" |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1020 | no_policies = Policies.get_policy_bodies() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1021 | current_ctx.set(ctx.instance.relationships[0]) |
| 1022 | ctx_type = ctx.type |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1023 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1024 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1025 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1026 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1027 | CurrentCtx.reset() |
| 1028 | ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value))) |
| 1029 | assert ctx_type == 'cloudify.relationships.depends_on' |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1030 | assert no_policies == [] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1031 | assert str(excinfo.value) == "can only invoke update_policies_on_node on node" |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1032 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1033 | def test_defenses_on_decorators(): |
| 1034 | """test defenses of code mainly for code coverage""" |
| 1035 | should_be_none = Policies.gather_policies_to_node()(None) |
| 1036 | assert should_be_none is None |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1037 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1038 | should_be_none = Policies.update_policies_on_node()(None) |
| 1039 | assert should_be_none is None |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1040 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1041 | def monkeyed_set_policies_boom(policies): |
| 1042 | """monkeypatch for exception""" |
| 1043 | raise Exception("Policies._set_policies boom: {0}".format(json.dumps(policies or {}))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1044 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1045 | @pytest.fixture() |
| 1046 | def fix_boom_gather(monkeypatch): |
| 1047 | """monkeyed boom""" |
| 1048 | monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._set_policies', |
| 1049 | monkeyed_set_policies_boom) |
| 1050 | return fix_boom_gather # provide the fixture value |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1051 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1052 | @cfy_ctx(include_bad=True, include_good=False) |
| 1053 | @pytest.mark.usefixtures("fix_boom_gather") |
| 1054 | def test_exception_on_gather(): |
| 1055 | """test exception on gather""" |
| 1056 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1057 | node_configure() |
| 1058 | |
| 1059 | ctx.logger.info("monkeyed_set_policies_boom: {0}".format(str(excinfo.value))) |
| 1060 | assert str(excinfo.value).startswith("Failed to set the policies ") |
| 1061 | |
| 1062 | def monkeyed_update_policies_boom(policies): |
| 1063 | """monkeypatch for exception""" |
| 1064 | raise Exception("Policies._update_policies boom") |
| 1065 | |
| 1066 | @pytest.fixture() |
| 1067 | def fix_boom_update_policies(monkeypatch): |
| 1068 | """monkeyed boom""" |
| 1069 | monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._update_policies', |
| 1070 | monkeyed_update_policies_boom) |
| 1071 | return fix_boom_update_policies # provide the fixture value |
| 1072 | |
| 1073 | @cfy_ctx(include_bad=True, include_good=False) |
| 1074 | @pytest.mark.usefixtures("fix_boom_update_policies") |
| 1075 | def test_exception_on_update(): |
| 1076 | """test exception on update_policies""" |
| 1077 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1078 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
| 1079 | |
| 1080 | ctx.logger.info("monkeyed_update_policies_boom: {0}".format(str(excinfo.value))) |
| 1081 | assert str(excinfo.value).startswith("Failed to update the policies ") |
| 1082 | |
| 1083 | @cfy_ctx(include_bad=True, include_good=False) |
| 1084 | def test_defenses_on_policy_update(): |
| 1085 | """test defenses on policy_update""" |
| 1086 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
| 1087 | ctx.logger.info("policy_update() ok") |
| 1088 | |
| 1089 | @cfy_ctx(include_bad=True, include_good=False) |
| 1090 | def test_defenses_on_set_policies(): |
| 1091 | """test defenses of code mainly for code coverage""" |
| 1092 | node_configure() |
| 1093 | |
| 1094 | runtime_properties = ctx.instance.runtime_properties |
| 1095 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 1096 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 1097 | assert dcae_policy.POLICIES in runtime_properties |
| 1098 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1099 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 1100 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1101 | Policies._set_policies({}) |
| 1102 | |
| 1103 | assert dcae_policy.POLICIES not in runtime_properties |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1104 | |
| 1105 | Policies._set_policies({}) |
| 1106 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 1107 | assert dcae_policy.POLICIES not in runtime_properties |