def physical_cpu_mhz_total(x=int_(min=0, max=8), y=int_(min=0, max=8)): with MockTransaction: connection = libvirt.virConnect() expect(common).physical_cpu_count(connection). \ and_return(x).once() expect(common).physical_cpu_mhz(connection). \ and_return(y).once() assert common.physical_cpu_mhz_total(connection) == x * y
def get_host_characteristics_long(ram=int_(min=1, max=4000), cores=int_(min=1, max=8), mhz=int_(min=1, max=3000)): with MockTransaction: connection = libvirt.virConnect() expect(connection).getInfo().and_return( ['x86_64', long(ram), cores, mhz, 1, 1, 4, 2]).once() assert collector.get_host_characteristics(connection) == \ (cores * mhz, long(ram))
def get_max_ram(uuid=str_(of='abc123-', min_length=36, max_length=36), x=int_(min=0)): with MockTransaction: connection = libvirt.virConnect() domain = mock('domain') expect(connection).lookupByUUIDString(uuid). \ and_return(domain).once() expect(domain).maxMemory().and_return(x).once() assert manager.get_max_ram(connection, uuid) == int(x / 1024)
def get_cpu_time(uuid=str_(of='abc123-', min_length=36, max_length=36), x=int_(min=0)): with MockTransaction: connection = libvirt.virConnect() domain = mock('domain') expect(connection).lookupByUUIDString(uuid). \ and_return(domain).once() expect(domain).getCPUStats(True, 0). \ and_return([{'cpu_time': x}]).once() assert collector.get_cpu_time(connection, uuid) == x
def get_max_ram_none( uuid=str_(of='abc123-', min_length=36, max_length=36) ): with MockTransaction: def raise_libvirt_error(): raise libvirt.libvirtError(None) connection = libvirt.virConnect() expect(connection).lookupByUUIDString(uuid). \ and_call(lambda _: raise_libvirt_error()) assert manager.get_max_ram(connection, uuid) is None
def get_host_characteristics( ram=int_(min=1, max=4000), cores=int_(min=1, max=8), mhz=int_(min=1, max=3000) ): with MockTransaction: connection = libvirt.virConnect() expect(connection).getInfo().and_return( ['x86_64', ram, cores, mhz, 1, 1, 4, 2]).once() assert collector.get_host_characteristics(connection) == \ (cores * mhz, ram)
def get_max_ram_long( uuid=str_(of='abc123-', min_length=36, max_length=36), x=int_(min=0) ): with MockTransaction: connection = libvirt.virConnect() domain = mock('domain') expect(connection).lookupByUUIDString(uuid). \ and_return(domain).once() expect(domain).maxMemory().and_return(long(x)).once() assert manager.get_max_ram(connection, uuid) == long(x) / 1024
def get_max_ram_none(uuid=str_(of='abc123-', min_length=36, max_length=36)): with MockTransaction: def raise_libvirt_error(): raise libvirt.libvirtError(None) connection = libvirt.virConnect() expect(connection).lookupByUUIDString(uuid). \ and_call(lambda _: raise_libvirt_error()) assert manager.get_max_ram(connection, uuid) is None
def get_cpu_time( uuid=str_(of='abc123-', min_length=36, max_length=36), x=int_(min=0) ): with MockTransaction: connection = libvirt.virConnect() domain = mock('domain') expect(connection).lookupByUUIDString(uuid). \ and_return(domain).once() expect(domain).getCPUStats(True, 0). \ and_return([{'cpu_time': x}]).once() assert collector.get_cpu_time(connection, uuid) == x
def test_domain_name_increment(self): vc = libvirt.virConnect() d = libvirt.virDomain(vc) c = Cluster('unittest', 'template_domain', vc) vc.listAllDomains = MagicMock(return_value=[]) self.assertEqual('unittest-00', c.next_domain_name()) d.name = MagicMock(return_value='unittest-00') vc.listAllDomains = MagicMock(return_value=[d]) self.assertEqual('unittest-01', c.next_domain_name())
def get_ram( data=dict_( keys=str_(of='abc123-', min_length=36, max_length=36), values=int_(min=1, max=100), min_length=0, max_length=10 ) ): with MockTransaction: def mock_get_max_ram(vir_connection, uuid): return data[uuid] connection = libvirt.virConnect() when(manager).get_max_ram(connection, any_string). \ then_call(mock_get_max_ram) assert manager.get_ram(connection, data.keys()) == data
def get_ram(data=dict_(keys=str_(of='abc123-', min_length=36, max_length=36), values=int_(min=1, max=100), min_length=0, max_length=10)): with MockTransaction: def mock_get_max_ram(vir_connection, uuid): return data[uuid] connection = libvirt.virConnect() when(manager).get_max_ram(connection, any_string). \ then_call(mock_get_max_ram) assert manager.get_ram(connection, data.keys()) == data
def get_current_vms( ids=dict_( keys=int_(min=0, max=1000), values=str_(of='abc123-', min_length=36, max_length=36), min_length=0, max_length=10 ) ): with MockTransaction: def init_vm(id): vm = mock('vm') expect(vm).UUIDString().and_return(ids[id]).once() expect(vm).state(0).and_return([id * 13, id]).once() return vm connection = libvirt.virConnect() expect(connection).listDomainsID().and_return(ids.keys()).once() if ids: expect(connection).lookupByID(any_int) \ .and_call(lambda id: init_vm(id)) expected = dict((v, k * 13) for k, v in ids.items()) assert collector.get_current_vms(connection) == expected
def get_current_vms(ids=dict_(keys=int_(min=0, max=1000), values=str_(of='abc123-', min_length=36, max_length=36), min_length=0, max_length=10)): with MockTransaction: def init_vm(id): vm = mock('vm') expect(vm).UUIDString().and_return(ids[id]).once() expect(vm).state(0).and_return([id * 13, id]).once() return vm connection = libvirt.virConnect() expect(connection).listDomainsID().and_return(ids.keys()).once() if ids: expect(connection).lookupByID(any_int) \ .and_call(lambda id: init_vm(id)) expected = dict((v, k * 13) for k, v in ids.items()) assert collector.get_current_vms(connection) == expected
def setUp(self, mock_lopen): mock_lopen.return_value = libvirt.virConnect() try: self.lvirt = LibvirtConn() except libvirt.libvirtError as le: self.fail(le.get_error_message())
def get_cpu_mhz( cpus=int_(min=1, max=8), current_time=float_(min=100, max=1000), time_period=float_(min=1, max=100), vm_data=dict_( keys=str_(of='abc123-', min_length=36, max_length=36), values=two(of=int_(min=1, max=100)), min_length=0, max_length=10 ), added_vms=dict_( keys=str_(of='abc123-', min_length=36, max_length=36), values=tuple_(int_(min=1, max=100), list_(of=int_(min=1, max=3000), min_length=0, max_length=10)), min_length=0, max_length=5 ) ): with MockTransaction: def mock_get_cpu_time(vir_connection, uuid): if uuid in original_vm_data: return original_vm_data[uuid][0] + \ original_vm_data[uuid][1] else: return added_vms[uuid][0] original_vm_data = dict(vm_data) previous_time = current_time - time_period connection = libvirt.virConnect() when(collector).get_cpu_time(connection, any_string). \ then_call(mock_get_cpu_time) previous_cpu_time = {} cpu_mhz = {} for uuid, data in vm_data.items(): previous_cpu_time[uuid] = data[0] if vm_data: to_remove = random.randrange(len(vm_data)) for _ in xrange(to_remove): tmp = random.choice(vm_data.keys()) del vm_data[tmp] vms = vm_data.keys() current_cpu_time = {} for uuid in vms: current_cpu_time[uuid] = vm_data[uuid][0] + vm_data[uuid][1] cpu_mhz[uuid] = collector.calculate_cpu_mhz( cpus, previous_time, current_time, vm_data[uuid][0], vm_data[uuid][0] + vm_data[uuid][1]) added_vm_data = {} if added_vms: for uuid, data in added_vms.items(): current_cpu_time[uuid] = data[0] added_vm_data[uuid] = data[1] if data[1]: cpu_mhz[uuid] = data[1][-1] vms.extend(added_vms.keys()) result = collector.get_cpu_mhz( connection, cpus, previous_cpu_time, previous_time, current_time, vms, {}, added_vm_data) assert result[0] == current_cpu_time assert result[1] == cpu_mhz
def get_cpu_mhz(cpus=int_(min=1, max=8), current_time=float_(min=100, max=1000), time_period=float_(min=1, max=100), vm_data=dict_(keys=str_(of='abc123-', min_length=36, max_length=36), values=two(of=int_(min=1, max=100)), min_length=0, max_length=10), added_vms=dict_(keys=str_(of='abc123-', min_length=36, max_length=36), values=tuple_( int_(min=1, max=100), list_(of=int_(min=1, max=3000), min_length=0, max_length=10)), min_length=0, max_length=5)): with MockTransaction: def mock_get_cpu_time(vir_connection, uuid): if uuid in original_vm_data: return original_vm_data[uuid][0] + \ original_vm_data[uuid][1] else: return added_vms[uuid][0] original_vm_data = dict(vm_data) previous_time = current_time - time_period connection = libvirt.virConnect() when(collector).get_cpu_time(connection, any_string). \ then_call(mock_get_cpu_time) previous_cpu_time = {} cpu_mhz = {} for uuid, data in vm_data.items(): previous_cpu_time[uuid] = data[0] if vm_data: to_remove = random.randrange(len(vm_data)) for _ in xrange(to_remove): tmp = random.choice(vm_data.keys()) del vm_data[tmp] vms = vm_data.keys() current_cpu_time = {} for uuid in vms: current_cpu_time[uuid] = vm_data[uuid][0] + vm_data[uuid][1] cpu_mhz[uuid] = collector.calculate_cpu_mhz( cpus, previous_time, current_time, vm_data[uuid][0], vm_data[uuid][0] + vm_data[uuid][1]) added_vm_data = {} if added_vms: for uuid, data in added_vms.items(): current_cpu_time[uuid] = data[0] added_vm_data[uuid] = data[1] if data[1]: cpu_mhz[uuid] = data[1][-1] vms.extend(added_vms.keys()) result = collector.get_cpu_mhz(connection, cpus, previous_cpu_time, previous_time, current_time, vms, {}, added_vm_data) assert result[0] == current_cpu_time assert result[1] == cpu_mhz
def physical_cpu_mhz(x=int_(min=0, max=8)): with MockTransaction: connection = libvirt.virConnect() expect(connection).getInfo().and_return([0, 0, 0, x]).once() assert common.physical_cpu_mhz(connection) == x
def test_next_mac_address(self): vc = libvirt.virConnect() c = Cluster('unittest', 'template_domain', vc) next_mac = c.next_mac_address('00:00:00:00:00:09') self.assertEquals('00:00:00:00:00:0a', next_mac)