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.
17
A fake (in-memory) hypervisor+api. Allows nova testing w/o KVM and libvirt.
21
from xml.etree import ElementTree
24
class FakeVirtConnection(object):
25
# FIXME: networkCreateXML, listNetworks don't do anything since
26
# they aren't exercised in tests yet
34
if not hasattr(cls, '_instance'):
38
def lookupByID(self, i):
39
return self.instances[str(i)]
41
def listDomainsID(self):
42
return self.instances.keys()
44
def listNetworks(self):
47
def lookupByName(self, instance_id):
48
for x in self.instances.values():
49
if x.name() == instance_id:
51
raise Exception('no instance found for instance_id: %s' % instance_id)
53
def networkCreateXML(self, xml):
56
def createXML(self, xml, flags):
58
xml_stringio = StringIO.StringIO(xml)
60
my_xml = ElementTree.parse(xml_stringio)
61
name = my_xml.find('name').text
63
fake_instance = FakeVirtInstance(conn=self,
64
index=str(self.next_index),
67
self.instances[str(self.next_index)] = fake_instance
70
def _removeInstance(self, i):
71
self.instances.pop(str(i))
74
class FakeVirtInstance(object):
83
def __init__(self, conn, index, name, xml):
85
self._destroyed = False
88
self._state = self.RUNNING
94
if self._state == self.SHUTOFF:
95
raise Exception('instance already destroyed: %s' % self.name())
96
self._state = self.SHUTDOWN
97
self._conn._removeInstance(self._index)
100
return [self._state, 0, 2, 0, 0]
102
def XMLDesc(self, flags):
103
return open('fakevirtinstance.xml', 'r').read()
105
def blockStats(self, disk):
106
return [0L, 0L, 0L, 0L, null]
108
def interfaceStats(self, iface):
109
return [0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L]