1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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
15
"""Tests for :module:'heat.engine.resources.nova_utls'."""
19
from heat.common import exception
20
from heat.engine.resources import nova_utils
21
from heat.tests.common import HeatTestCase
24
class NovaUtilsTests(HeatTestCase):
26
Basic tests for the helper methods in
27
:module:'heat.engine.resources.nova_utils'.
31
super(NovaUtilsTests, self).setUp()
32
self.nova_client = self.m.CreateMockAnything()
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'
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])
45
self.assertEqual(img_id, nova_utils.get_image_id(self.nova_client,
47
self.assertEqual(img_id, nova_utils.get_image_id(self.nova_client,
49
self.assertRaises(exception.ImageNotFound, nova_utils.get_image_id,
50
self.nova_client, 'noimage')
53
def test_get_flavor_id(self):
54
"""Tests the get_flavor_id function."""
55
flav_id = str(uuid.uuid4())
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])
63
self.assertEqual(flav_id, nova_utils.get_flavor_id(self.nova_client,
65
self.assertRaises(exception.FlavorMissing, nova_utils.get_flavor_id,
66
self.nova_client, 'noflavor')
69
def test_get_keypair(self):
70
"""Tests the get_keypair function."""
71
my_pub_key = 'a cool public key string'
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])
79
self.assertEqual(my_key, nova_utils.get_keypair(self.nova_client,
81
self.assertRaises(exception.UserKeyPairMissing, nova_utils.get_keypair,
82
self.nova_client, 'notakey')
85
def test_build_userdata(self):
86
"""Tests the build_userdata function."""
87
resource = self.m.CreateMockAnything()
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
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)