blob: 37ab9f63d544fb07a3ace134495f555710fe9ebc [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 Shatovb73f81e2018-03-23 14:34:34 -040043CONFIG_NAME = "ConfigName"
Alex Shatov90f21052018-01-10 11:39:32 -050044
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040045MONKEYED_POLICY_ID = 'monkeyed.Config_peach'
46MONKEYED_POLICY_ID_2 = 'monkeyed.Config_peach_2'
Alex Shatov90f21052018-01-10 11:39:32 -050047MONKEYED_POLICY_ID_M = 'monkeyed.Config_multi'
48MONKEYED_POLICY_ID_M_2 = 'monkeyed.Config_multi_2'
49MONKEYED_POLICY_ID_M_3 = 'monkeyed.Config_multi_3'
50MONKEYED_POLICY_ID_B = 'monkeyed.Config_both'
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040051APPLICATION_CONFIG = "application_config"
Alex Shatov90f21052018-01-10 11:39:32 -050052LOG_FILE = 'logs/test_onap_dcae_dcaepolicy_lib.log'
53LOREM_IPSUM = """Lorem ipsum dolor sit amet consectetur ametist""".split()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040054
55RUN_TS = datetime.utcnow()
56
57class MonkeyedLogHandler(object):
58 """keep the shared logger handler here"""
59 _log_handler = None
60
61 @staticmethod
62 def add_handler_to(logger):
63 """adds the local handler to the logger"""
64 if not MonkeyedLogHandler._log_handler:
65 MonkeyedLogHandler._log_handler = logging.FileHandler(LOG_FILE)
66 MonkeyedLogHandler._log_handler.setLevel(logging.DEBUG)
67 formatter = logging.Formatter(
68 fmt='%(asctime)s.%(msecs)03d %(levelname)+8s ' + \
69 '%(threadName)s %(name)s.%(funcName)s: %(message)s', \
70 datefmt='%Y%m%d_%H%M%S')
71 MonkeyedLogHandler._log_handler.setFormatter(formatter)
72 logger.addHandler(MonkeyedLogHandler._log_handler)
73
74class MonkeyedPolicyBody(object):
75 """policy body that policy-engine returns"""
76 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -050077 def create_policy_body(policy_id, policy_version=1, priority=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040078 """returns a fake policy-body"""
79 prev_ver = policy_version - 1
80 timestamp = RUN_TS + timedelta(hours=prev_ver)
81
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040082 this_ver = str(policy_version)
83 config = {
Alex Shatov90f21052018-01-10 11:39:32 -050084 "policy_updated_from_ver": str(prev_ver),
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040085 "policy_updated_to_ver": this_ver,
Alex Shatov90f21052018-01-10 11:39:32 -050086 "policy_hello": LOREM_IPSUM[prev_ver % len(LOREM_IPSUM)],
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040087 "policy_updated_ts": timestamp.isoformat()[:-3] + 'Z',
88 "updated_policy_id": policy_id
89 }
Alex Shatov90f21052018-01-10 11:39:32 -050090 config.update(copy.deepcopy(kwargs) or {})
91
92 matching_conditions = {
93 "ONAPName": "DCAE",
Alex Shatovb73f81e2018-03-23 14:34:34 -040094 CONFIG_NAME: "alex_config_name"
Alex Shatov90f21052018-01-10 11:39:32 -050095 }
96 if priority is not None:
97 matching_conditions["priority"] = priority
98
Alex Shatov8cdeeeb2017-09-26 17:03:11 -040099 return {
100 "policyConfigMessage": "Config Retrieved! ",
101 "policyConfigStatus": "CONFIG_RETRIEVED",
102 "type": "JSON",
103 POLICY_NAME: "{0}.{1}.xml".format(policy_id, this_ver),
104 POLICY_VERSION: this_ver,
105 POLICY_CONFIG: config,
Alex Shatov90f21052018-01-10 11:39:32 -0500106 "matchingConditions": matching_conditions,
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400107 "responseAttributes": {},
108 "property": None
109 }
110
111 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -0500112 def create_policy_bare(policy_id, policy_version=1, priority=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400113 """returns the whole policy object for policy_id and policy_version"""
114 return {
Alex Shatov90f21052018-01-10 11:39:32 -0500115 POLICY_ID: policy_id,
116 POLICY_BODY: MonkeyedPolicyBody.create_policy_body(
117 policy_id, policy_version, priority, **kwargs)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400118 }
119
120 @staticmethod
Alex Shatov90f21052018-01-10 11:39:32 -0500121 def create_policy(policy_id, policy_version=1, policy_persistent=True, priority=None, **kwargs):
122 """returns the whole policy object for policy_id and policy_version"""
123 policy = MonkeyedPolicyBody.create_policy_bare(
124 policy_id, policy_version, priority, **kwargs)
125 policy[dcae_policy.POLICY_PERSISTENT] = bool(policy_persistent)
126 return policy
127
128 @staticmethod
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400129 def is_the_same_dict(policy_body_1, policy_body_2):
130 """check whether both policy_body objects are the same"""
131 if not isinstance(policy_body_1, dict) or not isinstance(policy_body_2, dict):
132 return False
133 for key in policy_body_1.keys():
134 if key not in policy_body_2:
135 return False
Alex Shatov90f21052018-01-10 11:39:32 -0500136
137 val_1 = policy_body_1[key]
138 val_2 = policy_body_2[key]
139 if isinstance(val_1, dict) \
140 and not MonkeyedPolicyBody.is_the_same_dict(val_1, val_2):
141 return False
142 if (val_1 is None and val_2 is not None) \
143 or (val_1 is not None and val_2 is None) \
144 or (val_1 != val_2):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400145 return False
146 return True
147
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400148class MonkeyedNode(object):
149 """node in cloudify"""
150 BLUEPRINT_ID = 'test_dcae_policy_bp_id'
151 DEPLOYMENT_ID = 'test_dcae_policy_dpl_id'
152 EXECUTION_ID = 'test_dcae_policy_exe_id'
153
Alex Shatov90f21052018-01-10 11:39:32 -0500154 def __init__(self, node_id, node_name, node_type, properties=None,
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400155 relationships=None, runtime_properties=None):
156 self.node_id = node_id
157 self.node_name = node_name
158 self.ctx = MockCloudifyContextFull(
159 node_id=self.node_id,
160 node_name=self.node_name,
161 node_type=node_type,
162 blueprint_id=MonkeyedNode.BLUEPRINT_ID,
163 deployment_id=MonkeyedNode.DEPLOYMENT_ID,
164 execution_id=MonkeyedNode.EXECUTION_ID,
165 properties=properties,
166 relationships=relationships,
167 runtime_properties=runtime_properties
168 )
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400169
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500170def get_app_config():
171 """just get the config"""
172 config = copy.deepcopy(dict(ctx.instance.runtime_properties.get(APPLICATION_CONFIG, {})))
173 if not config:
174 config = copy.deepcopy(dict(ctx.node.properties.get(APPLICATION_CONFIG, {})))
175 return config
176
Alex Shatov90f21052018-01-10 11:39:32 -0500177def operation_node_configure(**kwargs):
178 """do the node-configure operation"""
179 ctx.logger.info("operation_node_configure kwargs: {0}".format(kwargs))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400180
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500181 app_config = get_app_config()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400182 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500183 ctx.logger.info("property app_config: {0}".format(json.dumps(app_config)))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400184
Alex Shatov90f21052018-01-10 11:39:32 -0500185@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task')
186@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500187def node_configure(**kwargs):
188 """decorate with @Policies.gather_policies_to_node on policy consumer node to
189 bring all policies to runtime_properties["policies"]
190 """
191 operation_node_configure(**kwargs)
192
193@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500194@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500195def node_configure_wrong_order_path(**kwargs):
196 """wrong data in param policy_apply_order_path"""
197 operation_node_configure(**kwargs)
198
199@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='exe_task')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500200@Policies.gather_policies_to_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500201def node_configure_empty_order_path(**kwargs):
202 """wrong data in param policy_apply_order_path"""
203 operation_node_configure(**kwargs)
204
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400205@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500206@Policies.update_policies_on_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500207def policy_update(updated_policies, removed_policies=None, **kwargs):
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400208 """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"]
209
210 :updated_policies: contains the list of changed policy-configs when configs_only=True (default).
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400211 """
Alex Shatov90f21052018-01-10 11:39:32 -0500212 # This is how to merge the policies into default app_config object
213 # (runtime_properties[APPLICATION_CONFIG] = application_config)
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500214 app_config = get_app_config()
215 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400216
217 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
218
Alex Shatov90f21052018-01-10 11:39:32 -0500219@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500220@Policies.update_policies_on_node()
Alex Shatov90f21052018-01-10 11:39:32 -0500221def policy_update_not_only_config(updated_policies, removed_policies=None, **kwargs):
222 """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"]
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400223
Alex Shatov90f21052018-01-10 11:39:32 -0500224 :updated_policies: contains the list of changed policy-configs when configs_only=True (default).
Alex Shatov90f21052018-01-10 11:39:32 -0500225 """
226 # This is how to merge the policies into default app_config object
227 # (runtime_properties[APPLICATION_CONFIG] = application_config)
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500228 app_config = get_app_config()
229 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov90f21052018-01-10 11:39:32 -0500230
231 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
232
233@CtxLogger.log_ctx(pre_log=True, after_log=True, exe_task='execute_operation')
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500234@Policies.update_policies_on_node()
235def policy_update_many_calcs(updated_policies, removed_policies=None, policies=None, **kwargs):
Alex Shatov90f21052018-01-10 11:39:32 -0500236 """decorate with @Policies.update_policies_on_node() to update runtime_properties["policies"]
237
238 :updated_policies: contains the list of changed policy-configs when configs_only=True (default).
Alex Shatov90f21052018-01-10 11:39:32 -0500239 """
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500240 app_config = get_app_config()
241 ctx.logger.info("app_config {0}".format(json.dumps(app_config)))
Alex Shatov90f21052018-01-10 11:39:32 -0500242
243 ctx.instance.runtime_properties[APPLICATION_CONFIG] = app_config
244
Alex Shatov90f21052018-01-10 11:39:32 -0500245class CurrentCtx(object):
246 """cloudify context"""
247 _node_ms = None
248
249 @staticmethod
250 def set_current_ctx(include_bad=True, include_good=True):
251 """set up the nodes for cloudify"""
252 Policies._init()
253
254 def add_target_to_relationships(relationships, node):
255 """adds the node as the target of relationships"""
256 relationships.append({TARGET_NODE_ID: node.node_id, TARGET_NODE_NAME: node.node_name})
257
258 relationships = []
259 if include_good:
260 node_policy = MonkeyedNode(
261 'dcae_policy_node_id',
262 'dcae_policy_node_name',
263 dcae_policy.DCAE_POLICY_TYPE,
264 {POLICY_ID: MONKEYED_POLICY_ID},
265 None,
266 {POLICY_BODY: MonkeyedPolicyBody.create_policy_body(
267 MONKEYED_POLICY_ID, priority="1")}
268 )
269 add_target_to_relationships(relationships, node_policy)
270
271 if include_bad:
272 bad_policy_2 = MonkeyedNode(
273 'bad_policy_2_node_id',
274 'bad_policy_2_node_name',
275 dcae_policy.DCAE_POLICY_TYPE,
276 {POLICY_ID: MONKEYED_POLICY_ID_2},
277 None,
278 None
279 )
280 add_target_to_relationships(relationships, bad_policy_2)
281
282 if include_good:
283 node_policy_2 = MonkeyedNode(
284 'dcae_policy_node_id_2',
285 'dcae_policy_node_name_2',
286 dcae_policy.DCAE_POLICY_TYPE,
287 {POLICY_ID: MONKEYED_POLICY_ID_2},
288 None,
289 {POLICY_BODY: MonkeyedPolicyBody.create_policy_body(
290 MONKEYED_POLICY_ID_2, 4, priority="2")}
291 )
292 add_target_to_relationships(relationships, node_policy_2)
293
294 if include_bad:
295 bad_policy_3 = MonkeyedNode(
296 'bad_policy_3_node_id',
297 'bad_policy_3_node_name',
298 dcae_policy.DCAE_POLICY_TYPE,
299 {POLICY_ID: MONKEYED_POLICY_ID_2},
300 None,
301 None
302 )
303 add_target_to_relationships(relationships, bad_policy_3)
304
305 bad_policy_4 = MonkeyedNode(
306 'bad_policy_4_node_id',
307 'bad_policy_4_node_name',
308 dcae_policy.DCAE_POLICY_TYPE,
309 None,
310 None,
311 None
312 )
313 add_target_to_relationships(relationships, bad_policy_4)
314
315 weird_policy_5 = MonkeyedNode(
316 'weird_policy_5_node_id',
317 'weird_policy_5_node_name',
318 dcae_policy.DCAE_POLICY_TYPE,
319 None,
320 None,
321 {POLICY_BODY: MonkeyedPolicyBody.create_policy_body(
322 MONKEYED_POLICY_ID, 3, priority="2")}
323 )
324 add_target_to_relationships(relationships, weird_policy_5)
325
326 if include_good:
327 node_policies = MonkeyedNode(
328 'dcae_policies_node_id',
329 'dcae_policies_node_name',
330 dcae_policy.DCAE_POLICIES_TYPE,
Alex Shatovb73f81e2018-03-23 14:34:34 -0400331 {dcae_policy.POLICY_FILTER: {
332 POLICY_NAME: MONKEYED_POLICY_ID_M + ".*",
333 dcae_policy.CONFIG_ATTRIBUTES: json.dumps({
334 CONFIG_NAME: "alex_config_name"
335 })
336 }},
Alex Shatov90f21052018-01-10 11:39:32 -0500337 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"""
Alex Shatovb73f81e2018-03-23 14:34:34 -0400474 node_configure()
Alex Shatov90f21052018-01-10 11:39:32 -0500475
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)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400484def test_policies_to_node():
Alex Shatov90f21052018-01-10 11:39:32 -0500485 """test gather_policies_to_node"""
486 node_configure()
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400487
Alex Shatov90f21052018-01-10 11:39:32 -0500488 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 assert MONKEYED_POLICY_ID in policies
496 expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1")
497 policy = policies[MONKEYED_POLICY_ID]
498 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1)))
499 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy)))
500 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1)
501 assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy)
502
503 assert MONKEYED_POLICY_ID_B in policies
504 expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5")
505 policy = policies[MONKEYED_POLICY_ID_B]
506 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_b)))
507 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy)))
508 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b)
509 assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy)
510
511 assert MONKEYED_POLICY_ID_2 in policies
512 expected_2 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 4, priority="2")
513 policy = policies[MONKEYED_POLICY_ID_2]
514 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(expected_2)))
515 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
516 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_2)
517 assert MonkeyedPolicyBody.is_the_same_dict(expected_2, policy)
518
519 assert MONKEYED_POLICY_ID_M in policies
520 expected_m = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M, 2, False)
521 policy = policies[MONKEYED_POLICY_ID_M]
522 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(expected_m)))
523 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M, json.dumps(policy)))
524 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_m)
525 assert MonkeyedPolicyBody.is_the_same_dict(expected_m, policy)
526
527@cfy_ctx(include_bad=True)
528def test_update_policies():
529 """test policy_update"""
530 node_configure()
531
532 runtime_properties = ctx.instance.runtime_properties
533 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
534
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500535 assert dcae_policy.POLICIES in runtime_properties
536 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500537 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
538
Alex Shatov90f21052018-01-10 11:39:32 -0500539 updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
540 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
541 False, priority="1")
542 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
543
544 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
545 assert MONKEYED_POLICY_ID_M_2 not in policies
546
547 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
548
549 policy_update(updated_policies=[updated_policy],
550 added_policies={
551 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500552 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500553 },
554 removed_policies=[MONKEYED_POLICY_ID_M])
555
556 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
557 assert MONKEYED_POLICY_ID_M not in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500558
559 assert MONKEYED_POLICY_ID_M_2 in policies
560 policy = policies[MONKEYED_POLICY_ID_M_2]
561 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
562 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
563 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
564
565 assert MONKEYED_POLICY_ID_2 in policies
566 policy = policies[MONKEYED_POLICY_ID_2]
567 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
568 assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy)
569 assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy)
570
571 assert MONKEYED_POLICY_ID in policies
572 policy = policies[MONKEYED_POLICY_ID]
573 expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1")
574 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1)))
575 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy)))
576 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1)
577 assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy)
578
579 assert MONKEYED_POLICY_ID_B in policies
580 policy = policies[MONKEYED_POLICY_ID_B]
581 expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5")
582 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1)))
583 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy)))
584 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b)
585 assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy)
586
Alex Shatov90f21052018-01-10 11:39:32 -0500587@cfy_ctx(include_bad=True)
588def test_update_not_only_config():
589 """test policy_update"""
590 node_configure()
591
592 runtime_properties = ctx.instance.runtime_properties
593 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
594
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500595 assert dcae_policy.POLICIES in runtime_properties
596 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500597 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
598
Alex Shatov90f21052018-01-10 11:39:32 -0500599 updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
600 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
601 False, priority="1")
602 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
603
604 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
605 assert MONKEYED_POLICY_ID_M_2 not in policies
606
607 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
608
609 policy_update_not_only_config(updated_policies=[updated_policy],
610 added_policies={
611 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500612 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500613 },
614 removed_policies=[MONKEYED_POLICY_ID_M])
615
616 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
617 assert MONKEYED_POLICY_ID_M not in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500618
619 assert MONKEYED_POLICY_ID_M_2 in policies
620 policy = policies[MONKEYED_POLICY_ID_M_2]
621 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
622 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
623 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
624
625 assert MONKEYED_POLICY_ID_2 in policies
626 policy = policies[MONKEYED_POLICY_ID_2]
627 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
628 assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy)
629 assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy)
630
631 assert MONKEYED_POLICY_ID in policies
632 policy = policies[MONKEYED_POLICY_ID]
633 expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1")
634 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1)))
635 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy)))
636 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1)
637 assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy)
638
639 assert MONKEYED_POLICY_ID_B in policies
640 policy = policies[MONKEYED_POLICY_ID_B]
641 expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5")
642 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1)))
643 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy)))
644 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b)
645 assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy)
646
Alex Shatov90f21052018-01-10 11:39:32 -0500647@cfy_ctx(include_bad=True)
648def test_update_policies_not():
649 """test policy_update - ignore all policies with junk params"""
650 node_configure()
651
652 runtime_properties = ctx.instance.runtime_properties
653 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
654
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500655 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov90f21052018-01-10 11:39:32 -0500656 assert APPLICATION_CONFIG in runtime_properties
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500657 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500658 app_config = runtime_properties[APPLICATION_CONFIG]
659 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
660 ctx.logger.info("app_config: {0}".format(json.dumps(app_config)))
661
Alex Shatov90f21052018-01-10 11:39:32 -0500662 expected_policies = copy.deepcopy(policies)
663 expected_app_config = copy.deepcopy(app_config)
Alex Shatov90f21052018-01-10 11:39:32 -0500664
665 existing_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID, priority="1")
666 damaged_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_2)
667 del damaged_policy[POLICY_BODY][POLICY_CONFIG]
668 updated_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_3, 3)
669 added_policy = MonkeyedPolicyBody.create_policy_bare(MONKEYED_POLICY_ID_M_2, 4)
670 junk_policy_filter_id = "<<<junk_removed_policy_id>>>"
671 unexpected_removed_policy_id = "<<<junk_removed_policy_id>>>"
672 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
673
674 assert MONKEYED_POLICY_ID in policies
675 assert MONKEYED_POLICY_ID_2 in policies
676 assert MONKEYED_POLICY_ID_M_2 not in policies
677 assert MONKEYED_POLICY_ID_M_3 not in policies
678 assert unexpected_removed_policy_id not in policies
679 assert junk_policy_filter_id not in runtime_properties.get(dcae_policy.POLICY_FILTERS, {})
680
681 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
682
683 policy_update(updated_policies=[existing_policy, damaged_policy, updated_policy],
684 added_policies={
685 junk_policy_filter_id: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500686 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}},
Alex Shatov90f21052018-01-10 11:39:32 -0500687 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500688 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_2: damaged_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500689 },
690 removed_policies=[unexpected_removed_policy_id])
691
692 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
693
694 ctx.logger.info("policies not changed: {0}".format(json.dumps(policies)))
695 assert MonkeyedPolicyBody.is_the_same_dict(policies, expected_policies)
696 assert MonkeyedPolicyBody.is_the_same_dict(expected_policies, policies)
697
698 ctx.logger.info("app_config not changed: {0}".format(json.dumps(app_config)))
699 assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_app_config)
700 assert MonkeyedPolicyBody.is_the_same_dict(expected_app_config, app_config)
701
Alex Shatov90f21052018-01-10 11:39:32 -0500702@cfy_ctx(include_bad=True)
703def test_update_many_calcs():
704 """test policy_update"""
705 node_configure()
706
707 runtime_properties = ctx.instance.runtime_properties
708 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
709
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500710 assert dcae_policy.POLICIES in runtime_properties
711 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500712 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
713
Alex Shatov90f21052018-01-10 11:39:32 -0500714 updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
715 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
716 False, priority="1")
717 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
718
719 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
720 assert MONKEYED_POLICY_ID_M_2 not in policies
721
722 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
723
724 policy_update_many_calcs(updated_policies=[updated_policy],
725 added_policies={
726 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500727 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500728 },
729 removed_policies=[MONKEYED_POLICY_ID_M])
730
731 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
732 assert MONKEYED_POLICY_ID_M not in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500733
734 assert MONKEYED_POLICY_ID_M_2 in policies
735 policy = policies[MONKEYED_POLICY_ID_M_2]
736 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
737 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
738 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
739
740 assert MONKEYED_POLICY_ID_2 in policies
741 policy = policies[MONKEYED_POLICY_ID_2]
742 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
743 assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy)
744 assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy)
745
746 assert MONKEYED_POLICY_ID in policies
747 policy = policies[MONKEYED_POLICY_ID]
748 expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1")
749 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1)))
750 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy)))
751 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1)
752 assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy)
753
754 assert MONKEYED_POLICY_ID_B in policies
755 policy = policies[MONKEYED_POLICY_ID_B]
756 expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5")
757 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1)))
758 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy)))
759 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b)
760 assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy)
761
Alex Shatov90f21052018-01-10 11:39:32 -0500762@cfy_ctx(include_bad=True)
763def test_remove_all_policies():
764 """test policy_update - remove all policies"""
765 node_configure()
766
767 runtime_properties = ctx.instance.runtime_properties
768 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
769
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500770 assert dcae_policy.POLICIES in runtime_properties
771 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500772 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
773
774 remove_policy_ids = policies.keys()
775
776 policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids)
777
778 ctx.logger.info("removed: {0}".format(remove_policy_ids))
779 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500780 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500781 assert Policies.get_policy_bodies() == []
Alex Shatov90f21052018-01-10 11:39:32 -0500782
783 assert APPLICATION_CONFIG in runtime_properties
784 assert APPLICATION_CONFIG in ctx.node.properties
785 app_config = runtime_properties[APPLICATION_CONFIG]
786 expected_config = dict(ctx.node.properties[APPLICATION_CONFIG])
787 ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config)))
788 assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config)
789 assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config)
790
791@cfy_ctx(include_bad=True)
792def test_remove_all_policies_twice():
793 """test policy_update - remove all policies twice"""
794 node_configure()
795
796 runtime_properties = ctx.instance.runtime_properties
797 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
798
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500799 assert dcae_policy.POLICIES in runtime_properties
800 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500801 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
802
803 remove_policy_ids = policies.keys()
804
805 policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids)
806 policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids)
807
808 ctx.logger.info("removed: {0}".format(remove_policy_ids))
809 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500810 assert dcae_policy.POLICIES in runtime_properties
Alex Shatov2b3d24b2018-03-02 13:46:11 -0500811 assert Policies.get_policy_bodies() == []
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400812
Alex Shatov90f21052018-01-10 11:39:32 -0500813 assert APPLICATION_CONFIG in runtime_properties
814 assert APPLICATION_CONFIG in ctx.node.properties
815 app_config = runtime_properties[APPLICATION_CONFIG]
816 expected_config = dict(ctx.node.properties[APPLICATION_CONFIG])
817 ctx.logger.info("expected = default application_config: {0}".format(json.dumps(app_config)))
818 assert MonkeyedPolicyBody.is_the_same_dict(app_config, expected_config)
819 assert MonkeyedPolicyBody.is_the_same_dict(expected_config, app_config)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -0400820
Alex Shatov90f21052018-01-10 11:39:32 -0500821@cfy_ctx(include_bad=True)
822def test_remove_then_update():
823 """test policy_update"""
824 node_configure()
825
826 runtime_properties = ctx.instance.runtime_properties
827 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
828
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500829 assert dcae_policy.POLICIES in runtime_properties
830 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500831 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
832
Alex Shatov90f21052018-01-10 11:39:32 -0500833 remove_policy_ids = policies.keys()
834 policy_update(updated_policies=None, added_policies=None, removed_policies=remove_policy_ids)
835
836 updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
837 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
838 False, priority="1")
839 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
840
841 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
842 assert MONKEYED_POLICY_ID_M_2 not in policies
843
844 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
845
846 policy_update(updated_policies=[updated_policy],
847 added_policies={
848 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500849 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500850 },
851 removed_policies=[MONKEYED_POLICY_ID_M])
852
853 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
854 assert MONKEYED_POLICY_ID_M not in policies
855
856 assert MONKEYED_POLICY_ID_M_2 in policies
857 policy = policies[MONKEYED_POLICY_ID_M_2]
858 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
859 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
860 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
861
862 assert MONKEYED_POLICY_ID_2 in policies
863 policy = policies[MONKEYED_POLICY_ID_2]
864 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
865 assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy)
866 assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy)
867
868 assert MONKEYED_POLICY_ID in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500869 assert MONKEYED_POLICY_ID_B in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500870
871@cfy_ctx(include_bad=True)
872def test_remove_update_many_calcs():
873 """test policy_update"""
874 node_configure()
875
876 runtime_properties = ctx.instance.runtime_properties
877 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
878
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500879 assert dcae_policy.POLICIES in runtime_properties
880 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500881 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
882
Alex Shatov90f21052018-01-10 11:39:32 -0500883 remove_policy_ids = policies.keys()
884 policy_update_many_calcs(updated_policies=None,
885 added_policies=None,
886 removed_policies=remove_policy_ids)
887
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500888 assert dcae_policy.POLICIES in runtime_properties
889 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500890 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
891
Alex Shatov90f21052018-01-10 11:39:32 -0500892 updated_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
893 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
894 False, priority="1")
895 ctx.logger.info("policy_update: [{0}]".format(json.dumps(updated_policy)))
896
897 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
898 assert MONKEYED_POLICY_ID_M_2 not in policies
899
900 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
901
902 policy_update_many_calcs(updated_policies=[updated_policy],
903 added_policies={
904 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500905 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500906 },
907 removed_policies=[MONKEYED_POLICY_ID_M])
908
909 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
910 assert MONKEYED_POLICY_ID_M not in policies
911
912 assert MONKEYED_POLICY_ID_M_2 in policies
913 policy = policies[MONKEYED_POLICY_ID_M_2]
914 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
915 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
916 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
917
918 assert MONKEYED_POLICY_ID_2 in policies
919 policy = policies[MONKEYED_POLICY_ID_2]
920 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
921 assert MonkeyedPolicyBody.is_the_same_dict(policy, updated_policy)
922 assert MonkeyedPolicyBody.is_the_same_dict(updated_policy, policy)
923
924 assert MONKEYED_POLICY_ID in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500925 assert MONKEYED_POLICY_ID_B in policies
Alex Shatov90f21052018-01-10 11:39:32 -0500926
927@cfy_ctx(include_bad=True)
928def test_bad_update_many_calcs():
929 """test policy_update"""
930 node_configure()
931
932 runtime_properties = ctx.instance.runtime_properties
933 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
934
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500935 assert dcae_policy.POLICIES in runtime_properties
936 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -0500937 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
938
Alex Shatov90f21052018-01-10 11:39:32 -0500939 damaged_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_2, 2, priority="aa20")
940 damaged_policy[POLICY_BODY][POLICY_CONFIG] = ["damaged config"]
941
942 added_policy = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_M_2, 2,
943 False, priority="1")
944 added_policy[POLICY_BODY][POLICY_CONFIG] = {"unexpected":"foo", "none": None}
945
946 ctx.logger.info("policy_update: [{0}]".format(json.dumps(damaged_policy)))
947
948 ctx.logger.info("policy[{0}]: not yet in policies".format(MONKEYED_POLICY_ID_M_2))
949 assert MONKEYED_POLICY_ID_M_2 not in policies
950
951 policy_filter_ids = runtime_properties.get(dcae_policy.POLICY_FILTERS, {}).keys() or ["--"]
952
953 policy_update_many_calcs(updated_policies=[damaged_policy],
954 added_policies={
955 policy_filter_ids[0]: {
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500956 dcae_policy.POLICIES: {MONKEYED_POLICY_ID_M_2: added_policy}}
Alex Shatov90f21052018-01-10 11:39:32 -0500957 },
958 removed_policies=[MONKEYED_POLICY_ID_M])
959
960 ctx.logger.info("policy[{0}]: removed".format(MONKEYED_POLICY_ID_M))
961 assert MONKEYED_POLICY_ID_M not in policies
962
963 assert MONKEYED_POLICY_ID_M_2 in policies
964 policy = policies[MONKEYED_POLICY_ID_M_2]
965 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_M_2, json.dumps(policy)))
966 assert MonkeyedPolicyBody.is_the_same_dict(policy, added_policy)
967 assert MonkeyedPolicyBody.is_the_same_dict(added_policy, policy)
968
969 assert MONKEYED_POLICY_ID_2 in policies
970 policy = policies[MONKEYED_POLICY_ID_2]
971 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_2, json.dumps(policy)))
972 assert MonkeyedPolicyBody.is_the_same_dict(policy, damaged_policy)
973 assert MonkeyedPolicyBody.is_the_same_dict(damaged_policy, policy)
974
975 assert MONKEYED_POLICY_ID in policies
976 policy = policies[MONKEYED_POLICY_ID]
977 expected_1 = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID, priority="1")
978 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(expected_1)))
979 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID, json.dumps(policy)))
980 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_1)
981 assert MonkeyedPolicyBody.is_the_same_dict(expected_1, policy)
982
983 assert MONKEYED_POLICY_ID_B in policies
984 policy = policies[MONKEYED_POLICY_ID_B]
985 expected_b = MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID_B, 4, priority="1.5")
986 ctx.logger.info("expected[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(expected_1)))
987 ctx.logger.info("policy[{0}]: {1}".format(MONKEYED_POLICY_ID_B, json.dumps(policy)))
988 assert MonkeyedPolicyBody.is_the_same_dict(policy, expected_b)
989 assert MonkeyedPolicyBody.is_the_same_dict(expected_b, policy)
990
Alex Shatov90f21052018-01-10 11:39:32 -0500991@cfy_ctx(include_bad=True, include_good=False)
992def test_bad_policies():
993 """test bad policy nodes"""
994 node_configure()
995
996 runtime_properties = ctx.instance.runtime_properties
997 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
998
Alex Shatovcfdd67d2018-02-23 14:56:36 -0500999 assert dcae_policy.POLICIES in runtime_properties
1000 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -05001001 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
1002
Alex Shatov90f21052018-01-10 11:39:32 -05001003@cfy_ctx(include_bad=True, include_good=False)
1004def test_wrong_ctx_node_configure():
1005 """test wrong ctx"""
1006 current_ctx.set(ctx.instance.relationships[0])
1007 ctx_type = ctx.type
1008
1009 with pytest.raises(NonRecoverableError) as excinfo:
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001010 node_configure()
1011
Alex Shatov90f21052018-01-10 11:39:32 -05001012 CurrentCtx.reset()
1013 ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value)))
1014 assert ctx_type == 'cloudify.relationships.depends_on'
1015 assert str(excinfo.value) == "can only invoke gather_policies_to_node on node"
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001016
Alex Shatov90f21052018-01-10 11:39:32 -05001017@cfy_ctx(include_bad=True, include_good=False)
1018def test_wrong_ctx_policy_update():
1019 """test wrong ctx"""
Alex Shatov2b3d24b2018-03-02 13:46:11 -05001020 no_policies = Policies.get_policy_bodies()
Alex Shatov90f21052018-01-10 11:39:32 -05001021 current_ctx.set(ctx.instance.relationships[0])
1022 ctx_type = ctx.type
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001023
Alex Shatov90f21052018-01-10 11:39:32 -05001024 with pytest.raises(NonRecoverableError) as excinfo:
1025 policy_update(updated_policies=None, added_policies=None, removed_policies=None)
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001026
Alex Shatov90f21052018-01-10 11:39:32 -05001027 CurrentCtx.reset()
1028 ctx.logger.info("{0} not a node boom: {1}".format(ctx_type, str(excinfo.value)))
1029 assert ctx_type == 'cloudify.relationships.depends_on'
Alex Shatov2b3d24b2018-03-02 13:46:11 -05001030 assert no_policies == []
Alex Shatov90f21052018-01-10 11:39:32 -05001031 assert str(excinfo.value) == "can only invoke update_policies_on_node on node"
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001032
Alex Shatov90f21052018-01-10 11:39:32 -05001033def test_defenses_on_decorators():
1034 """test defenses of code mainly for code coverage"""
1035 should_be_none = Policies.gather_policies_to_node()(None)
1036 assert should_be_none is None
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001037
Alex Shatov90f21052018-01-10 11:39:32 -05001038 should_be_none = Policies.update_policies_on_node()(None)
1039 assert should_be_none is None
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001040
Alex Shatov90f21052018-01-10 11:39:32 -05001041def monkeyed_set_policies_boom(policies):
1042 """monkeypatch for exception"""
1043 raise Exception("Policies._set_policies boom: {0}".format(json.dumps(policies or {})))
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001044
Alex Shatov90f21052018-01-10 11:39:32 -05001045@pytest.fixture()
1046def fix_boom_gather(monkeypatch):
1047 """monkeyed boom"""
1048 monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._set_policies',
1049 monkeyed_set_policies_boom)
1050 return fix_boom_gather # provide the fixture value
Alex Shatov8cdeeeb2017-09-26 17:03:11 -04001051
Alex Shatov90f21052018-01-10 11:39:32 -05001052@cfy_ctx(include_bad=True, include_good=False)
1053@pytest.mark.usefixtures("fix_boom_gather")
1054def test_exception_on_gather():
1055 """test exception on gather"""
1056 with pytest.raises(NonRecoverableError) as excinfo:
1057 node_configure()
1058
1059 ctx.logger.info("monkeyed_set_policies_boom: {0}".format(str(excinfo.value)))
1060 assert str(excinfo.value).startswith("Failed to set the policies ")
1061
1062def monkeyed_update_policies_boom(policies):
1063 """monkeypatch for exception"""
1064 raise Exception("Policies._update_policies boom")
1065
1066@pytest.fixture()
1067def fix_boom_update_policies(monkeypatch):
1068 """monkeyed boom"""
1069 monkeypatch.setattr('onap_dcae_dcaepolicy_lib.dcae_policy.Policies._update_policies',
1070 monkeyed_update_policies_boom)
1071 return fix_boom_update_policies # provide the fixture value
1072
1073@cfy_ctx(include_bad=True, include_good=False)
1074@pytest.mark.usefixtures("fix_boom_update_policies")
1075def test_exception_on_update():
1076 """test exception on update_policies"""
1077 with pytest.raises(NonRecoverableError) as excinfo:
1078 policy_update(updated_policies=None, added_policies=None, removed_policies=None)
1079
1080 ctx.logger.info("monkeyed_update_policies_boom: {0}".format(str(excinfo.value)))
1081 assert str(excinfo.value).startswith("Failed to update the policies ")
1082
1083@cfy_ctx(include_bad=True, include_good=False)
1084def test_defenses_on_policy_update():
1085 """test defenses on policy_update"""
1086 policy_update(updated_policies=None, added_policies=None, removed_policies=None)
1087 ctx.logger.info("policy_update() ok")
1088
1089@cfy_ctx(include_bad=True, include_good=False)
1090def test_defenses_on_set_policies():
1091 """test defenses of code mainly for code coverage"""
1092 node_configure()
1093
1094 runtime_properties = ctx.instance.runtime_properties
1095 ctx.logger.info("runtime_properties: {0}".format(json.dumps(runtime_properties)))
1096
Alex Shatovcfdd67d2018-02-23 14:56:36 -05001097 assert dcae_policy.POLICIES in runtime_properties
1098 policies = runtime_properties[dcae_policy.POLICIES]
Alex Shatov90f21052018-01-10 11:39:32 -05001099 ctx.logger.info("policies: {0}".format(json.dumps(policies)))
1100
Alex Shatov2b3d24b2018-03-02 13:46:11 -05001101 Policies._set_policies({})
1102
1103 assert dcae_policy.POLICIES not in runtime_properties
Alex Shatov90f21052018-01-10 11:39:32 -05001104
1105 Policies._set_policies({})
1106
Alex Shatovcfdd67d2018-02-23 14:56:36 -05001107 assert dcae_policy.POLICIES not in runtime_properties