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
|
#
# 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.
import json
import os
import six
import uuid
import yaml
import mock
from heat.common import exception
from heat.common import template_format
from heat.common import urlfetch
from heat.engine import attributes
from heat.engine import environment
from heat.engine import parser
from heat.engine import properties
from heat.engine import resource
from heat.engine import resources
from heat.engine.resources import template_resource
from heat.engine import rsrc_defn
from heat.engine import support
from heat.tests.common import HeatTestCase
from heat.tests import generic_resource as generic_rsrc
from heat.tests import utils
empty_template = {"HeatTemplateFormatVersion": "2012-12-12"}
class MyCloudResource(generic_rsrc.GenericResource):
pass
class ProviderTemplateTest(HeatTestCase):
def setUp(self):
super(ProviderTemplateTest, self).setUp()
resource._register_class('OS::ResourceType',
generic_rsrc.GenericResource)
resource._register_class('myCloud::ResourceType',
MyCloudResource)
def test_get_os_empty_registry(self):
# assertion: with an empty environment we get the correct
# default class.
env_str = {'resource_registry': {}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(generic_rsrc.GenericResource, cls)
def test_get_mine_global_map(self):
# assertion: with a global rule we get the "mycloud" class.
env_str = {'resource_registry': {"OS::*": "myCloud::*"}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(MyCloudResource, cls)
def test_get_mine_type_map(self):
# assertion: with a global rule we get the "mycloud" class.
env_str = {'resource_registry': {
"OS::ResourceType": "myCloud::ResourceType"}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(MyCloudResource, cls)
def test_get_mine_resource_map(self):
# assertion: with a global rule we get the "mycloud" class.
env_str = {'resource_registry': {'resources': {'fred': {
"OS::ResourceType": "myCloud::ResourceType"}}}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(MyCloudResource, cls)
def test_get_os_no_match(self):
# assertion: make sure 'fred' doesn't match 'jerry'.
env_str = {'resource_registry': {'resources': {'jerry': {
"OS::ResourceType": "myCloud::ResourceType"}}}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(generic_rsrc.GenericResource, cls)
def test_to_parameters(self):
"""Tests property conversion to parameter values."""
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Foo': {'Type': 'String'},
'AList': {'Type': 'CommaDelimitedList'},
'ListEmpty': {'Type': 'CommaDelimitedList'},
'ANum': {'Type': 'Number'},
'AMap': {'Type': 'Json'},
},
'Outputs': {
'Foo': {'Value': 'bar'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
attributes_schema = {"Foo": attributes.Schema("A test attribute")}
properties_schema = {
"Foo": {"Type": "String"},
"AList": {"Type": "List"},
"ListEmpty": {"Type": "List"},
"ANum": {"Type": "Number"},
"AMap": {"Type": "Map"}
}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
map_prop_val = {
"key1": "val1",
"key2": ["lval1", "lval2", "lval3"],
"key3": {
"key4": 4,
"key5": False
}
}
prop_vals = {
"Foo": "Bar",
"AList": ["one", "two", "three"],
"ListEmpty": [],
"ANum": 5,
"AMap": map_prop_val,
}
definition = rsrc_defn.ResourceDefinition('test_t_res',
'DummyResource',
prop_vals)
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
temp_res.validate()
converted_params = temp_res.child_params()
self.assertTrue(converted_params)
for key in DummyResource.properties_schema:
self.assertIn(key, converted_params)
# verify String conversion
self.assertEqual("Bar", converted_params.get("Foo"))
# verify List conversion
self.assertEqual("one,two,three", converted_params.get("AList"))
# verify Number conversion
self.assertEqual(5, converted_params.get("ANum"))
# verify Map conversion
self.assertEqual(map_prop_val, converted_params.get("AMap"))
with mock.patch.object(properties.Properties, '__getitem__') as m_get:
m_get.side_effect = ValueError('boom')
# If the property doesn't exist on INIT, return default value
temp_res.action = temp_res.INIT
converted_params = temp_res.child_params()
for key in DummyResource.properties_schema:
self.assertIn(key, converted_params)
self.assertEqual({}, converted_params['AMap'])
self.assertEqual(0, converted_params['ANum'])
# If the property doesn't exist past INIT, then error out
temp_res.action = temp_res.CREATE
self.assertRaises(ValueError, temp_res.child_params)
def test_attributes_extra(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Outputs': {
'Foo': {'Value': 'bar'},
'Blarg': {'Value': 'wibble'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {}
attributes_schema = {"Foo": attributes.Schema("A test attribute")}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource")
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertIsNone(temp_res.validate())
def test_attributes_missing(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Outputs': {
'Blarg': {'Value': 'wibble'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {}
attributes_schema = {"Foo": attributes.Schema("A test attribute")}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource")
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertRaises(exception.StackValidationFailed,
temp_res.validate)
def test_properties_normal(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Foo': {'Type': 'String'},
'Blarg': {'Type': 'String', 'Default': 'wibble'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {"Foo":
properties.Schema(properties.Schema.STRING,
required=True)}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource",
{"Foo": "bar"})
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertIsNone(temp_res.validate())
def test_properties_missing(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Blarg': {'Type': 'String', 'Default': 'wibble'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {"Foo":
properties.Schema(properties.Schema.STRING,
required=True)}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource")
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertRaises(exception.StackValidationFailed,
temp_res.validate)
def test_properties_extra_required(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Blarg': {'Type': 'String'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template, files=files),
env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource",
{"Blarg": "wibble"})
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertRaises(exception.StackValidationFailed,
temp_res.validate)
def test_properties_type_mismatch(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Foo': {'Type': 'String'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {"Foo":
properties.Schema(properties.Schema.MAP)}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(
{'HeatTemplateFormatVersion': '2012-12-12'},
files=files), env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource",
{"Foo": "bar"})
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
ex = self.assertRaises(exception.StackValidationFailed,
temp_res.validate)
self.assertEqual("Property Foo type mismatch between facade "
"DummyResource (Map) and provider (String)",
six.text_type(ex))
def test_properties_type_match(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Length': {'Type': 'Number'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {"Length":
properties.Schema(properties.Schema.INTEGER)}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(
{'HeatTemplateFormatVersion': '2012-12-12'},
files=files), env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource",
{"Length": 10})
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertIsNone(temp_res.validate())
def test_boolean_type_provider(self):
provider = {
'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {
'Foo': {'Type': 'Boolean'},
},
}
files = {'test_resource.template': json.dumps(provider)}
class DummyResource(object):
support_status = support.SupportStatus()
properties_schema = {"Foo":
properties.Schema(properties.Schema.BOOLEAN)}
attributes_schema = {}
env = environment.Environment()
resource._register_class('DummyResource', DummyResource)
env.load({'resource_registry':
{'DummyResource': 'test_resource.template'}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(
{'HeatTemplateFormatVersion': '2012-12-12'},
files=files), env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
"DummyResource",
{"Foo": "False"})
temp_res = template_resource.TemplateResource('test_t_res',
definition, stack)
self.assertIsNone(temp_res.validate())
def test_get_template_resource(self):
# assertion: if the name matches {.yaml|.template} we get the
# TemplateResource class.
env_str = {'resource_registry': {'resources': {'fred': {
"OS::ResourceType": "some_magic.yaml"}}}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertEqual(template_resource.TemplateResource, cls)
def test_get_template_resource_class(self):
test_templ_name = 'file:///etc/heatr/frodo.yaml'
minimal_temp = json.dumps({'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {},
'Resources': {}})
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('file',)).AndReturn(minimal_temp)
self.m.ReplayAll()
env_str = {'resource_registry': {'resources': {'fred': {
"OS::ResourceType": test_templ_name}}}}
env = environment.Environment(env_str)
cls = env.get_class('OS::ResourceType', 'fred')
self.assertNotEqual(template_resource.TemplateResource, cls)
self.assertTrue(issubclass(cls, template_resource.TemplateResource))
self.assertTrue(hasattr(cls, "properties_schema"))
self.assertTrue(hasattr(cls, "attributes_schema"))
self.m.VerifyAll()
def test_template_as_resource(self):
"""
Test that the resulting resource has the right prop and attrib schema.
Note that this test requires the Wordpress_Single_Instance.yaml
template in the templates directory since we want to test using a
non-trivial template.
"""
test_templ_name = "WordPress_Single_Instance.yaml"
path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'templates', test_templ_name)
# check if its in the directory list vs. exists to work around
# case-insensitive file systems
self.assertIn(test_templ_name, os.listdir(os.path.dirname(path)))
with open(path) as test_templ_file:
test_templ = test_templ_file.read()
self.assertTrue(test_templ, "Empty test template")
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('file',))\
.AndRaise(urlfetch.URLFetchError(_('Failed to retrieve template')))
urlfetch.get(test_templ_name,
allowed_schemes=('http', 'https')).AndReturn(test_templ)
parsed_test_templ = template_format.parse(test_templ)
self.m.ReplayAll()
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template),
stack_id=str(uuid.uuid4()))
properties = {
"KeyName": "mykeyname",
"DBName": "wordpress1",
"DBUsername": "wpdbuser",
"DBPassword": "wpdbpass",
"DBRootPassword": "wpdbrootpass",
"LinuxDistribution": "U10"
}
definition = rsrc_defn.ResourceDefinition("test_templ_resource",
test_templ_name,
properties)
templ_resource = resource.Resource("test_templ_resource", definition,
stack)
self.m.VerifyAll()
self.assertIsInstance(templ_resource,
template_resource.TemplateResource)
for prop in parsed_test_templ.get("Parameters", {}):
self.assertIn(prop, templ_resource.properties)
for attrib in parsed_test_templ.get("Outputs", {}):
self.assertIn(attrib, templ_resource.attributes)
for k, v in properties.items():
self.assertEqual(v, templ_resource.properties[k])
self.assertEqual(
{'WordPress_Single_Instance.yaml':
'WordPress_Single_Instance.yaml', 'resources': {}},
stack.env.user_env_as_dict()["resource_registry"])
self.assertNotIn('WordPress_Single_Instance.yaml',
resources.global_env().registry._registry)
def test_persisted_unregistered_provider_templates(self):
"""
Test that templates persisted in the database prior to
https://review.openstack.org/#/c/79953/1 are registered correctly.
"""
env = {'resource_registry': {'http://example.com/test.template': None,
'resources': {}}}
#A KeyError will be thrown prior to this fix.
environment.Environment(env=env)
def test_system_template_retrieve_by_file(self):
# make sure that a TemplateResource defined in the global environment
# can be created and the template retrieved using the "file:"
# scheme.
g_env = resources.global_env()
test_templ_name = 'file:///etc/heatr/frodo.yaml'
g_env.load({'resource_registry':
{'Test::Frodo': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template),
stack_id=str(uuid.uuid4()))
minimal_temp = json.dumps({'HeatTemplateFormatVersion': '2012-12-12',
'Parameters': {},
'Resources': {}})
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('http', 'https',
'file')).AndReturn(minimal_temp)
self.m.ReplayAll()
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Frodo')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
self.assertIsNone(temp_res.validate())
self.m.VerifyAll()
def test_user_template_not_retrieved_by_file(self):
# make sure that a TemplateResource defined in the user environment
# can NOT be retrieved using the "file:" scheme, validation should fail
env = environment.Environment()
test_templ_name = 'file:///etc/heatr/flippy.yaml'
env.load({'resource_registry':
{'Test::Flippy': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template), env=env,
stack_id=str(uuid.uuid4()))
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Flippy')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
self.assertRaises(exception.StackValidationFailed, temp_res.validate)
def test_system_template_retrieve_fail(self):
# make sure that a TemplateResource defined in the global environment
# fails gracefully if the template file specified is inaccessible
# we should be able to create the TemplateResource object, but
# validation should fail, when the second attempt to access it is
# made in validate()
g_env = resources.global_env()
test_templ_name = 'file:///etc/heatr/frodo.yaml'
g_env.load({'resource_registry':
{'Test::Frodo': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template),
stack_id=str(uuid.uuid4()))
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('http', 'https',
'file'))\
.AndRaise(urlfetch.URLFetchError(_('Failed to retrieve template')))
self.m.ReplayAll()
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Frodo')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
self.assertRaises(exception.StackValidationFailed, temp_res.validate)
self.m.VerifyAll()
def test_user_template_retrieve_fail(self):
# make sure that a TemplateResource defined in the user environment
# fails gracefully if the template file specified is inaccessible
# we should be able to create the TemplateResource object, but
# validation should fail, when the second attempt to access it is
# made in validate()
env = environment.Environment()
test_templ_name = 'http://heatr/noexist.yaml'
env.load({'resource_registry':
{'Test::Flippy': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template), env=env,
stack_id=str(uuid.uuid4()))
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('http', 'https'))\
.AndRaise(urlfetch.URLFetchError(_('Failed to retrieve template')))
self.m.ReplayAll()
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Flippy')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
self.assertRaises(exception.StackValidationFailed, temp_res.validate)
self.m.VerifyAll()
def test_user_template_retrieve_fail_ext(self):
# make sure that a TemplateResource defined in the user environment
# fails gracefully if the template file is the wrong extension
# we should be able to create the TemplateResource object, but
# validation should fail, when the second attempt to access it is
# made in validate()
env = environment.Environment()
test_templ_name = 'http://heatr/letter_to_granny.docx'
env.load({'resource_registry':
{'Test::Flippy': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template), env=env,
stack_id=str(uuid.uuid4()))
self.m.ReplayAll()
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Flippy')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
self.assertRaises(exception.StackValidationFailed, temp_res.validate)
self.m.VerifyAll()
class NestedProvider(HeatTestCase):
"""Prove that we can use the registry in a nested provider."""
def setUp(self):
super(NestedProvider, self).setUp()
utils.setup_dummy_db()
def test_nested_env(self):
main_templ = '''
heat_template_version: 2013-05-23
resources:
secret2:
type: My::NestedSecret
outputs:
secret1:
value: { get_attr: [secret1, value] }
'''
nested_templ = '''
heat_template_version: 2013-05-23
resources:
secret2:
type: My::Secret
outputs:
value:
value: { get_attr: [secret2, value] }
'''
env_templ = '''
resource_registry:
"My::Secret": "OS::Heat::RandomString"
"My::NestedSecret": nested.yaml
'''
env = environment.Environment()
env.load(yaml.load(env_templ))
templ = parser.Template(template_format.parse(main_templ),
files={'nested.yaml': nested_templ})
stack = parser.Stack(utils.dummy_context(),
utils.random_name(),
templ, env=env)
stack.store()
stack.create()
self.assertEqual((stack.CREATE, stack.COMPLETE), stack.state)
def test_no_infinite_recursion(self):
"""Prove that we can override a python resource.
And use that resource within the template resource.
"""
main_templ = '''
heat_template_version: 2013-05-23
resources:
secret2:
type: OS::Heat::RandomString
outputs:
secret1:
value: { get_attr: [secret1, value] }
'''
nested_templ = '''
heat_template_version: 2013-05-23
resources:
secret2:
type: OS::Heat::RandomString
outputs:
value:
value: { get_attr: [secret2, value] }
'''
env_templ = '''
resource_registry:
"OS::Heat::RandomString": nested.yaml
'''
env = environment.Environment()
env.load(yaml.load(env_templ))
templ = parser.Template(template_format.parse(main_templ),
files={'nested.yaml': nested_templ})
stack = parser.Stack(utils.dummy_context(),
utils.random_name(),
templ, env=env)
stack.store()
stack.create()
self.assertEqual((stack.CREATE, stack.COMPLETE), stack.state)
class ProviderTemplateUpdateTest(HeatTestCase):
main_template = '''
HeatTemplateFormatVersion: '2012-12-12'
Resources:
the_nested:
Type: the.yaml
Properties:
one: my_name
Outputs:
identifier:
Value: {Ref: the_nested}
value:
Value: {'Fn::GetAtt': [the_nested, the_str]}
'''
main_template_2 = '''
HeatTemplateFormatVersion: '2012-12-12'
Resources:
the_nested:
Type: the.yaml
Properties:
one: updated_name
Outputs:
identifier:
Value: {Ref: the_nested}
value:
Value: {'Fn::GetAtt': [the_nested, the_str]}
'''
initial_tmpl = '''
HeatTemplateFormatVersion: '2012-12-12'
Parameters:
one:
Default: foo
Type: String
Resources:
NestedResource:
Type: OS::Heat::RandomString
Properties:
salt: {Ref: one}
Outputs:
the_str:
Value: {'Fn::GetAtt': [NestedResource, value]}
'''
prop_change_tmpl = '''
HeatTemplateFormatVersion: '2012-12-12'
Parameters:
one:
Default: yikes
Type: String
two:
Default: foo
Type: String
Resources:
NestedResource:
Type: OS::Heat::RandomString
Properties:
salt: {Ref: one}
Outputs:
the_str:
Value: {'Fn::GetAtt': [NestedResource, value]}
'''
attr_change_tmpl = '''
HeatTemplateFormatVersion: '2012-12-12'
Parameters:
one:
Default: foo
Type: String
Resources:
NestedResource:
Type: OS::Heat::RandomString
Properties:
salt: {Ref: one}
Outputs:
the_str:
Value: {'Fn::GetAtt': [NestedResource, value]}
something_else:
Value: just_a_string
'''
content_change_tmpl = '''
HeatTemplateFormatVersion: '2012-12-12'
Parameters:
one:
Default: foo
Type: String
Resources:
NestedResource:
Type: OS::Heat::RandomString
Properties:
salt: yum
Outputs:
the_str:
Value: {'Fn::GetAtt': [NestedResource, value]}
'''
EXPECTED = (REPLACE, UPDATE, NOCHANGE) = ('replace', 'update', 'nochange')
scenarios = [
('no_changes', dict(template=main_template,
provider=initial_tmpl,
expect=NOCHANGE)),
('main_tmpl_change', dict(template=main_template_2,
provider=initial_tmpl,
expect=UPDATE)),
('provider_change', dict(template=main_template,
provider=content_change_tmpl,
expect=UPDATE)),
('provider_props_change', dict(template=main_template,
provider=prop_change_tmpl,
expect=REPLACE)),
('provider_attr_change', dict(template=main_template,
provider=attr_change_tmpl,
expect=REPLACE)),
]
def setUp(self):
super(ProviderTemplateUpdateTest, self).setUp()
self.ctx = utils.dummy_context('test_username', 'aaaa', 'password')
def create_stack(self):
t = template_format.parse(self.main_template)
tmpl = parser.Template(t, files={'the.yaml': self.initial_tmpl})
stack = parser.Stack(self.ctx, utils.random_name(), tmpl)
stack.store()
stack.create()
self.assertEqual((stack.CREATE, stack.COMPLETE), stack.state)
return stack
def test_template_resource_update_template_schema(self):
stack = self.create_stack()
self.stack = stack
initial_id = stack.output('identifier')
initial_val = stack.output('value')
tmpl = parser.Template(template_format.parse(self.template),
files={'the.yaml': self.provider})
updated_stack = parser.Stack(self.ctx, stack.name, tmpl)
stack.update(updated_stack)
self.assertEqual(('UPDATE', 'COMPLETE'), stack.state)
if self.expect == self.REPLACE:
self.assertNotEqual(initial_id,
stack.output('identifier'))
self.assertNotEqual(initial_val,
stack.output('value'))
elif self.expect == self.NOCHANGE:
self.assertEqual(initial_id,
stack.output('identifier'))
self.assertEqual(initial_val,
stack.output('value'))
else:
self.assertEqual(initial_id,
stack.output('identifier'))
self.assertNotEqual(initial_val,
stack.output('value'))
self.m.VerifyAll()
class ProviderTemplateAdoptTest(HeatTestCase):
main_template = '''
HeatTemplateFormatVersion: '2012-12-12'
Resources:
the_nested:
Type: the.yaml
Properties:
one: my_name
Outputs:
identifier:
Value: {Ref: the_nested}
value:
Value: {'Fn::GetAtt': [the_nested, the_str]}
'''
nested_tmpl = '''
HeatTemplateFormatVersion: '2012-12-12'
Parameters:
one:
Default: foo
Type: String
Resources:
RealRandom:
Type: OS::Heat::RandomString
Properties:
salt: {Ref: one}
Outputs:
the_str:
Value: {'Fn::GetAtt': [RealRandom, value]}
'''
def setUp(self):
super(ProviderTemplateAdoptTest, self).setUp()
self.ctx = utils.dummy_context('test_username', 'aaaa', 'password')
def _yaml_to_json(self, yaml_templ):
return yaml.load(yaml_templ)
def test_abandon(self):
t = template_format.parse(self.main_template)
tmpl = parser.Template(t, files={'the.yaml': self.nested_tmpl})
stack = parser.Stack(self.ctx, utils.random_name(), tmpl)
stack.store()
stack.create()
self.assertEqual((stack.CREATE, stack.COMPLETE), stack.state)
info = stack.prepare_abandon()
self.assertEqual(self._yaml_to_json(self.main_template),
info['template'])
self.assertEqual(self._yaml_to_json(self.nested_tmpl),
info['resources']['the_nested']['template'])
self.m.VerifyAll()
def test_adopt(self):
data = {'resources': {u'the_nested': {
'resources': {u'RealRandom': {
'resource_data': {
u'value': u'N8hE5C7ijdGn4RwnuygbAokGHnTq4cFJ'},
'resource_id': 'N8hE5C7ijdGn4RwnuygbAokGHnTq4cFJ'}}}}}
t = template_format.parse(self.main_template)
tmpl = parser.Template(t, files={'the.yaml': self.nested_tmpl})
stack = parser.Stack(self.ctx, utils.random_name(), tmpl,
adopt_stack_data=data)
self.stack = stack
stack.store()
stack.adopt()
self.assertEqual(('ADOPT', 'COMPLETE'), stack.state)
nested_res = data['resources']['the_nested']['resources']
self.assertEqual(nested_res['RealRandom']['resource_data']['value'],
stack.output('value'))
self.m.VerifyAll()
|