blob: 81958f01d03f18e32b0495b64a655acbc2ee4b04 [file] [log] [blame]
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001# ============LICENSE_START=======================================================
Lusheng Jicc307002018-02-12 11:04:54 -05002# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04003# ================================================================================
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 Shatov90f21052018-01-10 11:39:32 -050019"""tests of decorators around the cloudify operations to handle policy actions"""
20
21import copy
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040022import json
23import logging
24from datetime import datetime, timedelta
Alex Shatov90f21052018-01-10 11:39:32 -050025from functools import wraps
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040026
27import pytest
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040028from cloudify import ctx
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040029from cloudify.exceptions import NonRecoverableError
Alex Shatov90f21052018-01-10 11:39:32 -050030from cloudify.state import current_ctx
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040031
Alex Shatovcfdd67d2018-02-23 14:56:36 -050032from onap_dcae_dcaepolicy_lib import dcae_policy
Alex Shatov90f21052018-01-10 11:39:32 -050033from onap_dcae_dcaepolicy_lib.dcae_policy import Policies
Alex Shatov90f21052018-01-10 11:39:32 -050034from tests.log_ctx import CtxLogger
35from tests.mock_cloudify_ctx import (TARGET_NODE_ID, TARGET_NODE_NAME,
36 MockCloudifyContextFull)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040037
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040038POLICY_ID = 'policy_id'
39POLICY_VERSION = "policyVersion"
40POLICY_NAME = "policyName"
41POLICY_BODY = 'policy_body'
42POLICY_CONFIG = 'config'
Alex Shatov90f21052018-01-10 11:39:32 -050043
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040044MONKEYED_POLICY_ID = 'monkeyed.Config_peach'
45MONKEYED_POLICY_ID_2 = 'monkeyed.Config_peach_2'
Alex Shatov90f21052018-01-10 11:39:32 -050046MONKEYED_POLICY_ID_M = 'monkeyed.Config_multi'
47MONKEYED_POLICY_ID_M_2 = 'monkeyed.Config_multi_2'
48MONKEYED_POLICY_ID_M_3 = 'monkeyed.Config_multi_3'
49MONKEYED_POLICY_ID_B = 'monkeyed.Config_both'
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040050APPLICATION_CONFIG = "application_config"
Alex Shatov90f21052018-01-10 11:39:32 -050051LOG_FILE = 'logs/test_onap_dcae_dcaepolicy_lib.log'
52LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040053
54RUN_TS = datetime.utcnow()
55
56class 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
73class MonkeyedPolicyBody(object):
74 """policy body that policy-engine returns"""
75 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -050076 def create_policy_body(policy_id, policy_version=1, priority=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040077 """returns a fake policy-body"""
78 prev_ver = policy_version - 1
79 timestamp = RUN_TS + timedelta(hours=prev_ver)
80
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040081 this_ver = str(policy_version)
82 config = {
Alex Shatov90f21052018-01-10 11:39:32 -050083 "policy_updated_from_ver": str(prev_ver),
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040084 "policy_updated_to_ver": this_ver,
Alex Shatov90f21052018-01-10 11:39:32 -050085 "policy_hello": LOREM_IPSUM[prev_ver % len(LOREM_IPSUM)],
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040086 "policy_updated_ts": timestamp.isoformat()[:-3] + 'Z',
87 "updated_policy_id": policy_id
88 }
Alex Shatov90f21052018-01-10 11:39:32 -050089 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 Shatov8cdeeeb2017-09-26 17:03:11 -040098 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 Shatov90f21052018-01-10 11:39:32 -0500105 "matchingConditions": matching_conditions,
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400106 "responseAttributes": {},
107 "property": None
108 }
109
110 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -0500111 def create_policy_bare(policy_id, policy_version=1, priority=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400112 """returns the whole policy object for policy_id and policy_version"""
113 return {
Alex Shatov90f21052018-01-10 11:39:32 -0500114 POLICY_ID: policy_id,
115 POLICY_BODY: MonkeyedPolicyBody.create_policy_body(
116 policy_id, policy_version, priority, **kwargs)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400117 }
118
119 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -0500120 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 Shatov8cdeeeb2017-09-26 17:03:11 -0400128 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 Shatov90f21052018-01-10 11:39:32 -0500135
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 Shatov8cdeeeb2017-09-26 17:03:11 -0400144 return False
145 return True
146
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400147class 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 Shatov90f21052018-01-10 11:39:32 -0500153 def __init__(self, node_id, node_name, node_type, properties=None,
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400154 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 Shatov8cdeeeb2017-09-26 17:03:11 -0400168
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500169def 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 Shatov90f21052018-01-10 11:39:32 -0500176def operation_node_configure(**kwargs):
177 """do the node-configure operation"""
178 ctx.logger.info("operation_node_configure kwargs: {0}".format(kwargs))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400179
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500180 app_config = get_app_config()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400181 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500182 ctx.logger.info("property app_config: {0}".format(json.dumps(app_config)))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400183
Alex Shatov90f21052018-01-10 11:39:32 -0500184@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task')
185@Policies.gather_policies_to_node()
186def 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 Shatov2b3d24b2018-03-02 13:46:11 -0500191@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500192def 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 Shatov2b3d24b2018-03-02 13:46:11 -0500199@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500200def 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 Shatov2b3d24b2018-03-02 13:46:11 -0500205@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500206def node_configure_empty_order_path(**kwargs):
207 """wrong data in param policy_apply_order_path"""
208 operation_node_configure(**kwargs)
209
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400210@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500211@Policies.update_policies_on_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500212def policy_update(updated_policies, removed_policies=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400213 """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 Shatov8cdeeeb2017-09-26 17:03:11 -0400216 """
Alex Shatov90f21052018-01-10 11:39:32 -0500217 # This is how to merge the policies into default app_config object
218 # (runtime_properties[APPLICATION_CONFIG] = application_config)
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500219 app_config = get_app_config()
220 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400221
222 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
223
Alex Shatov90f21052018-01-10 11:39:32 -0500224@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500225@Policies.update_policies_on_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500226def 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 Shatov8cdeeeb2017-09-26 17:03:11 -0400228
Alex Shatov90f21052018-01-10 11:39:32 -0500229 :updated_policies: contains the list of changed policy-configs when configs_only=True (default).
Alex Shatov90f21052018-01-10 11:39:32 -0500230 """
231 # This is how to merge the policies into default app_config object
232 # (runtime_properties[APPLICATION_CONFIG] = application_config)
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500233 app_config = get_app_config()
234 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov90f21052018-01-10 11:39:32 -0500235
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 Shatov2b3d24b2018-03-02 13:46:11 -0500239@Policies.update_policies_on_node()
240def policy_update_many_calcs(updated_policies, removed_policies=None, policies=None, **kwargs):
Alex Shatov90f21052018-01-10 11:39:32 -0500241 """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 Shatov90f21052018-01-10 11:39:32 -0500244 """
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500245 app_config = get_app_config()
246 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov90f21052018-01-10 11:39:32 -0500247
248 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
249
Alex Shatov90f21052018-01-10 11:39:32 -0500250class 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 Shatov2b3d24b2018-03-02 13:46:11 -0500435 {APPLICATION_CONFIG: MonkeyedPolicyBody.create_policy_body(
Alex Shatov90f21052018-01-10 11:39:32 -0500436 "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 Shatov90f21052018-01-10 11:39:32 -0500448def 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)
472def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500479 assert dcae_policy.POLICIES in runtime_properties
480 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500481 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
482
Alex Shatov90f21052018-01-10 11:39:32 -0500483@cfy_ctx(include_bad=True)
484def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500491 assert dcae_policy.POLICIES in runtime_properties
492 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500493 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
494
Alex Shatov90f21052018-01-10 11:39:32 -0500495@cfy_ctx(include_bad=True)
496def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500503 assert dcae_policy.POLICIES in runtime_properties
504 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500505 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
506
Alex Shatov90f21052018-01-10 11:39:32 -0500507@cfy_ctx(include_bad=True)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400508def test_policies_to_node():
Alex Shatov90f21052018-01-10 11:39:32 -0500509 """test gather_policies_to_node"""
510 node_configure()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400511
Alex Shatov90f21052018-01-10 11:39:32 -0500512 runtime_properties = ctx.instance.runtime_properties
513 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
514
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500515 assert dcae_policy.POLICIES in runtime_properties
516 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500517 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
518
Alex Shatov90f21052018-01-10 11:39:32 -0500519 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)
552def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500559 assert dcae_policy.POLICIES in runtime_properties
560 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500561 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
562
Alex Shatov90f21052018-01-10 11:39:32 -0500563 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 Shatovcfdd67d2018-02-23 14:56:36 -0500576 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500577 },
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 Shatov90f21052018-01-10 11:39:32 -0500582
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 Shatov90f21052018-01-10 11:39:32 -0500611@cfy_ctx(include_bad=True)
612def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500619 assert dcae_policy.POLICIES in runtime_properties
620 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500621 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
622
Alex Shatov90f21052018-01-10 11:39:32 -0500623 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 Shatovcfdd67d2018-02-23 14:56:36 -0500636 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500637 },
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 Shatov90f21052018-01-10 11:39:32 -0500642
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 Shatov90f21052018-01-10 11:39:32 -0500671@cfy_ctx(include_bad=True)
672def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500679 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov90f21052018-01-10 11:39:32 -0500680 assert APPLICATION_CONFIG in runtime_properties
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500681 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500682 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 Shatov90f21052018-01-10 11:39:32 -0500686 expected_policies = copy.deepcopy(policies)
687 expected_app_config = copy.deepcopy(app_config)
Alex Shatov90f21052018-01-10 11:39:32 -0500688
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 Shatovcfdd67d2018-02-23 14:56:36 -0500710 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}},
Alex Shatov90f21052018-01-10 11:39:32 -0500711 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500712 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_2: damaged_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500713 },
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 Shatov90f21052018-01-10 11:39:32 -0500726@cfy_ctx(include_bad=True)
727def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500734 assert dcae_policy.POLICIES in runtime_properties
735 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500736 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
737
Alex Shatov90f21052018-01-10 11:39:32 -0500738 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 Shatovcfdd67d2018-02-23 14:56:36 -0500751 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500752 },
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 Shatov90f21052018-01-10 11:39:32 -0500757
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 Shatov90f21052018-01-10 11:39:32 -0500786@cfy_ctx(include_bad=True)
787def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500794 assert dcae_policy.POLICIES in runtime_properties
795 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500796 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 Shatovcfdd67d2018-02-23 14:56:36 -0500804 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500805 assert Policies.get_policy_bodies() == []
Alex Shatov90f21052018-01-10 11:39:32 -0500806
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)
816def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500823 assert dcae_policy.POLICIES in runtime_properties
824 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500825 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 Shatovcfdd67d2018-02-23 14:56:36 -0500834 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500835 assert Policies.get_policy_bodies() == []
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400836
Alex Shatov90f21052018-01-10 11:39:32 -0500837 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 Shatov8cdeeeb2017-09-26 17:03:11 -0400844
Alex Shatov90f21052018-01-10 11:39:32 -0500845@cfy_ctx(include_bad=True)
846def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500853 assert dcae_policy.POLICIES in runtime_properties
854 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500855 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
856
Alex Shatov90f21052018-01-10 11:39:32 -0500857 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 Shatovcfdd67d2018-02-23 14:56:36 -0500873 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500874 },
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 Shatov90f21052018-01-10 11:39:32 -0500893 assert MONKEYED_POLICY_ID_B in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500894
895@cfy_ctx(include_bad=True)
896def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500903 assert dcae_policy.POLICIES in runtime_properties
904 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500905 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
906
Alex Shatov90f21052018-01-10 11:39:32 -0500907 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 Shatovcfdd67d2018-02-23 14:56:36 -0500912 assert dcae_policy.POLICIES in runtime_properties
913 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500914 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
915
Alex Shatov90f21052018-01-10 11:39:32 -0500916 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 Shatovcfdd67d2018-02-23 14:56:36 -0500929 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500930 },
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 Shatov90f21052018-01-10 11:39:32 -0500949 assert MONKEYED_POLICY_ID_B in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500950
951@cfy_ctx(include_bad=True)
952def 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 Shatovcfdd67d2018-02-23 14:56:36 -0500959 assert dcae_policy.POLICIES in runtime_properties
960 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500961 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
962
Alex Shatov90f21052018-01-10 11:39:32 -0500963 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 Shatovcfdd67d2018-02-23 14:56:36 -0500980 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500981 },
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 Shatov90f21052018-01-10 11:39:32 -05001015@cfy_ctx(include_bad=True, include_good=False)
1016def 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 Shatovcfdd67d2018-02-23 14:56:36 -05001023 assert dcae_policy.POLICIES in runtime_properties
1024 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -05001025 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
1026
Alex Shatov90f21052018-01-10 11:39:32 -05001027@cfy_ctx(include_bad=True, include_good=False)
1028def 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 Shatov8cdeeeb2017-09-26 17:03:11 -04001034 node_configure()
1035
Alex Shatov90f21052018-01-10 11:39:32 -05001036 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 Shatov8cdeeeb2017-09-26 17:03:11 -04001040
Alex Shatov90f21052018-01-10 11:39:32 -05001041@cfy_ctx(include_bad=True, include_good=False)
1042def test_wrong_ctx_policy_update():
1043 """test wrong ctx"""
Alex Shatov2b3d24b2018-03-02 13:46:11 -05001044 no_policies = Policies.get_policy_bodies()
Alex Shatov90f21052018-01-10 11:39:32 -05001045 current_ctx.set(ctx.instance.relationships[0])
1046 ctx_type = ctx.type
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001047
Alex Shatov90f21052018-01-10 11:39:32 -05001048 with pytest.raises(NonRecoverableError) as excinfo:
1049 policy_update(updated_policies=None, added_policies=None, removed_policies=None)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001050
Alex Shatov90f21052018-01-10 11:39:32 -05001051 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 Shatov2b3d24b2018-03-02 13:46:11 -05001054 assert no_policies == []
Alex Shatov90f21052018-01-10 11:39:32 -05001055 assert str(excinfo.value) == "can only invoke update_policies_on_node on node"
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001056
Alex Shatov90f21052018-01-10 11:39:32 -05001057def 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 Shatov8cdeeeb2017-09-26 17:03:11 -04001061
Alex Shatov90f21052018-01-10 11:39:32 -05001062 should_be_none = Policies.update_policies_on_node()(None)
1063 assert should_be_none is None
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001064
Alex Shatov90f21052018-01-10 11:39:32 -05001065def monkeyed_set_policies_boom(policies):
1066 """monkeypatch for exception"""
1067 raise Exception("Policies._set_policies boom: {0}".format(json.dumps(policies or {})))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001068
Alex Shatov90f21052018-01-10 11:39:32 -05001069@pytest.fixture()
1070def 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 Shatov8cdeeeb2017-09-26 17:03:11 -04001075
Alex Shatov90f21052018-01-10 11:39:32 -05001076@cfy_ctx(include_bad=True, include_good=False)
1077@pytest.mark.usefixtures("fix_boom_gather")
1078def 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
1086def monkeyed_update_policies_boom(policies):
1087 """monkeypatch for exception"""
1088 raise Exception("Policies._update_policies boom")
1089
1090@pytest.fixture()
1091def 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")
1099def 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)
1108def 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)
1114def 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 Shatovcfdd67d2018-02-23 14:56:36 -05001121 assert dcae_policy.POLICIES in runtime_properties
1122 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -05001123 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
1124
Alex Shatov2b3d24b2018-03-02 13:46:11 -05001125 Policies._set_policies({})
1126
1127 assert dcae_policy.POLICIES not in runtime_properties
Alex Shatov90f21052018-01-10 11:39:32 -05001128
1129 Policies._set_policies({})
1130
Alex Shatovcfdd67d2018-02-23 14:56:36 -05001131 assert dcae_policy.POLICIES not in runtime_properties