~ubuntu-branches/ubuntu/wily/heat/wily

1.1.16 by Chuck Short
Import upstream version 2014.2~b3
1
#
2
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
3
#    not use this file except in compliance with the License. You may obtain
4
#    a copy of the License at
5
#
6
#         http://www.apache.org/licenses/LICENSE-2.0
7
#
8
#    Unless required by applicable law or agreed to in writing, software
9
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
#    License for the specific language governing permissions and limitations
12
#    under the License.
13
1.1.22 by Chuck Short
Import upstream version 2015.1~b2
14
import uuid
15
16
from glanceclient import exc as glance_exceptions
1.1.21 by Chuck Short
Import upstream version 2015.1~b1
17
import mock
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
18
import six
19
20
from heat.common import exception
1.1.21 by Chuck Short
Import upstream version 2015.1~b1
21
from heat.engine.clients.os import glance
22
from heat.tests import common
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
23
from heat.tests import utils
24
25
1.1.21 by Chuck Short
Import upstream version 2015.1~b1
26
class GlanceUtilsTests(common.HeatTestCase):
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
27
    """
28
    Basic tests for the helper methods in
29
    :module:'heat.engine.resources.clients.os.glance'.
30
    """
31
32
    def setUp(self):
33
        super(GlanceUtilsTests, self).setUp()
1.1.23 by James Page
Import upstream version 2015.1~b3
34
        self.glance_client = mock.MagicMock()
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
35
        con = utils.dummy_context()
36
        c = con.clients
37
        self.glance_plugin = c.client_plugin('glance')
38
        self.glance_plugin._client = self.glance_client
1.1.23 by James Page
Import upstream version 2015.1~b3
39
        self.my_image = mock.MagicMock()
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
40
41
    def test_get_image_id(self):
42
        """Tests the get_image_id function."""
43
        img_id = str(uuid.uuid4())
44
        img_name = 'myfakeimage'
1.1.23 by James Page
Import upstream version 2015.1~b3
45
        self.my_image.id = img_id
46
        self.my_image.name = img_name
47
        self.glance_client.images.get.return_value = self.my_image
48
        self.glance_client.images.list.side_effect = ([self.my_image], [])
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
49
        self.assertEqual(img_id, self.glance_plugin.get_image_id(img_id))
50
        self.assertEqual(img_id, self.glance_plugin.get_image_id(img_name))
1.1.26 by Corey Bryant
Import upstream version 5.0.0~b1
51
        self.assertRaises(exception.EntityNotFound,
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
52
                          self.glance_plugin.get_image_id, 'noimage')
1.1.23 by James Page
Import upstream version 2015.1~b3
53
54
        calls = [mock.call(filters={'name': img_name}),
55
                 mock.call(filters={'name': 'noimage'})]
56
        self.glance_client.images.get.assert_called_once_with(img_id)
57
        self.glance_client.images.list.assert_has_calls(calls)
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
58
59
    def test_get_image_id_by_name_in_uuid(self):
60
        """Tests the get_image_id function by name in uuid."""
61
        img_id = str(uuid.uuid4())
62
        img_name = str(uuid.uuid4())
1.1.23 by James Page
Import upstream version 2015.1~b3
63
        self.my_image.id = img_id
64
        self.my_image.name = img_name
65
        self.glance_client.images.get.side_effect = [
66
            glance_exceptions.HTTPNotFound()]
67
        self.glance_client.images.list.return_value = [self.my_image]
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
68
69
        self.assertEqual(img_id, self.glance_plugin.get_image_id(img_name))
1.1.23 by James Page
Import upstream version 2015.1~b3
70
        self.glance_client.images.get.assert_called_once_with(img_name)
71
        self.glance_client.images.list.assert_called_once_with(
72
            filters={'name': img_name})
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
73
74
    def test_get_image_id_glance_exception(self):
75
        """Test get_image_id when glance raises an exception."""
76
        # Simulate HTTP exception
77
        img_name = str(uuid.uuid4())
1.1.23 by James Page
Import upstream version 2015.1~b3
78
        self.glance_client.images.list.side_effect = [
79
            glance_exceptions.ClientException("Error")]
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
80
81
        expected_error = "Error retrieving image list from glance: Error"
82
        e = self.assertRaises(exception.Error,
83
                              self.glance_plugin.get_image_id_by_name,
84
                              img_name)
85
        self.assertEqual(expected_error, six.text_type(e))
1.1.23 by James Page
Import upstream version 2015.1~b3
86
        self.glance_client.images.list.assert_called_once_with(
87
            filters={'name': img_name})
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
88
89
    def test_get_image_id_not_found(self):
90
        """Tests the get_image_id function while image is not found."""
91
        img_name = str(uuid.uuid4())
1.1.23 by James Page
Import upstream version 2015.1~b3
92
        self.glance_client.images.get.side_effect = [
93
            glance_exceptions.HTTPNotFound()]
94
        self.glance_client.images.list.return_value = []
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
95
1.1.26 by Corey Bryant
Import upstream version 5.0.0~b1
96
        self.assertRaises(exception.EntityNotFound,
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
97
                          self.glance_plugin.get_image_id, img_name)
1.1.23 by James Page
Import upstream version 2015.1~b3
98
        self.glance_client.images.get.assert_called_once_with(img_name)
99
        self.glance_client.images.list.assert_called_once_with(
100
            filters={'name': img_name})
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
101
102
    def test_get_image_id_name_ambiguity(self):
103
        """Tests the get_image_id function while name ambiguity ."""
104
        img_name = 'ambiguity_name'
1.1.23 by James Page
Import upstream version 2015.1~b3
105
        self.my_image.name = img_name
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
106
1.1.23 by James Page
Import upstream version 2015.1~b3
107
        self.glance_client.images.list.return_value = [self.my_image,
108
                                                       self.my_image]
1.1.16 by Chuck Short
Import upstream version 2014.2~b3
109
        self.assertRaises(exception.PhysicalResourceNameAmbiguity,
110
                          self.glance_plugin.get_image_id, img_name)
1.1.23 by James Page
Import upstream version 2015.1~b3
111
        self.glance_client.images.list.assert_called_once_with(
112
            filters={'name': img_name})
1.1.21 by Chuck Short
Import upstream version 2015.1~b1
113
114
115
class ImageConstraintTest(common.HeatTestCase):
116
117
    def setUp(self):
118
        super(ImageConstraintTest, self).setUp()
119
        self.ctx = utils.dummy_context()
120
        self.mock_get_image = mock.Mock()
121
        self.ctx.clients.client_plugin(
122
            'glance').get_image_id = self.mock_get_image
123
        self.constraint = glance.ImageConstraint()
124
125
    def test_validation(self):
126
        self.mock_get_image.return_value = "id1"
127
        self.assertTrue(self.constraint.validate("foo", self.ctx))
128
129
    def test_validation_error(self):
1.1.26 by Corey Bryant
Import upstream version 5.0.0~b1
130
        self.mock_get_image.side_effect = exception.EntityNotFound(
131
            entity='Image', name='bar')
1.1.21 by Chuck Short
Import upstream version 2015.1~b1
132
        self.assertFalse(self.constraint.validate("bar", self.ctx))