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

« back to all changes in this revision

Viewing changes to heat/tests/test_nova_utils.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:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
"""Tests for :module:'heat.engine.resources.nova_utls'."""
 
16
 
 
17
import uuid
 
18
 
 
19
from heat.common import exception
 
20
from heat.engine.resources import nova_utils
 
21
from heat.tests.common import HeatTestCase
 
22
 
 
23
 
 
24
class NovaUtilsTests(HeatTestCase):
 
25
    """
 
26
    Basic tests for the helper methods in
 
27
    :module:'heat.engine.resources.nova_utils'.
 
28
    """
 
29
 
 
30
    def setUp(self):
 
31
        super(NovaUtilsTests, self).setUp()
 
32
        self.nova_client = self.m.CreateMockAnything()
 
33
 
 
34
    def test_get_image_id(self):
 
35
        """Tests the get_image_id function."""
 
36
        my_image = self.m.CreateMockAnything()
 
37
        img_id = str(uuid.uuid4())
 
38
        img_name = 'myfakeimage'
 
39
        my_image.id = img_id
 
40
        my_image.name = img_name
 
41
        self.nova_client.images = self.m.CreateMockAnything()
 
42
        self.nova_client.images.get(img_id).AndReturn(my_image)
 
43
        self.nova_client.images.list().MultipleTimes().AndReturn([my_image])
 
44
        self.m.ReplayAll()
 
45
        self.assertEqual(img_id, nova_utils.get_image_id(self.nova_client,
 
46
                                                         img_id))
 
47
        self.assertEqual(img_id, nova_utils.get_image_id(self.nova_client,
 
48
                                                         'myfakeimage'))
 
49
        self.assertRaises(exception.ImageNotFound, nova_utils.get_image_id,
 
50
                          self.nova_client, 'noimage')
 
51
        self.m.VerifyAll()
 
52
 
 
53
    def test_get_flavor_id(self):
 
54
        """Tests the get_flavor_id function."""
 
55
        flav_id = str(uuid.uuid4())
 
56
        flav_name = 'X-Large'
 
57
        my_flavor = self.m.CreateMockAnything()
 
58
        my_flavor.name = flav_name
 
59
        my_flavor.id = flav_id
 
60
        self.nova_client.flavors = self.m.CreateMockAnything()
 
61
        self.nova_client.flavors.list().MultipleTimes().AndReturn([my_flavor])
 
62
        self.m.ReplayAll()
 
63
        self.assertEqual(flav_id, nova_utils.get_flavor_id(self.nova_client,
 
64
                                                           flav_name))
 
65
        self.assertRaises(exception.FlavorMissing, nova_utils.get_flavor_id,
 
66
                          self.nova_client, 'noflavor')
 
67
        self.m.VerifyAll()
 
68
 
 
69
    def test_get_keypair(self):
 
70
        """Tests the get_keypair function."""
 
71
        my_pub_key = 'a cool public key string'
 
72
        my_key_name = 'mykey'
 
73
        my_key = self.m.CreateMockAnything()
 
74
        my_key.public_key = my_pub_key
 
75
        my_key.name = my_key_name
 
76
        self.nova_client.keypairs = self.m.CreateMockAnything()
 
77
        self.nova_client.keypairs.list().MultipleTimes().AndReturn([my_key])
 
78
        self.m.ReplayAll()
 
79
        self.assertEqual(my_key, nova_utils.get_keypair(self.nova_client,
 
80
                                                        my_key_name))
 
81
        self.assertRaises(exception.UserKeyPairMissing, nova_utils.get_keypair,
 
82
                          self.nova_client, 'notakey')
 
83
        self.m.VerifyAll()
 
84
 
 
85
    def test_build_userdata(self):
 
86
        """Tests the build_userdata function."""
 
87
        resource = self.m.CreateMockAnything()
 
88
        resource.t = {}
 
89
        self.m.StubOutWithMock(nova_utils.cfg, 'CONF')
 
90
        cnf = nova_utils.cfg.CONF
 
91
        cnf.instance_user = 'testuser'
 
92
        cnf.heat_metadata_server_url = 'http://localhost:123'
 
93
        cnf.heat_watch_server_url = 'http://localhost:345'
 
94
        cnf.instance_connection_is_secure = False
 
95
        cnf.instance_connection_https_validate_certificates = False
 
96
        self.m.ReplayAll()
 
97
        data = nova_utils.build_userdata(resource)
 
98
        self.assertTrue("Content-Type: text/cloud-config;" in data)
 
99
        self.assertTrue("Content-Type: text/cloud-boothook;" in data)
 
100
        self.assertTrue("Content-Type: text/part-handler;" in data)
 
101
        self.assertTrue("Content-Type: text/x-cfninitdata;" in data)
 
102
        self.assertTrue("Content-Type: text/x-shellscript;" in data)
 
103
        self.assertTrue("http://localhost:345" in data)
 
104
        self.assertTrue("http://localhost:123" in data)
 
105
        self.assertTrue("[Boto]" in data)
 
106
        self.assertTrue('testuser' in data)
 
107
        self.m.VerifyAll()