summaryrefslogtreecommitdiff
path: root/heat/tests/test_attributes.py
blob: cf50f31a72695a3767a2c9ffb8a13fb88aef5048 (plain)
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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

#    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.

from heat.engine import attributes
from heat.tests import common


class AttributeTest(common.HeatTestCase):
    """Test the Attribute class."""

    def test_as_output(self):
        """Test that Attribute looks right when viewed as an Output."""
        expected = {
            "Value": '{"Fn::GetAtt": ["test_resource", "test1"]}',
            "Description": "The first test attribute"
        }
        attr = attributes.Attribute("test1", "The first test attribute")
        self.assertEqual(expected, attr.as_output("test_resource"))


class AttributesTest(common.HeatTestCase):
    """Test the Attributes class."""

    attributes_schema = {
        "test1": "Test attrib 1",
        "test2": "Test attrib 2",
        "test3": "Test attrib 3"
    }

    def setUp(self):
        super(AttributesTest, self).setUp()
        self.addCleanup(self.m.VerifyAll)

    def test_get_attribute(self):
        """Test that we get the attribute values we expect."""
        test_resolver = lambda x: "value1"
        self.m.ReplayAll()
        attribs = attributes.Attributes('test resource',
                                        self.attributes_schema,
                                        test_resolver)
        self.assertEqual("value1", attribs['test1'])

    def test_get_attribute_none(self):
        """Test that we get the attribute values we expect."""
        test_resolver = lambda x: None
        self.m.ReplayAll()
        attribs = attributes.Attributes('test resource',
                                        self.attributes_schema,
                                        test_resolver)
        self.assertEqual(None, attribs['test1'])

    def test_get_attribute_nonexist(self):
        """Test that we get the attribute values we expect."""
        test_resolver = lambda x: "value1"
        self.m.ReplayAll()
        attribs = attributes.Attributes('test resource',
                                        self.attributes_schema,
                                        test_resolver)
        self.assertRaises(KeyError, attribs.__getitem__, 'not there')

    def test_as_outputs(self):
        """Test that Output format works as expected."""
        expected = {
            "test1": {
                "Value": '{"Fn::GetAtt": ["test_resource", "test1"]}',
                "Description": "Test attrib 1"
            },
            "test2": {
                "Value": '{"Fn::GetAtt": ["test_resource", "test2"]}',
                "Description": "Test attrib 2"
            },
            "test3": {
                "Value": '{"Fn::GetAtt": ["test_resource", "test3"]}',
                "Description": "Test attrib 3"
            }
        }
        MyTestResourceClass = self.m.CreateMockAnything()
        MyTestResourceClass.attributes_schema = {
            "test1": "Test attrib 1",
            "test2": "Test attrib 2",
            "test3": "Test attrib 3"
        }
        self.m.ReplayAll()
        self.assertEqual(
            expected,
            attributes.Attributes.as_outputs("test_resource",
                                             MyTestResourceClass))