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 | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 43 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 44 | MONKEYED_POLICY_ID = 'monkeyed.Config_peach' |
| 45 | MONKEYED_POLICY_ID_2 = 'monkeyed.Config_peach_2' |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 46 | MONKEYED_POLICY_ID_M = 'monkeyed.Config_multi' |
| 47 | MONKEYED_POLICY_ID_M_2 = 'monkeyed.Config_multi_2' |
| 48 | MONKEYED_POLICY_ID_M_3 = 'monkeyed.Config_multi_3' |
| 49 | MONKEYED_POLICY_ID_B = 'monkeyed.Config_both' |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 50 | APPLICATION_CONFIG = "application_config" |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 51 | LOG_FILE = 'logs/test_onap_dcae_dcaepolicy_lib.log' |
| 52 | LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 53 | |
| 54 | RUN_TS = datetime.utcnow() |
| 55 | |
| 56 | class MonkeyedLogHandler(object): |
| 57 | """keep the shared logger handler here""" |
| 58 | _log_handler = None |
| 59 | |
| 60 | @staticmethod |
| 61 | def add_handler_to(logger): |
| 62 | """adds the local handler to the logger""" |
| 63 | if not MonkeyedLogHandler._log_handler: |
| 64 | MonkeyedLogHandler._log_handler = logging.FileHandler(LOG_FILE) |
| 65 | MonkeyedLogHandler._log_handler.setLevel(logging.DEBUG) |
| 66 | formatter = logging.Formatter( |
| 67 | fmt='%(asctime)s.%(msecs)03d %(levelname)+8s ' + \ |
| 68 | '%(threadName)s %(name)s.%(funcName)s: %(message)s', \ |
| 69 | datefmt='%Y%m%d_%H%M%S') |
| 70 | MonkeyedLogHandler._log_handler.setFormatter(formatter) |
| 71 | logger.addHandler(MonkeyedLogHandler._log_handler) |
| 72 | |
| 73 | class MonkeyedPolicyBody(object): |
| 74 | """policy body that policy-engine returns""" |
| 75 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 76 | def create_policy_body(policy_id, policy_version=1, priority=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 77 | """returns a fake policy-body""" |
| 78 | prev_ver = policy_version - 1 |
| 79 | timestamp = RUN_TS + timedelta(hours=prev_ver) |
| 80 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 81 | this_ver = str(policy_version) |
| 82 | config = { |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 83 | "policy_updated_from_ver": str(prev_ver), |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 84 | "policy_updated_to_ver": this_ver, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 85 | "policy_hello": LOREM_IPSUM[prev_ver % len(LOREM_IPSUM)], |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 86 | "policy_updated_ts": timestamp.isoformat()[:-3] + 'Z', |
| 87 | "updated_policy_id": policy_id |
| 88 | } |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 89 | config.update(copy.deepcopy(kwargs) or {}) |
| 90 | |
| 91 | matching_conditions = { |
| 92 | "ONAPName": "DCAE", |
| 93 | "ConfigName": "alex_config_name" |
| 94 | } |
| 95 | if priority is not None: |
| 96 | matching_conditions["priority"] = priority |
| 97 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 98 | return { |
| 99 | "policyConfigMessage": "Config Retrieved! ", |
| 100 | "policyConfigStatus": "CONFIG_RETRIEVED", |
| 101 | "type": "JSON", |
| 102 | POLICY_NAME: "{0}.{1}.xml".format(policy_id, this_ver), |
| 103 | POLICY_VERSION: this_ver, |
| 104 | POLICY_CONFIG: config, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 105 | "matchingConditions": matching_conditions, |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 106 | "responseAttributes": {}, |
| 107 | "property": None |
| 108 | } |
| 109 | |
| 110 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 111 | def create_policy_bare(policy_id, policy_version=1, priority=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 112 | """returns the whole policy object for policy_id and policy_version""" |
| 113 | return { |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 114 | POLICY_ID: policy_id, |
| 115 | POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 116 | policy_id, policy_version, priority, **kwargs) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @staticmethod |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 120 | def create_policy(policy_id, policy_version=1, policy_persistent=True, priority=None, **kwargs): |
| 121 | """returns the whole policy object for policy_id and policy_version""" |
| 122 | policy = MonkeyedPolicyBody.create_policy_bare( |
| 123 | policy_id, policy_version, priority, **kwargs) |
| 124 | policy[dcae_policy.POLICY_PERSISTENT] = bool(policy_persistent) |
| 125 | return policy |
| 126 | |
| 127 | @staticmethod |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 128 | def is_the_same_dict(policy_body_1, policy_body_2): |
| 129 | """check whether both policy_body objects are the same""" |
| 130 | if not isinstance(policy_body_1, dict) or not isinstance(policy_body_2, dict): |
| 131 | return False |
| 132 | for key in policy_body_1.keys(): |
| 133 | if key not in policy_body_2: |
| 134 | return False |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 135 | |
| 136 | val_1 = policy_body_1[key] |
| 137 | val_2 = policy_body_2[key] |
| 138 | if isinstance(val_1, dict) \ |
| 139 | and not MonkeyedPolicyBody.is_the_same_dict(val_1, val_2): |
| 140 | return False |
| 141 | if (val_1 is None and val_2 is not None) \ |
| 142 | or (val_1 is not None and val_2 is None) \ |
| 143 | or (val_1 != val_2): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 144 | return False |
| 145 | return True |
| 146 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 147 | class MonkeyedNode(object): |
| 148 | """node in cloudify""" |
| 149 | BLUEPRINT_ID = 'test_dcae_policy_bp_id' |
| 150 | DEPLOYMENT_ID = 'test_dcae_policy_dpl_id' |
| 151 | EXECUTION_ID = 'test_dcae_policy_exe_id' |
| 152 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 153 | def __init__(self, node_id, node_name, node_type, properties=None, |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 154 | relationships=None, runtime_properties=None): |
| 155 | self.node_id = node_id |
| 156 | self.node_name = node_name |
| 157 | self.ctx = MockCloudifyContextFull( |
| 158 | node_id=self.node_id, |
| 159 | node_name=self.node_name, |
| 160 | node_type=node_type, |
| 161 | blueprint_id=MonkeyedNode.BLUEPRINT_ID, |
| 162 | deployment_id=MonkeyedNode.DEPLOYMENT_ID, |
| 163 | execution_id=MonkeyedNode.EXECUTION_ID, |
| 164 | properties=properties, |
| 165 | relationships=relationships, |
| 166 | runtime_properties=runtime_properties |
| 167 | ) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 168 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 169 | def get_app_config(): |
| 170 | """just get the config""" |
| 171 | config = copy.deepcopy(dict(ctx.instance.runtime_properties.get(APPLICATION_CONFIG, {}))) |
| 172 | if not config: |
| 173 | config = copy.deepcopy(dict(ctx.node.properties.get(APPLICATION_CONFIG, {}))) |
| 174 | return config |
| 175 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 176 | def operation_node_configure(**kwargs): |
| 177 | """do the node-configure operation""" |
| 178 | ctx.logger.info("operation_node_configure kwargs: {0}".format(kwargs)) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 179 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 180 | app_config = get_app_config() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 181 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 182 | ctx.logger.info("property app_config: {0}".format(json.dumps(app_config))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 183 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 184 | @CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task') |
| 185 | @Policies.gather_policies_to_node() |
| 186 | def node_configure_default_order(**kwargs): |
| 187 | """default policy sorting because no param of policy_apply_order_path""" |
| 188 | operation_node_configure(**kwargs) |
| 189 | |
| 190 | @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] | 191 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 192 | def node_configure(**kwargs): |
| 193 | """decorate with @Policies.gather_policies_to_node on policy consumer node to |
| 194 | bring all policies to runtime_properties["policies"] |
| 195 | """ |
| 196 | operation_node_configure(**kwargs) |
| 197 | |
| 198 | @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] | 199 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 200 | def node_configure_wrong_order_path(**kwargs): |
| 201 | """wrong data in param policy_apply_order_path""" |
| 202 | operation_node_configure(**kwargs) |
| 203 | |
| 204 | @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] | 205 | @Policies.gather_policies_to_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 206 | def node_configure_empty_order_path(**kwargs): |
| 207 | """wrong data in param policy_apply_order_path""" |
| 208 | operation_node_configure(**kwargs) |
| 209 | |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 210 | @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] | 211 | @Policies.update_policies_on_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 212 | def policy_update(updated_policies, removed_policies=None, **kwargs): |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 213 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
| 214 | |
| 215 | :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] | 216 | """ |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 217 | # This is how to merge the policies into default app_config object |
| 218 | # (runtime_properties[APPLICATION_CONFIG] = application_config) |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 219 | app_config = get_app_config() |
| 220 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 221 | |
| 222 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 223 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 224 | @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] | 225 | @Policies.update_policies_on_node() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 226 | def policy_update_not_only_config(updated_policies, removed_policies=None, **kwargs): |
| 227 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 228 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 229 | :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] | 230 | """ |
| 231 | # This is how to merge the policies into default app_config object |
| 232 | # (runtime_properties[APPLICATION_CONFIG] = application_config) |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 233 | app_config = get_app_config() |
| 234 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 235 | |
| 236 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 237 | |
| 238 | @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] | 239 | @Policies.update_policies_on_node() |
| 240 | 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] | 241 | """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"] |
| 242 | |
| 243 | :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] | 244 | """ |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 245 | app_config = get_app_config() |
| 246 | ctx.logger.info("app_config {0}".format(json.dumps(app_config))) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 247 | |
| 248 | ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config |
| 249 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 250 | class CurrentCtx(object): |
| 251 | """cloudify context""" |
| 252 | _node_ms = None |
| 253 | |
| 254 | @staticmethod |
| 255 | def set_current_ctx(include_bad=True, include_good=True): |
| 256 | """set up the nodes for cloudify""" |
| 257 | Policies._init() |
| 258 | |
| 259 | def add_target_to_relationships(relationships, node): |
| 260 | """adds the node as the target of relationships""" |
| 261 | relationships.append({TARGET_NODE_ID: node.node_id, TARGET_NODE_NAME: node.node_name}) |
| 262 | |
| 263 | relationships = [] |
| 264 | if include_good: |
| 265 | node_policy = MonkeyedNode( |
| 266 | 'dcae_policy_node_id', |
| 267 | 'dcae_policy_node_name', |
| 268 | dcae_policy.DCAE_POLICY_TYPE, |
| 269 | {POLICY_ID: MONKEYED_POLICY_ID}, |
| 270 | None, |
| 271 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 272 | MONKEYED_POLICY_ID, priority="1")} |
| 273 | ) |
| 274 | add_target_to_relationships(relationships, node_policy) |
| 275 | |
| 276 | if include_bad: |
| 277 | bad_policy_2 = MonkeyedNode( |
| 278 | 'bad_policy_2_node_id', |
| 279 | 'bad_policy_2_node_name', |
| 280 | dcae_policy.DCAE_POLICY_TYPE, |
| 281 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 282 | None, |
| 283 | None |
| 284 | ) |
| 285 | add_target_to_relationships(relationships, bad_policy_2) |
| 286 | |
| 287 | if include_good: |
| 288 | node_policy_2 = MonkeyedNode( |
| 289 | 'dcae_policy_node_id_2', |
| 290 | 'dcae_policy_node_name_2', |
| 291 | dcae_policy.DCAE_POLICY_TYPE, |
| 292 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 293 | None, |
| 294 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 295 | MONKEYED_POLICY_ID_2, 4, priority="2")} |
| 296 | ) |
| 297 | add_target_to_relationships(relationships, node_policy_2) |
| 298 | |
| 299 | if include_bad: |
| 300 | bad_policy_3 = MonkeyedNode( |
| 301 | 'bad_policy_3_node_id', |
| 302 | 'bad_policy_3_node_name', |
| 303 | dcae_policy.DCAE_POLICY_TYPE, |
| 304 | {POLICY_ID: MONKEYED_POLICY_ID_2}, |
| 305 | None, |
| 306 | None |
| 307 | ) |
| 308 | add_target_to_relationships(relationships, bad_policy_3) |
| 309 | |
| 310 | bad_policy_4 = MonkeyedNode( |
| 311 | 'bad_policy_4_node_id', |
| 312 | 'bad_policy_4_node_name', |
| 313 | dcae_policy.DCAE_POLICY_TYPE, |
| 314 | None, |
| 315 | None, |
| 316 | None |
| 317 | ) |
| 318 | add_target_to_relationships(relationships, bad_policy_4) |
| 319 | |
| 320 | weird_policy_5 = MonkeyedNode( |
| 321 | 'weird_policy_5_node_id', |
| 322 | 'weird_policy_5_node_name', |
| 323 | dcae_policy.DCAE_POLICY_TYPE, |
| 324 | None, |
| 325 | None, |
| 326 | {POLICY_BODY: MonkeyedPolicyBody.create_policy_body( |
| 327 | MONKEYED_POLICY_ID, 3, priority="2")} |
| 328 | ) |
| 329 | add_target_to_relationships(relationships, weird_policy_5) |
| 330 | |
| 331 | if include_good: |
| 332 | node_policies = MonkeyedNode( |
| 333 | 'dcae_policies_node_id', |
| 334 | 'dcae_policies_node_name', |
| 335 | dcae_policy.DCAE_POLICIES_TYPE, |
| 336 | {dcae_policy.POLICY_FILTER: {POLICY_NAME: MONKEYED_POLICY_ID_M + ".*"}}, |
| 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""" |
| 474 | node_configure_default_order() |
| 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) |
| 484 | def test_policies_wrong_order(): |
| 485 | """test gather_policies_to_node""" |
| 486 | node_configure_wrong_order_path() |
| 487 | |
| 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 | @cfy_ctx(include_bad=True) |
| 496 | def test_policies_empty_order(): |
| 497 | """test gather_policies_to_node""" |
| 498 | node_configure_empty_order_path() |
| 499 | |
| 500 | runtime_properties = ctx.instance.runtime_properties |
| 501 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 502 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 503 | assert dcae_policy.POLICIES in runtime_properties |
| 504 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 505 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 506 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 507 | @cfy_ctx(include_bad=True) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 508 | def test_policies_to_node(): |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 509 | """test gather_policies_to_node""" |
| 510 | node_configure() |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 511 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 512 | runtime_properties = ctx.instance.runtime_properties |
| 513 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 514 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 515 | assert dcae_policy.POLICIES in runtime_properties |
| 516 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 517 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 518 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 519 | assert MONKEYED_POLICY_ID in policies |
| 520 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 521 | policy = policies[MONKEYED_POLICY_ID] |
| 522 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 523 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 524 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 525 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 526 | |
| 527 | assert MONKEYED_POLICY_ID_B in policies |
| 528 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 529 | policy = policies[MONKEYED_POLICY_ID_B] |
| 530 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_b))) |
| 531 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 532 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 533 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 534 | |
| 535 | assert MONKEYED_POLICY_ID_2 in policies |
| 536 | expected_2 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 4, priority="2") |
| 537 | policy = policies[MONKEYED_POLICY_ID_2] |
| 538 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(expected_2))) |
| 539 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 540 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_2) |
| 541 | assert MonkeyedPolicyBody.is_the_same_dict(expected_2, policy) |
| 542 | |
| 543 | assert MONKEYED_POLICY_ID_M in policies |
| 544 | expected_m = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M, 2, False) |
| 545 | policy = policies[MONKEYED_POLICY_ID_M] |
| 546 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(expected_m))) |
| 547 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(policy))) |
| 548 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_m) |
| 549 | assert MonkeyedPolicyBody.is_the_same_dict(expected_m, policy) |
| 550 | |
| 551 | @cfy_ctx(include_bad=True) |
| 552 | def test_update_policies(): |
| 553 | """test policy_update""" |
| 554 | node_configure() |
| 555 | |
| 556 | runtime_properties = ctx.instance.runtime_properties |
| 557 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 558 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 559 | assert dcae_policy.POLICIES in runtime_properties |
| 560 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 561 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 562 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 563 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 564 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 565 | False, priority="1") |
| 566 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 567 | |
| 568 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 569 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 570 | |
| 571 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 572 | |
| 573 | policy_update(updated_policies=[updated_policy], |
| 574 | added_policies={ |
| 575 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 576 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 577 | }, |
| 578 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 579 | |
| 580 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 581 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 582 | |
| 583 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 584 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 585 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 586 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 587 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 588 | |
| 589 | assert MONKEYED_POLICY_ID_2 in policies |
| 590 | policy = policies[MONKEYED_POLICY_ID_2] |
| 591 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 592 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 593 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 594 | |
| 595 | assert MONKEYED_POLICY_ID in policies |
| 596 | policy = policies[MONKEYED_POLICY_ID] |
| 597 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 598 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 599 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 600 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 601 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 602 | |
| 603 | assert MONKEYED_POLICY_ID_B in policies |
| 604 | policy = policies[MONKEYED_POLICY_ID_B] |
| 605 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 606 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 607 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 608 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 609 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 610 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 611 | @cfy_ctx(include_bad=True) |
| 612 | def test_update_not_only_config(): |
| 613 | """test policy_update""" |
| 614 | node_configure() |
| 615 | |
| 616 | runtime_properties = ctx.instance.runtime_properties |
| 617 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 618 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 619 | assert dcae_policy.POLICIES in runtime_properties |
| 620 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 621 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 622 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 623 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 624 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 625 | False, priority="1") |
| 626 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 627 | |
| 628 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 629 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 630 | |
| 631 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 632 | |
| 633 | policy_update_not_only_config(updated_policies=[updated_policy], |
| 634 | added_policies={ |
| 635 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 636 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 637 | }, |
| 638 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 639 | |
| 640 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 641 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 642 | |
| 643 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 644 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 645 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 646 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 647 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 648 | |
| 649 | assert MONKEYED_POLICY_ID_2 in policies |
| 650 | policy = policies[MONKEYED_POLICY_ID_2] |
| 651 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 652 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 653 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 654 | |
| 655 | assert MONKEYED_POLICY_ID in policies |
| 656 | policy = policies[MONKEYED_POLICY_ID] |
| 657 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 658 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 659 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 660 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 661 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 662 | |
| 663 | assert MONKEYED_POLICY_ID_B in policies |
| 664 | policy = policies[MONKEYED_POLICY_ID_B] |
| 665 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 666 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 667 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 668 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 669 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 670 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 671 | @cfy_ctx(include_bad=True) |
| 672 | def test_update_policies_not(): |
| 673 | """test policy_update - ignore all policies with junk params""" |
| 674 | node_configure() |
| 675 | |
| 676 | runtime_properties = ctx.instance.runtime_properties |
| 677 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 678 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 679 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 680 | assert APPLICATION_CONFIG in runtime_properties |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 681 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 682 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 683 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 684 | ctx.logger.info("app_config: {0}".format(json.dumps(app_config))) |
| 685 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 686 | expected_policies = copy.deepcopy(policies) |
| 687 | expected_app_config = copy.deepcopy(app_config) |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 688 | |
| 689 | existing_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID, priority="1") |
| 690 | damaged_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_2) |
| 691 | del damaged_policy[POLICY_BODY][POLICY_CONFIG] |
| 692 | updated_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_3, 3) |
| 693 | added_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_2, 4) |
| 694 | junk_policy_filter_id = "<<<junk_removed_policy_id>>>" |
| 695 | unexpected_removed_policy_id = "<<<junk_removed_policy_id>>>" |
| 696 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 697 | |
| 698 | assert MONKEYED_POLICY_ID in policies |
| 699 | assert MONKEYED_POLICY_ID_2 in policies |
| 700 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 701 | assert MONKEYED_POLICY_ID_M_3 not in policies |
| 702 | assert unexpected_removed_policy_id not in policies |
| 703 | assert junk_policy_filter_id not in runtime_properties.get(dcae_policy.POLICY_FILTERS, {}) |
| 704 | |
| 705 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 706 | |
| 707 | policy_update(updated_policies=[existing_policy, damaged_policy, updated_policy], |
| 708 | added_policies={ |
| 709 | junk_policy_filter_id: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 710 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}, |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 711 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 712 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_2: damaged_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 713 | }, |
| 714 | removed_policies=[unexpected_removed_policy_id]) |
| 715 | |
| 716 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 717 | |
| 718 | ctx.logger.info("policies not changed: {0}".format(json.dumps(policies))) |
| 719 | assert MonkeyedPolicyBody.is_the_same_dict(policies, expected_policies) |
| 720 | assert MonkeyedPolicyBody.is_the_same_dict(expected_policies, policies) |
| 721 | |
| 722 | ctx.logger.info("app_config not changed: {0}".format(json.dumps(app_config))) |
| 723 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_app_config) |
| 724 | assert MonkeyedPolicyBody.is_the_same_dict(expected_app_config, app_config) |
| 725 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 726 | @cfy_ctx(include_bad=True) |
| 727 | def test_update_many_calcs(): |
| 728 | """test policy_update""" |
| 729 | node_configure() |
| 730 | |
| 731 | runtime_properties = ctx.instance.runtime_properties |
| 732 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 733 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 734 | assert dcae_policy.POLICIES in runtime_properties |
| 735 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 736 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 737 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 738 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 739 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 740 | False, priority="1") |
| 741 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 742 | |
| 743 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 744 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 745 | |
| 746 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 747 | |
| 748 | policy_update_many_calcs(updated_policies=[updated_policy], |
| 749 | added_policies={ |
| 750 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 751 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 752 | }, |
| 753 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 754 | |
| 755 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 756 | assert MONKEYED_POLICY_ID_M not in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 757 | |
| 758 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 759 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 760 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 761 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 762 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 763 | |
| 764 | assert MONKEYED_POLICY_ID_2 in policies |
| 765 | policy = policies[MONKEYED_POLICY_ID_2] |
| 766 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 767 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 768 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 769 | |
| 770 | assert MONKEYED_POLICY_ID in policies |
| 771 | policy = policies[MONKEYED_POLICY_ID] |
| 772 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 773 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 774 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 775 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 776 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 777 | |
| 778 | assert MONKEYED_POLICY_ID_B in policies |
| 779 | policy = policies[MONKEYED_POLICY_ID_B] |
| 780 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 781 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 782 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 783 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 784 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 785 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 786 | @cfy_ctx(include_bad=True) |
| 787 | def test_remove_all_policies(): |
| 788 | """test policy_update - remove all policies""" |
| 789 | node_configure() |
| 790 | |
| 791 | runtime_properties = ctx.instance.runtime_properties |
| 792 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 793 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 794 | assert dcae_policy.POLICIES in runtime_properties |
| 795 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 796 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 797 | |
| 798 | remove_policy_ids = policies.keys() |
| 799 | |
| 800 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 801 | |
| 802 | ctx.logger.info("removed: {0}".format(remove_policy_ids)) |
| 803 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 804 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 805 | assert Policies.get_policy_bodies() == [] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 806 | |
| 807 | assert APPLICATION_CONFIG in runtime_properties |
| 808 | assert APPLICATION_CONFIG in ctx.node.properties |
| 809 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 810 | expected_config = dict(ctx.node.properties[APPLICATION_CONFIG]) |
| 811 | ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config))) |
| 812 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config) |
| 813 | assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config) |
| 814 | |
| 815 | @cfy_ctx(include_bad=True) |
| 816 | def test_remove_all_policies_twice(): |
| 817 | """test policy_update - remove all policies twice""" |
| 818 | node_configure() |
| 819 | |
| 820 | runtime_properties = ctx.instance.runtime_properties |
| 821 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 822 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 823 | assert dcae_policy.POLICIES in runtime_properties |
| 824 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 825 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 826 | |
| 827 | remove_policy_ids = policies.keys() |
| 828 | |
| 829 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 830 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 831 | |
| 832 | ctx.logger.info("removed: {0}".format(remove_policy_ids)) |
| 833 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 834 | assert dcae_policy.POLICIES in runtime_properties |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 835 | assert Policies.get_policy_bodies() == [] |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 836 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 837 | assert APPLICATION_CONFIG in runtime_properties |
| 838 | assert APPLICATION_CONFIG in ctx.node.properties |
| 839 | app_config = runtime_properties[APPLICATION_CONFIG] |
| 840 | expected_config = dict(ctx.node.properties[APPLICATION_CONFIG]) |
| 841 | ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config))) |
| 842 | assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config) |
| 843 | assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 844 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 845 | @cfy_ctx(include_bad=True) |
| 846 | def test_remove_then_update(): |
| 847 | """test policy_update""" |
| 848 | node_configure() |
| 849 | |
| 850 | runtime_properties = ctx.instance.runtime_properties |
| 851 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 852 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 853 | assert dcae_policy.POLICIES in runtime_properties |
| 854 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 855 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 856 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 857 | remove_policy_ids = policies.keys() |
| 858 | policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids) |
| 859 | |
| 860 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 861 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 862 | False, priority="1") |
| 863 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 864 | |
| 865 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 866 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 867 | |
| 868 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 869 | |
| 870 | policy_update(updated_policies=[updated_policy], |
| 871 | added_policies={ |
| 872 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 873 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 874 | }, |
| 875 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 876 | |
| 877 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 878 | assert MONKEYED_POLICY_ID_M not in policies |
| 879 | |
| 880 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 881 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 882 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 883 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 884 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 885 | |
| 886 | assert MONKEYED_POLICY_ID_2 in policies |
| 887 | policy = policies[MONKEYED_POLICY_ID_2] |
| 888 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 889 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 890 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 891 | |
| 892 | assert MONKEYED_POLICY_ID in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 893 | assert MONKEYED_POLICY_ID_B in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 894 | |
| 895 | @cfy_ctx(include_bad=True) |
| 896 | def test_remove_update_many_calcs(): |
| 897 | """test policy_update""" |
| 898 | node_configure() |
| 899 | |
| 900 | runtime_properties = ctx.instance.runtime_properties |
| 901 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 902 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 903 | assert dcae_policy.POLICIES in runtime_properties |
| 904 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 905 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 906 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 907 | remove_policy_ids = policies.keys() |
| 908 | policy_update_many_calcs(updated_policies=None, |
| 909 | added_policies=None, |
| 910 | removed_policies=remove_policy_ids) |
| 911 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 912 | assert dcae_policy.POLICIES in runtime_properties |
| 913 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 914 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 915 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 916 | updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 917 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 918 | False, priority="1") |
| 919 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy))) |
| 920 | |
| 921 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 922 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 923 | |
| 924 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 925 | |
| 926 | policy_update_many_calcs(updated_policies=[updated_policy], |
| 927 | added_policies={ |
| 928 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 929 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 930 | }, |
| 931 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 932 | |
| 933 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 934 | assert MONKEYED_POLICY_ID_M not in policies |
| 935 | |
| 936 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 937 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 938 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 939 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 940 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 941 | |
| 942 | assert MONKEYED_POLICY_ID_2 in policies |
| 943 | policy = policies[MONKEYED_POLICY_ID_2] |
| 944 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 945 | assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy) |
| 946 | assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy) |
| 947 | |
| 948 | assert MONKEYED_POLICY_ID in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 949 | assert MONKEYED_POLICY_ID_B in policies |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 950 | |
| 951 | @cfy_ctx(include_bad=True) |
| 952 | def test_bad_update_many_calcs(): |
| 953 | """test policy_update""" |
| 954 | node_configure() |
| 955 | |
| 956 | runtime_properties = ctx.instance.runtime_properties |
| 957 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 958 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 959 | assert dcae_policy.POLICIES in runtime_properties |
| 960 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 961 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 962 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 963 | damaged_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20") |
| 964 | damaged_policy[POLICY_BODY][POLICY_CONFIG] = ["damaged config"] |
| 965 | |
| 966 | added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2, |
| 967 | False, priority="1") |
| 968 | added_policy[POLICY_BODY][POLICY_CONFIG] = {"unexpected":"foo", "none": None} |
| 969 | |
| 970 | ctx.logger.info("policy_update: [{0}]".format(json.dumps(damaged_policy))) |
| 971 | |
| 972 | ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2)) |
| 973 | assert MONKEYED_POLICY_ID_M_2 not in policies |
| 974 | |
| 975 | policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"] |
| 976 | |
| 977 | policy_update_many_calcs(updated_policies=[damaged_policy], |
| 978 | added_policies={ |
| 979 | policy_filter_ids[0]: { |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 980 | dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}} |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 981 | }, |
| 982 | removed_policies=[MONKEYED_POLICY_ID_M]) |
| 983 | |
| 984 | ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M)) |
| 985 | assert MONKEYED_POLICY_ID_M not in policies |
| 986 | |
| 987 | assert MONKEYED_POLICY_ID_M_2 in policies |
| 988 | policy = policies[MONKEYED_POLICY_ID_M_2] |
| 989 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy))) |
| 990 | assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy) |
| 991 | assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy) |
| 992 | |
| 993 | assert MONKEYED_POLICY_ID_2 in policies |
| 994 | policy = policies[MONKEYED_POLICY_ID_2] |
| 995 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy))) |
| 996 | assert MonkeyedPolicyBody.is_the_same_dict(policy, damaged_policy) |
| 997 | assert MonkeyedPolicyBody.is_the_same_dict(damaged_policy, policy) |
| 998 | |
| 999 | assert MONKEYED_POLICY_ID in policies |
| 1000 | policy = policies[MONKEYED_POLICY_ID] |
| 1001 | expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1") |
| 1002 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1))) |
| 1003 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy))) |
| 1004 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1) |
| 1005 | assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy) |
| 1006 | |
| 1007 | assert MONKEYED_POLICY_ID_B in policies |
| 1008 | policy = policies[MONKEYED_POLICY_ID_B] |
| 1009 | expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5") |
| 1010 | ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1))) |
| 1011 | ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy))) |
| 1012 | assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b) |
| 1013 | assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy) |
| 1014 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1015 | @cfy_ctx(include_bad=True, include_good=False) |
| 1016 | def test_bad_policies(): |
| 1017 | """test bad policy nodes""" |
| 1018 | node_configure() |
| 1019 | |
| 1020 | runtime_properties = ctx.instance.runtime_properties |
| 1021 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 1022 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 1023 | assert dcae_policy.POLICIES in runtime_properties |
| 1024 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1025 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 1026 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1027 | @cfy_ctx(include_bad=True, include_good=False) |
| 1028 | def test_wrong_ctx_node_configure(): |
| 1029 | """test wrong ctx""" |
| 1030 | current_ctx.set(ctx.instance.relationships[0]) |
| 1031 | ctx_type = ctx.type |
| 1032 | |
| 1033 | with pytest.raises(NonRecoverableError) as excinfo: |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1034 | node_configure() |
| 1035 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1036 | CurrentCtx.reset() |
| 1037 | ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value))) |
| 1038 | assert ctx_type == 'cloudify.relationships.depends_on' |
| 1039 | 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] | 1040 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1041 | @cfy_ctx(include_bad=True, include_good=False) |
| 1042 | def test_wrong_ctx_policy_update(): |
| 1043 | """test wrong ctx""" |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1044 | no_policies = Policies.get_policy_bodies() |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1045 | current_ctx.set(ctx.instance.relationships[0]) |
| 1046 | ctx_type = ctx.type |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1047 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1048 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1049 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1050 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1051 | CurrentCtx.reset() |
| 1052 | ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value))) |
| 1053 | assert ctx_type == 'cloudify.relationships.depends_on' |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1054 | assert no_policies == [] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1055 | 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] | 1056 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1057 | def test_defenses_on_decorators(): |
| 1058 | """test defenses of code mainly for code coverage""" |
| 1059 | should_be_none = Policies.gather_policies_to_node()(None) |
| 1060 | assert should_be_none is None |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1061 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1062 | should_be_none = Policies.update_policies_on_node()(None) |
| 1063 | assert should_be_none is None |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1064 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1065 | def monkeyed_set_policies_boom(policies): |
| 1066 | """monkeypatch for exception""" |
| 1067 | raise Exception("Policies._set_policies boom: {0}".format(json.dumps(policies or {}))) |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1068 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1069 | @pytest.fixture() |
| 1070 | def fix_boom_gather(monkeypatch): |
| 1071 | """monkeyed boom""" |
| 1072 | monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._set_policies', |
| 1073 | monkeyed_set_policies_boom) |
| 1074 | return fix_boom_gather # provide the fixture value |
Alex Shatov | 8cdeeeb | 2017-09-26 17:03:11 -0400 | [diff] [blame] | 1075 | |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1076 | @cfy_ctx(include_bad=True, include_good=False) |
| 1077 | @pytest.mark.usefixtures("fix_boom_gather") |
| 1078 | def test_exception_on_gather(): |
| 1079 | """test exception on gather""" |
| 1080 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1081 | node_configure() |
| 1082 | |
| 1083 | ctx.logger.info("monkeyed_set_policies_boom: {0}".format(str(excinfo.value))) |
| 1084 | assert str(excinfo.value).startswith("Failed to set the policies ") |
| 1085 | |
| 1086 | def monkeyed_update_policies_boom(policies): |
| 1087 | """monkeypatch for exception""" |
| 1088 | raise Exception("Policies._update_policies boom") |
| 1089 | |
| 1090 | @pytest.fixture() |
| 1091 | def fix_boom_update_policies(monkeypatch): |
| 1092 | """monkeyed boom""" |
| 1093 | monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._update_policies', |
| 1094 | monkeyed_update_policies_boom) |
| 1095 | return fix_boom_update_policies # provide the fixture value |
| 1096 | |
| 1097 | @cfy_ctx(include_bad=True, include_good=False) |
| 1098 | @pytest.mark.usefixtures("fix_boom_update_policies") |
| 1099 | def test_exception_on_update(): |
| 1100 | """test exception on update_policies""" |
| 1101 | with pytest.raises(NonRecoverableError) as excinfo: |
| 1102 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
| 1103 | |
| 1104 | ctx.logger.info("monkeyed_update_policies_boom: {0}".format(str(excinfo.value))) |
| 1105 | assert str(excinfo.value).startswith("Failed to update the policies ") |
| 1106 | |
| 1107 | @cfy_ctx(include_bad=True, include_good=False) |
| 1108 | def test_defenses_on_policy_update(): |
| 1109 | """test defenses on policy_update""" |
| 1110 | policy_update(updated_policies=None, added_policies=None, removed_policies=None) |
| 1111 | ctx.logger.info("policy_update() ok") |
| 1112 | |
| 1113 | @cfy_ctx(include_bad=True, include_good=False) |
| 1114 | def test_defenses_on_set_policies(): |
| 1115 | """test defenses of code mainly for code coverage""" |
| 1116 | node_configure() |
| 1117 | |
| 1118 | runtime_properties = ctx.instance.runtime_properties |
| 1119 | ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties))) |
| 1120 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 1121 | assert dcae_policy.POLICIES in runtime_properties |
| 1122 | policies = runtime_properties[dcae_policy.POLICIES] |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1123 | ctx.logger.info("policies: {0}".format(json.dumps(policies))) |
| 1124 | |
Alex Shatov | 2b3d24b | 2018-03-02 13:46:11 -0500 | [diff] [blame] | 1125 | Policies._set_policies({}) |
| 1126 | |
| 1127 | assert dcae_policy.POLICIES not in runtime_properties |
Alex Shatov | 90f2105 | 2018-01-10 11:39:32 -0500 | [diff] [blame] | 1128 | |
| 1129 | Policies._set_policies({}) |
| 1130 | |
Alex Shatov | cfdd67d | 2018-02-23 14:56:36 -0500 | [diff] [blame] | 1131 | assert dcae_policy.POLICIES not in runtime_properties |