~ubuntu-branches/ubuntu/saucy/heat/saucy-updates

« back to all changes in this revision

Viewing changes to heat/tests/test_resource.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-08 15:23:59 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20130808152359-187gmaw0nx1oduxy
Tags: upstream-2013.2~b2.a186.g2b4b248
ImportĀ upstreamĀ versionĀ 2013.2~b2.a186.g2b4b248

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
import itertools
16
16
from eventlet.support import greenlets as greenlet
17
17
 
18
 
from heat.common import context
19
18
from heat.common import exception
20
19
from heat.engine import parser
21
20
from heat.engine import resource
25
24
 
26
25
from heat.tests import generic_resource as generic_rsrc
27
26
from heat.tests.common import HeatTestCase
 
27
from heat.tests.utils import dummy_context
28
28
from heat.tests.utils import setup_dummy_db
29
29
 
30
30
 
32
32
    def setUp(self):
33
33
        super(ResourceTest, self).setUp()
34
34
        setup_dummy_db()
35
 
        self.stack = parser.Stack(None, 'test_stack', parser.Template({}),
 
35
        self.stack = parser.Stack(dummy_context(), 'test_stack',
 
36
                                  parser.Template({}),
36
37
                                  stack_id=uuidutils.generate_uuid())
37
38
 
38
39
        resource._register_class('GenericResourceType',
58
59
    def test_state_defaults(self):
59
60
        tmpl = {'Type': 'Foo'}
60
61
        res = generic_rsrc.GenericResource('test_res_def', tmpl, self.stack)
61
 
        self.assertEqual(res.state, (None, None))
 
62
        self.assertEqual(res.state, (res.INIT, res.COMPLETE))
62
63
        self.assertEqual(res.status_reason, '')
63
64
 
64
65
    def test_state_set(self):
77
78
        self.assertRaises(ValueError, res.state_set, 'foo', res.COMPLETE)
78
79
        self.assertRaises(ValueError, res.state_set, res.CREATE, 'bla')
79
80
 
 
81
    def test_state_del_stack(self):
 
82
        tmpl = {'Type': 'Foo'}
 
83
        self.stack.action = self.stack.DELETE
 
84
        self.stack.status = self.stack.IN_PROGRESS
 
85
        res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack)
 
86
        self.assertEqual(res.DELETE, res.action)
 
87
        self.assertEqual(res.COMPLETE, res.status)
 
88
 
80
89
    def test_type(self):
81
90
        tmpl = {'Type': 'Foo'}
82
91
        res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack)
149
158
        tmpl1 = {'Type': 'Foo'}
150
159
        tmpl2 = {'Type': 'Foo'}
151
160
        tmpl3 = {'Type': 'Bar'}
152
 
        stack2 = parser.Stack(None, 'test_stack', parser.Template({}),
153
 
                              stack_id=-1)
 
161
        stack2 = parser.Stack(dummy_context(), 'test_stack',
 
162
                              parser.Template({}), stack_id=-1)
154
163
        res1 = generic_rsrc.GenericResource('test_resource', tmpl1, self.stack)
155
164
        res2 = generic_rsrc.GenericResource('test_resource', tmpl2, stack2)
156
165
        res3 = generic_rsrc.GenericResource('test_resource2', tmpl3, stack2)
272
281
        self.assertRaises(exception.ResourceFailure, create)
273
282
        self.assertEqual((res.CREATE, res.FAILED), res.state)
274
283
 
 
284
    def test_create_resource_after_destroy(self):
 
285
        tmpl = {'Type': 'GenericResourceType'}
 
286
        rname = 'test_res_id_none'
 
287
        res = generic_rsrc.ResourceWithProps(rname, tmpl, self.stack)
 
288
        res.id = 'test_res_id'
 
289
        (res.action, res.status) = (res.INIT, res.DELETE)
 
290
        self.assertRaises(exception.ResourceFailure, res.create)
 
291
        res.destroy()
 
292
        res.state_reset()
 
293
        scheduler.TaskRunner(res.create)()
 
294
        self.assertEqual((res.CREATE, res.COMPLETE), res.state)
 
295
 
275
296
    def test_update_ok(self):
276
297
        tmpl = {'Type': 'GenericResourceType', 'Properties': {'Foo': 'abc'}}
277
298
        res = generic_rsrc.ResourceWithProps('test_resource', tmpl, self.stack)
463
484
        self.assertRaises(exception.ResourceFailure, resume)
464
485
        self.assertEqual((res.RESUME, res.FAILED), res.state)
465
486
 
 
487
    def test_resource_class_to_template(self):
 
488
 
 
489
        class TestResource(resource.Resource):
 
490
            list_schema = {'wont_show_up': {'Type': 'Number'}}
 
491
            map_schema = {'will_show_up': {'Type': 'Integer'}}
 
492
 
 
493
            properties_schema = {
 
494
                'name': {'Type': 'String'},
 
495
                'bool': {'Type': 'Boolean'},
 
496
                'implemented': {'Type': 'String',
 
497
                                'Implemented': True,
 
498
                                'AllowedPattern': '.*',
 
499
                                'MaxLength': 7,
 
500
                                'MinLength': 2,
 
501
                                'Required': True},
 
502
                'not_implemented': {'Type': 'String',
 
503
                                    'Implemented': False},
 
504
                'number': {'Type': 'Number',
 
505
                           'MaxValue': 77,
 
506
                           'MinValue': 41,
 
507
                           'Default': 42},
 
508
                'list': {'Type': 'List', 'Schema': {'Type': 'Map',
 
509
                         'Schema': list_schema}},
 
510
                'map': {'Type': 'Map', 'Schema': {'Type': 'Map',
 
511
                        'Schema': map_schema}},
 
512
            }
 
513
 
 
514
            attributes_schema = {
 
515
                'output1': 'output1_desc',
 
516
                'output2': 'output2_desc'
 
517
            }
 
518
 
 
519
        expected_template = {
 
520
            'Parameters': {
 
521
                'name': {'Type': 'String'},
 
522
                'bool': {'Type': 'Boolean'},
 
523
                'implemented': {
 
524
                    'Type': 'String',
 
525
                    'AllowedPattern': '.*',
 
526
                    'MaxLength': 7,
 
527
                    'MinLength': 2
 
528
                },
 
529
                'number': {'Type': 'Number',
 
530
                           'MaxValue': 77,
 
531
                           'MinValue': 41,
 
532
                           'Default': 42},
 
533
                'list': {'Type': 'CommaDelimitedList'},
 
534
                'map': {'Type': 'Json'}
 
535
            },
 
536
            'Resources': {
 
537
                'TestResource': {
 
538
                    'Type': 'Test::Resource::resource',
 
539
                    'Properties': {
 
540
                        'name': {'Ref': 'name'},
 
541
                        'bool': {'Ref': 'bool'},
 
542
                        'implemented': {'Ref': 'implemented'},
 
543
                        'number': {'Ref': 'number'},
 
544
                        'list': {'Fn::Split': {'Ref': 'list'}},
 
545
                        'map': {'Ref': 'map'}
 
546
                    }
 
547
                }
 
548
            },
 
549
            'Outputs': {
 
550
                'output1': {
 
551
                    'Description': 'output1_desc',
 
552
                    'Value': '{"Fn::GetAtt": ["TestResource", "output1"]}'
 
553
                },
 
554
                'output2': {
 
555
                    'Description': 'output2_desc',
 
556
                    'Value': '{"Fn::GetAtt": ["TestResource", "output2"]}'
 
557
                }
 
558
            }
 
559
        }
 
560
        self.assertEqual(expected_template,
 
561
                         TestResource.resource_to_template(
 
562
                         'Test::Resource::resource')
 
563
                         )
 
564
 
466
565
 
467
566
class MetadataTest(HeatTestCase):
468
567
    def setUp(self):
472
571
            'Metadata': {'Test': 'Initial metadata'}
473
572
        }
474
573
        setup_dummy_db()
475
 
        ctx = context.get_admin_context()
476
 
        self.m.StubOutWithMock(ctx, 'username')
477
 
        ctx.username = 'metadata_test_user'
478
 
        self.stack = parser.Stack(ctx, 'test_stack', parser.Template({}))
 
574
        self.stack = parser.Stack(dummy_context(),
 
575
                                  'test_stack', parser.Template({}))
479
576
        self.stack.store()
480
577
        self.res = generic_rsrc.GenericResource('metadata_resource',
481
578
                                                tmpl, self.stack)