def __init__(self, agent_config): self.logger = logging.getLogger(__name__) prefix = socket.gethostname() suffix = str(agent_config.host_port) self._uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, prefix + suffix)) tempdir = mkdtemp(prefix='disk', delete=True) self.disk_manager = FakeDiskManager(self, os.path.join(tempdir, 'disk')) self.vm_manager = FakeVmManager(self) self.network_manager = FakeNetworkManager(self, agent_config.networks) self.system = FakeSystem(self) datastores = agent_config.datastores # For fake hypervisor, we assume there is always one image datastore. if agent_config.image_datastores: image_datastore = list(agent_config.image_datastores)[0]["name"] else: image_datastore = None self.datastore_manager = FakeDatastoreManager(self.system, datastores, image_datastore) self.image_manager = FakeImageManager(self, image_datastore) self.image_manager.copy_to_datastores( "ttylinux", self.datastore_manager.get_datastore_ids())
def __init__(self, agent_config): self.logger = logging.getLogger(__name__) self._multi_agent_id = agent_config.multi_agent_id prefix = socket.gethostname() suffix = str(agent_config.host_port) self._uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, prefix + suffix)) tempdir = mkdtemp(prefix='disk', delete=True) self.disk_manager = FakeDiskManager(self, os.path.join(tempdir, 'disk')) self.vm_manager = FakeVmManager(self) self.network_manager = FakeNetworkManager(self, agent_config.networks) self.system = FakeSystem(self) datastores = agent_config.datastores # For fake hypervisor, we assume there is always one image datastore. if agent_config.image_datastores: image_datastore = list(agent_config.image_datastores)[0]["name"] else: image_datastore = None self.datastore_manager = FakeDatastoreManager(self.system, datastores, image_datastore) self.image_manager = FakeImageManager(self, image_datastore) self.image_manager.copy_to_datastores( "ttylinux", self.datastore_manager.get_datastore_ids())
def test_vminfo(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB)]) vm_metadata = { "project": "p1", "vendor": "v1", } spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vminfo(spec, vm_metadata) vmm.create_vm(vm_id, spec) got_metadata = vmm.get_vminfo(vm_id) assert_that(got_metadata, equal_to(vm_metadata))
def test_vminfo(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [ QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB) ]) vm_metadata = { "project": "p1", "vendor": "v1", } spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vminfo(spec, vm_metadata) vmm.create_vm(vm_id, spec) got_metadata = vmm.get_vminfo(vm_id) assert_that(got_metadata, equal_to(vm_metadata))
def test_vnc_port(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB)]) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, none()) expected = set(range(5900, 5905)) for p in expected: vm_id = str(uuid.uuid4()) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vnc_port(spec, p) vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, equal_to(p)) ports = vmm.get_occupied_vnc_ports() # The following 2 asserts test equality of ports and expected assert_that(len(ports), equal_to(len(expected))) assert_that(len(ports), equal_to(len(expected.union(ports))))
class FakeHypervisor(object): """Manage Fake Hypervisor.""" def __init__(self, agent_config): self.logger = logging.getLogger(__name__) prefix = socket.gethostname() suffix = str(agent_config.host_port) self._uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, prefix + suffix)) tempdir = mkdtemp(prefix='disk', delete=True) self.disk_manager = FakeDiskManager(self, os.path.join(tempdir, 'disk')) self.vm_manager = FakeVmManager(self) self.network_manager = FakeNetworkManager(self, agent_config.networks) self.system = FakeSystem(self) datastores = agent_config.datastores # For fake hypervisor, we assume there is always one image datastore. if agent_config.image_datastores: image_datastore = list(agent_config.image_datastores)[0]["name"] else: image_datastore = None self.datastore_manager = FakeDatastoreManager(self.system, datastores, image_datastore) self.image_manager = FakeImageManager(self, image_datastore) self.image_manager.copy_to_datastores( "ttylinux", self.datastore_manager.get_datastore_ids()) @property def uuid(self): return self._uuid def check_image(self, image_id, datastore_id): return self.image_manager.\ check_image(image_id, datastore_id) @staticmethod def datastore_id(name): return str(uuid.uuid5(uuid.NAMESPACE_DNS, str(name))) def acquire_vim_ticket(self): return 'cst-526d7e8f-b126-1686-9b49-bde6f34f0be8--tp-71-18-5C-87' + \ '-F9-DB-C1-B9-D7-92-7A-19-99-1E-45-56-73-D6-CC-99' def acquire_cgi_ticket(self, url, op): return '52524918-2252-f24d-3a2b-2609c0fe795e' def add_update_listener(self, listener): # Only triggers VM update listener self.vm_manager.add_update_listener(listener) def remove_update_listener(self, listener): self.vm_manager.remove_update_listener(listener) def transfer_image(self, source_image_id, source_datastore, destination_image_id, destination_datastore, host, port): return "" def receive_image(self, image_id, datastore, imported_vm_name, metadata, manifest): pass def set_memory_overcommit(self, memory_overcommit): pass
class FakeHypervisor(object): """Manage Fake Hypervisor.""" def __init__(self, agent_config): self.logger = logging.getLogger(__name__) self._multi_agent_id = agent_config.multi_agent_id prefix = socket.gethostname() suffix = str(agent_config.host_port) self._uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, prefix + suffix)) tempdir = mkdtemp(prefix='disk', delete=True) self.disk_manager = FakeDiskManager(self, os.path.join(tempdir, 'disk')) self.vm_manager = FakeVmManager(self) self.network_manager = FakeNetworkManager(self, agent_config.networks) self.system = FakeSystem(self) datastores = agent_config.datastores # For fake hypervisor, we assume there is always one image datastore. if agent_config.image_datastores: image_datastore = list(agent_config.image_datastores)[0]["name"] else: image_datastore = None self.datastore_manager = FakeDatastoreManager(self.system, datastores, image_datastore) self.image_manager = FakeImageManager(self, image_datastore) self.image_manager.copy_to_datastores( "ttylinux", self.datastore_manager.get_datastore_ids()) @property def uuid(self): return self._uuid @property def config(self): config = gen.hypervisor.fake.ttypes.FakeConfig() config.fake_id = "value" return TSerialization.serialize(config) @property def multi_agent_id(self): return self._config.multi_agent_id def normalized_load(self): return 42 def check_image(self, image_id, datastore_id): return self.image_manager.\ check_image(image_id, datastore_id) @staticmethod def datastore_id(name): return str(uuid.uuid5(uuid.NAMESPACE_DNS, str(name))) def acquire_vim_ticket(self): return 'cst-526d7e8f-b126-1686-9b49-bde6f34f0be8--tp-71-18-5C-87' + \ '-F9-DB-C1-B9-D7-92-7A-19-99-1E-45-56-73-D6-CC-99' def acquire_cgi_ticket(self, url, op): return '52524918-2252-f24d-3a2b-2609c0fe795e' def add_update_listener(self, listener): # Only triggers VM update listener self.vm_manager.add_update_listener(listener) def remove_update_listener(self, listener): self.vm_manager.remove_update_listener(listener) def transfer_image(self, source_image_id, source_datastore, destination_image_id, destination_datastore, host, port): return "" def receive_image(self, image_id, datastore, imported_vm_name): pass
def test_vnc_port(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [ QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB) ]) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, none()) expected = set(range(5900, 5905)) for p in expected: vm_id = str(uuid.uuid4()) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vnc_port(spec, p) vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, equal_to(p)) ports = vmm.get_occupied_vnc_ports() # The following 2 asserts test equality of ports and expected assert_that(len(ports), equal_to(len(expected))) assert_that(len(ports), equal_to(len(expected.union(ports))))