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 exception
28
from nova import flags
30
from nova import utils
31
from nova.compute import model
32
from nova.compute import node
37
class InstanceXmlTestCase(test.TrialTestCase):
38
# @defer.inlineCallbacks
39
def test_serialization(self):
40
# TODO: Reimplement this, it doesn't make sense in redis-land
44
# first_node = node.Node()
45
# inst = yield first_node.run_instance(instance_id)
47
# # force the state so that we can verify that it changes
48
# inst._s['state'] = node.Instance.NOSTATE
50
# self.assert_(ElementTree.parse(StringIO.StringIO(xml)))
52
# second_node = node.Node()
53
# new_inst = node.Instance.fromXml(second_node._conn, pool=second_node._pool, xml=xml)
54
# self.assertEqual(new_inst.state, node.Instance.RUNNING)
55
# rv = yield first_node.terminate_instance(instance_id)
58
class NodeConnectionTestCase(test.TrialTestCase):
60
logging.getLogger().setLevel(logging.DEBUG)
61
super(NodeConnectionTestCase, self).setUp()
62
self.flags(fake_libvirt=True,
66
self.node = node.Node()
68
def create_instance(self):
69
instdir = model.InstanceDirectory()
71
# TODO(ja): add ami, ari, aki, user_data
72
inst['reservation_id'] = 'r-fakeres'
73
inst['launch_time'] = '10'
74
inst['owner_id'] = 'fake'
75
inst['node_name'] = FLAGS.node_name
76
inst['mac_address'] = utils.generate_mac()
77
inst['ami_launch_index'] = 0
79
return inst['instance_id']
81
@defer.inlineCallbacks
82
def test_run_describe_terminate(self):
83
instance_id = self.create_instance()
85
rv = yield self.node.run_instance(instance_id)
87
rv = yield self.node.describe_instances()
88
self.assertEqual(rv[instance_id].name, instance_id)
90
rv = yield self.node.terminate_instance(instance_id)
92
rv = yield self.node.describe_instances()
93
self.assertEqual(rv, {})
95
@defer.inlineCallbacks
96
def test_reboot(self):
97
instance_id = self.create_instance()
98
rv = yield self.node.run_instance(instance_id)
100
rv = yield self.node.describe_instances()
101
logging.debug("describe_instances returns %s" % (rv))
102
self.assertEqual(rv[instance_id].name, instance_id)
104
yield self.node.reboot_instance(instance_id)
106
rv = yield self.node.describe_instances()
107
self.assertEqual(rv[instance_id].name, instance_id)
108
rv = yield self.node.terminate_instance(instance_id)
110
@defer.inlineCallbacks
111
def test_console_output(self):
112
instance_id = self.create_instance()
113
rv = yield self.node.run_instance(instance_id)
115
console = yield self.node.get_console_output(instance_id)
116
self.assert_(console)
117
rv = yield self.node.terminate_instance(instance_id)
119
@defer.inlineCallbacks
120
def test_run_instance_existing(self):
121
instance_id = self.create_instance()
122
rv = yield self.node.run_instance(instance_id)
124
rv = yield self.node.describe_instances()
125
self.assertEqual(rv[instance_id].name, instance_id)
127
self.assertRaises(exception.Error, self.node.run_instance, instance_id)
128
rv = yield self.node.terminate_instance(instance_id)