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