def lookup(self, name): dom = self.get_vm(name, self.conn) info = dom.info() state = DOM_STATE_MAP[info[0]] screenshot = None # (type, listen, port, passwd, passwdValidTo) graphics = self._vm_get_graphics(name) graphics_port = graphics[2] graphics_port = graphics_port if state == 'running' else None try: if state == 'running' and self._has_video(dom): screenshot = self.vmscreenshot.lookup(name) elif state == 'shutoff': # reset vm stats when it is powered off to avoid sending # incorrect (old) data stats[dom.UUIDString()] = {} except NotFoundError: pass with self.objstore as session: try: extra_info = session.get('vm', dom.UUIDString()) except NotFoundError: extra_info = {} icon = extra_info.get('icon') vm_stats = stats.get(dom.UUIDString(), {}) res = {} res['cpu_utilization'] = vm_stats.get('cpu', 0) res['net_throughput'] = vm_stats.get('net_io', 0) res['net_throughput_peak'] = vm_stats.get('max_net_io', 100) res['io_throughput'] = vm_stats.get('disk_io', 0) res['io_throughput_peak'] = vm_stats.get('max_disk_io', 100) users, groups = self._get_access_info(dom) if state == 'shutoff': xml = dom.XMLDesc(0) val = xpath_get_text(xml, XPATH_DOMAIN_MEMORY)[0] unit_list = xpath_get_text(xml, XPATH_DOMAIN_MEMORY_UNIT) if len(unit_list) > 0: unit = unit_list[0] else: unit = 'KiB' memory = convert_data_size(val, unit, 'MiB') else: memory = info[2] >> 10 return { 'name': name, 'state': state, 'stats': res, 'uuid': dom.UUIDString(), 'memory': memory, 'cpus': info[3], 'screenshot': screenshot, 'icon': icon, # (type, listen, port, passwd, passwdValidTo) 'graphics': { "type": graphics[0], "listen": graphics[1], "port": graphics_port, "passwd": graphics[3], "passwdValidTo": graphics[4] }, 'users': users, 'groups': groups, 'access': 'full', 'persistent': True if dom.isPersistent() else False }
def lookup(self, name): dom = self.get_vm(name, self.conn) info = dom.info() state = DOM_STATE_MAP[info[0]] screenshot = None # (type, listen, port, passwd, passwdValidTo) graphics = self._vm_get_graphics(name) graphics_port = graphics[2] graphics_port = graphics_port if state == 'running' else None try: if state == 'running' and self._has_video(dom): screenshot = self.vmscreenshot.lookup(name) elif state == 'shutoff': # reset vm stats when it is powered off to avoid sending # incorrect (old) data self.stats[dom.UUIDString()] = {} except NotFoundError: pass with self.objstore as session: try: extra_info = session.get('vm', dom.UUIDString()) except NotFoundError: extra_info = {} icon = extra_info.get('icon') self._update_guest_stats(name) vm_stats = self.stats.get(dom.UUIDString(), {}) res = {} res['cpu_utilization'] = vm_stats.get('cpu', 0) res['net_throughput'] = vm_stats.get('net_io', 0) res['net_throughput_peak'] = vm_stats.get('max_net_io', 100) res['io_throughput'] = vm_stats.get('disk_io', 0) res['io_throughput_peak'] = vm_stats.get('max_disk_io', 100) users, groups = self._get_access_info(dom) if state == 'shutoff': xml = dom.XMLDesc(0) val = xpath_get_text(xml, XPATH_DOMAIN_MEMORY)[0] unit_list = xpath_get_text(xml, XPATH_DOMAIN_MEMORY_UNIT) if len(unit_list) > 0: unit = unit_list[0] else: unit = 'KiB' memory = convert_data_size(val, unit, 'MiB') else: memory = info[2] >> 10 return {'name': name, 'state': state, 'stats': res, 'uuid': dom.UUIDString(), 'memory': memory, 'cpus': info[3], 'screenshot': screenshot, 'icon': icon, # (type, listen, port, passwd, passwdValidTo) 'graphics': {"type": graphics[0], "listen": graphics[1], "port": graphics_port, "passwd": graphics[3], "passwdValidTo": graphics[4]}, 'users': users, 'groups': groups, 'access': 'full', 'persistent': True if dom.isPersistent() else False }
def test_convert_data_size(self): failure_data = [{ 'val': None, 'from': 'MiB' }, { 'val': self, 'from': 'MiB' }, { 'val': 1, 'from': None }, { 'val': 1, 'from': '' }, { 'val': 1, 'from': 'foo' }, { 'val': 1, 'from': 'kib' }, { 'val': 1, 'from': 'MiB', 'to': None }, { 'val': 1, 'from': 'MiB', 'to': '' }, { 'val': 1, 'from': 'MiB', 'to': 'foo' }, { 'val': 1, 'from': 'MiB', 'to': 'kib' }] for d in failure_data: if 'to' in d: self.assertRaises(InvalidParameter, convert_data_size, d['val'], d['from'], d['to']) else: self.assertRaises(InvalidParameter, convert_data_size, d['val'], d['from']) success_data = [{ 'got': convert_data_size(5, 'MiB', 'MiB'), 'want': 5 }, { 'got': convert_data_size(5, 'MiB', 'KiB'), 'want': 5120 }, { 'got': convert_data_size(5, 'MiB', 'M'), 'want': 5.24288 }, { 'got': convert_data_size(5, 'MiB', 'GiB'), 'want': 0.0048828125 }, { 'got': convert_data_size(5, 'MiB', 'Tb'), 'want': 4.194304e-05 }, { 'got': convert_data_size(5, 'KiB', 'MiB'), 'want': 0.0048828125 }, { 'got': convert_data_size(5, 'M', 'MiB'), 'want': 4.76837158203125 }, { 'got': convert_data_size(5, 'GiB', 'MiB'), 'want': 5120 }, { 'got': convert_data_size(5, 'Tb', 'MiB'), 'want': 596046.4477539062 }, { 'got': convert_data_size(5, 'MiB'), 'want': convert_data_size(5, 'MiB', 'B') }] for d in success_data: self.assertEquals(d['got'], d['want'])
def test_convert_data_size(self): failure_data = [{'val': None, 'from': 'MiB'}, {'val': self, 'from': 'MiB'}, {'val': 1, 'from': None}, {'val': 1, 'from': ''}, {'val': 1, 'from': 'foo'}, {'val': 1, 'from': 'kib'}, {'val': 1, 'from': 'MiB', 'to': None}, {'val': 1, 'from': 'MiB', 'to': ''}, {'val': 1, 'from': 'MiB', 'to': 'foo'}, {'val': 1, 'from': 'MiB', 'to': 'kib'}] for d in failure_data: if 'to' in d: self.assertRaises(InvalidParameter, convert_data_size, d['val'], d['from'], d['to']) else: self.assertRaises(InvalidParameter, convert_data_size, d['val'], d['from']) success_data = [{'got': convert_data_size(5, 'MiB', 'MiB'), 'want': 5}, {'got': convert_data_size(5, 'MiB', 'KiB'), 'want': 5120}, {'got': convert_data_size(5, 'MiB', 'M'), 'want': 5.24288}, {'got': convert_data_size(5, 'MiB', 'GiB'), 'want': 0.0048828125}, {'got': convert_data_size(5, 'MiB', 'Tb'), 'want': 4.194304e-05}, {'got': convert_data_size(5, 'KiB', 'MiB'), 'want': 0.0048828125}, {'got': convert_data_size(5, 'M', 'MiB'), 'want': 4.76837158203125}, {'got': convert_data_size(5, 'GiB', 'MiB'), 'want': 5120}, {'got': convert_data_size(5, 'Tb', 'MiB'), 'want': 596046.4477539062}, {'got': convert_data_size(5, 'MiB'), 'want': convert_data_size(5, 'MiB', 'B')}] for d in success_data: self.assertEquals(d['got'], d['want'])