1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
# Copyright [2010] [Anso Labs, LLC]
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain 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,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
20
from xml.etree import ElementTree
22
from nova import vendor
24
from tornado import ioloop
25
from twisted.internet import defer
27
from nova import flags
30
from nova.auth import users
31
from nova.compute import node
32
from nova.endpoint import api
33
from nova.endpoint import cloud
39
class CloudTestCase(test.BaseTestCase):
41
super(CloudTestCase, self).setUp()
42
self.flags(fake_libvirt=True,
47
self.conn = rpc.Connection.instance()
48
logging.getLogger().setLevel(logging.DEBUG)
51
self.cloud = cloud.CloudController()
52
self.cloud_consumer = rpc.AdapterConsumer(connection=self.conn,
53
topic=FLAGS.cloud_topic,
55
self.injected.append(self.cloud_consumer.attach_to_tornado(self.ioloop))
58
self.node = node.Node()
59
self.node_consumer = rpc.AdapterConsumer(connection=self.conn,
60
topic=FLAGS.compute_topic,
62
self.injected.append(self.node_consumer.attach_to_tornado(self.ioloop))
64
user_mocker = mox.Mox()
65
self.admin = user_mocker.CreateMock(users.User)
66
self.admin.is_authorized(mox.IgnoreArg()).AndReturn(True)
67
self.context = api.APIRequestContext(handler=None,user=self.admin)
69
def test_console_output(self):
70
if FLAGS.fake_libvirt:
71
logging.debug("Can't test instances without a real virtual env.")
74
inst = yield self.node.run_instance(instance_id)
75
output = yield self.cloud.get_console_output(self.context, [instance_id])
78
rv = yield self.node.terminate_instance(instance_id)
80
def test_run_instances(self):
81
if FLAGS.fake_libvirt:
82
logging.debug("Can't test instances without a real virtual env.")
84
image_id = FLAGS.default_image
85
instance_type = FLAGS.default_instance_type
87
kwargs = {'image_id': image_id,
88
'instance_type': instance_type,
89
'max_count': max_count}
90
rv = yield self.cloud.run_instances(self.context, **kwargs)
91
# TODO: check for proper response
92
instance = rv['reservationSet'][0][rv['reservationSet'][0].keys()[0]][0]
93
logging.debug("Need to watch instance %s until it's running..." % instance['instance_id'])
95
rv = yield defer.succeed(time.sleep(1))
96
info = self.cloud._get_instance(instance['instance_id'])
97
logging.debug(info['state'])
98
if info['state'] == node.Instance.RUNNING:
102
if not FLAGS.fake_libvirt:
103
time.sleep(45) # Should use boto for polling here
104
for reservations in rv['reservationSet']:
105
# for res_id in reservations.keys():
106
# logging.debug(reservations[res_id])
107
# for instance in reservations[res_id]:
108
for instance in reservations[reservations.keys()[0]]:
109
logging.debug("Terminating instance %s" % instance['instance_id'])
110
rv = yield self.node.terminate_instance(instance['instance_id'])
112
def test_instance_update_state(self):
115
'reservation_id': 'r-1',
116
'instance_id': 'i-%s' % num,
117
'image_id': 'ami-%s' % num,
118
'private_dns_name': '10.0.0.%s' % num,
119
'dns_name': '10.0.0%s' % num,
120
'ami_launch_index': str(num),
121
'instance_type': 'fake',
122
'availability_zone': 'fake',
125
'ramdisk_id': 'fake',
126
'groups': ['default'],
127
'product_codes': None,
132
rv = self.cloud.format_instances(self.admin)
134
self.assert_(len(rv['reservationSet']) == 0)
136
# simulate launch of 5 instances
137
# self.cloud.instances['pending'] = {}
140
# self.cloud.instances['pending'][inst['instance_id']] = inst
142
#rv = self.cloud.format_instances(self.admin)
143
#self.assert_(len(rv['reservationSet']) == 1)
144
#self.assert_(len(rv['reservationSet'][0]['instances_set']) == 5)
146
# report 4 nodes each having 1 of the instances
148
# self.cloud.update_state('instances', {('node-%s' % i): {('i-%s' % i): instance(i)}})
150
# one instance should be pending still
151
#self.assert_(len(self.cloud.instances['pending'].keys()) == 1)
153
# check that the reservations collapse
154
#rv = self.cloud.format_instances(self.admin)
155
#self.assert_(len(rv['reservationSet']) == 1)
156
#self.assert_(len(rv['reservationSet'][0]['instances_set']) == 5)
158
# check that we can get metadata for each instance
160
# data = self.cloud.get_metadata(instance(i)['private_dns_name'])
161
# self.assert_(data['meta-data']['ami-id'] == 'ami-%s' % i)